blob: e8a603c9e1479e368cb5f29279e9fe873e05faed [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 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020 * URGENT TODO:
21 * - For xsi-driven schema acquisition, augment the IDCs after every
22 * acquisition episode (xmlSchemaAugmentIDC).
23 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000024 * NOTES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025 * - Elimated item creation for: <restriction>, <extension>,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026 * <simpleContent>, <complexContent>, <list>, <union>
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000028 * PROBLEMS:
29 * - http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0337.html
30 * IDC XPath expression and chameleon includes: the targetNamespace is changed, so
31 * XPath will have trouble to resolve to this namespace, since not known.
32 *
33 *
34 * CONSTRAINTS:
35 *
36 * Schema Component Constraint:
37 * All Group Limited (cos-all-limited)
38 * Status: complete
39 * (1.2)
40 * In xmlSchemaGroupDefReferenceTermFixup() and
41 * (2)
42 * In xmlSchemaParseModelGroup()
43 * TODO: Actually this should go to component-level checks,
44 * but is done here due to performance. Move it to an other layer
45 * is schema construction via an API is implemented.
Daniel Veillardb0f397e2003-12-23 23:30:53 +000046 */
Daniel Veillard4255d502002-04-16 15:50:10 +000047#define IN_LIBXML
48#include "libxml.h"
49
50#ifdef LIBXML_SCHEMAS_ENABLED
51
52#include <string.h>
53#include <libxml/xmlmemory.h>
54#include <libxml/parser.h>
55#include <libxml/parserInternals.h>
56#include <libxml/hash.h>
Daniel Veillard5a872412002-05-22 06:40:27 +000057#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000058#include <libxml/xmlschemas.h>
59#include <libxml/schemasInternals.h>
60#include <libxml/xmlschemastypes.h>
61#include <libxml/xmlautomata.h>
62#include <libxml/xmlregexp.h>
Daniel Veillardbe9c6322003-11-22 20:37:51 +000063#include <libxml/dict.h>
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000064#include <libxml/encoding.h>
65#include <libxml/xmlIO.h>
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000066#ifdef LIBXML_PATTERN_ENABLED
67#include <libxml/pattern.h>
68#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000069#ifdef LIBXML_READER_ENABLED
70#include <libxml/xmlreader.h>
71#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000072
Daniel Veillarda84c0b32003-06-02 16:58:46 +000073/* #define DEBUG 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000074
Daniel Veillard82bbbd42003-05-11 20:16:09 +000075/* #define DEBUG_CONTENT 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000076
Daniel Veillard82bbbd42003-05-11 20:16:09 +000077/* #define DEBUG_TYPE 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000078
Daniel Veillard118aed72002-09-24 14:13:13 +000079/* #define DEBUG_CONTENT_REGEXP 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000080
Daniel Veillard4255d502002-04-16 15:50:10 +000081/* #define DEBUG_AUTOMATA 1 */
82
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000083/* #define DEBUG_IDC */
84
85/* #define DEBUG_IDC_NODE_TABLE */
86
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000087/* #define WXS_ELEM_DECL_CONS_ENABLED */
88
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000089#ifdef DEBUG_IDC
90 #ifndef DEBUG_IDC_NODE_TABLE
91 #define DEBUG_IDC_NODE_TABLE
92 #endif
Daniel Veillarddee23482008-04-11 12:58:43 +000093#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000094
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000095/* #define ENABLE_PARTICLE_RESTRICTION 1 */
96
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000097#define ENABLE_REDEFINE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000098
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000099/* #define ENABLE_NAMED_LOCALS */
100
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +0000101/* #define ENABLE_IDC_NODE_TABLES_TEST */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000102
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000103#define DUMP_CONTENT_MODEL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000104
105#ifdef LIBXML_READER_ENABLED
106/* #define XML_SCHEMA_READER_ENABLED */
107#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000108
Daniel Veillard4255d502002-04-16 15:50:10 +0000109#define UNBOUNDED (1 << 30)
110#define TODO \
111 xmlGenericError(xmlGenericErrorContext, \
112 "Unimplemented block at %s:%d\n", \
113 __FILE__, __LINE__);
114
William M. Brack2f2a6632004-08-20 23:09:47 +0000115#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +0000116
Daniel Veillard4255d502002-04-16 15:50:10 +0000117/*
118 * The XML Schemas namespaces
119 */
120static const xmlChar *xmlSchemaNs = (const xmlChar *)
121 "http://www.w3.org/2001/XMLSchema";
122
123static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
124 "http://www.w3.org/2001/XMLSchema-instance";
125
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000126static const xmlChar *xmlNamespaceNs = (const xmlChar *)
127 "http://www.w3.org/2000/xmlns/";
128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000129/*
130* Come casting macros.
131*/
132#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +0000133#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
134#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000135#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
136#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
137#define WXS_PTC_CAST (xmlSchemaParticlePtr)
138#define WXS_TYPE_CAST (xmlSchemaTypePtr)
139#define WXS_ELEM_CAST (xmlSchemaElementPtr)
140#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
141#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
142#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
143#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
144#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
145#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
146#define WXS_IDC_CAST (xmlSchemaIDCPtr)
147#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
148#define WXS_LIST_CAST (xmlSchemaItemListPtr)
Daniel Veillardc0826a72004-08-10 14:17:33 +0000149
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000150/*
151* Macros to query common properties of components.
152*/
153#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
Daniel Veillard4255d502002-04-16 15:50:10 +0000154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000155#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
156/*
157* Macros for element declarations.
158*/
159#define WXS_ELEM_TYPEDEF(e) (e)->subtypes
Daniel Veillardc0826a72004-08-10 14:17:33 +0000160
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000161#define WXS_SUBST_HEAD(item) (item)->refDecl
162/*
163* Macros for attribute declarations.
164*/
165#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
166/*
167* Macros for attribute uses.
168*/
169#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000170
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000171#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000172
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000173#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
174
175#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
176/*
177* Macros for attribute groups.
178*/
179#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
180#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
181/*
182* Macros for particles.
183*/
184#define WXS_PARTICLE(p) WXS_PTC_CAST (p)
185
186#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
187
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000188#define WXS_PARTICLE_TERM_AS_ELEM(p) (WXS_ELEM_CAST WXS_PARTICLE_TERM(p))
189
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000190#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
191/*
192* Macros for model groups definitions.
193*/
194#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
195/*
196* Macros for model groups.
197*/
198#define WXS_IS_MODEL_GROUP(i) \
199 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
200 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
201 ((i)->type == XML_SCHEMA_TYPE_ALL))
202
203#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
204/*
205* Macros for schema buckets.
206*/
207#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
208 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
209
210#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
211 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
212
213#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
214
215#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
216/*
217* Macros for complex/simple types.
218*/
219#define WXS_IS_ANYTYPE(i) \
220 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
221 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
222
223#define WXS_IS_COMPLEX(i) \
224 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
225 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
226
227#define WXS_IS_SIMPLE(item) \
228 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
229 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000230 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000231
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000232#define WXS_IS_ANY_SIMPLE_TYPE(i) \
233 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
234 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000235
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000236#define WXS_IS_RESTRICTION(t) \
237 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000238
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000239#define WXS_IS_EXTENSION(t) \
240 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
241
242#define WXS_IS_TYPE_NOT_FIXED(i) \
243 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
244 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
245
246#define WXS_IS_TYPE_NOT_FIXED_1(item) \
247 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000248 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000249
250#define WXS_TYPE_IS_GLOBAL(t) ((t)->flags & XML_SCHEMAS_TYPE_GLOBAL)
251
252#define WXS_TYPE_IS_LOCAL(t) (((t)->flags & XML_SCHEMAS_TYPE_GLOBAL) == 0)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000253/*
254* Macros for exclusively for complex types.
255*/
256#define WXS_HAS_COMPLEX_CONTENT(item) \
257 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
258 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000259 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
260
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000261#define WXS_HAS_SIMPLE_CONTENT(item) \
262 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000263 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
264
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000265#define WXS_HAS_MIXED_CONTENT(item) \
266 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000267
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000268#define WXS_EMPTIABLE(t) \
269 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000270
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000271#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000272
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000273#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000274
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000275#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
276/*
277* Macros for exclusively for simple types.
278*/
279#define WXS_LIST_ITEMTYPE(t) (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000280
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000281#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000282
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000283#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000284
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000285#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
286/*
287* Misc parser context macros.
288*/
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000289#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
290
291#define WXS_HAS_BUCKETS(ctx) \
292( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
293(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
294
295#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
296
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000297#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000298
299#define WXS_SCHEMA(ctx) (ctx)->schema
300
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000301#define WXS_ADD_LOCAL(ctx, item) \
302 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000303
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000304#define WXS_ADD_GLOBAL(ctx, item) \
305 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000307#define WXS_ADD_PENDING(ctx, item) \
308 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
309/*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000310* xmlSchemaItemList macros.
311*/
312#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
313/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000314* Misc macros.
315*/
316#define IS_SCHEMA(node, type) \
317 ((node != NULL) && (node->ns != NULL) && \
318 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
319 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000320
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000321#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000322
323/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000324* Since we put the default/fixed values into the dict, we can
325* use pointer comparison for those values.
326* REMOVED: (xmlStrEqual((v1), (v2)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000327*/
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000328#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000329
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000330#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000331
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000332#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000333
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000334#define HFAILURE if (res == -1) goto exit_failure;
335
336#define HERROR if (res != 0) goto exit_error;
337
338#define HSTOP(ctx) if ((ctx)->stop) goto exit;
339/*
340* Some flags used for various schema constraints.
341*/
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000342#define SUBSET_RESTRICTION 1<<0
343#define SUBSET_EXTENSION 1<<1
344#define SUBSET_SUBSTITUTION 1<<2
345#define SUBSET_LIST 1<<3
346#define SUBSET_UNION 1<<4
347
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000348typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
349typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000350
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000351typedef struct _xmlSchemaItemList xmlSchemaItemList;
352typedef xmlSchemaItemList *xmlSchemaItemListPtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000353struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000354 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000355 int nbItems; /* used for dynamic addition of schemata */
356 int sizeItems; /* used for dynamic addition of schemata */
357};
358
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000359#define XML_SCHEMA_CTXT_PARSER 1
360#define XML_SCHEMA_CTXT_VALIDATOR 2
361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000362typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
363typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
364struct _xmlSchemaAbstractCtxt {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000365 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000366};
367
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000368typedef struct _xmlSchemaBucket xmlSchemaBucket;
369typedef xmlSchemaBucket *xmlSchemaBucketPtr;
370
371#define XML_SCHEMA_SCHEMA_MAIN 0
372#define XML_SCHEMA_SCHEMA_IMPORT 1
373#define XML_SCHEMA_SCHEMA_INCLUDE 2
374#define XML_SCHEMA_SCHEMA_REDEFINE 3
375
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000376/**
Daniel Veillarddee23482008-04-11 12:58:43 +0000377 * xmlSchemaSchemaRelation:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000378 *
379 * Used to create a graph of schema relationships.
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000380 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000381typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
382typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
383struct _xmlSchemaSchemaRelation {
384 xmlSchemaSchemaRelationPtr next;
385 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
386 const xmlChar *importNamespace;
387 xmlSchemaBucketPtr bucket;
388};
389
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000390#define XML_SCHEMA_BUCKET_MARKED 1<<0
391#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
392
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000393struct _xmlSchemaBucket {
394 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000395 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000396 const xmlChar *schemaLocation;
397 const xmlChar *origTargetNamespace;
398 const xmlChar *targetNamespace;
399 xmlDocPtr doc;
400 xmlSchemaSchemaRelationPtr relations;
401 int located;
402 int parsed;
403 int imported;
404 int preserveDoc;
Daniel Veillarddee23482008-04-11 12:58:43 +0000405 xmlSchemaItemListPtr globals; /* Global components. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000406 xmlSchemaItemListPtr locals; /* Local components. */
407};
408
409/**
Daniel Veillarddee23482008-04-11 12:58:43 +0000410 * xmlSchemaImport:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000411 * (extends xmlSchemaBucket)
412 *
413 * Reflects a schema. Holds some information
414 * about the schema and its toplevel components. Duplicate
415 * toplevel components are not checked at this level.
416 */
417typedef struct _xmlSchemaImport xmlSchemaImport;
418typedef xmlSchemaImport *xmlSchemaImportPtr;
419struct _xmlSchemaImport {
420 int type; /* Main OR import OR include. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000421 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000422 const xmlChar *schemaLocation; /* The URI of the schema document. */
423 /* For chameleon includes, @origTargetNamespace will be NULL */
424 const xmlChar *origTargetNamespace;
Daniel Veillarddee23482008-04-11 12:58:43 +0000425 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000426 * For chameleon includes, @targetNamespace will be the
Daniel Veillarddee23482008-04-11 12:58:43 +0000427 * targetNamespace of the including schema.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000428 */
429 const xmlChar *targetNamespace;
430 xmlDocPtr doc; /* The schema node-tree. */
431 /* @relations will hold any included/imported/redefined schemas. */
432 xmlSchemaSchemaRelationPtr relations;
433 int located;
434 int parsed;
435 int imported;
436 int preserveDoc;
437 xmlSchemaItemListPtr globals;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000438 xmlSchemaItemListPtr locals;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000439 /* The imported schema. */
440 xmlSchemaPtr schema;
441};
442
443/*
444* (extends xmlSchemaBucket)
445*/
446typedef struct _xmlSchemaInclude xmlSchemaInclude;
447typedef xmlSchemaInclude *xmlSchemaIncludePtr;
448struct _xmlSchemaInclude {
449 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000450 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000451 const xmlChar *schemaLocation;
452 const xmlChar *origTargetNamespace;
453 const xmlChar *targetNamespace;
454 xmlDocPtr doc;
455 xmlSchemaSchemaRelationPtr relations;
456 int located;
457 int parsed;
458 int imported;
459 int preserveDoc;
Daniel Veillarddee23482008-04-11 12:58:43 +0000460 xmlSchemaItemListPtr globals; /* Global components. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000461 xmlSchemaItemListPtr locals; /* Local components. */
462
463 /* The owning main or import schema bucket. */
464 xmlSchemaImportPtr ownerImport;
465};
466
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000467/**
468 * xmlSchemaBasicItem:
469 *
470 * The abstract base type for schema components.
471 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000472typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
473typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
474struct _xmlSchemaBasicItem {
475 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000476};
477
478/**
479 * xmlSchemaAnnotItem:
480 *
481 * The abstract base type for annotated schema components.
482 * (Extends xmlSchemaBasicItem)
483 */
484typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
485typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
486struct _xmlSchemaAnnotItem {
487 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000488 xmlSchemaAnnotPtr annot;
489};
490
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000491/**
492 * xmlSchemaTreeItem:
493 *
494 * The abstract base type for tree-like structured schema components.
495 * (Extends xmlSchemaAnnotItem)
496 */
497typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
498typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
499struct _xmlSchemaTreeItem {
500 xmlSchemaTypeType type;
501 xmlSchemaAnnotPtr annot;
502 xmlSchemaTreeItemPtr next;
503 xmlSchemaTreeItemPtr children;
504};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000505
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000506
507#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
508/**
509 * xmlSchemaAttributeUsePtr:
510 *
511 * The abstract base type for tree-like structured schema components.
512 * (Extends xmlSchemaTreeItem)
513 */
514typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
515typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
516struct _xmlSchemaAttributeUse {
517 xmlSchemaTypeType type;
518 xmlSchemaAnnotPtr annot;
519 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000520 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000521 * The attr. decl. OR a QName-ref. to an attr. decl. OR
522 * a QName-ref. to an attribute group definition.
523 */
524 xmlSchemaAttributePtr attrDecl;
525
526 int flags;
527 xmlNodePtr node;
528 int occurs; /* required, optional */
529 const xmlChar * defValue;
530 xmlSchemaValPtr defVal;
531};
532
533/**
534 * xmlSchemaAttributeUseProhibPtr:
535 *
536 * A helper component to reflect attribute prohibitions.
537 * (Extends xmlSchemaBasicItem)
538 */
539typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
540typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
541struct _xmlSchemaAttributeUseProhib {
542 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
543 xmlNodePtr node;
544 const xmlChar *name;
545 const xmlChar *targetNamespace;
546 int isRef;
547};
548
549/**
550 * xmlSchemaRedef:
551 */
552typedef struct _xmlSchemaRedef xmlSchemaRedef;
553typedef xmlSchemaRedef *xmlSchemaRedefPtr;
554struct _xmlSchemaRedef {
555 xmlSchemaRedefPtr next;
556 xmlSchemaBasicItemPtr item; /* The redefining component. */
557 xmlSchemaBasicItemPtr reference; /* The referencing component. */
558 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
559 const xmlChar *refName; /* The name of the to-be-redefined component. */
560 const xmlChar *refTargetNs; /* The target namespace of the
561 to-be-redefined comp. */
562 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
563};
564
565/**
566 * xmlSchemaConstructionCtxt:
567 */
568typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
569typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
570struct _xmlSchemaConstructionCtxt {
571 xmlSchemaPtr mainSchema; /* The main schema. */
572 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
573 xmlDictPtr dict;
574 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
575 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000576 xmlSchemaBucketPtr bucket; /* The current schema bucket */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000577 xmlSchemaItemListPtr pending; /* All Components of all schemas that
578 need to be fixed. */
579 xmlHashTablePtr substGroups;
580 xmlSchemaRedefPtr redefs;
581 xmlSchemaRedefPtr lastRedef;
582};
583
584#define XML_SCHEMAS_PARSE_ERROR 1
585#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
586
587struct _xmlSchemaParserCtxt {
588 int type;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +0000589 void *errCtxt; /* user specific error context */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000590 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
591 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Kasimier T. Buchcikdfbc33a2006-01-03 10:51:59 +0000592 int err;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000593 int nberrors;
594 xmlStructuredErrorFunc serror;
595
596 xmlSchemaConstructionCtxtPtr constructor;
597 int ownsConstructor; /* TODO: Move this to parser *flags*. */
598
599 /* xmlSchemaPtr topschema; */
600 /* xmlHashTablePtr namespaces; */
601
602 xmlSchemaPtr schema; /* The main schema in use */
603 int counter;
604
605 const xmlChar *URL;
606 xmlDocPtr doc;
607 int preserve; /* Whether the doc should be freed */
608
609 const char *buffer;
610 int size;
611
612 /*
613 * Used to build complex element content models
614 */
615 xmlAutomataPtr am;
616 xmlAutomataStatePtr start;
617 xmlAutomataStatePtr end;
618 xmlAutomataStatePtr state;
619
620 xmlDictPtr dict; /* dictionnary for interned string names */
621 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
622 int options;
623 xmlSchemaValidCtxtPtr vctxt;
624 int isS4S;
625 int isRedefine;
626 int xsiAssemble;
627 int stop; /* If the parser should stop; i.e. a critical error. */
628 const xmlChar *targetNamespace;
629 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
630
631 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000632 int redefCounter; /* Used for redefinitions. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000633 xmlSchemaItemListPtr attrProhibs;
634};
635
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000636/**
637 * xmlSchemaQNameRef:
638 *
639 * A component reference item (not a schema component)
640 * (Extends xmlSchemaBasicItem)
641 */
642typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
643typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
644struct _xmlSchemaQNameRef {
645 xmlSchemaTypeType type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000646 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000647 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000648 const xmlChar *name;
649 const xmlChar *targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000650 xmlNodePtr node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000651};
652
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000653/**
654 * xmlSchemaParticle:
655 *
656 * A particle component.
657 * (Extends xmlSchemaTreeItem)
658 */
659typedef struct _xmlSchemaParticle xmlSchemaParticle;
660typedef xmlSchemaParticle *xmlSchemaParticlePtr;
661struct _xmlSchemaParticle {
662 xmlSchemaTypeType type;
663 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000664 xmlSchemaTreeItemPtr next; /* next particle */
665 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
666 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
667 etc.) */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000668 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000669 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000670 xmlNodePtr node;
671};
672
673/**
674 * xmlSchemaModelGroup:
675 *
676 * A model group component.
677 * (Extends xmlSchemaTreeItem)
678 */
679typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
680typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
681struct _xmlSchemaModelGroup {
682 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
683 xmlSchemaAnnotPtr annot;
684 xmlSchemaTreeItemPtr next; /* not used */
685 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
686 xmlNodePtr node;
687};
688
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000689#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000690#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000691/**
692 * xmlSchemaModelGroupDef:
693 *
694 * A model group definition component.
695 * (Extends xmlSchemaTreeItem)
696 */
697typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
698typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
699struct _xmlSchemaModelGroupDef {
700 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
701 xmlSchemaAnnotPtr annot;
702 xmlSchemaTreeItemPtr next; /* not used */
703 xmlSchemaTreeItemPtr children; /* the "model group" */
704 const xmlChar *name;
705 const xmlChar *targetNamespace;
706 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000707 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000708};
709
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000710typedef struct _xmlSchemaIDC xmlSchemaIDC;
711typedef xmlSchemaIDC *xmlSchemaIDCPtr;
712
713/**
714 * xmlSchemaIDCSelect:
715 *
716 * The identity-constraint "field" and "selector" item, holding the
717 * XPath expression.
718 */
719typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
720typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000721struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000722 xmlSchemaIDCSelectPtr next;
723 xmlSchemaIDCPtr idc;
724 int index; /* an index position if significant for IDC key-sequences */
725 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000726 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000727};
728
729/**
730 * xmlSchemaIDC:
731 *
732 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000733 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000734 */
735
736struct _xmlSchemaIDC {
737 xmlSchemaTypeType type;
738 xmlSchemaAnnotPtr annot;
739 xmlSchemaIDCPtr next;
740 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000741 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000742 const xmlChar *targetNamespace;
743 xmlSchemaIDCSelectPtr selector;
744 xmlSchemaIDCSelectPtr fields;
745 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000746 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000747};
748
749/**
750 * xmlSchemaIDCAug:
751 *
752 * The augmented IDC information used for validation.
753 */
754typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
755typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
756struct _xmlSchemaIDCAug {
757 xmlSchemaIDCAugPtr next; /* next in a list */
758 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000759 int keyrefDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000760 tables need to be bubbled upwards */
761};
762
763/**
764 * xmlSchemaPSVIIDCKeySequence:
765 *
766 * The key sequence of a node table item.
767 */
768typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
769typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
770struct _xmlSchemaPSVIIDCKey {
771 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000772 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000773};
774
775/**
776 * xmlSchemaPSVIIDCNode:
777 *
778 * The node table item of a node table.
779 */
780typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
781typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
782struct _xmlSchemaPSVIIDCNode {
783 xmlNodePtr node;
784 xmlSchemaPSVIIDCKeyPtr *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000785 int nodeLine;
Daniel Veillarddee23482008-04-11 12:58:43 +0000786 int nodeQNameID;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000787
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000788};
789
790/**
791 * xmlSchemaPSVIIDCBinding:
792 *
793 * The identity-constraint binding item of the [identity-constraint table].
794 */
795typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
796typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
797struct _xmlSchemaPSVIIDCBinding {
798 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
799 xmlSchemaIDCPtr definition; /* the IDC definition */
800 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
801 int nbNodes; /* number of entries in the node table */
802 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000803 xmlSchemaItemListPtr dupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000804};
805
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000806
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000807#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
808#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
809
810#define XPATH_STATE_OBJ_MATCHES -2
811#define XPATH_STATE_OBJ_BLOCKED -3
812
813typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
814typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
815
816/**
817 * xmlSchemaIDCStateObj:
818 *
819 * The state object used to evaluate XPath expressions.
820 */
821typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
822typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
823struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000824 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000825 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000826 int depth; /* depth of creation */
827 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000828 int nbHistory;
829 int sizeHistory;
830 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
831 matcher */
832 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000833 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000834};
835
836#define IDC_MATCHER 0
837
838/**
839 * xmlSchemaIDCMatcher:
840 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000841 * Used to evaluate IDC selectors (and fields).
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000842 */
843struct _xmlSchemaIDCMatcher {
844 int type;
845 int depth; /* the tree depth at creation time */
846 xmlSchemaIDCMatcherPtr next; /* next in the list */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000847 xmlSchemaIDCMatcherPtr nextCached; /* next in the cache list */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000848 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000849 int idcType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000850 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
851 elements */
852 int sizeKeySeqs;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000853 xmlSchemaItemListPtr targets; /* list of target-node
854 (xmlSchemaPSVIIDCNodePtr) entries */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000855};
856
857/*
858* Element info flags.
859*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000860#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
861#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
862#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
863#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
864
865#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
866#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
867#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
868
869#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
870#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
871#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
872#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000873
874/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000875 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000876 *
877 * Holds information of an element node.
878 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000879struct _xmlSchemaNodeInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000880 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000881 xmlNodePtr node;
Daniel Veillarddee23482008-04-11 12:58:43 +0000882 int nodeLine;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000883 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000884 const xmlChar *nsName;
885 const xmlChar *value;
886 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000887 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000888
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000889 int flags; /* combination of node info flags */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000890
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000891 int valNeeded;
892 int normVal;
893
894 xmlSchemaElementPtr decl; /* the element/attribute declaration */
895 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000896 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
897 for the scope element*/
898 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
899 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000900 xmlRegExecCtxtPtr regexCtxt;
901
902 const xmlChar **nsBindings; /* Namespace bindings on this element */
903 int nbNsBindings;
Daniel Veillarddee23482008-04-11 12:58:43 +0000904 int sizeNsBindings;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000905
906 int hasKeyrefs;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +0000907 int appliedXPath; /* Indicates that an XPath has been applied. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000908};
909
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000910#define XML_SCHEMAS_ATTR_UNKNOWN 1
911#define XML_SCHEMAS_ATTR_ASSESSED 2
912#define XML_SCHEMAS_ATTR_PROHIBITED 3
913#define XML_SCHEMAS_ATTR_ERR_MISSING 4
914#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
915#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
916#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
917#define XML_SCHEMAS_ATTR_DEFAULT 8
918#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
919#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
920#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
921#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
922#define XML_SCHEMAS_ATTR_WILD_SKIP 13
923#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
924#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
925#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
926#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000927/*
928* @metaType values of xmlSchemaAttrInfo.
929*/
930#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
931#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
932#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
933#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
934#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000935
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000936typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
937typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
938struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000939 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000940 xmlNodePtr node;
Daniel Veillarddee23482008-04-11 12:58:43 +0000941 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000942 const xmlChar *localName;
943 const xmlChar *nsName;
944 const xmlChar *value;
945 xmlSchemaValPtr val; /* the pre-computed value if any */
946 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
947 int flags; /* combination of node info flags */
948
949 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000950 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000951 int state;
952 int metaType;
953 const xmlChar *vcValue; /* the value constraint value */
954 xmlSchemaNodeInfoPtr parent;
955};
956
957
958#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000959/**
960 * xmlSchemaValidCtxt:
961 *
962 * A Schemas validation context
963 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000964struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000965 int type;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000966 void *errCtxt; /* user specific data block */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000967 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000968 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000969 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000970
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000971 xmlSchemaPtr schema; /* The schema in use */
972 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000973 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000974 xmlCharEncoding enc;
975 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000976 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000977 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000978
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000979 int err;
980 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000981
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000982 xmlNodePtr node;
983 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000984 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000985
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000986 xmlRegExecCtxtPtr regexp;
987 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000988
Daniel Veillardc0826a72004-08-10 14:17:33 +0000989 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000990 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000991 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000992 xmlSchemaParserCtxtPtr pctxt;
993 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000994
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000995 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000996 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000997 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000998 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000999
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001000 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
1001
1002 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
1003 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +00001004 xmlSchemaIDCMatcherPtr idcMatcherCache; /* Cache for IDC matcher objects. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001005
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001006 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
1007 int nbIdcNodes;
1008 int sizeIdcNodes;
1009
1010 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1011 int nbIdcKeys;
1012 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001013
1014 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001015
1016 xmlDictPtr dict;
1017
Daniel Veillard39e5c892005-07-03 22:48:50 +00001018#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001019 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001020#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001021
1022 xmlSchemaAttrInfoPtr *attrInfos;
1023 int nbAttrInfos;
1024 int sizeAttrInfos;
1025
1026 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001027 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001028 int hasKeyrefs;
1029 int createIDCNodeTables;
1030 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001031};
1032
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001033/**
1034 * xmlSchemaSubstGroup:
1035 *
1036 *
1037 */
1038typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1039typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1040struct _xmlSchemaSubstGroup {
1041 xmlSchemaElementPtr head;
1042 xmlSchemaItemListPtr members;
1043};
1044
Daniel Veillard4255d502002-04-16 15:50:10 +00001045/************************************************************************
1046 * *
1047 * Some predeclarations *
1048 * *
1049 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001050
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001051static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1052 xmlSchemaPtr schema,
1053 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001054static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1055 xmlSchemaPtr schema,
1056 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001057static int
1058xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001059 xmlSchemaAbstractCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001060static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001061xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1062static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001063xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1064 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001065static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001066xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1067 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001068static void
1069xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001070static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001071xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001072static xmlSchemaTreeItemPtr
1073xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1074 xmlNodePtr node, xmlSchemaTypeType type,
1075 int withParticle);
1076static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001077xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001078static xmlSchemaTypeLinkPtr
1079xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001080static void
1081xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1082 const char *funcName,
1083 const char *message);
1084static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001085xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001086 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001087 xmlSchemaTypePtr baseType,
1088 int subset);
1089static void
1090xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001091 xmlSchemaParserCtxtPtr ctxt);
1092static void
1093xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001094static xmlSchemaQNameRefPtr
1095xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1096 xmlSchemaPtr schema,
1097 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001098
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001099/************************************************************************
1100 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001101 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001102 * *
1103 ************************************************************************/
1104
1105/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001106 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001107 * @type: the type of the schema item
1108 *
1109 * Returns the component name of a schema item.
1110 */
1111static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001112xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001113{
1114 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001115 case XML_SCHEMA_TYPE_BASIC:
1116 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001117 case XML_SCHEMA_TYPE_SIMPLE:
1118 return(BAD_CAST "simple type definition");
1119 case XML_SCHEMA_TYPE_COMPLEX:
1120 return(BAD_CAST "complex type definition");
1121 case XML_SCHEMA_TYPE_ELEMENT:
1122 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001123 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1124 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001125 case XML_SCHEMA_TYPE_ATTRIBUTE:
1126 return(BAD_CAST "attribute declaration");
1127 case XML_SCHEMA_TYPE_GROUP:
1128 return(BAD_CAST "model group definition");
1129 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1130 return(BAD_CAST "attribute group definition");
1131 case XML_SCHEMA_TYPE_NOTATION:
1132 return(BAD_CAST "notation declaration");
1133 case XML_SCHEMA_TYPE_SEQUENCE:
1134 return(BAD_CAST "model group (sequence)");
1135 case XML_SCHEMA_TYPE_CHOICE:
1136 return(BAD_CAST "model group (choice)");
1137 case XML_SCHEMA_TYPE_ALL:
1138 return(BAD_CAST "model group (all)");
1139 case XML_SCHEMA_TYPE_PARTICLE:
1140 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001141 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001142 return(BAD_CAST "unique identity-constraint");
1143 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001144 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001145 return(BAD_CAST "key identity-constraint");
1146 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001147 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001148 return(BAD_CAST "keyref identity-constraint");
1149 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001150 case XML_SCHEMA_TYPE_ANY:
1151 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001152 case XML_SCHEMA_EXTRA_QNAMEREF:
1153 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001154 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1155 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001156 default:
1157 return(BAD_CAST "Not a schema component");
1158 }
1159}
1160
1161/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001162 * xmlSchemaGetComponentTypeStr:
1163 * @type: the type of the schema item
1164 *
1165 * Returns the component name of a schema item.
1166 */
1167static const xmlChar *
1168xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1169{
1170 switch (item->type) {
1171 case XML_SCHEMA_TYPE_BASIC:
1172 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1173 return(BAD_CAST "complex type definition");
1174 else
Daniel Veillarddee23482008-04-11 12:58:43 +00001175 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001176 default:
1177 return(xmlSchemaItemTypeToStr(item->type));
1178 }
1179}
1180
1181/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001182 * xmlSchemaGetComponentNode:
1183 * @item: a schema component
1184 *
1185 * Returns node associated with the schema component.
1186 * NOTE that such a node need not be available; plus, a component's
1187 * node need not to reflect the component directly, since there is no
1188 * one-to-one relationship between the XML Schema representation and
1189 * the component representation.
1190 */
1191static xmlNodePtr
1192xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1193{
1194 switch (item->type) {
1195 case XML_SCHEMA_TYPE_ELEMENT:
1196 return (((xmlSchemaElementPtr) item)->node);
1197 case XML_SCHEMA_TYPE_ATTRIBUTE:
1198 return (((xmlSchemaAttributePtr) item)->node);
1199 case XML_SCHEMA_TYPE_COMPLEX:
1200 case XML_SCHEMA_TYPE_SIMPLE:
1201 return (((xmlSchemaTypePtr) item)->node);
1202 case XML_SCHEMA_TYPE_ANY:
1203 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1204 return (((xmlSchemaWildcardPtr) item)->node);
1205 case XML_SCHEMA_TYPE_PARTICLE:
1206 return (((xmlSchemaParticlePtr) item)->node);
1207 case XML_SCHEMA_TYPE_SEQUENCE:
1208 case XML_SCHEMA_TYPE_CHOICE:
1209 case XML_SCHEMA_TYPE_ALL:
1210 return (((xmlSchemaModelGroupPtr) item)->node);
1211 case XML_SCHEMA_TYPE_GROUP:
1212 return (((xmlSchemaModelGroupDefPtr) item)->node);
1213 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1214 return (((xmlSchemaAttributeGroupPtr) item)->node);
1215 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1216 case XML_SCHEMA_TYPE_IDC_KEY:
1217 case XML_SCHEMA_TYPE_IDC_KEYREF:
1218 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001219 case XML_SCHEMA_EXTRA_QNAMEREF:
1220 return(((xmlSchemaQNameRefPtr) item)->node);
1221 /* TODO: What to do with NOTATIONs?
1222 case XML_SCHEMA_TYPE_NOTATION:
1223 return (((xmlSchemaNotationPtr) item)->node);
1224 */
1225 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1226 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001227 default:
1228 return (NULL);
1229 }
1230}
1231
1232#if 0
1233/**
1234 * xmlSchemaGetNextComponent:
1235 * @item: a schema component
1236 *
1237 * Returns the next sibling of the schema component.
1238 */
1239static xmlSchemaBasicItemPtr
1240xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1241{
1242 switch (item->type) {
1243 case XML_SCHEMA_TYPE_ELEMENT:
1244 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1245 case XML_SCHEMA_TYPE_ATTRIBUTE:
1246 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1247 case XML_SCHEMA_TYPE_COMPLEX:
1248 case XML_SCHEMA_TYPE_SIMPLE:
1249 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1250 case XML_SCHEMA_TYPE_ANY:
1251 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1252 return (NULL);
1253 case XML_SCHEMA_TYPE_PARTICLE:
1254 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1255 case XML_SCHEMA_TYPE_SEQUENCE:
1256 case XML_SCHEMA_TYPE_CHOICE:
1257 case XML_SCHEMA_TYPE_ALL:
1258 return (NULL);
1259 case XML_SCHEMA_TYPE_GROUP:
1260 return (NULL);
1261 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1262 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1263 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1264 case XML_SCHEMA_TYPE_IDC_KEY:
1265 case XML_SCHEMA_TYPE_IDC_KEYREF:
1266 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1267 default:
1268 return (NULL);
1269 }
1270}
1271#endif
1272
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001273
1274/**
1275 * xmlSchemaFormatQName:
1276 * @buf: the string buffer
1277 * @namespaceName: the namespace name
1278 * @localName: the local name
1279 *
1280 * Returns the given QName in the format "{namespaceName}localName" or
1281 * just "localName" if @namespaceName is NULL.
1282 *
1283 * Returns the localName if @namespaceName is NULL, a formatted
1284 * string otherwise.
1285 */
1286static const xmlChar*
1287xmlSchemaFormatQName(xmlChar **buf,
1288 const xmlChar *namespaceName,
1289 const xmlChar *localName)
1290{
1291 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001292 if (namespaceName != NULL) {
1293 *buf = xmlStrdup(BAD_CAST "{");
1294 *buf = xmlStrcat(*buf, namespaceName);
1295 *buf = xmlStrcat(*buf, BAD_CAST "}");
1296 }
1297 if (localName != NULL) {
1298 if (namespaceName == NULL)
1299 return(localName);
1300 *buf = xmlStrcat(*buf, localName);
1301 } else {
1302 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
Daniel Veillarddee23482008-04-11 12:58:43 +00001303 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001304 return ((const xmlChar *) *buf);
1305}
1306
Daniel Veillarddee23482008-04-11 12:58:43 +00001307static const xmlChar*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001308xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1309{
1310 if (ns != NULL)
1311 return (xmlSchemaFormatQName(buf, ns->href, localName));
1312 else
1313 return (xmlSchemaFormatQName(buf, NULL, localName));
1314}
1315
1316static const xmlChar *
1317xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1318{
1319 switch (item->type) {
1320 case XML_SCHEMA_TYPE_ELEMENT:
1321 return (((xmlSchemaElementPtr) item)->name);
1322 case XML_SCHEMA_TYPE_ATTRIBUTE:
1323 return (((xmlSchemaAttributePtr) item)->name);
1324 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1325 return (((xmlSchemaAttributeGroupPtr) item)->name);
1326 case XML_SCHEMA_TYPE_BASIC:
1327 case XML_SCHEMA_TYPE_SIMPLE:
1328 case XML_SCHEMA_TYPE_COMPLEX:
1329 return (((xmlSchemaTypePtr) item)->name);
1330 case XML_SCHEMA_TYPE_GROUP:
1331 return (((xmlSchemaModelGroupDefPtr) item)->name);
1332 case XML_SCHEMA_TYPE_IDC_KEY:
1333 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1334 case XML_SCHEMA_TYPE_IDC_KEYREF:
1335 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001336 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1337 if (WXS_ATTRUSE_DECL(item) != NULL) {
1338 return(xmlSchemaGetComponentName(
1339 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1340 } else
1341 return(NULL);
1342 case XML_SCHEMA_EXTRA_QNAMEREF:
1343 return (((xmlSchemaQNameRefPtr) item)->name);
1344 case XML_SCHEMA_TYPE_NOTATION:
1345 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001346 default:
1347 /*
1348 * Other components cannot have names.
1349 */
1350 break;
1351 }
1352 return (NULL);
1353}
1354
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001355#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1356#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1357/*
1358static const xmlChar *
1359xmlSchemaGetQNameRefName(void *ref)
1360{
1361 return(((xmlSchemaQNameRefPtr) ref)->name);
1362}
1363
1364static const xmlChar *
1365xmlSchemaGetQNameRefTargetNs(void *ref)
1366{
1367 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1368}
1369*/
1370
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001371static const xmlChar *
1372xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1373{
1374 switch (item->type) {
1375 case XML_SCHEMA_TYPE_ELEMENT:
1376 return (((xmlSchemaElementPtr) item)->targetNamespace);
1377 case XML_SCHEMA_TYPE_ATTRIBUTE:
1378 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1379 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1380 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1381 case XML_SCHEMA_TYPE_BASIC:
1382 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1383 case XML_SCHEMA_TYPE_SIMPLE:
1384 case XML_SCHEMA_TYPE_COMPLEX:
1385 return (((xmlSchemaTypePtr) item)->targetNamespace);
1386 case XML_SCHEMA_TYPE_GROUP:
1387 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1388 case XML_SCHEMA_TYPE_IDC_KEY:
1389 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1390 case XML_SCHEMA_TYPE_IDC_KEYREF:
1391 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001392 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1393 if (WXS_ATTRUSE_DECL(item) != NULL) {
1394 return(xmlSchemaGetComponentTargetNs(
1395 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1396 }
1397 /* TODO: Will returning NULL break something? */
1398 break;
1399 case XML_SCHEMA_EXTRA_QNAMEREF:
1400 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1401 case XML_SCHEMA_TYPE_NOTATION:
1402 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001403 default:
1404 /*
1405 * Other components cannot have names.
1406 */
1407 break;
1408 }
1409 return (NULL);
1410}
1411
1412static const xmlChar*
1413xmlSchemaGetComponentQName(xmlChar **buf,
1414 void *item)
1415{
1416 return (xmlSchemaFormatQName(buf,
1417 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1418 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1419}
1420
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001421static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001422xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001423{
1424 xmlChar *str = NULL;
1425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001426 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001427 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001428 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1429 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001430 *buf = xmlStrcat(*buf, BAD_CAST "'");
1431 FREE_AND_NULL(str);
1432 return(*buf);
1433}
1434
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001435static const xmlChar*
1436xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1437{
1438 return(xmlSchemaGetComponentDesignation(buf, idc));
1439}
1440
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001441/**
1442 * xmlSchemaWildcardPCToString:
1443 * @pc: the type of processContents
1444 *
1445 * Returns a string representation of the type of
1446 * processContents.
1447 */
1448static const xmlChar *
1449xmlSchemaWildcardPCToString(int pc)
1450{
1451 switch (pc) {
1452 case XML_SCHEMAS_ANY_SKIP:
1453 return (BAD_CAST "skip");
1454 case XML_SCHEMAS_ANY_LAX:
1455 return (BAD_CAST "lax");
1456 case XML_SCHEMAS_ANY_STRICT:
1457 return (BAD_CAST "strict");
1458 default:
1459 return (BAD_CAST "invalid process contents");
1460 }
1461}
1462
1463/**
1464 * xmlSchemaGetCanonValueWhtspExt:
1465 * @val: the precomputed value
1466 * @retValue: the returned value
1467 * @ws: the whitespace type of the value
1468 *
1469 * Get a the cononical representation of the value.
1470 * The caller has to free the returned retValue.
1471 *
1472 * Returns 0 if the value could be built and -1 in case of
1473 * API errors or if the value type is not supported yet.
1474 */
1475static int
1476xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1477 xmlSchemaWhitespaceValueType ws,
1478 xmlChar **retValue)
1479{
1480 int list;
1481 xmlSchemaValType valType;
1482 const xmlChar *value, *value2 = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00001483
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001484
1485 if ((retValue == NULL) || (val == NULL))
1486 return (-1);
1487 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1488 *retValue = NULL;
1489 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00001490 value = NULL;
1491 valType = xmlSchemaGetValType(val);
1492 switch (valType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001493 case XML_SCHEMAS_STRING:
1494 case XML_SCHEMAS_NORMSTRING:
1495 case XML_SCHEMAS_ANYSIMPLETYPE:
1496 value = xmlSchemaValueGetAsString(val);
1497 if (value != NULL) {
1498 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1499 value2 = xmlSchemaCollapseString(value);
1500 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1501 value2 = xmlSchemaWhiteSpaceReplace(value);
1502 if (value2 != NULL)
1503 value = value2;
1504 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001505 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001506 default:
1507 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1508 if (value2 != NULL)
1509 xmlFree((xmlChar *) value2);
1510 goto internal_error;
1511 }
1512 value = value2;
1513 }
1514 if (*retValue == NULL)
1515 if (value == NULL) {
1516 if (! list)
1517 *retValue = xmlStrdup(BAD_CAST "");
1518 } else
1519 *retValue = xmlStrdup(value);
1520 else if (value != NULL) {
1521 /* List. */
1522 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1523 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1524 }
1525 FREE_AND_NULL(value2)
1526 val = xmlSchemaValueGetNext(val);
1527 } while (val != NULL);
1528
1529 return (0);
1530internal_error:
1531 if (*retValue != NULL)
1532 xmlFree((xmlChar *) (*retValue));
1533 if (value2 != NULL)
1534 xmlFree((xmlChar *) value2);
1535 return (-1);
1536}
1537
1538/**
1539 * xmlSchemaFormatItemForReport:
1540 * @buf: the string buffer
1541 * @itemDes: the designation of the item
1542 * @itemName: the name of the item
Daniel Veillarddee23482008-04-11 12:58:43 +00001543 * @item: the item as an object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001544 * @itemNode: the node of the item
1545 * @local: the local name
1546 * @parsing: if the function is used during the parse
1547 *
1548 * Returns a representation of the given item used
Daniel Veillarddee23482008-04-11 12:58:43 +00001549 * for error reports.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001550 *
Daniel Veillarddee23482008-04-11 12:58:43 +00001551 * The following order is used to build the resulting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001552 * designation if the arguments are not NULL:
1553 * 1a. If itemDes not NULL -> itemDes
1554 * 1b. If (itemDes not NULL) and (itemName not NULL)
1555 * -> itemDes + itemName
1556 * 2. If the preceding was NULL and (item not NULL) -> item
1557 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
Daniel Veillarddee23482008-04-11 12:58:43 +00001558 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001559 * If the itemNode is an attribute node, the name of the attribute
1560 * will be appended to the result.
1561 *
1562 * Returns the formatted string and sets @buf to the resulting value.
Daniel Veillarddee23482008-04-11 12:58:43 +00001563 */
1564static xmlChar*
1565xmlSchemaFormatItemForReport(xmlChar **buf,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001566 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001567 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001568 xmlNodePtr itemNode)
1569{
1570 xmlChar *str = NULL;
1571 int named = 1;
1572
1573 if (*buf != NULL) {
1574 xmlFree(*buf);
1575 *buf = NULL;
1576 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001577
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001578 if (itemDes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00001579 *buf = xmlStrdup(itemDes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001580 } else if (item != NULL) {
1581 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001582 case XML_SCHEMA_TYPE_BASIC: {
1583 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1584
1585 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001586 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001587 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001588 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001589 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001590 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1591 else
1592 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001593 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001594 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001595 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001596 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001597 case XML_SCHEMA_TYPE_SIMPLE: {
1598 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1599
1600 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 *buf = xmlStrdup(BAD_CAST"");
1602 } else {
1603 *buf = xmlStrdup(BAD_CAST "local ");
1604 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001605 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001606 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001607 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001608 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001609 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001610 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1611 else
1612 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001613 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001614 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001615 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001616 *buf = xmlStrcat(*buf, BAD_CAST "'");
1617 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001618 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001619 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001620 case XML_SCHEMA_TYPE_COMPLEX: {
1621 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1622
1623 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001624 *buf = xmlStrdup(BAD_CAST "");
1625 else
1626 *buf = xmlStrdup(BAD_CAST "local ");
1627 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001628 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001629 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001630 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001631 *buf = xmlStrcat(*buf, BAD_CAST "'");
1632 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001633 }
1634 break;
1635 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1636 xmlSchemaAttributeUsePtr ause;
Daniel Veillarddee23482008-04-11 12:58:43 +00001637
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001638 ause = WXS_ATTR_USE_CAST item;
1639 *buf = xmlStrdup(BAD_CAST "attribute use ");
1640 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1641 *buf = xmlStrcat(*buf, BAD_CAST "'");
1642 *buf = xmlStrcat(*buf,
1643 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1644 FREE_AND_NULL(str)
1645 *buf = xmlStrcat(*buf, BAD_CAST "'");
1646 } else {
1647 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1648 }
1649 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001650 break;
1651 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1652 xmlSchemaAttributePtr attr;
Daniel Veillarddee23482008-04-11 12:58:43 +00001653
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001654 attr = (xmlSchemaAttributePtr) item;
1655 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1656 *buf = xmlStrcat(*buf, BAD_CAST " '");
1657 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1658 attr->targetNamespace, attr->name));
1659 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001660 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001661 }
1662 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001663 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1664 xmlSchemaGetComponentDesignation(buf, item);
1665 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001666 case XML_SCHEMA_TYPE_ELEMENT: {
1667 xmlSchemaElementPtr elem;
1668
Daniel Veillarddee23482008-04-11 12:58:43 +00001669 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001670 *buf = xmlStrdup(BAD_CAST "element decl.");
1671 *buf = xmlStrcat(*buf, BAD_CAST " '");
1672 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1673 elem->targetNamespace, elem->name));
1674 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001675 }
1676 break;
1677 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1678 case XML_SCHEMA_TYPE_IDC_KEY:
Daniel Veillarddee23482008-04-11 12:58:43 +00001679 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001680 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1681 *buf = xmlStrdup(BAD_CAST "unique '");
1682 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1683 *buf = xmlStrdup(BAD_CAST "key '");
1684 else
1685 *buf = xmlStrdup(BAD_CAST "keyRef '");
1686 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1687 *buf = xmlStrcat(*buf, BAD_CAST "'");
1688 break;
1689 case XML_SCHEMA_TYPE_ANY:
1690 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1691 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1692 ((xmlSchemaWildcardPtr) item)->processContents));
1693 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1694 break;
1695 case XML_SCHEMA_FACET_MININCLUSIVE:
1696 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1697 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1698 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1699 case XML_SCHEMA_FACET_TOTALDIGITS:
1700 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1701 case XML_SCHEMA_FACET_PATTERN:
1702 case XML_SCHEMA_FACET_ENUMERATION:
1703 case XML_SCHEMA_FACET_WHITESPACE:
1704 case XML_SCHEMA_FACET_LENGTH:
1705 case XML_SCHEMA_FACET_MAXLENGTH:
1706 case XML_SCHEMA_FACET_MINLENGTH:
1707 *buf = xmlStrdup(BAD_CAST "facet '");
1708 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1709 *buf = xmlStrcat(*buf, BAD_CAST "'");
1710 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001711 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001712 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001713 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001714 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001715 *buf = xmlStrcat(*buf, BAD_CAST "'");
1716 FREE_AND_NULL(str)
1717 }
1718 break;
1719 case XML_SCHEMA_TYPE_SEQUENCE:
1720 case XML_SCHEMA_TYPE_CHOICE:
1721 case XML_SCHEMA_TYPE_ALL:
1722 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001723 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1724 break;
1725 case XML_SCHEMA_TYPE_NOTATION: {
1726 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1727 *buf = xmlStrcat(*buf, BAD_CAST " '");
1728 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1729 *buf = xmlStrcat(*buf, BAD_CAST "'");
1730 FREE_AND_NULL(str);
1731 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001732 default:
1733 named = 0;
1734 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001735 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001736 named = 0;
1737
1738 if ((named == 0) && (itemNode != NULL)) {
1739 xmlNodePtr elem;
1740
1741 if (itemNode->type == XML_ATTRIBUTE_NODE)
1742 elem = itemNode->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00001743 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001744 elem = itemNode;
1745 *buf = xmlStrdup(BAD_CAST "Element '");
1746 if (elem->ns != NULL) {
1747 *buf = xmlStrcat(*buf,
1748 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1749 FREE_AND_NULL(str)
1750 } else
1751 *buf = xmlStrcat(*buf, elem->name);
1752 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +00001753
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001754 }
1755 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1756 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1757 if (itemNode->ns != NULL) {
1758 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1759 itemNode->ns->href, itemNode->name));
1760 FREE_AND_NULL(str)
1761 } else
1762 *buf = xmlStrcat(*buf, itemNode->name);
1763 *buf = xmlStrcat(*buf, BAD_CAST "'");
1764 }
1765 FREE_AND_NULL(str)
Daniel Veillarddee23482008-04-11 12:58:43 +00001766
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001767 return (*buf);
1768}
1769
1770/**
1771 * xmlSchemaFormatFacetEnumSet:
1772 * @buf: the string buffer
1773 * @type: the type holding the enumeration facets
1774 *
1775 * Builds a string consisting of all enumeration elements.
1776 *
1777 * Returns a string of all enumeration elements.
1778 */
1779static const xmlChar *
1780xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1781 xmlChar **buf, xmlSchemaTypePtr type)
1782{
1783 xmlSchemaFacetPtr facet;
1784 xmlSchemaWhitespaceValueType ws;
1785 xmlChar *value = NULL;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001786 int res, found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001787
1788 if (*buf != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00001789 xmlFree(*buf);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001790 *buf = NULL;
1791
1792 do {
1793 /*
1794 * Use the whitespace type of the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +00001795 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001796 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1797 for (facet = type->facets; facet != NULL; facet = facet->next) {
1798 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1799 continue;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001800 found = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001801 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1802 ws, &value);
1803 if (res == -1) {
1804 xmlSchemaInternalErr(actxt,
1805 "xmlSchemaFormatFacetEnumSet",
1806 "compute the canonical lexical representation");
1807 if (*buf != NULL)
1808 xmlFree(*buf);
1809 *buf = NULL;
1810 return (NULL);
1811 }
1812 if (*buf == NULL)
1813 *buf = xmlStrdup(BAD_CAST "'");
1814 else
1815 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1816 *buf = xmlStrcat(*buf, BAD_CAST value);
1817 *buf = xmlStrcat(*buf, BAD_CAST "'");
1818 if (value != NULL) {
1819 xmlFree((xmlChar *)value);
1820 value = NULL;
1821 }
1822 }
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001823 /*
1824 * The enumeration facet of a type restricts the enumeration
1825 * facet of the ancestor type; i.e., such restricted enumerations
1826 * do not belong to the set of the given type. Thus we break
1827 * on the first found enumeration.
1828 */
1829 if (found)
1830 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001831 type = type->baseType;
1832 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1833
1834 return ((const xmlChar *) *buf);
1835}
1836
1837/************************************************************************
1838 * *
1839 * Error functions *
1840 * *
1841 ************************************************************************/
1842
1843#if 0
1844static void
1845xmlSchemaErrMemory(const char *msg)
1846{
1847 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1848 msg);
1849}
1850#endif
1851
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001852static void
1853xmlSchemaPSimpleErr(const char *msg)
1854{
1855 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1856 msg);
1857}
1858
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001859/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001860 * xmlSchemaPErrMemory:
1861 * @node: a context node
1862 * @extra: extra informations
1863 *
1864 * Handle an out of memory condition
1865 */
1866static void
1867xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1868 const char *extra, xmlNodePtr node)
1869{
1870 if (ctxt != NULL)
1871 ctxt->nberrors++;
1872 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1873 extra);
1874}
1875
1876/**
1877 * xmlSchemaPErr:
1878 * @ctxt: the parsing context
1879 * @node: the context node
1880 * @error: the error code
1881 * @msg: the error message
1882 * @str1: extra data
1883 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001884 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001885 * Handle a parser error
1886 */
1887static void
1888xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1889 const char *msg, const xmlChar * str1, const xmlChar * str2)
1890{
1891 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001892 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001893 void *data = NULL;
1894
1895 if (ctxt != NULL) {
1896 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001897 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001898 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001899 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001900 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001901 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001902 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001903 error, XML_ERR_ERROR, NULL, 0,
1904 (const char *) str1, (const char *) str2, NULL, 0, 0,
1905 msg, str1, str2);
1906}
1907
1908/**
1909 * xmlSchemaPErr2:
1910 * @ctxt: the parsing context
1911 * @node: the context node
1912 * @node: the current child
1913 * @error: the error code
1914 * @msg: the error message
1915 * @str1: extra data
1916 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001917 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001918 * Handle a parser error
1919 */
1920static void
1921xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1922 xmlNodePtr child, int error,
1923 const char *msg, const xmlChar * str1, const xmlChar * str2)
1924{
1925 if (child != NULL)
1926 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1927 else
1928 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1929}
1930
Daniel Veillard01fa6152004-06-29 17:04:39 +00001931
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001932/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001933 * xmlSchemaPErrExt:
1934 * @ctxt: the parsing context
1935 * @node: the context node
Daniel Veillarddee23482008-04-11 12:58:43 +00001936 * @error: the error code
Daniel Veillard3646d642004-06-02 19:19:14 +00001937 * @strData1: extra data
1938 * @strData2: extra data
1939 * @strData3: extra data
1940 * @msg: the message
1941 * @str1: extra parameter for the message display
1942 * @str2: extra parameter for the message display
1943 * @str3: extra parameter for the message display
1944 * @str4: extra parameter for the message display
1945 * @str5: extra parameter for the message display
Daniel Veillarddee23482008-04-11 12:58:43 +00001946 *
Daniel Veillard3646d642004-06-02 19:19:14 +00001947 * Handle a parser error
1948 */
1949static void
1950xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
Daniel Veillarddee23482008-04-11 12:58:43 +00001951 const xmlChar * strData1, const xmlChar * strData2,
1952 const xmlChar * strData3, const char *msg, const xmlChar * str1,
Daniel Veillard3646d642004-06-02 19:19:14 +00001953 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1954 const xmlChar * str5)
1955{
1956
1957 xmlGenericErrorFunc channel = NULL;
1958 xmlStructuredErrorFunc schannel = NULL;
1959 void *data = NULL;
1960
1961 if (ctxt != NULL) {
1962 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001963 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001964 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001965 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001966 schannel = ctxt->serror;
1967 }
1968 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1969 error, XML_ERR_ERROR, NULL, 0,
Daniel Veillarddee23482008-04-11 12:58:43 +00001970 (const char *) strData1, (const char *) strData2,
1971 (const char *) strData3, 0, 0, msg, str1, str2,
William M. Brack803812b2004-06-03 02:11:24 +00001972 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001973}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001974
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001975/************************************************************************
1976 * *
1977 * Allround error functions *
1978 * *
1979 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001980
1981/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001982 * xmlSchemaVTypeErrMemory:
1983 * @node: a context node
1984 * @extra: extra informations
1985 *
1986 * Handle an out of memory condition
1987 */
1988static void
1989xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1990 const char *extra, xmlNodePtr node)
1991{
1992 if (ctxt != NULL) {
1993 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001994 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001995 }
1996 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1997 extra);
1998}
1999
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002000static void
2001xmlSchemaPSimpleInternalErr(xmlNodePtr node,
2002 const char *msg, const xmlChar *str)
2003{
2004 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
2005 msg, (const char *) str);
2006}
2007
2008#define WXS_ERROR_TYPE_ERROR 1
2009#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002010/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002011 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002012 * @ctxt: the validation context
2013 * @node: the context node
2014 * @error: the error code
2015 * @msg: the error message
2016 * @str1: extra data
2017 * @str2: extra data
2018 * @str3: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002019 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002020 * Handle a validation error
2021 */
2022static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002023xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002024 xmlErrorLevel errorLevel,
2025 int error, xmlNodePtr node, int line, const char *msg,
2026 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002027 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002028{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002029 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002030 xmlGenericErrorFunc channel = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002031 void *data = NULL;
2032
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002033 if (ctxt != NULL) {
2034 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2035 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002036 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002037 if (errorLevel != XML_ERR_WARNING) {
2038 vctxt->nberrors++;
2039 vctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002040 channel = vctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002041 } else {
2042 channel = vctxt->warning;
2043 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002044 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002045 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002046
2047 /*
2048 * Error node. If we specify a line number, then
2049 * do not channel any node to the error function.
2050 */
2051 if (line == 0) {
2052 if ((node == NULL) &&
2053 (vctxt->depth >= 0) &&
2054 (vctxt->inode != NULL)) {
2055 node = vctxt->inode->node;
2056 }
2057 /*
2058 * Get filename and line if no node-tree.
2059 */
2060 if ((node == NULL) &&
2061 (vctxt->parserCtxt != NULL) &&
2062 (vctxt->parserCtxt->input != NULL)) {
2063 file = vctxt->parserCtxt->input->filename;
2064 line = vctxt->parserCtxt->input->line;
2065 }
2066 } else {
2067 /*
2068 * Override the given node's (if any) position
2069 * and channel only the given line number.
2070 */
2071 node = NULL;
2072 /*
2073 * Get filename.
2074 */
2075 if (vctxt->doc != NULL)
2076 file = (const char *) vctxt->doc->URL;
2077 else if ((vctxt->parserCtxt != NULL) &&
2078 (vctxt->parserCtxt->input != NULL))
2079 file = vctxt->parserCtxt->input->filename;
Daniel Veillarddee23482008-04-11 12:58:43 +00002080 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002081 __xmlRaiseError(schannel, channel, data, ctxt,
2082 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002083 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002084 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002085 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002086
2087 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2088 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002089 if (errorLevel != XML_ERR_WARNING) {
2090 pctxt->nberrors++;
2091 pctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002092 channel = pctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002093 } else {
2094 channel = pctxt->warning;
2095 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002096 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002097 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002098 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002099 node, XML_FROM_SCHEMASP, error,
2100 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002101 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002102 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002103 } else {
2104 TODO
2105 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002106 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002107}
2108
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002109/**
2110 * xmlSchemaErr3:
2111 * @ctxt: the validation context
2112 * @node: the context node
2113 * @error: the error code
2114 * @msg: the error message
2115 * @str1: extra data
2116 * @str2: extra data
2117 * @str3: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002118 *
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002119 * Handle a validation error
2120 */
2121static void
Daniel Veillarddee23482008-04-11 12:58:43 +00002122xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002123 int error, xmlNodePtr node, const char *msg,
2124 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2125{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002126 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2127 msg, str1, str2, str3, NULL);
2128}
2129
2130static void
Daniel Veillarddee23482008-04-11 12:58:43 +00002131xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002132 int error, xmlNodePtr node, const char *msg,
2133 const xmlChar *str1, const xmlChar *str2,
2134 const xmlChar *str3, const xmlChar *str4)
2135{
2136 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2137 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002138}
2139
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002140static void
2141xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2142 int error, xmlNodePtr node, const char *msg,
2143 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002144{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002145 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002146}
2147
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002148static xmlChar *
2149xmlSchemaFormatNodeForError(xmlChar ** msg,
2150 xmlSchemaAbstractCtxtPtr actxt,
2151 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002152{
2153 xmlChar *str = NULL;
2154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002155 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002156 if ((node != NULL) &&
2157 (node->type != XML_ELEMENT_NODE) &&
2158 (node->type != XML_ATTRIBUTE_NODE))
2159 {
Daniel Veillarddee23482008-04-11 12:58:43 +00002160 /*
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002161 * Don't try to format other nodes than element and
2162 * attribute nodes.
2163 * Play save and return an empty string.
2164 */
2165 *msg = xmlStrdup(BAD_CAST "");
2166 return(*msg);
2167 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002168 if (node != NULL) {
2169 /*
2170 * Work on tree nodes.
2171 */
2172 if (node->type == XML_ATTRIBUTE_NODE) {
2173 xmlNodePtr elem = node->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00002174
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002175 *msg = xmlStrdup(BAD_CAST "Element '");
2176 if (elem->ns != NULL)
2177 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2178 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002179 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002180 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2181 NULL, elem->name));
2182 FREE_AND_NULL(str);
2183 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002184 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002185 } else {
2186 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002187 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002188 if (node->ns != NULL)
2189 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2190 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002191 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002192 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2193 NULL, node->name));
2194 FREE_AND_NULL(str);
2195 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2196 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2197 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2198 /*
2199 * Work on node infos.
Daniel Veillarddee23482008-04-11 12:58:43 +00002200 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002201 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2202 xmlSchemaNodeInfoPtr ielem =
2203 vctxt->elemInfos[vctxt->depth];
2204
2205 *msg = xmlStrdup(BAD_CAST "Element '");
2206 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2207 ielem->nsName, ielem->localName));
2208 FREE_AND_NULL(str);
2209 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002210 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002211 } else {
2212 *msg = xmlStrdup(BAD_CAST "Element '");
2213 }
2214 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2215 vctxt->inode->nsName, vctxt->inode->localName));
2216 FREE_AND_NULL(str);
2217 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002218 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
Daniel Veillarddee23482008-04-11 12:58:43 +00002219 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002220 * Hmm, no node while parsing?
2221 * Return an empty string, in case NULL will break something.
2222 */
2223 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002224 } else {
2225 TODO
2226 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002227 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002228 /*
2229 * VAL TODO: The output of the given schema component is currently
2230 * disabled.
2231 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002232#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002233 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2234 *msg = xmlStrcat(*msg, BAD_CAST " [");
2235 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2236 NULL, type, NULL, 0));
2237 FREE_AND_NULL(str)
2238 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002239 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002240#endif
2241 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002242}
2243
Daniel Veillardc0826a72004-08-10 14:17:33 +00002244static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002245xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247 const char *message,
2248 const xmlChar *str1,
2249 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002250{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002251 xmlChar *msg = NULL;
2252
Daniel Veillard14b56432006-03-09 18:41:40 +00002253 if (actxt == NULL)
2254 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002255 msg = xmlStrdup(BAD_CAST "Internal error: ");
2256 msg = xmlStrcat(msg, BAD_CAST funcName);
Daniel Veillarddee23482008-04-11 12:58:43 +00002257 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002258 msg = xmlStrcat(msg, BAD_CAST message);
2259 msg = xmlStrcat(msg, BAD_CAST ".\n");
2260
2261 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2262 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002263 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002264
Daniel Veillarddee23482008-04-11 12:58:43 +00002265 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002266 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002267 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002268
2269 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002270}
2271
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002272static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002273xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2274 const char *funcName,
2275 const char *message)
2276{
2277 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2278}
2279
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002280#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002281static void
2282xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2283 const char *funcName,
2284 const char *message,
2285 const xmlChar *str1,
2286 const xmlChar *str2)
2287{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002288 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002289 str1, str2);
2290}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002291#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002292
2293static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002294xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2295 xmlParserErrors error,
2296 xmlNodePtr node,
2297 xmlSchemaBasicItemPtr item,
2298 const char *message,
2299 const xmlChar *str1, const xmlChar *str2,
2300 const xmlChar *str3, const xmlChar *str4)
2301{
2302 xmlChar *msg = NULL;
2303
2304 if ((node == NULL) && (item != NULL) &&
2305 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2306 node = WXS_ITEM_NODE(item);
2307 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2308 msg = xmlStrcat(msg, BAD_CAST ": ");
2309 } else
2310 xmlSchemaFormatNodeForError(&msg, actxt, node);
2311 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002312 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002313 xmlSchemaErr4(actxt, error, node,
2314 (const char *) msg, str1, str2, str3, str4);
2315 FREE_AND_NULL(msg)
2316}
2317
2318static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002319xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2320 xmlParserErrors error,
2321 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002322 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002323 const char *message,
2324 const xmlChar *str1,
2325 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002326{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002327 xmlSchemaCustomErr4(actxt, error, node, item,
Daniel Veillarddee23482008-04-11 12:58:43 +00002328 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002329}
2330
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002331
2332
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002333static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002334xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2335 xmlParserErrors error,
2336 xmlNodePtr node,
2337 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2338 const char *message,
2339 const xmlChar *str1,
2340 const xmlChar *str2,
2341 const xmlChar *str3)
2342{
2343 xmlChar *msg = NULL;
2344
2345 xmlSchemaFormatNodeForError(&msg, actxt, node);
2346 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002347 msg = xmlStrcat(msg, BAD_CAST ".\n");
2348
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002349 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002350 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2351 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002352
2353 FREE_AND_NULL(msg)
2354}
2355
2356
2357
2358static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002359xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2360 xmlParserErrors error,
2361 xmlSchemaPSVIIDCNodePtr idcNode,
2362 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2363 const char *message,
2364 const xmlChar *str1,
2365 const xmlChar *str2)
2366{
2367 xmlChar *msg = NULL, *qname = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002368
2369 msg = xmlStrdup(BAD_CAST "Element '%s': ");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002370 msg = xmlStrcat(msg, (const xmlChar *) message);
2371 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002372 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002373 error, NULL, idcNode->nodeLine, (const char *) msg,
2374 xmlSchemaFormatQName(&qname,
2375 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
Daniel Veillarddee23482008-04-11 12:58:43 +00002376 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002377 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002378 FREE_AND_NULL(qname);
2379 FREE_AND_NULL(msg);
2380}
2381
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002382static int
2383xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2384 xmlNodePtr node)
2385{
2386 if (node != NULL)
2387 return (node->type);
2388 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2389 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2390 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2391 return (-1);
2392}
2393
2394static int
2395xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2396{
2397 switch (item->type) {
2398 case XML_SCHEMA_TYPE_COMPLEX:
2399 case XML_SCHEMA_TYPE_SIMPLE:
2400 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2401 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002402 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002403 case XML_SCHEMA_TYPE_GROUP:
2404 return (1);
2405 case XML_SCHEMA_TYPE_ELEMENT:
2406 if ( ((xmlSchemaElementPtr) item)->flags &
2407 XML_SCHEMAS_ELEM_GLOBAL)
2408 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002409 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002410 case XML_SCHEMA_TYPE_ATTRIBUTE:
2411 if ( ((xmlSchemaAttributePtr) item)->flags &
2412 XML_SCHEMAS_ATTR_GLOBAL)
2413 return(1);
2414 break;
2415 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002416 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002417 return(1);
2418 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002419 return (0);
2420}
2421
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002422static void
2423xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2424 xmlParserErrors error,
2425 xmlNodePtr node,
2426 const xmlChar *value,
2427 xmlSchemaTypePtr type,
2428 int displayValue)
2429{
2430 xmlChar *msg = NULL;
2431
2432 xmlSchemaFormatNodeForError(&msg, actxt, node);
2433
2434 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2435 XML_ATTRIBUTE_NODE))
2436 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2437 else
2438 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2439 "value of ");
2440
2441 if (! xmlSchemaIsGlobalItem(type))
2442 msg = xmlStrcat(msg, BAD_CAST "the local ");
2443 else
2444 msg = xmlStrcat(msg, BAD_CAST "the ");
2445
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002446 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002447 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002448 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002449 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002450 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002451 msg = xmlStrcat(msg, BAD_CAST "union type");
2452
2453 if (xmlSchemaIsGlobalItem(type)) {
2454 xmlChar *str = NULL;
2455 msg = xmlStrcat(msg, BAD_CAST " '");
2456 if (type->builtInType != 0) {
2457 msg = xmlStrcat(msg, BAD_CAST "xs:");
2458 msg = xmlStrcat(msg, type->name);
Daniel Veillarddee23482008-04-11 12:58:43 +00002459 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002460 msg = xmlStrcat(msg,
2461 xmlSchemaFormatQName(&str,
2462 type->targetNamespace, type->name));
2463 msg = xmlStrcat(msg, BAD_CAST "'");
2464 FREE_AND_NULL(str);
2465 }
2466 msg = xmlStrcat(msg, BAD_CAST ".\n");
2467 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2468 XML_ATTRIBUTE_NODE))
2469 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2470 else
2471 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2472 FREE_AND_NULL(msg)
2473}
2474
Daniel Veillardc0826a72004-08-10 14:17:33 +00002475static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002476xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2477 xmlSchemaNodeInfoPtr ni,
2478 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002479{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002480 if (node != NULL) {
2481 if (node->ns != NULL)
2482 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2483 else
2484 return (xmlSchemaFormatQName(str, NULL, node->name));
2485 } else if (ni != NULL)
2486 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2487 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002488}
2489
Daniel Veillardc0826a72004-08-10 14:17:33 +00002490static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002491xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2492 xmlParserErrors error,
2493 xmlSchemaAttrInfoPtr ni,
2494 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002495{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002496 xmlChar *msg = NULL, *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002497
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002498 xmlSchemaFormatNodeForError(&msg, actxt, node);
2499 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2500 xmlSchemaErr(actxt, error, node, (const char *) msg,
2501 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
Daniel Veillarddee23482008-04-11 12:58:43 +00002502 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002503 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002505}
2506
Daniel Veillardc0826a72004-08-10 14:17:33 +00002507static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002508xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2509 xmlParserErrors error,
2510 xmlNodePtr node,
2511 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002512 const char *message,
2513 int nbval,
2514 int nbneg,
2515 xmlChar **values)
2516{
2517 xmlChar *str = NULL, *msg = NULL;
2518 xmlChar *localName, *nsName;
2519 const xmlChar *cur, *end;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00002520 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00002521
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002522 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002523 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002524 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002525 /*
2526 * Note that is does not make sense to report that we have a
2527 * wildcard here, since the wildcard might be unfolded into
2528 * multiple transitions.
2529 */
2530 if (nbval + nbneg > 0) {
2531 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002532 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002533 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002534 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002535 nsName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002536
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002537 for (i = 0; i < nbval + nbneg; i++) {
2538 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002539 if (cur == NULL)
2540 continue;
2541 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2542 (cur[3] == ' ')) {
Daniel Veillard77005e62005-07-19 16:26:18 +00002543 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002544 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002545 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002546 /*
2547 * Get the local name.
2548 */
2549 localName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002550
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002551 end = cur;
2552 if (*end == '*') {
2553 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002554 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002555 } else {
2556 while ((*end != 0) && (*end != '|'))
2557 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002558 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Daniel Veillarddee23482008-04-11 12:58:43 +00002559 }
2560 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002561 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002562 /*
2563 * Skip "*|*" if they come with negated expressions, since
2564 * they represent the same negated wildcard.
2565 */
2566 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2567 /*
2568 * Get the namespace name.
2569 */
2570 cur = end;
2571 if (*end == '*') {
2572 nsName = xmlStrdup(BAD_CAST "{*}");
2573 } else {
2574 while (*end != 0)
2575 end++;
Daniel Veillarddee23482008-04-11 12:58:43 +00002576
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002577 if (i >= nbval)
2578 nsName = xmlStrdup(BAD_CAST "{##other:");
2579 else
2580 nsName = xmlStrdup(BAD_CAST "{");
Daniel Veillarddee23482008-04-11 12:58:43 +00002581
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002582 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2583 nsName = xmlStrcat(nsName, BAD_CAST "}");
2584 }
2585 str = xmlStrcat(str, BAD_CAST nsName);
2586 FREE_AND_NULL(nsName)
2587 } else {
2588 FREE_AND_NULL(localName);
2589 continue;
2590 }
Daniel Veillarddee23482008-04-11 12:58:43 +00002591 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002592 str = xmlStrcat(str, BAD_CAST localName);
2593 FREE_AND_NULL(localName);
Daniel Veillarddee23482008-04-11 12:58:43 +00002594
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002595 if (i < nbval + nbneg -1)
2596 str = xmlStrcat(str, BAD_CAST ", ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002597 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002598 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002599 msg = xmlStrcat(msg, BAD_CAST str);
2600 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002601 } else
2602 msg = xmlStrcat(msg, BAD_CAST "\n");
2603 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002604 xmlFree(msg);
2605}
2606
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002607static void
2608xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2609 xmlParserErrors error,
2610 xmlNodePtr node,
2611 const xmlChar *value,
2612 unsigned long length,
2613 xmlSchemaTypePtr type,
2614 xmlSchemaFacetPtr facet,
2615 const char *message,
2616 const xmlChar *str1,
2617 const xmlChar *str2)
2618{
2619 xmlChar *str = NULL, *msg = NULL;
2620 xmlSchemaTypeType facetType;
2621 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2622
2623 xmlSchemaFormatNodeForError(&msg, actxt, node);
2624 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2625 facetType = XML_SCHEMA_FACET_ENUMERATION;
2626 /*
2627 * If enumerations are validated, one must not expect the
2628 * facet to be given.
Daniel Veillarddee23482008-04-11 12:58:43 +00002629 */
2630 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002631 facetType = facet->type;
2632 msg = xmlStrcat(msg, BAD_CAST "[");
2633 msg = xmlStrcat(msg, BAD_CAST "facet '");
2634 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2635 msg = xmlStrcat(msg, BAD_CAST "'] ");
2636 if (message == NULL) {
2637 /*
2638 * Use a default message.
2639 */
2640 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2641 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2642 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2643
2644 char len[25], actLen[25];
2645
2646 /* FIXME, TODO: What is the max expected string length of the
2647 * this value?
2648 */
2649 if (nodeType == XML_ATTRIBUTE_NODE)
2650 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2651 else
2652 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2653
2654 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2655 snprintf(actLen, 24, "%lu", length);
2656
2657 if (facetType == XML_SCHEMA_FACET_LENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002658 msg = xmlStrcat(msg,
2659 BAD_CAST "this differs from the allowed length of '%s'.\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002660 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002661 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002662 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2663 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002664 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002665 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002666
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002667 if (nodeType == XML_ATTRIBUTE_NODE)
2668 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2669 value, (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002670 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002671 xmlSchemaErr(actxt, error, node, (const char *) msg,
2672 (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002673
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002674 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2675 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2676 "of the set {%s}.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002677 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002678 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2679 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2680 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2681 "by the pattern '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002682 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002683 facet->value);
2684 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2685 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2686 "minimum value allowed ('%s').\n");
2687 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2688 facet->value);
2689 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2690 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2691 "maximum value allowed ('%s').\n");
2692 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2693 facet->value);
2694 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002695 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002696 "'%s'.\n");
2697 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2698 facet->value);
2699 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002700 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002701 "'%s'.\n");
2702 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2703 facet->value);
2704 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2705 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2706 "digits than are allowed ('%s').\n");
2707 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2708 facet->value);
2709 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2710 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2711 "digits than are allowed ('%s').\n");
2712 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2713 facet->value);
Daniel Veillarddee23482008-04-11 12:58:43 +00002714 } else if (nodeType == XML_ATTRIBUTE_NODE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002715 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002716 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2717 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002718 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2719 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2720 }
2721 } else {
2722 msg = xmlStrcat(msg, (const xmlChar *) message);
2723 msg = xmlStrcat(msg, BAD_CAST ".\n");
2724 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
Daniel Veillarddee23482008-04-11 12:58:43 +00002725 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002726 FREE_AND_NULL(str)
2727 xmlFree(msg);
2728}
2729
2730#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002731 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002732
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002733#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002734
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002735#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2736#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002737
2738#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2739
2740
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002741/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002742 * xmlSchemaPMissingAttrErr:
2743 * @ctxt: the schema validation context
2744 * @ownerDes: the designation of the owner
2745 * @ownerName: the name of the owner
2746 * @ownerItem: the owner as a schema object
2747 * @ownerElem: the owner as an element node
2748 * @node: the parent element node of the missing attribute node
2749 * @type: the corresponding type of the attribute node
2750 *
2751 * Reports an illegal attribute.
2752 */
2753static void
2754xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002755 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002756 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002757 xmlNodePtr ownerElem,
2758 const char *name,
2759 const char *message)
2760{
2761 xmlChar *des = NULL;
2762
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002763 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2764
Daniel Veillardc0826a72004-08-10 14:17:33 +00002765 if (message != NULL)
2766 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002767 else
2768 xmlSchemaPErr(ctxt, ownerElem, error,
2769 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002770 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002771 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002772}
2773
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002774
Daniel Veillardc0826a72004-08-10 14:17:33 +00002775/**
2776 * xmlSchemaPResCompAttrErr:
2777 * @ctxt: the schema validation context
2778 * @error: the error code
2779 * @ownerDes: the designation of the owner
2780 * @ownerItem: the owner as a schema object
2781 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002782 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002783 * @refName: the referenced local name
2784 * @refURI: the referenced namespace URI
2785 * @message: optional message
2786 *
2787 * Used to report QName attribute values that failed to resolve
2788 * to schema components.
2789 */
2790static void
2791xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002792 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002793 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002794 xmlNodePtr ownerElem,
2795 const char *name,
2796 const xmlChar *refName,
2797 const xmlChar *refURI,
2798 xmlSchemaTypeType refType,
2799 const char *refTypeStr)
2800{
2801 xmlChar *des = NULL, *strA = NULL;
2802
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002803 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002804 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002805 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002806 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002807 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002808 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2809 "%s.\n", BAD_CAST des, BAD_CAST name,
2810 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002811 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002812 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002813 FREE_AND_NULL(strA)
2814}
2815
William M. Brack2f2a6632004-08-20 23:09:47 +00002816/**
2817 * xmlSchemaPCustomAttrErr:
2818 * @ctxt: the schema parser context
2819 * @error: the error code
2820 * @ownerDes: the designation of the owner
2821 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002822 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002823 *
2824 * Reports an illegal attribute during the parse.
2825 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002826static void
2827xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002828 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002829 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002830 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002831 xmlAttrPtr attr,
2832 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002833{
2834 xmlChar *des = NULL;
2835
2836 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002837 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002838 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002839 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002840 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002841 } else
2842 des = *ownerDes;
Daniel Veillard14b56432006-03-09 18:41:40 +00002843 if (attr == NULL) {
2844 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2845 "%s, attribute '%s': %s.\n",
Daniel Veillardaac7c682006-03-10 13:40:16 +00002846 BAD_CAST des, (const xmlChar *) "Unknown",
2847 (const xmlChar *) msg, NULL, NULL);
Daniel Veillard14b56432006-03-09 18:41:40 +00002848 } else {
2849 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2850 "%s, attribute '%s': %s.\n",
2851 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2852 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002853 if (ownerDes == NULL)
2854 FREE_AND_NULL(des);
2855}
2856
2857/**
2858 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002859 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002860 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002861 * @ownerDes: the designation of the attribute's owner
2862 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002863 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002864 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002865 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002866 */
2867static void
2868xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002869 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002870 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002871 xmlAttrPtr attr)
2872{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002873 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002874
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002875 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2876 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2877 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2878 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2879 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002880 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002881 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002882}
2883
William M. Brack2f2a6632004-08-20 23:09:47 +00002884/**
2885 * xmlSchemaPCustomErr:
2886 * @ctxt: the schema parser context
2887 * @error: the error code
2888 * @itemDes: the designation of the schema item
2889 * @item: the schema item
2890 * @itemElem: the node of the schema item
2891 * @message: the error message
2892 * @str1: an optional param for the error message
2893 * @str2: an optional param for the error message
2894 * @str3: an optional param for the error message
2895 *
2896 * Reports an error during parsing.
2897 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002898static void
2899xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002900 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002901 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002902 xmlNodePtr itemElem,
2903 const char *message,
2904 const xmlChar *str1,
2905 const xmlChar *str2,
2906 const xmlChar *str3)
2907{
2908 xmlChar *des = NULL, *msg = NULL;
2909
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002910 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002911 msg = xmlStrdup(BAD_CAST "%s: ");
2912 msg = xmlStrcat(msg, (const xmlChar *) message);
2913 msg = xmlStrcat(msg, BAD_CAST ".\n");
2914 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002915 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002916 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002917 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002918 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002919 FREE_AND_NULL(msg);
2920}
2921
William M. Brack2f2a6632004-08-20 23:09:47 +00002922/**
2923 * xmlSchemaPCustomErr:
2924 * @ctxt: the schema parser context
2925 * @error: the error code
2926 * @itemDes: the designation of the schema item
2927 * @item: the schema item
2928 * @itemElem: the node of the schema item
2929 * @message: the error message
2930 * @str1: the optional param for the error message
2931 *
2932 * Reports an error during parsing.
2933 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002934static void
2935xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002936 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002937 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002938 xmlNodePtr itemElem,
2939 const char *message,
2940 const xmlChar *str1)
2941{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002942 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002943 str1, NULL, NULL);
2944}
2945
William M. Brack2f2a6632004-08-20 23:09:47 +00002946/**
2947 * xmlSchemaPAttrUseErr:
2948 * @ctxt: the schema parser context
2949 * @error: the error code
2950 * @itemDes: the designation of the schema type
2951 * @item: the schema type
2952 * @itemElem: the node of the schema type
2953 * @attr: the invalid schema attribute
2954 * @message: the error message
2955 * @str1: the optional param for the error message
2956 *
2957 * Reports an attribute use error during parsing.
2958 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002959static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002960xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002961 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002962 xmlNodePtr node,
2963 xmlSchemaBasicItemPtr ownerItem,
2964 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002965 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002966 const xmlChar *str1, const xmlChar *str2,
2967 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002968{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002969 xmlChar *str = NULL, *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002970
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002971 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00002972 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002973 msg = xmlStrcat(msg,
2974 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002975 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002976 FREE_AND_NULL(str);
2977 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002978 msg = xmlStrcat(msg, (const xmlChar *) message);
2979 msg = xmlStrcat(msg, BAD_CAST ".\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002980 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002981 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002982 xmlFree(msg);
2983}
2984
William M. Brack2f2a6632004-08-20 23:09:47 +00002985/**
2986 * xmlSchemaPIllegalFacetAtomicErr:
2987 * @ctxt: the schema parser context
2988 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002989 * @type: the schema type
2990 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002991 * @facet: the illegal facet
2992 *
2993 * Reports an illegal facet for atomic simple types.
2994 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002995static void
2996xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002997 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002998 xmlSchemaTypePtr type,
2999 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003000 xmlSchemaFacetPtr facet)
3001{
3002 xmlChar *des = NULL, *strT = NULL;
3003
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003004 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
3005 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003006 "%s: The facet '%s' is not allowed on types derived from the "
3007 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003008 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003009 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00003010 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003011 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003012 FREE_AND_NULL(strT);
3013}
3014
William M. Brack2f2a6632004-08-20 23:09:47 +00003015/**
3016 * xmlSchemaPIllegalFacetListUnionErr:
3017 * @ctxt: the schema parser context
3018 * @error: the error code
3019 * @itemDes: the designation of the schema item involved
3020 * @item: the schema item involved
3021 * @facet: the illegal facet
3022 *
3023 * Reports an illegal facet for <list> and <union>.
3024 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003025static void
3026xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003027 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003028 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003029 xmlSchemaFacetPtr facet)
3030{
Daniel Veillardb2947172006-03-27 09:45:01 +00003031 xmlChar *des = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003032
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003033 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3034 type->node);
3035 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003036 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003037 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003038 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003039}
3040
3041/**
3042 * xmlSchemaPMutualExclAttrErr:
3043 * @ctxt: the schema validation context
3044 * @error: the error code
3045 * @elemDes: the designation of the parent element node
3046 * @attr: the bad attribute node
3047 * @type: the corresponding type of the attribute node
3048 *
3049 * Reports an illegal attribute.
3050 */
3051static void
3052xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3053 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003054 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003055 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003056 const char *name1,
3057 const char *name2)
3058{
3059 xmlChar *des = NULL;
3060
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003061 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003062 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003063 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003064 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003065 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003066}
3067
3068/**
3069 * xmlSchemaPSimpleTypeErr:
3070 * @ctxt: the schema validation context
3071 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003072 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003073 * @ownerDes: the designation of the owner
Daniel Veillarddee23482008-04-11 12:58:43 +00003074 * @ownerItem: the schema object if existent
Daniel Veillardc0826a72004-08-10 14:17:33 +00003075 * @node: the validated node
3076 * @value: the validated value
3077 *
3078 * Reports a simple type validation error.
3079 * TODO: Should this report the value of an element as well?
3080 */
3081static void
Daniel Veillarddee23482008-04-11 12:58:43 +00003082xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003083 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003084 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003085 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003086 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003087 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003088 const xmlChar *value,
3089 const char *message,
3090 const xmlChar *str1,
3091 const xmlChar *str2)
3092{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003093 xmlChar *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00003094
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003095 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003096 if (message == NULL) {
3097 /*
3098 * Use default messages.
Daniel Veillarddee23482008-04-11 12:58:43 +00003099 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003100 if (type != NULL) {
3101 if (node->type == XML_ATTRIBUTE_NODE)
3102 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3103 else
3104 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
Daniel Veillarddee23482008-04-11 12:58:43 +00003105 "valid value of ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003106 if (! xmlSchemaIsGlobalItem(type))
3107 msg = xmlStrcat(msg, BAD_CAST "the local ");
3108 else
3109 msg = xmlStrcat(msg, BAD_CAST "the ");
Daniel Veillarddee23482008-04-11 12:58:43 +00003110
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003111 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003112 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003113 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003114 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003115 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003116 msg = xmlStrcat(msg, BAD_CAST "union type");
Daniel Veillarddee23482008-04-11 12:58:43 +00003117
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003118 if (xmlSchemaIsGlobalItem(type)) {
3119 xmlChar *str = NULL;
3120 msg = xmlStrcat(msg, BAD_CAST " '");
3121 if (type->builtInType != 0) {
3122 msg = xmlStrcat(msg, BAD_CAST "xs:");
3123 msg = xmlStrcat(msg, type->name);
Daniel Veillarddee23482008-04-11 12:58:43 +00003124 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003125 msg = xmlStrcat(msg,
3126 xmlSchemaFormatQName(&str,
3127 type->targetNamespace, type->name));
3128 msg = xmlStrcat(msg, BAD_CAST "'.");
3129 FREE_AND_NULL(str);
3130 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003131 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003132 if (node->type == XML_ATTRIBUTE_NODE)
3133 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3134 else
3135 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3136 "valid.");
Daniel Veillarddee23482008-04-11 12:58:43 +00003137 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003138 if (expected) {
3139 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3140 msg = xmlStrcat(msg, BAD_CAST expected);
3141 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3142 } else
3143 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003144 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003145 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3146 else
3147 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3148 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003149 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003150 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003151 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003152 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003154 /* Cleanup. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003155 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003156}
3157
William M. Brack2f2a6632004-08-20 23:09:47 +00003158/**
3159 * xmlSchemaPContentErr:
3160 * @ctxt: the schema parser context
3161 * @error: the error code
3162 * @onwerDes: the designation of the holder of the content
3163 * @ownerItem: the owner item of the holder of the content
3164 * @ownerElem: the node of the holder of the content
3165 * @child: the invalid child node
3166 * @message: the optional error message
3167 * @content: the optional string describing the correct content
3168 *
3169 * Reports an error concerning the content of a schema element.
3170 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003171static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003172xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003173 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003174 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003175 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003176 xmlNodePtr child,
3177 const char *message,
3178 const char *content)
3179{
3180 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003181
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003182 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003183 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003184 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3185 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003186 BAD_CAST des, BAD_CAST message);
3187 else {
3188 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003189 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3190 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003191 BAD_CAST des, BAD_CAST content);
3192 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003193 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3194 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003195 BAD_CAST des, NULL);
3196 }
3197 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003198 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003199}
3200
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003201/************************************************************************
3202 * *
3203 * Streamable error functions *
3204 * *
3205 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003206
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003207
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003208
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003209
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003210/************************************************************************
3211 * *
3212 * Validation helper functions *
3213 * *
3214 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003215
Daniel Veillardc0826a72004-08-10 14:17:33 +00003216
Daniel Veillard4255d502002-04-16 15:50:10 +00003217/************************************************************************
3218 * *
3219 * Allocation functions *
3220 * *
3221 ************************************************************************/
3222
3223/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003224 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003225 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003226 *
3227 * Allocate a new Schema structure.
3228 *
3229 * Returns the newly allocated structure or NULL in case or error
3230 */
3231static xmlSchemaPtr
3232xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3233{
3234 xmlSchemaPtr ret;
3235
3236 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3237 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003238 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 return (NULL);
3240 }
3241 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003242 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003243 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003244
3245 return (ret);
3246}
3247
3248/**
3249 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003250 *
3251 * Allocate a new Facet structure.
3252 *
3253 * Returns the newly allocated structure or NULL in case or error
3254 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003255xmlSchemaFacetPtr
3256xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003257{
3258 xmlSchemaFacetPtr ret;
3259
3260 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3261 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003262 return (NULL);
3263 }
3264 memset(ret, 0, sizeof(xmlSchemaFacet));
3265
3266 return (ret);
3267}
3268
3269/**
3270 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003271 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003272 * @node: a node
3273 *
3274 * Allocate a new annotation structure.
3275 *
3276 * Returns the newly allocated structure or NULL in case or error
3277 */
3278static xmlSchemaAnnotPtr
3279xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3280{
3281 xmlSchemaAnnotPtr ret;
3282
3283 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3284 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003285 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003286 return (NULL);
3287 }
3288 memset(ret, 0, sizeof(xmlSchemaAnnot));
3289 ret->content = node;
3290 return (ret);
3291}
3292
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003293static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003294xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003295{
3296 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003297
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003298 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3299 if (ret == NULL) {
3300 xmlSchemaPErrMemory(NULL,
3301 "allocating an item list structure", NULL);
3302 return (NULL);
3303 }
3304 memset(ret, 0, sizeof(xmlSchemaItemList));
3305 return (ret);
3306}
3307
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003308static void
3309xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3310{
3311 if (list->items != NULL) {
3312 xmlFree(list->items);
3313 list->items = NULL;
3314 }
3315 list->nbItems = 0;
3316 list->sizeItems = 0;
3317}
3318
3319static int
3320xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3321{
3322 if (list->items == NULL) {
3323 list->items = (void **) xmlMalloc(
3324 20 * sizeof(void *));
3325 if (list->items == NULL) {
3326 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3327 return(-1);
3328 }
3329 list->sizeItems = 20;
3330 } else if (list->sizeItems <= list->nbItems) {
3331 list->sizeItems *= 2;
3332 list->items = (void **) xmlRealloc(list->items,
3333 list->sizeItems * sizeof(void *));
3334 if (list->items == NULL) {
3335 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3336 list->sizeItems = 0;
3337 return(-1);
3338 }
3339 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003340 list->items[list->nbItems++] = item;
3341 return(0);
3342}
3343
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003344static int
3345xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3346 int initialSize,
3347 void *item)
3348{
3349 if (list->items == NULL) {
3350 if (initialSize <= 0)
3351 initialSize = 1;
3352 list->items = (void **) xmlMalloc(
3353 initialSize * sizeof(void *));
3354 if (list->items == NULL) {
3355 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3356 return(-1);
3357 }
3358 list->sizeItems = initialSize;
3359 } else if (list->sizeItems <= list->nbItems) {
3360 list->sizeItems *= 2;
3361 list->items = (void **) xmlRealloc(list->items,
3362 list->sizeItems * sizeof(void *));
3363 if (list->items == NULL) {
3364 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3365 list->sizeItems = 0;
3366 return(-1);
3367 }
3368 }
3369 list->items[list->nbItems++] = item;
3370 return(0);
3371}
3372
3373static int
3374xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003375{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003376 if (list->items == NULL) {
3377 list->items = (void **) xmlMalloc(
3378 20 * sizeof(void *));
3379 if (list->items == NULL) {
3380 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3381 return(-1);
3382 }
3383 list->sizeItems = 20;
3384 } else if (list->sizeItems <= list->nbItems) {
3385 list->sizeItems *= 2;
3386 list->items = (void **) xmlRealloc(list->items,
3387 list->sizeItems * sizeof(void *));
3388 if (list->items == NULL) {
3389 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3390 list->sizeItems = 0;
3391 return(-1);
3392 }
3393 }
3394 /*
3395 * Just append if the index is greater/equal than the item count.
3396 */
3397 if (idx >= list->nbItems) {
3398 list->items[list->nbItems++] = item;
3399 } else {
3400 int i;
3401 for (i = list->nbItems; i > idx; i--)
3402 list->items[i] = list->items[i-1];
3403 list->items[idx] = item;
3404 list->nbItems++;
3405 }
3406 return(0);
3407}
3408
3409#if 0 /* enable if ever needed */
3410static int
3411xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3412 int initialSize,
3413 void *item,
3414 int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003415{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003416 if (list->items == NULL) {
3417 if (initialSize <= 0)
3418 initialSize = 1;
3419 list->items = (void **) xmlMalloc(
3420 initialSize * sizeof(void *));
3421 if (list->items == NULL) {
3422 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3423 return(-1);
3424 }
3425 list->sizeItems = initialSize;
3426 } else if (list->sizeItems <= list->nbItems) {
3427 list->sizeItems *= 2;
3428 list->items = (void **) xmlRealloc(list->items,
3429 list->sizeItems * sizeof(void *));
3430 if (list->items == NULL) {
3431 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3432 list->sizeItems = 0;
3433 return(-1);
3434 }
3435 }
3436 /*
3437 * Just append if the index is greater/equal than the item count.
3438 */
3439 if (idx >= list->nbItems) {
3440 list->items[list->nbItems++] = item;
3441 } else {
3442 int i;
3443 for (i = list->nbItems; i > idx; i--)
3444 list->items[i] = list->items[i-1];
3445 list->items[idx] = item;
3446 list->nbItems++;
3447 }
3448 return(0);
3449}
3450#endif
3451
3452static int
3453xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3454{
3455 int i;
3456 if ((list->items == NULL) || (idx >= list->nbItems)) {
3457 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3458 "index error.\n");
3459 return(-1);
3460 }
3461
3462 if (list->nbItems == 1) {
3463 /* TODO: Really free the list? */
3464 xmlFree(list->items);
3465 list->items = NULL;
3466 list->nbItems = 0;
3467 list->sizeItems = 0;
3468 } else if (list->nbItems -1 == idx) {
3469 list->nbItems--;
Daniel Veillarddee23482008-04-11 12:58:43 +00003470 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003471 for (i = idx; i < list->nbItems -1; i++)
3472 list->items[i] = list->items[i+1];
3473 list->nbItems--;
3474 }
3475 return(0);
3476}
3477
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003478/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003479 * xmlSchemaItemListFree:
3480 * @annot: a schema type structure
3481 *
3482 * Deallocate a annotation structure
3483 */
3484static void
3485xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3486{
3487 if (list == NULL)
3488 return;
3489 if (list->items != NULL)
3490 xmlFree(list->items);
3491 xmlFree(list);
3492}
3493
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003494static void
3495xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003496{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003497 if (bucket == NULL)
3498 return;
3499 if (bucket->globals != NULL) {
3500 xmlSchemaComponentListFree(bucket->globals);
3501 xmlSchemaItemListFree(bucket->globals);
3502 }
3503 if (bucket->locals != NULL) {
3504 xmlSchemaComponentListFree(bucket->locals);
Daniel Veillarddee23482008-04-11 12:58:43 +00003505 xmlSchemaItemListFree(bucket->locals);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003506 }
3507 if (bucket->relations != NULL) {
3508 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3509 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00003510 prev = cur;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003511 cur = cur->next;
3512 xmlFree(prev);
3513 } while (cur != NULL);
3514 }
3515 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3516 xmlFreeDoc(bucket->doc);
Daniel Veillarddee23482008-04-11 12:58:43 +00003517 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003518 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003519 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3520 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003521 }
3522 xmlFree(bucket);
3523}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003524
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003525static xmlSchemaBucketPtr
3526xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003527 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003528{
3529 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003530 int size;
3531 xmlSchemaPtr mainSchema;
3532
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003533 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003534 PERROR_INT("xmlSchemaBucketCreate",
3535 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003536 return(NULL);
3537 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003538 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003539 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003540 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003541 size = sizeof(xmlSchemaInclude);
3542 else
3543 size = sizeof(xmlSchemaImport);
3544 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3545 if (ret == NULL) {
3546 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3547 return(NULL);
3548 }
3549 memset(ret, 0, size);
3550 ret->targetNamespace = targetNamespace;
3551 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003552 ret->globals = xmlSchemaItemListCreate();
3553 if (ret->globals == NULL) {
3554 xmlFree(ret);
3555 return(NULL);
3556 }
3557 ret->locals = xmlSchemaItemListCreate();
3558 if (ret->locals == NULL) {
3559 xmlFree(ret);
3560 return(NULL);
3561 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003562 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003563 * The following will assure that only the first bucket is marked as
3564 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3565 * For each following import buckets an xmlSchema will be created.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003566 * An xmlSchema will be created for every distinct targetNamespace.
3567 * We assign the targetNamespace to the schemata here.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003568 */
3569 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003570 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003571 PERROR_INT("xmlSchemaBucketCreate",
3572 "first bucket but it's an include or redefine");
3573 xmlSchemaBucketFree(ret);
3574 return(NULL);
3575 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003576 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003577 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3578 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003579 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3580 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003581 /*
3582 * Ensure that the main schema gets a targetNamespace.
3583 */
3584 mainSchema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003585 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +00003586 if (type == XML_SCHEMA_SCHEMA_MAIN) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003587 PERROR_INT("xmlSchemaBucketCreate",
3588 "main bucket but it's not the first one");
3589 xmlSchemaBucketFree(ret);
3590 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003591 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003592 /*
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003593 * Create a schema for imports and assign the
3594 * targetNamespace.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003595 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003596 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3597 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003598 xmlSchemaBucketFree(ret);
3599 return(NULL);
3600 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003601 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003602 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003604 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003605 int res;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003606 /*
3607 * Imports go into the "schemasImports" slot of the main *schema*.
3608 * Note that we create an import entry for the main schema as well; i.e.,
3609 * even if there's only one schema, we'll get an import.
3610 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003611 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003612 mainSchema->schemasImports = xmlHashCreateDict(5,
3613 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003614 if (mainSchema->schemasImports == NULL) {
3615 xmlSchemaBucketFree(ret);
3616 return(NULL);
3617 }
3618 }
3619 if (targetNamespace == NULL)
3620 res = xmlHashAddEntry(mainSchema->schemasImports,
3621 XML_SCHEMAS_NO_NAMESPACE, ret);
3622 else
3623 res = xmlHashAddEntry(mainSchema->schemasImports,
3624 targetNamespace, ret);
3625 if (res != 0) {
3626 PERROR_INT("xmlSchemaBucketCreate",
3627 "failed to add the schema bucket to the hash");
3628 xmlSchemaBucketFree(ret);
3629 return(NULL);
3630 }
3631 } else {
3632 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003633 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3634 WXS_INCBUCKET(ret)->ownerImport =
3635 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003636 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003637 WXS_INCBUCKET(ret)->ownerImport =
3638 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003639
3640 /* Includes got into the "includes" slot of the *main* schema. */
3641 if (mainSchema->includes == NULL) {
3642 mainSchema->includes = xmlSchemaItemListCreate();
3643 if (mainSchema->includes == NULL) {
3644 xmlSchemaBucketFree(ret);
3645 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003646 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003647 }
3648 xmlSchemaItemListAdd(mainSchema->includes, ret);
3649 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003650 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003651 * Add to list of all buckets; this is used for lookup
3652 * during schema construction time only.
3653 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003654 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003655 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003656 return(ret);
3657}
3658
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003659static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003660xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003661{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003662 if (*list == NULL) {
3663 *list = xmlSchemaItemListCreate();
3664 if (*list == NULL)
3665 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003666 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003667 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003668 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003669}
3670
3671/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003672 * xmlSchemaFreeAnnot:
3673 * @annot: a schema type structure
3674 *
3675 * Deallocate a annotation structure
3676 */
3677static void
3678xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3679{
3680 if (annot == NULL)
3681 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003682 if (annot->next == NULL) {
3683 xmlFree(annot);
3684 } else {
3685 xmlSchemaAnnotPtr prev;
3686
3687 do {
3688 prev = annot;
3689 annot = annot->next;
3690 xmlFree(prev);
3691 } while (annot != NULL);
3692 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003693}
3694
3695/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003696 * xmlSchemaFreeNotation:
3697 * @schema: a schema notation structure
3698 *
3699 * Deallocate a Schema Notation structure.
3700 */
3701static void
3702xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3703{
3704 if (nota == NULL)
3705 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003706 xmlFree(nota);
3707}
3708
3709/**
3710 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003711 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003712 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003713 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003714 */
3715static void
3716xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3717{
3718 if (attr == NULL)
3719 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003720 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003721 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003722 if (attr->defVal != NULL)
3723 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003724 xmlFree(attr);
3725}
3726
3727/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003728 * xmlSchemaFreeAttributeUse:
3729 * @use: an attribute use
3730 *
3731 * Deallocates an attribute use structure.
3732 */
3733static void
3734xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3735{
3736 if (use == NULL)
3737 return;
3738 if (use->annot != NULL)
3739 xmlSchemaFreeAnnot(use->annot);
3740 if (use->defVal != NULL)
3741 xmlSchemaFreeValue(use->defVal);
3742 xmlFree(use);
3743}
3744
3745/**
3746 * xmlSchemaFreeAttributeUseProhib:
3747 * @prohib: an attribute use prohibition
3748 *
3749 * Deallocates an attribute use structure.
3750 */
3751static void
3752xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3753{
3754 if (prohib == NULL)
3755 return;
3756 xmlFree(prohib);
3757}
3758
3759/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003760 * xmlSchemaFreeWildcardNsSet:
3761 * set: a schema wildcard namespace
3762 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003763 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003764 */
3765static void
3766xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3767{
3768 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003769
Daniel Veillard3646d642004-06-02 19:19:14 +00003770 while (set != NULL) {
3771 next = set->next;
3772 xmlFree(set);
3773 set = next;
3774 }
3775}
3776
3777/**
3778 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003779 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003780 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003781 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003782 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003783void
Daniel Veillard3646d642004-06-02 19:19:14 +00003784xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3785{
3786 if (wildcard == NULL)
3787 return;
3788 if (wildcard->annot != NULL)
3789 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003790 if (wildcard->nsSet != NULL)
3791 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3792 if (wildcard->negNsSet != NULL)
3793 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003794 xmlFree(wildcard);
3795}
3796
3797/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003798 * xmlSchemaFreeAttributeGroup:
3799 * @schema: a schema attribute group structure
3800 *
3801 * Deallocate a Schema Attribute Group structure.
3802 */
3803static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003804xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003805{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003806 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003807 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003808 if (attrGr->annot != NULL)
3809 xmlSchemaFreeAnnot(attrGr->annot);
3810 if (attrGr->attrUses != NULL)
3811 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3812 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003813}
3814
3815/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003816 * xmlSchemaFreeQNameRef:
3817 * @item: a QName reference structure
3818 *
3819 * Deallocatea a QName reference structure.
3820 */
3821static void
3822xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3823{
3824 xmlFree(item);
3825}
3826
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003827/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003828 * xmlSchemaFreeTypeLinkList:
3829 * @alink: a type link
3830 *
3831 * Deallocate a list of types.
3832 */
3833static void
3834xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3835{
3836 xmlSchemaTypeLinkPtr next;
3837
3838 while (link != NULL) {
3839 next = link->next;
3840 xmlFree(link);
3841 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003842 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003843}
3844
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003845static void
3846xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3847{
3848 xmlSchemaIDCStateObjPtr next;
3849 while (sto != NULL) {
3850 next = sto->next;
3851 if (sto->history != NULL)
3852 xmlFree(sto->history);
3853 if (sto->xpathCtxt != NULL)
3854 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3855 xmlFree(sto);
3856 sto = next;
3857 }
3858}
3859
3860/**
3861 * xmlSchemaFreeIDC:
3862 * @idc: a identity-constraint definition
3863 *
3864 * Deallocates an identity-constraint definition.
3865 */
3866static void
3867xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3868{
3869 xmlSchemaIDCSelectPtr cur, prev;
3870
3871 if (idcDef == NULL)
3872 return;
3873 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003874 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003875 /* Selector */
3876 if (idcDef->selector != NULL) {
3877 if (idcDef->selector->xpathComp != NULL)
3878 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3879 xmlFree(idcDef->selector);
3880 }
3881 /* Fields */
3882 if (idcDef->fields != NULL) {
3883 cur = idcDef->fields;
3884 do {
3885 prev = cur;
3886 cur = cur->next;
3887 if (prev->xpathComp != NULL)
3888 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003889 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003890 } while (cur != NULL);
3891 }
3892 xmlFree(idcDef);
3893}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003894
Daniel Veillard01fa6152004-06-29 17:04:39 +00003895/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003896 * xmlSchemaFreeElement:
3897 * @schema: a schema element structure
3898 *
3899 * Deallocate a Schema Element structure.
3900 */
3901static void
3902xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3903{
3904 if (elem == NULL)
3905 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003906 if (elem->annot != NULL)
3907 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003908 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003909 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003910 if (elem->defVal != NULL)
3911 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003912 xmlFree(elem);
3913}
3914
3915/**
3916 * xmlSchemaFreeFacet:
3917 * @facet: a schema facet structure
3918 *
3919 * Deallocate a Schema Facet structure.
3920 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003921void
Daniel Veillard4255d502002-04-16 15:50:10 +00003922xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3923{
3924 if (facet == NULL)
3925 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003926 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003927 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003928 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003929 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003930 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003931 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003932 xmlFree(facet);
3933}
3934
3935/**
3936 * xmlSchemaFreeType:
3937 * @type: a schema type structure
3938 *
3939 * Deallocate a Schema Type structure.
3940 */
3941void
3942xmlSchemaFreeType(xmlSchemaTypePtr type)
3943{
3944 if (type == NULL)
3945 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003946 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003947 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003948 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003949 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003950
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003951 facet = type->facets;
3952 while (facet != NULL) {
3953 next = facet->next;
3954 xmlSchemaFreeFacet(facet);
3955 facet = next;
3956 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003957 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003958 if (type->attrUses != NULL)
3959 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003960 if (type->memberTypes != NULL)
3961 xmlSchemaFreeTypeLinkList(type->memberTypes);
3962 if (type->facetSet != NULL) {
3963 xmlSchemaFacetLinkPtr next, link;
3964
3965 link = type->facetSet;
3966 do {
3967 next = link->next;
3968 xmlFree(link);
3969 link = next;
3970 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003971 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003972 if (type->contModel != NULL)
3973 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003974 xmlFree(type);
3975}
3976
3977/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003978 * xmlSchemaFreeModelGroupDef:
3979 * @item: a schema model group definition
3980 *
3981 * Deallocates a schema model group definition.
3982 */
3983static void
3984xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3985{
3986 if (item->annot != NULL)
3987 xmlSchemaFreeAnnot(item->annot);
3988 xmlFree(item);
3989}
3990
3991/**
3992 * xmlSchemaFreeModelGroup:
3993 * @item: a schema model group
3994 *
3995 * Deallocates a schema model group structure.
3996 */
3997static void
3998xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3999{
4000 if (item->annot != NULL)
4001 xmlSchemaFreeAnnot(item->annot);
4002 xmlFree(item);
4003}
4004
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004005static void
4006xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
4007{
4008 if ((list == NULL) || (list->nbItems == 0))
4009 return;
4010 {
4011 xmlSchemaTreeItemPtr item;
4012 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
4013 int i;
4014
4015 for (i = 0; i < list->nbItems; i++) {
4016 item = items[i];
4017 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004018 continue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004019 switch (item->type) {
4020 case XML_SCHEMA_TYPE_SIMPLE:
4021 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004022 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004023 break;
4024 case XML_SCHEMA_TYPE_ATTRIBUTE:
4025 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004026 break;
4027 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
4028 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
4029 break;
4030 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
4031 xmlSchemaFreeAttributeUseProhib(
4032 (xmlSchemaAttributeUseProhibPtr) item);
4033 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004034 case XML_SCHEMA_TYPE_ELEMENT:
4035 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4036 break;
4037 case XML_SCHEMA_TYPE_PARTICLE:
4038 if (item->annot != NULL)
4039 xmlSchemaFreeAnnot(item->annot);
4040 xmlFree(item);
4041 break;
4042 case XML_SCHEMA_TYPE_SEQUENCE:
4043 case XML_SCHEMA_TYPE_CHOICE:
4044 case XML_SCHEMA_TYPE_ALL:
4045 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4046 break;
4047 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4048 xmlSchemaFreeAttributeGroup(
4049 (xmlSchemaAttributeGroupPtr) item);
4050 break;
4051 case XML_SCHEMA_TYPE_GROUP:
4052 xmlSchemaFreeModelGroupDef(
4053 (xmlSchemaModelGroupDefPtr) item);
4054 break;
4055 case XML_SCHEMA_TYPE_ANY:
4056 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4057 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4058 break;
4059 case XML_SCHEMA_TYPE_IDC_KEY:
4060 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4061 case XML_SCHEMA_TYPE_IDC_KEYREF:
4062 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4063 break;
4064 case XML_SCHEMA_TYPE_NOTATION:
4065 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4066 break;
4067 case XML_SCHEMA_EXTRA_QNAMEREF:
4068 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4069 break;
4070 default: {
4071 /* TODO: This should never be hit. */
4072 xmlSchemaPSimpleInternalErr(NULL,
4073 "Internal error: xmlSchemaComponentListFree, "
4074 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004075 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004076 }
4077 break;
4078 }
4079 }
4080 list->nbItems = 0;
4081 }
4082}
4083
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004084/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004085 * xmlSchemaFree:
4086 * @schema: a schema structure
4087 *
4088 * Deallocate a Schema structure.
4089 */
4090void
4091xmlSchemaFree(xmlSchemaPtr schema)
4092{
4093 if (schema == NULL)
4094 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004095 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004096 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004097 TODO
4098 /*
4099 * Note that those slots are not responsible for freeing
4100 * schema components anymore; this will now be done by
4101 * the schema buckets.
4102 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004103 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004104 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004105 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004106 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004107 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004108 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004109 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004110 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004111 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004112 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004113 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004114 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004115 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004116 xmlHashFree(schema->idcDef, NULL);
4117
Daniel Veillard1d913862003-11-21 00:28:39 +00004118 if (schema->schemasImports != NULL)
4119 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004120 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004121 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004122 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4123 int i;
4124 for (i = 0; i < list->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +00004125 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004126 }
4127 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004128 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004129 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004130 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004131 /* Never free the doc here, since this will be done by the buckets. */
4132
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004133 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004134 xmlFree(schema);
4135}
4136
4137/************************************************************************
4138 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004139 * Debug functions *
4140 * *
4141 ************************************************************************/
4142
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004143#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004144
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004145static void
4146xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4147
Daniel Veillard4255d502002-04-16 15:50:10 +00004148/**
4149 * xmlSchemaElementDump:
4150 * @elem: an element
4151 * @output: the file output
4152 *
4153 * Dump the element
4154 */
4155static void
4156xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004157 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004158 const xmlChar * namespace ATTRIBUTE_UNUSED,
4159 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004160{
4161 if (elem == NULL)
4162 return;
4163
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004164
4165 fprintf(output, "Element");
4166 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4167 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004168 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004169 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004170 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004171 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004172#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004173 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004174 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004175 if (elem->maxOccurs >= UNBOUNDED)
4176 fprintf(output, "max: unbounded\n");
4177 else if (elem->maxOccurs != 1)
4178 fprintf(output, "max: %d\n", elem->maxOccurs);
4179 else
4180 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004181 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004182#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004183 /*
4184 * Misc other properties.
4185 */
4186 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4187 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4188 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004189 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004190 fprintf(output, " props: ");
4191 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4192 fprintf(output, "[fixed] ");
4193 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4194 fprintf(output, "[default] ");
4195 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4196 fprintf(output, "[abstract] ");
4197 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4198 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004199 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004200 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004201 /*
4202 * Default/fixed value.
4203 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004204 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004205 fprintf(output, " value: '%s'\n", elem->value);
4206 /*
4207 * Type.
4208 */
4209 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004210 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004211 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004212 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004213 else
4214 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004215 } else if (elem->subtypes != NULL) {
4216 /*
4217 * Dump local types.
4218 */
4219 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004220 }
4221 /*
4222 * Substitution group.
4223 */
4224 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004225 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004226 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004227 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004228 else
4229 fprintf(output, "\n");
4230 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004231}
4232
4233/**
4234 * xmlSchemaAnnotDump:
4235 * @output: the file output
4236 * @annot: a annotation
4237 *
4238 * Dump the annotation
4239 */
4240static void
4241xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4242{
4243 xmlChar *content;
4244
4245 if (annot == NULL)
4246 return;
4247
4248 content = xmlNodeGetContent(annot->content);
4249 if (content != NULL) {
4250 fprintf(output, " Annot: %s\n", content);
4251 xmlFree(content);
4252 } else
4253 fprintf(output, " Annot: empty\n");
4254}
4255
4256/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004257 * xmlSchemaContentModelDump:
4258 * @particle: the schema particle
4259 * @output: the file output
4260 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004261 *
4262 * Dump a SchemaType structure
4263 */
4264static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004265xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4266{
4267 xmlChar *str = NULL;
4268 xmlSchemaTreeItemPtr term;
4269 char shift[100];
4270 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004271
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004272 if (particle == NULL)
4273 return;
4274 for (i = 0;((i < depth) && (i < 25));i++)
4275 shift[2 * i] = shift[2 * i + 1] = ' ';
4276 shift[2 * i] = shift[2 * i + 1] = 0;
4277 fprintf(output, shift);
4278 if (particle->children == NULL) {
4279 fprintf(output, "MISSING particle term\n");
4280 return;
4281 }
4282 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004283 if (term == NULL) {
4284 fprintf(output, "(NULL)");
4285 } else {
4286 switch (term->type) {
4287 case XML_SCHEMA_TYPE_ELEMENT:
4288 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4289 ((xmlSchemaElementPtr)term)->targetNamespace,
4290 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004291 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004292 break;
4293 case XML_SCHEMA_TYPE_SEQUENCE:
4294 fprintf(output, "SEQUENCE");
4295 break;
4296 case XML_SCHEMA_TYPE_CHOICE:
4297 fprintf(output, "CHOICE");
4298 break;
4299 case XML_SCHEMA_TYPE_ALL:
4300 fprintf(output, "ALL");
4301 break;
4302 case XML_SCHEMA_TYPE_ANY:
4303 fprintf(output, "ANY");
4304 break;
4305 default:
4306 fprintf(output, "UNKNOWN\n");
4307 return;
4308 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004309 }
4310 if (particle->minOccurs != 1)
4311 fprintf(output, " min: %d", particle->minOccurs);
4312 if (particle->maxOccurs >= UNBOUNDED)
4313 fprintf(output, " max: unbounded");
4314 else if (particle->maxOccurs != 1)
4315 fprintf(output, " max: %d", particle->maxOccurs);
4316 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004317 if (term &&
4318 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4319 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4320 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4321 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004322 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4323 output, depth +1);
4324 }
4325 if (particle->next != NULL)
4326 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4327 output, depth);
4328}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004329
4330/**
4331 * xmlSchemaAttrUsesDump:
4332 * @uses: attribute uses list
Daniel Veillarddee23482008-04-11 12:58:43 +00004333 * @output: the file output
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004334 *
4335 * Dumps a list of attribute use components.
4336 */
4337static void
4338xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4339{
4340 xmlSchemaAttributeUsePtr use;
4341 xmlSchemaAttributeUseProhibPtr prohib;
4342 xmlSchemaQNameRefPtr ref;
4343 const xmlChar *name, *tns;
4344 xmlChar *str = NULL;
4345 int i;
4346
4347 if ((uses == NULL) || (uses->nbItems == 0))
4348 return;
4349
Daniel Veillarddee23482008-04-11 12:58:43 +00004350 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004351 for (i = 0; i < uses->nbItems; i++) {
4352 use = uses->items[i];
4353 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4354 fprintf(output, " [prohibition] ");
4355 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4356 name = prohib->name;
4357 tns = prohib->targetNamespace;
4358 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4359 fprintf(output, " [reference] ");
4360 ref = (xmlSchemaQNameRefPtr) use;
4361 name = ref->name;
4362 tns = ref->targetNamespace;
4363 } else {
4364 fprintf(output, " [use] ");
4365 name = WXS_ATTRUSE_DECL_NAME(use);
4366 tns = WXS_ATTRUSE_DECL_TNS(use);
4367 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004368 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004369 (const char *) xmlSchemaFormatQName(&str, tns, name));
4370 FREE_AND_NULL(str);
4371 }
4372}
4373
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004374/**
4375 * xmlSchemaTypeDump:
4376 * @output: the file output
4377 * @type: a type structure
4378 *
4379 * Dump a SchemaType structure
4380 */
4381static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004382xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4383{
4384 if (type == NULL) {
4385 fprintf(output, "Type: NULL\n");
4386 return;
4387 }
4388 fprintf(output, "Type: ");
4389 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004390 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004391 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004392 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004393 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004394 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004395 switch (type->type) {
4396 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004397 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 break;
4399 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004401 break;
4402 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 break;
4405 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004407 break;
4408 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004409 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004410 break;
4411 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 break;
4414 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004415 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004416 break;
4417 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004418 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004419 break;
4420 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004421 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004422 break;
4423 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004424 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004425 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004426 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004427 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004428 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004429 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004430 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004431 break;
4432 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004433 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004434 break;
4435 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004436 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004437 break;
4438 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004439 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004440 break;
4441 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004442 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004443 break;
4444 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004445 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004446 break;
4447 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004448 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004449 break;
4450 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004451 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004452 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004453 }
4454 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004455 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004456 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004457 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004458 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004459 else
4460 fprintf(output, "\n");
4461 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004462 if (type->attrUses != NULL)
4463 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004464 if (type->annot != NULL)
4465 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004466#ifdef DUMP_CONTENT_MODEL
4467 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4468 (type->subtypes != NULL)) {
4469 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4470 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004471 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004472#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004473}
4474
4475/**
4476 * xmlSchemaDump:
4477 * @output: the file output
4478 * @schema: a schema structure
4479 *
4480 * Dump a Schema structure.
4481 */
4482void
4483xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4484{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004485 if (output == NULL)
4486 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004487 if (schema == NULL) {
4488 fprintf(output, "Schemas: NULL\n");
4489 return;
4490 }
4491 fprintf(output, "Schemas: ");
4492 if (schema->name != NULL)
4493 fprintf(output, "%s, ", schema->name);
4494 else
4495 fprintf(output, "no name, ");
4496 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004497 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004498 else
4499 fprintf(output, "no target namespace");
4500 fprintf(output, "\n");
4501 if (schema->annot != NULL)
4502 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004503 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4504 output);
4505 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004506 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004507}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004508
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004509#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004510/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004511 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004512 * @vctxt: the WXS validation context
4513 *
4514 * Displays the current IDC table for debug purposes.
4515 */
4516static void
4517xmlSchemaDebugDumpIDCTable(FILE * output,
4518 const xmlChar *namespaceName,
4519 const xmlChar *localName,
4520 xmlSchemaPSVIIDCBindingPtr bind)
4521{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004522 xmlChar *str = NULL;
4523 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004524 xmlSchemaPSVIIDCNodePtr tab;
4525 xmlSchemaPSVIIDCKeyPtr key;
4526 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004527
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004528 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004529 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004530 FREE_AND_NULL(str)
4531
4532 if (bind == NULL)
4533 return;
4534 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004535 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Daniel Veillarddee23482008-04-11 12:58:43 +00004536 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004537 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004538 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004539 for (i = 0; i < bind->nbNodes; i++) {
4540 tab = bind->nodeTable[i];
4541 fprintf(output, " ( ");
4542 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004543 key = tab->keys[j];
4544 if ((key != NULL) && (key->val != NULL)) {
4545 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004546 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004547 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004548 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004549 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004550 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004551 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004552 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004553 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004554 else
4555 fprintf(output, "(key missing), ");
4556 }
4557 fprintf(output, ")\n");
4558 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004559 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004560 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004561 for (i = 0; i < bind->dupls->nbItems; i++) {
4562 tab = bind->dupls->items[i];
4563 fprintf(output, " ( ");
4564 for (j = 0; j < bind->definition->nbFields; j++) {
4565 key = tab->keys[j];
4566 if ((key != NULL) && (key->val != NULL)) {
4567 res = xmlSchemaGetCanonValue(key->val, &value);
4568 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004569 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004570 else
4571 fprintf(output, "CANON-VALUE-FAILED ");
4572 if (res == 0)
4573 FREE_AND_NULL(value)
4574 } else if (key != NULL)
4575 fprintf(output, "(no val), ");
4576 else
4577 fprintf(output, "(key missing), ");
4578 }
4579 fprintf(output, ")\n");
4580 }
4581 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004582 bind = bind->next;
4583 } while (bind != NULL);
4584}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004585#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004586#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004587
4588/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004589 * *
4590 * Utilities *
4591 * *
4592 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004593
Daniel Veillardc0826a72004-08-10 14:17:33 +00004594/**
4595 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004596 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004597 * @name: the name of the attribute
4598 *
4599 * Seeks an attribute with a name of @name in
4600 * no namespace.
4601 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004602 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004603 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004604static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004605xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004606{
4607 xmlAttrPtr prop;
4608
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004609 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004610 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004611 prop = node->properties;
4612 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004613 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004614 return(prop);
4615 prop = prop->next;
4616 }
4617 return (NULL);
4618}
4619
4620/**
4621 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004622 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004623 * @uri: the uri
4624 * @name: the name of the attribute
4625 *
4626 * Seeks an attribute with a local name of @name and
4627 * a namespace URI of @uri.
4628 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004629 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004630 */
4631static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004632xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004633{
4634 xmlAttrPtr prop;
4635
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004636 if ((node == NULL) || (name == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00004637 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004638 prop = node->properties;
4639 while (prop != NULL) {
4640 if ((prop->ns != NULL) &&
4641 xmlStrEqual(prop->name, BAD_CAST name) &&
4642 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004643 return(prop);
4644 prop = prop->next;
4645 }
4646 return (NULL);
4647}
4648
4649static const xmlChar *
4650xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4651{
4652 xmlChar *val;
4653 const xmlChar *ret;
4654
4655 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004656 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004657 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004658 ret = xmlDictLookup(ctxt->dict, val, -1);
4659 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004660 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004661}
4662
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004663static const xmlChar *
4664xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4665{
4666 return((const xmlChar*) xmlNodeGetContent(node));
4667}
4668
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004669/**
4670 * xmlSchemaGetProp:
4671 * @ctxt: the parser context
4672 * @node: the node
4673 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004674 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004675 * Read a attribute value and internalize the string
4676 *
4677 * Returns the string or NULL if not present.
4678 */
4679static const xmlChar *
4680xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4681 const char *name)
4682{
4683 xmlChar *val;
4684 const xmlChar *ret;
4685
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004686 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004687 if (val == NULL)
4688 return(NULL);
4689 ret = xmlDictLookup(ctxt->dict, val, -1);
4690 xmlFree(val);
4691 return(ret);
4692}
4693
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004694/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004695 * *
4696 * Parsing functions *
4697 * *
4698 ************************************************************************/
4699
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004700#define WXS_FIND_GLOBAL_ITEM(slot) \
4701 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4702 ret = xmlHashLookup(schema->slot, name); \
4703 if (ret != NULL) goto exit; \
4704 } \
4705 if (xmlHashSize(schema->schemasImports) > 1) { \
4706 xmlSchemaImportPtr import; \
4707 if (nsName == NULL) \
4708 import = xmlHashLookup(schema->schemasImports, \
4709 XML_SCHEMAS_NO_NAMESPACE); \
4710 else \
4711 import = xmlHashLookup(schema->schemasImports, nsName); \
4712 if (import == NULL) \
4713 goto exit; \
4714 ret = xmlHashLookup(import->schema->slot, name); \
4715 }
4716
Daniel Veillard4255d502002-04-16 15:50:10 +00004717/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004718 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004719 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004720 * @name: the element name
4721 * @ns: the element namespace
4722 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004723 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004724 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004725 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004726 */
4727static xmlSchemaElementPtr
4728xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004729 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004730{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004731 xmlSchemaElementPtr ret = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00004732
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004733 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004734 return(NULL);
4735 if (schema != NULL) {
4736 WXS_FIND_GLOBAL_ITEM(elemDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004737 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004738exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004739#ifdef DEBUG
4740 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004741 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004742 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004743 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004744 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004745 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004746 }
4747#endif
4748 return (ret);
4749}
4750
4751/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004752 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004753 * @schema: the main schema
4754 * @name: the type's name
4755 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004756 *
4757 * Lookup a type in the schemas or the predefined types
4758 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004759 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004760 */
4761static xmlSchemaTypePtr
4762xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004763 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004764{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004765 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004766
4767 if (name == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004768 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004769 /* First try the built-in types. */
Daniel Veillarddee23482008-04-11 12:58:43 +00004770 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004771 ret = xmlSchemaGetPredefinedType(name, nsName);
4772 if (ret != NULL)
4773 goto exit;
4774 /*
4775 * Note that we try the parsed schemas as well here
4776 * since one might have parsed the S4S, which contain more
4777 * than the built-in types.
4778 * TODO: Can we optimize this?
4779 */
4780 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004781 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004782 WXS_FIND_GLOBAL_ITEM(typeDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004783 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004784exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004785
Daniel Veillard4255d502002-04-16 15:50:10 +00004786#ifdef DEBUG
4787 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004788 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004789 fprintf(stderr, "Unable to lookup type %s", name);
4790 else
4791 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004792 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004793 }
4794#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004795 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004796}
4797
Daniel Veillard3646d642004-06-02 19:19:14 +00004798/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004799 * xmlSchemaGetAttributeDecl:
4800 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004801 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004802 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004803 *
4804 * Lookup a an attribute in the schema or imported schemas
4805 *
4806 * Returns the attribute declaration or NULL if not found.
4807 */
4808static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004809xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004810 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004811{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004812 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004813
4814 if ((name == NULL) || (schema == NULL))
4815 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004816 if (schema != NULL) {
4817 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004819exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004820#ifdef DEBUG
4821 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004822 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004823 fprintf(stderr, "Unable to lookup attribute %s", name);
4824 else
4825 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004826 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004827 }
4828#endif
4829 return (ret);
4830}
4831
4832/**
4833 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004834 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004835 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004836 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004837 *
4838 * Lookup a an attribute group in the schema or imported schemas
4839 *
4840 * Returns the attribute group definition or NULL if not found.
4841 */
4842static xmlSchemaAttributeGroupPtr
4843xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004844 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004845{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004846 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004847
4848 if ((name == NULL) || (schema == NULL))
4849 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004850 if (schema != NULL) {
4851 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4852 }
4853exit:
4854 /* TODO:
4855 if ((ret != NULL) && (ret->redef != NULL)) {
4856 * Return the last redefinition. *
4857 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004858 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004859 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004860#ifdef DEBUG
4861 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004862 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004863 fprintf(stderr, "Unable to lookup attribute group %s", name);
4864 else
4865 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004866 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004867 }
4868#endif
4869 return (ret);
4870}
4871
4872/**
4873 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004874 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004875 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004876 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004877 *
4878 * Lookup a group in the schema or imported schemas
4879 *
4880 * Returns the group definition or NULL if not found.
4881 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004882static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004883xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004884 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004885{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004886 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004887
4888 if ((name == NULL) || (schema == NULL))
4889 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004890 if (schema != NULL) {
4891 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004892 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004893exit:
Daniel Veillarddee23482008-04-11 12:58:43 +00004894
Daniel Veillard3646d642004-06-02 19:19:14 +00004895#ifdef DEBUG
4896 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004897 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004898 fprintf(stderr, "Unable to lookup group %s", name);
4899 else
4900 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004901 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004902 }
4903#endif
4904 return (ret);
4905}
4906
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004907static xmlSchemaNotationPtr
4908xmlSchemaGetNotation(xmlSchemaPtr schema,
4909 const xmlChar *name,
4910 const xmlChar *nsName)
4911{
4912 xmlSchemaNotationPtr ret = NULL;
4913
4914 if ((name == NULL) || (schema == NULL))
4915 return (NULL);
4916 if (schema != NULL) {
4917 WXS_FIND_GLOBAL_ITEM(notaDecl)
4918 }
4919exit:
4920 return (ret);
4921}
4922
4923static xmlSchemaIDCPtr
4924xmlSchemaGetIDC(xmlSchemaPtr schema,
4925 const xmlChar *name,
4926 const xmlChar *nsName)
4927{
4928 xmlSchemaIDCPtr ret = NULL;
4929
4930 if ((name == NULL) || (schema == NULL))
4931 return (NULL);
4932 if (schema != NULL) {
4933 WXS_FIND_GLOBAL_ITEM(idcDef)
4934 }
4935exit:
4936 return (ret);
4937}
4938
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004939/**
4940 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004941 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004942 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004943 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004944 *
4945 * Lookup a group in the schema or imported schemas
4946 *
4947 * Returns the group definition or NULL if not found.
4948 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004949static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004950xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4951 xmlSchemaTypeType itemType,
4952 const xmlChar *name,
4953 const xmlChar *targetNs)
4954{
4955 switch (itemType) {
4956 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004957 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004958 name, targetNs));
4959 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004960 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004961 name, targetNs));
4962 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004963 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004964 return (NULL);
4965 }
4966}
4967
Daniel Veillard4255d502002-04-16 15:50:10 +00004968/************************************************************************
4969 * *
4970 * Parsing functions *
4971 * *
4972 ************************************************************************/
4973
4974#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004975 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004976
4977/**
4978 * xmlSchemaIsBlank:
4979 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004980 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004981 *
4982 * Check if a string is ignorable
4983 *
4984 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4985 */
4986static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004987xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004988{
Daniel Veillard4255d502002-04-16 15:50:10 +00004989 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004990 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004991 if (len < 0) {
4992 while (*str != 0) {
4993 if (!(IS_BLANK_CH(*str)))
4994 return (0);
4995 str++;
4996 }
4997 } else while ((*str != 0) && (len != 0)) {
4998 if (!(IS_BLANK_CH(*str)))
4999 return (0);
5000 str++;
5001 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00005002 }
Daniel Veillarddee23482008-04-11 12:58:43 +00005003
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005004 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005005}
5006
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005007#define WXS_COMP_NAME(c, t) ((t) (c))->name
5008#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
5009/*
5010* xmlSchemaFindRedefCompInGraph:
5011* ATTENTION TODO: This uses pointer comp. for strings.
5012*/
5013static xmlSchemaBasicItemPtr
5014xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
5015 xmlSchemaTypeType type,
5016 const xmlChar *name,
5017 const xmlChar *nsName)
5018{
5019 xmlSchemaBasicItemPtr ret;
5020 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005021
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005022 if ((bucket == NULL) || (name == NULL))
5023 return(NULL);
5024 if ((bucket->globals == NULL) ||
5025 (bucket->globals->nbItems == 0))
5026 goto subschemas;
5027 /*
5028 * Search in global components.
5029 */
5030 for (i = 0; i < bucket->globals->nbItems; i++) {
5031 ret = bucket->globals->items[i];
5032 if (ret->type == type) {
5033 switch (type) {
5034 case XML_SCHEMA_TYPE_COMPLEX:
5035 case XML_SCHEMA_TYPE_SIMPLE:
5036 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5037 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5038 nsName))
5039 {
5040 return(ret);
5041 }
5042 break;
5043 case XML_SCHEMA_TYPE_GROUP:
5044 if ((WXS_COMP_NAME(ret,
5045 xmlSchemaModelGroupDefPtr) == name) &&
5046 (WXS_COMP_TNS(ret,
5047 xmlSchemaModelGroupDefPtr) == nsName))
5048 {
5049 return(ret);
5050 }
5051 break;
5052 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5053 if ((WXS_COMP_NAME(ret,
5054 xmlSchemaAttributeGroupPtr) == name) &&
5055 (WXS_COMP_TNS(ret,
5056 xmlSchemaAttributeGroupPtr) == nsName))
5057 {
5058 return(ret);
5059 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005060 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005061 default:
5062 /* Should not be hit. */
5063 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005064 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005065 }
5066 }
5067subschemas:
5068 /*
5069 * Process imported/included schemas.
5070 */
5071 if (bucket->relations != NULL) {
5072 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5073
5074 /*
5075 * TODO: Marking the bucket will not avoid multiple searches
5076 * in the same schema, but avoids at least circularity.
5077 */
5078 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5079 do {
5080 if ((rel->bucket != NULL) &&
5081 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5082 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5083 type, name, nsName);
5084 if (ret != NULL)
5085 return(ret);
5086 }
5087 rel = rel->next;
5088 } while (rel != NULL);
5089 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5090 }
5091 return(NULL);
5092}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005093
5094/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005095 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005096 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005097 * @schema: the schema being built
5098 * @name: the item name
5099 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005100 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005101 * *WARNING* this interface is highly subject to change
5102 *
5103 * Returns the new struture or NULL in case of error
5104 */
5105static xmlSchemaNotationPtr
5106xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005107 const xmlChar *name, const xmlChar *nsName,
5108 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005109{
5110 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005111
5112 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5113 return (NULL);
5114
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5116 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005117 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 return (NULL);
5119 }
5120 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005121 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005122 ret->name = name;
5123 ret->targetNamespace = nsName;
5124 /* TODO: do we need the node to be set?
5125 * ret->node = node;*/
5126 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005127 return (ret);
5128}
5129
Daniel Veillard4255d502002-04-16 15:50:10 +00005130/**
5131 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005132 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005133 * @schema: the schema being built
5134 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005135 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005136 *
5137 * Add an XML schema Attrribute declaration
5138 * *WARNING* this interface is highly subject to change
5139 *
5140 * Returns the new struture or NULL in case of error
5141 */
5142static xmlSchemaAttributePtr
5143xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005144 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005145 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005146{
5147 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005148
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005149 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005150 return (NULL);
5151
5152 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5153 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005154 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 return (NULL);
5156 }
5157 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005158 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5159 ret->node = node;
5160 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005161 ret->targetNamespace = nsName;
5162
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005163 if (topLevel)
5164 WXS_ADD_GLOBAL(ctxt, ret);
5165 else
5166 WXS_ADD_LOCAL(ctxt, ret);
5167 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005168 return (ret);
5169}
5170
5171/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005172 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005173 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005174 * @schema: the schema being built
5175 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005176 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005177 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005178 * Add an XML schema Attrribute declaration
5179 * *WARNING* this interface is highly subject to change
5180 *
5181 * Returns the new struture or NULL in case of error
5182 */
5183static xmlSchemaAttributeUsePtr
5184xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5185 xmlNodePtr node)
5186{
5187 xmlSchemaAttributeUsePtr ret = NULL;
5188
5189 if (pctxt == NULL)
5190 return (NULL);
5191
5192 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5193 if (ret == NULL) {
5194 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5195 return (NULL);
5196 }
5197 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5198 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5199 ret->node = node;
5200
5201 WXS_ADD_LOCAL(pctxt, ret);
5202 return (ret);
5203}
5204
5205/*
5206* xmlSchemaAddRedef:
5207*
5208* Adds a redefinition information. This is used at a later stage to:
5209* resolve references to the redefined components and to check constraints.
5210*/
5211static xmlSchemaRedefPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00005212xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005213 xmlSchemaBucketPtr targetBucket,
5214 void *item,
5215 const xmlChar *refName,
5216 const xmlChar *refTargetNs)
5217{
5218 xmlSchemaRedefPtr ret;
5219
5220 ret = (xmlSchemaRedefPtr)
5221 xmlMalloc(sizeof(xmlSchemaRedef));
5222 if (ret == NULL) {
5223 xmlSchemaPErrMemory(pctxt,
5224 "allocating redefinition info", NULL);
5225 return (NULL);
5226 }
5227 memset(ret, 0, sizeof(xmlSchemaRedef));
5228 ret->item = item;
5229 ret->targetBucket = targetBucket;
5230 ret->refName = refName;
5231 ret->refTargetNs = refTargetNs;
5232 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5233 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5234 else
5235 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5236 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5237
5238 return (ret);
5239}
5240
5241/**
5242 * xmlSchemaAddAttributeGroupDefinition:
5243 * @ctxt: a schema parser context
5244 * @schema: the schema being built
5245 * @name: the item name
5246 * @nsName: the target namespace
5247 * @node: the corresponding node
5248 *
5249 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005250 *
5251 * Returns the new struture or NULL in case of error
5252 */
5253static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005254xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5255 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5256 const xmlChar *name,
5257 const xmlChar *nsName,
5258 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005259{
5260 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005261
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005262 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005263 return (NULL);
5264
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005265 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005266 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005267 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005268 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005269 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005270 }
5271 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005272 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005273 ret->name = name;
5274 ret->targetNamespace = nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +00005275 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005277 /* TODO: Remove the flag. */
5278 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5279 if (pctxt->isRedefine) {
5280 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5281 ret, name, nsName);
5282 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005283 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005284 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005285 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005286 pctxt->redefCounter = 0;
5287 }
5288 WXS_ADD_GLOBAL(pctxt, ret);
5289 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005290 return (ret);
5291}
5292
5293/**
5294 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005295 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 * @schema: the schema being built
5297 * @name: the type name
5298 * @namespace: the type namespace
5299 *
5300 * Add an XML schema Element declaration
5301 * *WARNING* this interface is highly subject to change
5302 *
5303 * Returns the new struture or NULL in case of error
5304 */
5305static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005306xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005307 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005308 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005309{
5310 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005311
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005312 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005313 return (NULL);
5314
Daniel Veillard4255d502002-04-16 15:50:10 +00005315 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5316 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005317 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 return (NULL);
5319 }
5320 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005321 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5322 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005323 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005324 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005326 if (topLevel)
5327 WXS_ADD_GLOBAL(ctxt, ret);
5328 else
5329 WXS_ADD_LOCAL(ctxt, ret);
5330 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005331 return (ret);
5332}
5333
5334/**
5335 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005336 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005337 * @schema: the schema being built
5338 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005339 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005340 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005341 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 * *WARNING* this interface is highly subject to change
5343 *
5344 * Returns the new struture or NULL in case of error
5345 */
5346static xmlSchemaTypePtr
5347xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005348 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005349 const xmlChar * name, const xmlChar * nsName,
5350 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005351{
5352 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005353
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005354 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005355 return (NULL);
5356
5357 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5358 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005359 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005360 return (NULL);
5361 }
5362 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005363 ret->type = type;
5364 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005365 ret->targetNamespace = nsName;
5366 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005367 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005368 if (ctxt->isRedefine) {
5369 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5370 ret, name, nsName);
5371 if (ctxt->redef == NULL) {
5372 xmlFree(ret);
5373 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005374 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005375 ctxt->redefCounter = 0;
5376 }
5377 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005378 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005379 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005380 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005381 return (ret);
5382}
5383
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005384static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005385xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005386 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005387 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005388 const xmlChar *refNs)
5389{
5390 xmlSchemaQNameRefPtr ret;
5391
5392 ret = (xmlSchemaQNameRefPtr)
5393 xmlMalloc(sizeof(xmlSchemaQNameRef));
5394 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005395 xmlSchemaPErrMemory(pctxt,
5396 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005397 return (NULL);
5398 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005399 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005400 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5401 ret->name = refName;
5402 ret->targetNamespace = refNs;
5403 ret->item = NULL;
5404 ret->itemType = refType;
5405 /*
5406 * Store the reference item in the schema.
5407 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005408 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005409 return (ret);
5410}
5411
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005412static xmlSchemaAttributeUseProhibPtr
5413xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5414{
5415 xmlSchemaAttributeUseProhibPtr ret;
5416
5417 ret = (xmlSchemaAttributeUseProhibPtr)
5418 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5419 if (ret == NULL) {
5420 xmlSchemaPErrMemory(pctxt,
5421 "allocating attribute use prohibition", NULL);
5422 return (NULL);
5423 }
5424 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5425 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5426 WXS_ADD_LOCAL(pctxt, ret);
5427 return (ret);
5428}
5429
5430
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005431/**
5432 * xmlSchemaAddModelGroup:
5433 * @ctxt: a schema parser context
5434 * @schema: the schema being built
5435 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005436 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005437 *
5438 * Adds a schema model group
5439 * *WARNING* this interface is highly subject to change
5440 *
5441 * Returns the new struture or NULL in case of error
5442 */
5443static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005444xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5445 xmlSchemaPtr schema,
5446 xmlSchemaTypeType type,
5447 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005448{
5449 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005450
5451 if ((ctxt == NULL) || (schema == NULL))
5452 return (NULL);
5453
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005454 ret = (xmlSchemaModelGroupPtr)
5455 xmlMalloc(sizeof(xmlSchemaModelGroup));
5456 if (ret == NULL) {
5457 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5458 NULL);
5459 return (NULL);
5460 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005461 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005462 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005463 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005464 WXS_ADD_LOCAL(ctxt, ret);
5465 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5466 (type == XML_SCHEMA_TYPE_CHOICE))
5467 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005468 return (ret);
5469}
5470
5471
5472/**
5473 * xmlSchemaAddParticle:
5474 * @ctxt: a schema parser context
5475 * @schema: the schema being built
5476 * @node: the corresponding node in the schema doc
5477 * @min: the minOccurs
5478 * @max: the maxOccurs
5479 *
5480 * Adds an XML schema particle component.
5481 * *WARNING* this interface is highly subject to change
5482 *
5483 * Returns the new struture or NULL in case of error
5484 */
5485static xmlSchemaParticlePtr
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005486xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005487 xmlNodePtr node, int min, int max)
5488{
5489 xmlSchemaParticlePtr ret = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005490 if (ctxt == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005491 return (NULL);
5492
5493#ifdef DEBUG
5494 fprintf(stderr, "Adding particle component\n");
5495#endif
5496 ret = (xmlSchemaParticlePtr)
5497 xmlMalloc(sizeof(xmlSchemaParticle));
5498 if (ret == NULL) {
5499 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5500 NULL);
5501 return (NULL);
5502 }
5503 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5504 ret->annot = NULL;
5505 ret->node = node;
5506 ret->minOccurs = min;
5507 ret->maxOccurs = max;
5508 ret->next = NULL;
5509 ret->children = NULL;
5510
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005511 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005512 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005513 * Note that addition to pending components will be done locally
5514 * to the specific parsing function, since the most particles
5515 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005516 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005517 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005518 return (ret);
5519}
5520
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005521/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005522 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005523 * @ctxt: a schema validation context
5524 * @schema: the schema being built
5525 * @name: the group name
5526 *
5527 * Add an XML schema Group definition
5528 *
5529 * Returns the new struture or NULL in case of error
5530 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005531static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005532xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5533 xmlSchemaPtr schema,
5534 const xmlChar *name,
5535 const xmlChar *nsName,
5536 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005537{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005538 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005539
5540 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5541 return (NULL);
5542
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005543 ret = (xmlSchemaModelGroupDefPtr)
5544 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005545 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005546 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005547 return (NULL);
5548 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005549 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005550 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005551 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005552 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005553 ret->targetNamespace = nsName;
5554
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005555 if (ctxt->isRedefine) {
5556 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5557 ret, name, nsName);
5558 if (ctxt->redef == NULL) {
5559 xmlFree(ret);
5560 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005561 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005562 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005563 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005564 WXS_ADD_GLOBAL(ctxt, ret);
5565 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005566 return (ret);
5567}
5568
Daniel Veillard3646d642004-06-02 19:19:14 +00005569/**
5570 * xmlSchemaNewWildcardNs:
5571 * @ctxt: a schema validation context
5572 *
5573 * Creates a new wildcard namespace constraint.
5574 *
5575 * Returns the new struture or NULL in case of error
5576 */
5577static xmlSchemaWildcardNsPtr
5578xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5579{
5580 xmlSchemaWildcardNsPtr ret;
5581
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005582 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005583 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5584 if (ret == NULL) {
5585 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005586 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005587 }
5588 ret->value = NULL;
5589 ret->next = NULL;
5590 return (ret);
5591}
5592
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005593static xmlSchemaIDCPtr
5594xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5595 const xmlChar *name, const xmlChar *nsName,
5596 int category, xmlNodePtr node)
5597{
Daniel Veillarddee23482008-04-11 12:58:43 +00005598 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005599
5600 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5601 return (NULL);
5602
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005603 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5604 if (ret == NULL) {
5605 xmlSchemaPErrMemory(ctxt,
5606 "allocating an identity-constraint definition", NULL);
5607 return (NULL);
5608 }
5609 memset(ret, 0, sizeof(xmlSchemaIDC));
5610 /* The target namespace of the parent element declaration. */
5611 ret->targetNamespace = nsName;
5612 ret->name = name;
5613 ret->type = category;
Daniel Veillarddee23482008-04-11 12:58:43 +00005614 ret->node = node;
5615
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005616 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005617 /*
5618 * Only keyrefs need to be fixup up.
5619 */
5620 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005621 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005622 return (ret);
5623}
5624
Daniel Veillard3646d642004-06-02 19:19:14 +00005625/**
5626 * xmlSchemaAddWildcard:
5627 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005628 * @schema: a schema
5629 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005630 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005631 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005632 *
5633 * Returns the new struture or NULL in case of error
5634 */
5635static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005636xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5637 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005638{
5639 xmlSchemaWildcardPtr ret = NULL;
5640
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005641 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005642 return (NULL);
5643
5644 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5645 if (ret == NULL) {
5646 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5647 return (NULL);
5648 }
5649 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005650 ret->type = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00005651 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005652 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005653 return (ret);
5654}
5655
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005656static void
5657xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5658{
5659 if (group == NULL)
5660 return;
5661 if (group->members != NULL)
5662 xmlSchemaItemListFree(group->members);
5663 xmlFree(group);
5664}
5665
5666static xmlSchemaSubstGroupPtr
5667xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5668 xmlSchemaElementPtr head)
5669{
5670 xmlSchemaSubstGroupPtr ret;
5671
5672 /* Init subst group hash. */
5673 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5674 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5675 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5676 return(NULL);
5677 }
5678 /* Create a new substitution group. */
5679 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5680 if (ret == NULL) {
5681 xmlSchemaPErrMemory(NULL,
5682 "allocating a substitution group container", NULL);
5683 return(NULL);
5684 }
5685 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5686 ret->head = head;
5687 /* Create list of members. */
5688 ret->members = xmlSchemaItemListCreate();
5689 if (ret->members == NULL) {
5690 xmlSchemaSubstGroupFree(ret);
5691 return(NULL);
5692 }
5693 /* Add subst group to hash. */
5694 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5695 head->name, head->targetNamespace, ret) != 0) {
5696 PERROR_INT("xmlSchemaSubstGroupAdd",
5697 "failed to add a new substitution container");
5698 xmlSchemaSubstGroupFree(ret);
5699 return(NULL);
5700 }
5701 return(ret);
5702}
5703
5704static xmlSchemaSubstGroupPtr
5705xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5706 xmlSchemaElementPtr head)
Daniel Veillarddee23482008-04-11 12:58:43 +00005707{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005708 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5709 return(NULL);
5710 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5711 head->name, head->targetNamespace));
5712
5713}
5714
5715/**
5716 * xmlSchemaAddElementSubstitutionMember:
5717 * @pctxt: a schema parser context
5718 * @head: the head of the substitution group
5719 * @member: the new member of the substitution group
5720 *
5721 * Allocate a new annotation structure.
5722 *
5723 * Returns the newly allocated structure or NULL in case or error
5724 */
5725static int
5726xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5727 xmlSchemaElementPtr head,
5728 xmlSchemaElementPtr member)
5729{
5730 xmlSchemaSubstGroupPtr substGroup = NULL;
5731
5732 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5733 return (-1);
5734
5735 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5736 if (substGroup == NULL)
5737 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5738 if (substGroup == NULL)
5739 return(-1);
5740 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5741 return(-1);
5742 return(0);
5743}
5744
Daniel Veillard4255d502002-04-16 15:50:10 +00005745/************************************************************************
5746 * *
5747 * Utilities for parsing *
5748 * *
5749 ************************************************************************/
5750
Daniel Veillard4255d502002-04-16 15:50:10 +00005751/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005752 * xmlSchemaPValAttrNodeQNameValue:
5753 * @ctxt: a schema parser context
5754 * @schema: the schema context
5755 * @ownerDes: the designation of the parent element
5756 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005757 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005758 * @local: the resulting local part if found, the attribute value otherwise
5759 * @uri: the resulting namespace URI if found
5760 *
5761 * Extracts the local name and the URI of a QName value and validates it.
5762 * This one is intended to be used on attribute values that
5763 * should resolve to schema components.
5764 *
5765 * Returns 0, in case the QName is valid, a positive error code
5766 * if not valid and -1 if an internal error occurs.
5767 */
5768static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005769xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005770 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005771 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005772 xmlAttrPtr attr,
5773 const xmlChar *value,
5774 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005775 const xmlChar **local)
5776{
5777 const xmlChar *pref;
5778 xmlNsPtr ns;
5779 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005780
Daniel Veillardc0826a72004-08-10 14:17:33 +00005781 *uri = NULL;
5782 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005784 if (ret > 0) {
5785 xmlSchemaPSimpleTypeErr(ctxt,
5786 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5787 ownerItem, (xmlNodePtr) attr,
5788 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5789 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005790 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005791 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005792 } else if (ret < 0)
5793 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005794
5795 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005796 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005797 if (ns)
5798 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5799 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005800 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5801 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005802 /*
5803 * This one takes care of included schemas with no
5804 * target namespace.
5805 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005806 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005807 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005808 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005809 return (0);
5810 }
5811 /*
5812 * At this point xmlSplitQName3 has to return a local name.
5813 */
5814 *local = xmlSplitQName3(value, &len);
5815 *local = xmlDictLookup(ctxt->dict, *local, -1);
5816 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005817 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5818 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005819 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005820 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005821 ownerItem, (xmlNodePtr) attr,
5822 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5823 "The value '%s' of simple type 'xs:QName' has no "
5824 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005825 return (ctxt->err);
5826 } else {
5827 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005828 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005829 return (0);
5830}
5831
5832/**
5833 * xmlSchemaPValAttrNodeQName:
5834 * @ctxt: a schema parser context
5835 * @schema: the schema context
5836 * @ownerDes: the designation of the owner element
5837 * @ownerItem: the owner as a schema object
5838 * @attr: the attribute node
5839 * @local: the resulting local part if found, the attribute value otherwise
5840 * @uri: the resulting namespace URI if found
5841 *
5842 * Extracts and validates the QName of an attribute value.
5843 * This one is intended to be used on attribute values that
5844 * should resolve to schema components.
5845 *
5846 * Returns 0, in case the QName is valid, a positive error code
5847 * if not valid and -1 if an internal error occurs.
5848 */
5849static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005850xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005851 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005852 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005853 xmlAttrPtr attr,
5854 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005855 const xmlChar **local)
5856{
5857 const xmlChar *value;
5858
5859 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005860 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005861 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005862}
5863
5864/**
5865 * xmlSchemaPValAttrQName:
5866 * @ctxt: a schema parser context
5867 * @schema: the schema context
5868 * @ownerDes: the designation of the parent element
5869 * @ownerItem: the owner as a schema object
5870 * @ownerElem: the parent node of the attribute
5871 * @name: the name of the attribute
5872 * @local: the resulting local part if found, the attribute value otherwise
5873 * @uri: the resulting namespace URI if found
5874 *
5875 * Extracts and validates the QName of an attribute value.
5876 *
5877 * Returns 0, in case the QName is valid, a positive error code
5878 * if not valid and -1 if an internal error occurs.
5879 */
5880static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005881xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5882 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005883 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005884 xmlNodePtr ownerElem,
5885 const char *name,
5886 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005887 const xmlChar **local)
5888{
5889 xmlAttrPtr attr;
5890
5891 attr = xmlSchemaGetPropNode(ownerElem, name);
5892 if (attr == NULL) {
5893 *local = NULL;
5894 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005895 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005896 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005897 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005898 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005899}
5900
5901/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005902 * xmlSchemaPValAttrID:
5903 * @ctxt: a schema parser context
5904 * @schema: the schema context
5905 * @ownerDes: the designation of the parent element
5906 * @ownerItem: the owner as a schema object
5907 * @ownerElem: the parent node of the attribute
5908 * @name: the name of the attribute
5909 *
5910 * Extracts and validates the ID of an attribute value.
5911 *
5912 * Returns 0, in case the ID is valid, a positive error code
5913 * if not valid and -1 if an internal error occurs.
5914 */
5915static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005916xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005917{
5918 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005919 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005920
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005921 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005922 return(0);
5923 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5924 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005925 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005926 /*
5927 * NOTE: the IDness might have already be declared in the DTD
5928 */
5929 if (attr->atype != XML_ATTRIBUTE_ID) {
5930 xmlIDPtr res;
5931 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005932
5933 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005934 * TODO: Use xmlSchemaStrip here; it's not exported at this
5935 * moment.
5936 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005937 strip = xmlSchemaCollapseString(value);
5938 if (strip != NULL) {
5939 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005940 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005941 }
5942 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005943 if (res == NULL) {
5944 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005945 xmlSchemaPSimpleTypeErr(ctxt,
5946 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005947 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005948 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5949 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005950 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005951 } else
5952 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005953 }
5954 } else if (ret > 0) {
5955 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005956 xmlSchemaPSimpleTypeErr(ctxt,
5957 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005958 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005959 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5960 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5961 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005962 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005963 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005964 if (value != NULL)
5965 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005966
5967 return (ret);
5968}
5969
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005970static int
5971xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5972 xmlNodePtr ownerElem,
5973 const xmlChar *name)
5974{
5975 xmlAttrPtr attr;
5976
5977 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5978 if (attr == NULL)
5979 return(0);
5980 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5981
5982}
5983
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005984/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005985 * xmlGetMaxOccurs:
5986 * @ctxt: a schema validation context
5987 * @node: a subtree containing XML Schema informations
5988 *
5989 * Get the maxOccurs property
5990 *
5991 * Returns the default if not found, or the value
5992 */
5993static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005994xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5995 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005996{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005997 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005998 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005999 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006000
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006001 attr = xmlSchemaGetPropNode(node, "maxOccurs");
6002 if (attr == NULL)
6003 return (def);
6004 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006005
6006 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006007 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006008 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006009 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6010 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006011 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006012 val, NULL, NULL, NULL);
6013 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006014 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006015 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00006016 }
6017
6018 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006019 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006020 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006021 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006022 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006023 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6024 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006025 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006026 val, NULL, NULL, NULL);
6027 return (def);
6028 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006029 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006030 ret = ret * 10 + (*cur - '0');
6031 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006032 }
William M. Brack76e95df2003-10-18 16:20:14 +00006033 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006034 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006035 /*
6036 * TODO: Restrict the maximal value to Integer.
6037 */
6038 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006039 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006040 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6041 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006042 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006043 val, NULL, NULL, NULL);
6044 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006045 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006046 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006047}
6048
6049/**
6050 * xmlGetMinOccurs:
6051 * @ctxt: a schema validation context
6052 * @node: a subtree containing XML Schema informations
6053 *
6054 * Get the minOccurs property
6055 *
6056 * Returns the default if not found, or the value
6057 */
6058static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006059xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006060 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006061{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006062 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006063 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006064 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006065
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006066 attr = xmlSchemaGetPropNode(node, "minOccurs");
6067 if (attr == NULL)
6068 return (def);
6069 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006070 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006071 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006072 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006073 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006074 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006075 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6076 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006077 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006078 val, NULL, NULL, NULL);
6079 return (def);
6080 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006081 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006082 ret = ret * 10 + (*cur - '0');
6083 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006084 }
William M. Brack76e95df2003-10-18 16:20:14 +00006085 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006086 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006087 /*
6088 * TODO: Restrict the maximal value to Integer.
6089 */
6090 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006091 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006092 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6093 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006094 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006095 val, NULL, NULL, NULL);
6096 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006097 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006098 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006099}
6100
6101/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006102 * xmlSchemaPGetBoolNodeValue:
6103 * @ctxt: a schema validation context
6104 * @ownerDes: owner designation
6105 * @ownerItem: the owner as a schema item
6106 * @node: the node holding the value
6107 *
6108 * Converts a boolean string value into 1 or 0.
6109 *
6110 * Returns 0 or 1.
6111 */
6112static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006113xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006114 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006115 xmlNodePtr node)
6116{
6117 xmlChar *value = NULL;
6118 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006119
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006120 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006121 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006122 * 3.2.2.1 Lexical representation
Rob Richardsc6947bb2008-06-29 15:04:41 +00006123 * An instance of a datatype that is defined as �boolean�
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006124 * can have the following legal literals {true, false, 1, 0}.
6125 */
6126 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6127 res = 1;
6128 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6129 res = 0;
6130 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6131 res = 1;
6132 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006133 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006134 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006135 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006136 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006137 ownerItem, node,
6138 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6139 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006140 NULL, NULL, NULL);
6141 }
6142 if (value != NULL)
6143 xmlFree(value);
6144 return (res);
6145}
6146
6147/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006148 * xmlGetBooleanProp:
6149 * @ctxt: a schema validation context
6150 * @node: a subtree containing XML Schema informations
6151 * @name: the attribute name
6152 * @def: the default value
6153 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006154 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006155 *
6156 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006157 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006158 */
6159static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006160xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006161 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006162 const char *name, int def)
6163{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006164 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006165
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006166 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006167 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006168 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006169 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006170 * 3.2.2.1 Lexical representation
Rob Richardsc6947bb2008-06-29 15:04:41 +00006171 * An instance of a datatype that is defined as �boolean�
Daniel Veillardc0826a72004-08-10 14:17:33 +00006172 * can have the following legal literals {true, false, 1, 0}.
6173 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006174 if (xmlStrEqual(val, BAD_CAST "true"))
6175 def = 1;
6176 else if (xmlStrEqual(val, BAD_CAST "false"))
6177 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006178 else if (xmlStrEqual(val, BAD_CAST "1"))
6179 def = 1;
6180 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006181 def = 0;
6182 else {
6183 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006184 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006185 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006186 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006187 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6188 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006189 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006190 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006191}
6192
6193/************************************************************************
6194 * *
6195 * Shema extraction from an Infoset *
6196 * *
6197 ************************************************************************/
6198static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6199 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006200 xmlNodePtr node,
6201 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006202static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6203 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006204 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006205 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006206 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006207static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6208 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006209 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006210 xmlNodePtr node,
6211 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006212static xmlSchemaBasicItemPtr
6213xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6214 xmlSchemaPtr schema,
6215 xmlNodePtr node,
6216 xmlSchemaItemListPtr uses,
6217 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006218static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6219 xmlSchemaPtr schema,
6220 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006221static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006222xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6223 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006224
6225/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006226 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006227 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006228 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006229 * @ownerDes: the designation of the parent element
6230 * @ownerItem: the schema object owner if existent
6231 * @attr: the schema attribute node being validated
6232 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006233 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006234 *
6235 * Validates a value against the given built-in type.
6236 * This one is intended to be used internally for validation
6237 * of schema attribute values during parsing of the schema.
6238 *
6239 * Returns 0 if the value is valid, a positive error code
6240 * number otherwise and -1 in case of an internal or API error.
6241 */
6242static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006243xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006244 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006245 xmlAttrPtr attr,
6246 const xmlChar *value,
6247 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006248{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006249
6250 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006251
6252 /*
6253 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6254 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006255 */
6256 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006257 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006258 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6259 PERROR_INT("xmlSchemaPValAttrNodeValue",
6260 "the given type is not a built-in type");
6261 return (-1);
6262 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006263 switch (type->builtInType) {
6264 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006265 case XML_SCHEMAS_QNAME:
6266 case XML_SCHEMAS_ANYURI:
6267 case XML_SCHEMAS_TOKEN:
6268 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006269 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6270 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006271 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006272 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006273 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006274 "validation using the given type is not supported while "
6275 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006276 return (-1);
6277 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006278 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006279 /*
6280 * TODO: Should we use the S4S error codes instead?
6281 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006282 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006283 PERROR_INT("xmlSchemaPValAttrNodeValue",
6284 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006285 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006286 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006287 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006288 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6289 else
6290 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +00006291 xmlSchemaPSimpleTypeErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006292 ret, ownerItem, (xmlNodePtr) attr,
6293 type, NULL, value, NULL, NULL, NULL);
6294 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006295 return (ret);
6296}
6297
6298/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006299 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006300 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006301 * @ctxt: a schema parser context
6302 * @ownerDes: the designation of the parent element
6303 * @ownerItem: the schema object owner if existent
6304 * @attr: the schema attribute node being validated
6305 * @type: the built-in type to be validated against
6306 * @value: the resulting value if any
6307 *
6308 * Extracts and validates a value against the given built-in type.
6309 * This one is intended to be used internally for validation
6310 * of schema attribute values during parsing of the schema.
6311 *
6312 * Returns 0 if the value is valid, a positive error code
6313 * number otherwise and -1 in case of an internal or API error.
6314 */
6315static int
6316xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006317 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006318 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006319 xmlSchemaTypePtr type,
6320 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006321{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006322 const xmlChar *val;
6323
6324 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006325 return (-1);
6326
Daniel Veillardc0826a72004-08-10 14:17:33 +00006327 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6328 if (value != NULL)
6329 *value = val;
6330
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006331 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006332 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006333}
6334
6335/**
6336 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006337 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006338 * @ctxt: a schema parser context
6339 * @node: the element node of the attribute
6340 * @ownerDes: the designation of the parent element
6341 * @ownerItem: the schema object owner if existent
6342 * @ownerElem: the owner element node
6343 * @name: the name of the schema attribute node
6344 * @type: the built-in type to be validated against
6345 * @value: the resulting value if any
6346 *
6347 * Extracts and validates a value against the given built-in type.
6348 * This one is intended to be used internally for validation
6349 * of schema attribute values during parsing of the schema.
6350 *
6351 * Returns 0 if the value is valid, a positive error code
6352 * number otherwise and -1 in case of an internal or API error.
6353 */
6354static int
Daniel Veillarddee23482008-04-11 12:58:43 +00006355xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006356 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006357 xmlNodePtr ownerElem,
6358 const char *name,
6359 xmlSchemaTypePtr type,
6360 const xmlChar **value)
6361{
6362 xmlAttrPtr attr;
6363
6364 if ((ctxt == NULL) || (type == NULL)) {
6365 if (value != NULL)
6366 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006367 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006368 }
6369 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6370 if (value != NULL)
6371 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006372 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006373 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006374 "Internal error: xmlSchemaPValAttr, the given "
6375 "type '%s' is not a built-in type.\n",
6376 type->name, NULL);
6377 return (-1);
6378 }
6379 attr = xmlSchemaGetPropNode(ownerElem, name);
6380 if (attr == NULL) {
6381 if (value != NULL)
6382 *value = NULL;
6383 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006384 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006385 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006386 type, value));
6387}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006388
6389static int
6390xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006391 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006392 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006393 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006394 const xmlChar *namespaceName)
6395{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006396 /* TODO: Pointer comparison instead? */
6397 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006398 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006399 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006400 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006401 /*
6402 * Check if the referenced namespace was <import>ed.
6403 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006404 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006405 xmlSchemaSchemaRelationPtr rel;
6406
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006407 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006408 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006409 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006410 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006411 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006412 rel = rel->next;
6413 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006414 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006415 /*
6416 * No matching <import>ed namespace found.
6417 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006418 {
6419 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6420
6421 if (namespaceName == NULL)
6422 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6423 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6424 "References from this schema to components in no "
6425 "namespace are not allowed, since not indicated by an "
6426 "import statement", NULL, NULL);
6427 else
6428 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6429 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6430 "References from this schema to components in the "
6431 "namespace '%s' are not allowed, since not indicated by an "
6432 "import statement", namespaceName, NULL);
6433 }
6434 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006435}
6436
Daniel Veillardc0826a72004-08-10 14:17:33 +00006437/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006438 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006439 * @ctxt: a schema validation context
6440 * @schema: the schema being built
6441 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006442 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006443 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006444 * Parses attribute uses and attribute declarations and
Daniel Veillarddee23482008-04-11 12:58:43 +00006445 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006446 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006447static int
6448xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6449 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6450 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006451{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006452 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006453
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006454 while ((IS_SCHEMA((*child), "attribute")) ||
6455 (IS_SCHEMA((*child), "attributeGroup"))) {
6456 if (IS_SCHEMA((*child), "attribute")) {
6457 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6458 *list, parentType);
6459 } else {
6460 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6461 if ((item != NULL) && (hasRefs != NULL))
6462 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006463 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006464 if (item != NULL) {
6465 if (*list == NULL) {
6466 /* TODO: Customize grow factor. */
6467 *list = xmlSchemaItemListCreate();
6468 if (*list == NULL)
6469 return(-1);
6470 }
6471 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6472 return(-1);
6473 }
6474 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006475 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006476 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006477}
6478
6479/**
6480 * xmlSchemaParseAnnotation:
6481 * @ctxt: a schema validation context
6482 * @schema: the schema being built
6483 * @node: a subtree containing XML Schema informations
6484 *
6485 * parse a XML schema Attrribute declaration
6486 * *WARNING* this interface is highly subject to change
6487 *
William M. Bracke7091952004-05-11 15:09:58 +00006488 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006489 * 1 in case of success.
6490 */
6491static xmlSchemaAnnotPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006492xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006493{
6494 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006495 xmlNodePtr child = NULL;
6496 xmlAttrPtr attr;
6497 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006498
Daniel Veillardc0826a72004-08-10 14:17:33 +00006499 /*
6500 * INFO: S4S completed.
6501 */
6502 /*
6503 * id = ID
6504 * {any attributes with non-schema namespace . . .}>
6505 * Content: (appinfo | documentation)*
6506 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006507 if ((ctxt == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00006508 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006509 if (needed)
6510 ret = xmlSchemaNewAnnot(ctxt, node);
6511 else
6512 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006513 attr = node->properties;
6514 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006515 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006516 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006517 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006518 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006519
6520 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006521 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006522 }
6523 attr = attr->next;
6524 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006525 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006526 /*
6527 * And now for the children...
6528 */
6529 child = node->children;
6530 while (child != NULL) {
6531 if (IS_SCHEMA(child, "appinfo")) {
6532 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006533 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006534 * source = anyURI
6535 * {any attributes with non-schema namespace . . .}>
6536 * Content: ({any})*
6537 */
6538 attr = child->properties;
6539 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006540 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006541 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006542 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006543 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006544
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006545 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006546 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006547 }
6548 attr = attr->next;
6549 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006550 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006551 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006552 child = child->next;
6553 } else if (IS_SCHEMA(child, "documentation")) {
6554 /* TODO: make available the content of "documentation". */
6555 /*
6556 * source = anyURI
6557 * {any attributes with non-schema namespace . . .}>
6558 * Content: ({any})*
6559 */
6560 attr = child->properties;
6561 while (attr != NULL) {
6562 if (attr->ns == NULL) {
6563 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006564 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006565 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006566 }
6567 } else {
6568 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6569 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6570 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006571
6572 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006573 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006574 }
6575 }
6576 attr = attr->next;
6577 }
6578 /*
6579 * Attribute "xml:lang".
6580 */
6581 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6582 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006583 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006584 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006585 child = child->next;
6586 } else {
6587 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006588 xmlSchemaPContentErr(ctxt,
6589 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006590 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006591 barked = 1;
6592 child = child->next;
6593 }
6594 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006595
Daniel Veillard4255d502002-04-16 15:50:10 +00006596 return (ret);
6597}
6598
6599/**
6600 * xmlSchemaParseFacet:
6601 * @ctxt: a schema validation context
6602 * @schema: the schema being built
6603 * @node: a subtree containing XML Schema informations
6604 *
6605 * parse a XML schema Facet declaration
6606 * *WARNING* this interface is highly subject to change
6607 *
6608 * Returns the new type structure or NULL in case of error
6609 */
6610static xmlSchemaFacetPtr
6611xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006612 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006613{
6614 xmlSchemaFacetPtr facet;
6615 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006616 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006617
6618 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6619 return (NULL);
6620
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006621 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006622 if (facet == NULL) {
6623 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6624 return (NULL);
6625 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006627 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006628 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006629 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6630 "Facet %s has no value\n", node->name, NULL);
6631 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006632 return (NULL);
6633 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006634 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006635 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006636 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006637 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006638 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006639 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006640 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006641 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006642 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006643 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006644 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006645 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006646 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006647 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006648 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006649 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006650 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006651 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006652 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006653 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006654 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006655 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6656 } else if (IS_SCHEMA(node, "minLength")) {
6657 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6658 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006659 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6660 "Unknown facet type %s\n", node->name, NULL);
6661 xmlSchemaFreeFacet(facet);
6662 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006663 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006664 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006665 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006666 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6667 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6668 const xmlChar *fixed;
6669
6670 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6671 if (fixed != NULL) {
6672 if (xmlStrEqual(fixed, BAD_CAST "true"))
6673 facet->fixed = 1;
6674 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006675 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006676 child = node->children;
6677
6678 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006679 facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006680 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006681 }
6682 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006683 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6684 "Facet %s has unexpected child content\n",
6685 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006686 }
6687 return (facet);
6688}
6689
6690/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006691 * xmlSchemaParseWildcardNs:
6692 * @ctxt: a schema parser context
6693 * @wildc: the wildcard, already created
6694 * @node: a subtree containing XML Schema informations
6695 *
6696 * Parses the attribute "processContents" and "namespace"
6697 * of a xsd:anyAttribute and xsd:any.
6698 * *WARNING* this interface is highly subject to change
6699 *
6700 * Returns 0 if everything goes fine, a positive error code
6701 * if something is not valid and -1 if an internal error occurs.
6702 */
6703static int
6704xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006705 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006706 xmlSchemaWildcardPtr wildc,
6707 xmlNodePtr node)
6708{
6709 const xmlChar *pc, *ns, *dictnsItem;
6710 int ret = 0;
6711 xmlChar *nsItem;
6712 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6713 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006714
Daniel Veillardc0826a72004-08-10 14:17:33 +00006715 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6716 if ((pc == NULL)
6717 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6718 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6719 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6720 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6721 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6722 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6723 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006724 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006725 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006726 NULL, node,
6727 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006728 NULL, NULL, NULL);
6729 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006730 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006731 }
6732 /*
6733 * Build the namespace constraints.
6734 */
6735 attr = xmlSchemaGetPropNode(node, "namespace");
6736 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006737 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006738 wildc->any = 1;
6739 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6740 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006741 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006742 return (-1);
6743 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006744 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006745 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006746 const xmlChar *end, *cur;
6747
6748 cur = ns;
6749 do {
6750 while (IS_BLANK_CH(*cur))
6751 cur++;
6752 end = cur;
6753 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6754 end++;
6755 if (end == cur)
6756 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006757 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006758 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6759 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006760 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006761 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006762 NULL, (xmlNodePtr) attr,
6763 NULL,
6764 "((##any | ##other) | List of (xs:anyURI | "
6765 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006766 nsItem, NULL, NULL, NULL);
6767 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6768 } else {
6769 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006770 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006771 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6772 dictnsItem = NULL;
6773 } else {
6774 /*
6775 * Validate the item (anyURI).
6776 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006777 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006778 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6779 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6780 }
6781 /*
6782 * Avoid dublicate namespaces.
6783 */
6784 tmp = wildc->nsSet;
6785 while (tmp != NULL) {
6786 if (dictnsItem == tmp->value)
6787 break;
6788 tmp = tmp->next;
6789 }
6790 if (tmp == NULL) {
6791 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6792 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006793 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006794 return (-1);
6795 }
6796 tmp->value = dictnsItem;
6797 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006798 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006799 wildc->nsSet = tmp;
6800 else
6801 lastNs->next = tmp;
6802 lastNs = tmp;
6803 }
6804
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006805 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006806 xmlFree(nsItem);
6807 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006808 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006809 }
6810 return (ret);
6811}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006812
6813static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006814xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6815 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006816 xmlNodePtr node,
6817 int minOccurs,
6818 int maxOccurs) {
6819
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006820 if ((maxOccurs == 0) && ( minOccurs == 0))
6821 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006822 if (maxOccurs != UNBOUNDED) {
6823 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006824 * TODO: Maybe we should better not create the particle,
6825 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006826 * content model.
6827 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006828 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006829 * 3.9.6 Schema Component Constraint: Particle Correct
6830 *
6831 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006832 if (maxOccurs < 1) {
6833 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006834 * 2.2 {max occurs} must be greater than or equal to 1.
6835 */
6836 xmlSchemaPCustomAttrErr(ctxt,
6837 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006838 NULL, NULL,
6839 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006840 "The value must be greater than or equal to 1");
6841 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6842 } else if (minOccurs > maxOccurs) {
6843 /*
6844 * 2.1 {min occurs} must not be greater than {max occurs}.
6845 */
6846 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006847 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006848 NULL, NULL,
6849 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006850 "The value must not be greater than the value of 'maxOccurs'");
6851 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6852 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006853 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006854 return (0);
6855}
6856
Daniel Veillardc0826a72004-08-10 14:17:33 +00006857/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006858 * xmlSchemaParseAny:
6859 * @ctxt: a schema validation context
6860 * @schema: the schema being built
6861 * @node: a subtree containing XML Schema informations
6862 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006863 * Parsea a XML schema <any> element. A particle and wildcard
6864 * will be created (except if minOccurs==maxOccurs==0, in this case
6865 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006866 * *WARNING* this interface is highly subject to change
6867 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006868 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006869 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006870static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006871xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6872 xmlNodePtr node)
6873{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006874 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006875 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006876 xmlSchemaWildcardPtr wild;
6877 int min, max;
6878 xmlAttrPtr attr;
6879 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006880
6881 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6882 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006883 /*
6884 * Check for illegal attributes.
6885 */
6886 attr = node->properties;
6887 while (attr != NULL) {
6888 if (attr->ns == NULL) {
6889 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6890 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6891 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6892 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6893 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006894 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006895 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006896 }
6897 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006898 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006899 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006900 }
6901 attr = attr->next;
6902 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006903 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006904 /*
6905 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006906 */
6907 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6908 "(xs:nonNegativeInteger | unbounded)");
6909 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6910 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006911 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6912 /*
6913 * Create & parse the wildcard.
6914 */
6915 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6916 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006917 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006918 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006919 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006920 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006921 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006922 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006923 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006924 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006925 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006926 }
6927 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006928 xmlSchemaPContentErr(ctxt,
6929 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006930 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006931 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006932 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006933 /*
6934 * No component if minOccurs==maxOccurs==0.
6935 */
6936 if ((min == 0) && (max == 0)) {
6937 /* Don't free the wildcard, since it's already on the list. */
6938 return (NULL);
6939 }
6940 /*
6941 * Create the particle.
6942 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00006943 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006944 if (particle == NULL)
6945 return (NULL);
6946 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006947 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006948
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006949 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006950}
6951
6952/**
6953 * xmlSchemaParseNotation:
6954 * @ctxt: a schema validation context
6955 * @schema: the schema being built
6956 * @node: a subtree containing XML Schema informations
6957 *
6958 * parse a XML schema Notation declaration
6959 *
6960 * Returns the new structure or NULL in case of error
6961 */
6962static xmlSchemaNotationPtr
6963xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006964 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006965{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006966 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006967 xmlSchemaNotationPtr ret;
6968 xmlNodePtr child = NULL;
6969
6970 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6971 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006972 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006973 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006974 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6975 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006976 return (NULL);
6977 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006978 ret = xmlSchemaAddNotation(ctxt, schema, name,
6979 ctxt->targetNamespace, node);
6980 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006981 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006982 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006983
Daniel Veillard4255d502002-04-16 15:50:10 +00006984 child = node->children;
6985 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006986 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006987 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006988 }
6989 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006990 xmlSchemaPContentErr(ctxt,
6991 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006992 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006993 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006994 }
6995
6996 return (ret);
6997}
6998
6999/**
7000 * xmlSchemaParseAnyAttribute:
7001 * @ctxt: a schema validation context
7002 * @schema: the schema being built
7003 * @node: a subtree containing XML Schema informations
7004 *
7005 * parse a XML schema AnyAttrribute declaration
7006 * *WARNING* this interface is highly subject to change
7007 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007008 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00007009 */
Daniel Veillard3646d642004-06-02 19:19:14 +00007010static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007011xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
7012 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007013{
Daniel Veillard3646d642004-06-02 19:19:14 +00007014 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007015 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007016 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007017
7018 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7019 return (NULL);
7020
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007021 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
7022 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00007023 if (ret == NULL) {
7024 return (NULL);
7025 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007026 /*
7027 * Check for illegal attributes.
7028 */
7029 attr = node->properties;
7030 while (attr != NULL) {
7031 if (attr->ns == NULL) {
7032 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7033 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7034 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007035 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007036 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007037 }
7038 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007039 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007040 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007041 }
7042 attr = attr->next;
7043 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007044 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007045 /*
7046 * Parse the namespace list.
7047 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007048 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007049 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007050 /*
7051 * And now for the children...
7052 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007053 child = node->children;
7054 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007055 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007056 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007057 }
7058 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007059 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007060 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007061 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007062 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007063 }
7064
7065 return (ret);
7066}
7067
7068
7069/**
7070 * xmlSchemaParseAttribute:
7071 * @ctxt: a schema validation context
7072 * @schema: the schema being built
7073 * @node: a subtree containing XML Schema informations
7074 *
7075 * parse a XML schema Attrribute declaration
7076 * *WARNING* this interface is highly subject to change
7077 *
William M. Bracke7091952004-05-11 15:09:58 +00007078 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007079 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007080static xmlSchemaBasicItemPtr
7081xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7082 xmlSchemaPtr schema,
7083 xmlNodePtr node,
7084 xmlSchemaItemListPtr uses,
7085 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007086{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007087 const xmlChar *attrValue, *name = NULL, *ns = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007088 xmlSchemaAttributeUsePtr use = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007089 xmlNodePtr child = NULL;
7090 xmlAttrPtr attr;
7091 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7092 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7093 int nberrors, hasForm = 0, defValueType = 0;
7094
7095#define WXS_ATTR_DEF_VAL_DEFAULT 1
7096#define WXS_ATTR_DEF_VAL_FIXED 2
7097
7098 /*
7099 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7100 */
7101
7102 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7103 return (NULL);
7104 attr = xmlSchemaGetPropNode(node, "ref");
7105 if (attr != NULL) {
7106 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7107 NULL, attr, &tmpNs, &tmpName) != 0) {
7108 return (NULL);
7109 }
7110 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7111 return(NULL);
7112 isRef = 1;
7113 }
7114 nberrors = pctxt->nberrors;
7115 /*
7116 * Check for illegal attributes.
7117 */
7118 attr = node->properties;
7119 while (attr != NULL) {
7120 if (attr->ns == NULL) {
7121 if (isRef) {
7122 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7123 xmlSchemaPValAttrNodeID(pctxt, attr);
7124 goto attr_next;
7125 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7126 goto attr_next;
7127 }
7128 } else {
7129 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7130 goto attr_next;
7131 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7132 xmlSchemaPValAttrNodeID(pctxt, attr);
7133 goto attr_next;
7134 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7135 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7136 attr, &tmpNs, &tmpName);
7137 goto attr_next;
7138 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7139 /*
7140 * Evaluate the target namespace
7141 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007142 hasForm = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007143 attrValue = xmlSchemaGetNodeContent(pctxt,
7144 (xmlNodePtr) attr);
7145 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7146 ns = pctxt->targetNamespace;
7147 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7148 {
7149 xmlSchemaPSimpleTypeErr(pctxt,
7150 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7151 NULL, (xmlNodePtr) attr,
7152 NULL, "(qualified | unqualified)",
7153 attrValue, NULL, NULL, NULL);
7154 }
7155 goto attr_next;
7156 }
7157 }
7158 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7159
7160 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7161 /* TODO: Maybe we need to normalize the value beforehand. */
7162 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7163 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7164 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7165 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7166 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7167 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7168 else {
7169 xmlSchemaPSimpleTypeErr(pctxt,
7170 XML_SCHEMAP_INVALID_ATTR_USE,
7171 NULL, (xmlNodePtr) attr,
7172 NULL, "(optional | prohibited | required)",
7173 attrValue, NULL, NULL, NULL);
7174 }
7175 goto attr_next;
7176 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7177 /*
7178 * 3.2.3 : 1
7179 * default and fixed must not both be present.
7180 */
7181 if (defValue) {
7182 xmlSchemaPMutualExclAttrErr(pctxt,
7183 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7184 NULL, attr, "default", "fixed");
7185 } else {
7186 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7187 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7188 }
7189 goto attr_next;
7190 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7191 /*
7192 * 3.2.3 : 1
7193 * default and fixed must not both be present.
7194 */
7195 if (defValue) {
7196 xmlSchemaPMutualExclAttrErr(pctxt,
7197 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7198 NULL, attr, "default", "fixed");
7199 } else {
7200 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7201 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7202 }
7203 goto attr_next;
7204 }
7205 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7206 goto attr_next;
7207
7208 xmlSchemaPIllegalAttrErr(pctxt,
7209 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7210
7211attr_next:
7212 attr = attr->next;
Daniel Veillarddee23482008-04-11 12:58:43 +00007213 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007214 /*
7215 * 3.2.3 : 2
7216 * If default and use are both present, use must have
7217 * the actual value optional.
7218 */
7219 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7220 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7221 xmlSchemaPSimpleTypeErr(pctxt,
7222 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7223 NULL, node, NULL,
7224 "(optional | prohibited | required)", NULL,
7225 "The value of the attribute 'use' must be 'optional' "
7226 "if the attribute 'default' is present",
7227 NULL, NULL);
7228 }
7229 /*
7230 * We want correct attributes.
7231 */
7232 if (nberrors != pctxt->nberrors)
7233 return(NULL);
7234 if (! isRef) {
7235 xmlSchemaAttributePtr attrDecl;
7236
7237 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7238 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
Daniel Veillarddee23482008-04-11 12:58:43 +00007239 ns = pctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007240 /*
7241 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7242 * TODO: Move this to the component layer.
7243 */
7244 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7245 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7246 XML_SCHEMAP_NO_XSI,
7247 node, NULL,
7248 "The target namespace must not match '%s'",
7249 xmlSchemaInstanceNs, NULL);
7250 }
7251 attr = xmlSchemaGetPropNode(node, "name");
7252 if (attr == NULL) {
7253 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7254 NULL, node, "name", NULL);
7255 return (NULL);
7256 }
7257 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7258 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7259 return (NULL);
7260 }
7261 /*
7262 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7263 * TODO: Move this to the component layer.
7264 */
7265 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7266 xmlSchemaPSimpleTypeErr(pctxt,
7267 XML_SCHEMAP_NO_XMLNS,
7268 NULL, (xmlNodePtr) attr,
7269 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7270 "The value of the attribute must not match 'xmlns'",
7271 NULL, NULL);
7272 return (NULL);
7273 }
7274 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7275 goto check_children;
7276 /*
7277 * Create the attribute use component.
7278 */
7279 use = xmlSchemaAddAttributeUse(pctxt, node);
7280 if (use == NULL)
7281 return(NULL);
7282 use->occurs = occurs;
7283 /*
7284 * Create the attribute declaration.
7285 */
7286 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7287 if (attrDecl == NULL)
7288 return (NULL);
7289 if (tmpName != NULL) {
7290 attrDecl->typeName = tmpName;
7291 attrDecl->typeNs = tmpNs;
7292 }
7293 use->attrDecl = attrDecl;
7294 /*
7295 * Value constraint.
Daniel Veillarddee23482008-04-11 12:58:43 +00007296 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007297 if (defValue != NULL) {
7298 attrDecl->defValue = defValue;
7299 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7300 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7301 }
7302 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7303 xmlSchemaQNameRefPtr ref;
7304
7305 /*
7306 * Create the attribute use component.
7307 */
7308 use = xmlSchemaAddAttributeUse(pctxt, node);
7309 if (use == NULL)
7310 return(NULL);
7311 /*
7312 * We need to resolve the reference at later stage.
7313 */
7314 WXS_ADD_PENDING(pctxt, use);
7315 use->occurs = occurs;
7316 /*
7317 * Create a QName reference to the attribute declaration.
7318 */
7319 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7320 tmpName, tmpNs);
7321 if (ref == NULL)
7322 return(NULL);
7323 /*
7324 * Assign the reference. This will be substituted for the
7325 * referenced attribute declaration when the QName is resolved.
7326 */
7327 use->attrDecl = WXS_ATTR_CAST ref;
7328 /*
7329 * Value constraint.
7330 */
7331 if (defValue != NULL)
7332 use->defValue = defValue;
7333 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7334 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7335 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007337check_children:
7338 /*
7339 * And now for the children...
7340 */
7341 child = node->children;
7342 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007343 xmlSchemaAttributeUseProhibPtr prohib;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007344
7345 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007346 xmlSchemaParseAnnotation(pctxt, child, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007347 child = child->next;
7348 }
7349 if (child != NULL) {
7350 xmlSchemaPContentErr(pctxt,
7351 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7352 NULL, node, child, NULL,
7353 "(annotation?)");
7354 }
7355 /*
7356 * Check for pointlessness of attribute prohibitions.
7357 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007358 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007359 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7360 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7361 node, NULL,
7362 "Skipping attribute use prohibition, since it is "
7363 "pointless inside an <attributeGroup>",
7364 NULL, NULL, NULL);
7365 return(NULL);
7366 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7367 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7368 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7369 node, NULL,
7370 "Skipping attribute use prohibition, since it is "
7371 "pointless when extending a type",
7372 NULL, NULL, NULL);
7373 return(NULL);
7374 }
7375 if (! isRef) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007376 tmpName = name;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007377 tmpNs = ns;
7378 }
7379 /*
7380 * Check for duplicate attribute prohibitions.
7381 */
7382 if (uses) {
7383 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00007384
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007385 for (i = 0; i < uses->nbItems; i++) {
7386 use = uses->items[i];
7387 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7388 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7389 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7390 {
7391 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007392
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007393 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7394 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7395 node, NULL,
7396 "Skipping duplicate attribute use prohibition '%s'",
7397 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7398 NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007399 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007400 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007401 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007402 }
7403 }
7404 /*
7405 * Create the attribute prohibition helper component.
7406 */
7407 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7408 if (prohib == NULL)
7409 return(NULL);
7410 prohib->node = node;
7411 prohib->name = tmpName;
7412 prohib->targetNamespace = tmpNs;
7413 if (isRef) {
7414 /*
7415 * We need at least to resolve to the attribute declaration.
7416 */
7417 WXS_ADD_PENDING(pctxt, prohib);
7418 }
7419 return(WXS_BASIC_CAST prohib);
Daniel Veillarddee23482008-04-11 12:58:43 +00007420 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007421 if (IS_SCHEMA(child, "annotation")) {
7422 /*
7423 * TODO: Should this go into the attr decl?
7424 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007425 use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007426 child = child->next;
7427 }
7428 if (isRef) {
7429 if (child != NULL) {
7430 if (IS_SCHEMA(child, "simpleType"))
7431 /*
7432 * 3.2.3 : 3.2
7433 * If ref is present, then all of <simpleType>,
7434 * form and type must be absent.
7435 */
7436 xmlSchemaPContentErr(pctxt,
7437 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7438 NULL, node, child, NULL,
7439 "(annotation?)");
7440 else
7441 xmlSchemaPContentErr(pctxt,
7442 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7443 NULL, node, child, NULL,
7444 "(annotation?)");
7445 }
7446 } else {
7447 if (IS_SCHEMA(child, "simpleType")) {
7448 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7449 /*
7450 * 3.2.3 : 4
7451 * type and <simpleType> must not both be present.
7452 */
7453 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7454 NULL, node, child,
7455 "The attribute 'type' and the <simpleType> child "
7456 "are mutually exclusive", NULL);
7457 } else
7458 WXS_ATTRUSE_TYPEDEF(use) =
7459 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7460 child = child->next;
7461 }
7462 if (child != NULL)
7463 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7464 NULL, node, child, NULL,
7465 "(annotation?, simpleType?)");
7466 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007467 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007468 return (WXS_BASIC_CAST use);
7469}
7470
7471
7472static xmlSchemaAttributePtr
7473xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7474 xmlSchemaPtr schema,
7475 xmlNodePtr node)
7476{
7477 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007478 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007479 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007480 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007481
7482 /*
7483 * Note that the w3c spec assumes the schema to be validated with schema
7484 * for schemas beforehand.
7485 *
7486 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007487 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007488 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00007489 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007490 /*
7491 * 3.2.3 : 3.1
7492 * One of ref or name must be present, but not both
7493 */
7494 attr = xmlSchemaGetPropNode(node, "name");
7495 if (attr == NULL) {
7496 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7497 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007498 return (NULL);
7499 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007500 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7501 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7502 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007503 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007504 /*
7505 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7506 * TODO: Move this to the component layer.
7507 */
7508 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7509 xmlSchemaPSimpleTypeErr(pctxt,
7510 XML_SCHEMAP_NO_XMLNS,
7511 NULL, (xmlNodePtr) attr,
7512 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7513 "The value of the attribute must not match 'xmlns'",
7514 NULL, NULL);
7515 return (NULL);
7516 }
7517 /*
7518 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7519 * TODO: Move this to the component layer.
7520 * Or better leave it here and add it to the component layer
7521 * if we have a schema construction API.
7522 */
7523 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7524 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7525 XML_SCHEMAP_NO_XSI, node, NULL,
7526 "The target namespace must not match '%s'",
7527 xmlSchemaInstanceNs, NULL);
7528 }
7529
7530 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7531 pctxt->targetNamespace, node, 1);
7532 if (ret == NULL)
7533 return (NULL);
7534 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007535
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007536 /*
7537 * Check for illegal attributes.
7538 */
7539 attr = node->properties;
7540 while (attr != NULL) {
7541 if (attr->ns == NULL) {
7542 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7543 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7544 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7545 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7546 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7547 {
7548 xmlSchemaPIllegalAttrErr(pctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00007549 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007550 }
7551 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7552 xmlSchemaPIllegalAttrErr(pctxt,
7553 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7554 }
7555 attr = attr->next;
7556 }
7557 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7558 node, "type", &ret->typeNs, &ret->typeName);
Daniel Veillarddee23482008-04-11 12:58:43 +00007559
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007560 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007561 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007562 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007563 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007564 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007565 if (ret->defValue != NULL)
7566 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007567 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007568 * Attribute "default".
7569 */
7570 attr = xmlSchemaGetPropNode(node, "default");
7571 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007572 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007573 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007574 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007575 */
7576 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007577 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7578 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007579 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007580 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007581 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007582 /*
7583 * And now for the children...
7584 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007585 child = node->children;
7586 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007587 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007588 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007589 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007590 if (IS_SCHEMA(child, "simpleType")) {
7591 if (ret->typeName != NULL) {
7592 /*
7593 * 3.2.3 : 4
7594 * type and <simpleType> must not both be present.
7595 */
7596 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7597 NULL, node, child,
7598 "The attribute 'type' and the <simpleType> child "
7599 "are mutually exclusive", NULL);
7600 } else
7601 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7602 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007604 if (child != NULL)
7605 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7606 NULL, node, child, NULL,
7607 "(annotation?, simpleType?)");
7608
Daniel Veillard4255d502002-04-16 15:50:10 +00007609 return (ret);
7610}
7611
7612/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007613 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007614 * @ctxt: a schema validation context
7615 * @schema: the schema being built
7616 * @node: a subtree containing XML Schema informations
7617 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007618 * Parse an attribute group definition reference.
7619 * Note that a reference to an attribute group does not
7620 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007621 * *WARNING* this interface is highly subject to change
7622 *
7623 * Returns the attribute group or NULL in case of error.
7624 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007625static xmlSchemaQNameRefPtr
7626xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7627 xmlSchemaPtr schema,
7628 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007629{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007630 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007631 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007632 xmlAttrPtr attr;
7633 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007634
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007635 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007636 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007637
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007638 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007639 if (attr == NULL) {
7640 xmlSchemaPMissingAttrErr(pctxt,
7641 XML_SCHEMAP_S4S_ATTR_MISSING,
7642 NULL, node, "ref", NULL);
7643 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007644 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007645 xmlSchemaPValAttrNodeQName(pctxt, schema,
7646 NULL, attr, &refNs, &ref);
7647 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7648 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007649
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007650 /*
7651 * Check for illegal attributes.
7652 */
7653 attr = node->properties;
7654 while (attr != NULL) {
7655 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007656 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007657 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007658 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007659 xmlSchemaPIllegalAttrErr(pctxt,
7660 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007661 }
7662 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007663 xmlSchemaPIllegalAttrErr(pctxt,
7664 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007665 }
7666 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007667 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007668 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007669 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7670
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007671 /*
7672 * And now for the children...
7673 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007674 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007675 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007676 /*
7677 * TODO: We do not have a place to store the annotation, do we?
7678 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007679 xmlSchemaParseAnnotation(pctxt, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007680 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007681 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007682 if (child != NULL) {
7683 xmlSchemaPContentErr(pctxt,
7684 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7685 NULL, node, child, NULL,
7686 "(annotation?)");
7687 }
7688
7689 /*
7690 * Handle attribute group redefinitions.
7691 */
7692 if (pctxt->isRedefine && pctxt->redef &&
7693 (pctxt->redef->item->type ==
7694 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7695 (ref == pctxt->redef->refName) &&
7696 (refNs == pctxt->redef->refTargetNs))
7697 {
7698 /*
7699 * SPEC src-redefine:
7700 * (7.1) "If it has an <attributeGroup> among its contents
Rob Richardsc6947bb2008-06-29 15:04:41 +00007701 * the �actual value� of whose ref [attribute] is the same
7702 * as the �actual value� of its own name attribute plus
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007703 * target namespace, then it must have exactly one such group."
7704 */
7705 if (pctxt->redefCounter != 0) {
7706 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007707
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007708 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7709 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7710 "The redefining attribute group definition "
7711 "'%s' must not contain more than one "
7712 "reference to the redefined definition",
7713 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7714 FREE_AND_NULL(str);
7715 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007716 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007717 pctxt->redefCounter++;
7718 /*
7719 * URGENT TODO: How to ensure that the reference will not be
7720 * handled by the normal component resolution mechanism?
7721 */
7722 ret = xmlSchemaNewQNameRef(pctxt,
7723 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7724 if (ret == NULL)
7725 return(NULL);
7726 ret->node = node;
7727 pctxt->redef->reference = WXS_BASIC_CAST ret;
7728 } else {
7729 /*
7730 * Create a QName-reference helper component. We will substitute this
7731 * component for the attribute uses of the referenced attribute group
7732 * definition.
7733 */
7734 ret = xmlSchemaNewQNameRef(pctxt,
7735 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7736 if (ret == NULL)
7737 return(NULL);
7738 ret->node = node;
7739 /* Add to pending items, to be able to resolve the reference. */
7740 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00007741 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007742 return (ret);
7743}
7744
7745/**
7746 * xmlSchemaParseAttributeGroupDefinition:
7747 * @pctxt: a schema validation context
7748 * @schema: the schema being built
7749 * @node: a subtree containing XML Schema informations
7750 *
7751 * parse a XML schema Attribute Group declaration
7752 * *WARNING* this interface is highly subject to change
7753 *
7754 * Returns the attribute group definition or NULL in case of error.
7755 */
7756static xmlSchemaAttributeGroupPtr
7757xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7758 xmlSchemaPtr schema,
7759 xmlNodePtr node)
7760{
7761 const xmlChar *name;
7762 xmlSchemaAttributeGroupPtr ret;
7763 xmlNodePtr child = NULL;
7764 xmlAttrPtr attr;
7765 int hasRefs = 0;
7766
7767 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7768 return (NULL);
7769
7770 attr = xmlSchemaGetPropNode(node, "name");
7771 if (attr == NULL) {
7772 xmlSchemaPMissingAttrErr(pctxt,
7773 XML_SCHEMAP_S4S_ATTR_MISSING,
7774 NULL, node, "name", NULL);
7775 return (NULL);
7776 }
7777 /*
7778 * The name is crucial, exit if invalid.
7779 */
7780 if (xmlSchemaPValAttrNode(pctxt,
7781 NULL, attr,
7782 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7783 return (NULL);
7784 }
7785 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7786 name, pctxt->targetNamespace, node);
7787 if (ret == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00007788 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007789 /*
7790 * Check for illegal attributes.
7791 */
7792 attr = node->properties;
7793 while (attr != NULL) {
7794 if (attr->ns == NULL) {
7795 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7796 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7797 {
7798 xmlSchemaPIllegalAttrErr(pctxt,
7799 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7800 }
7801 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7802 xmlSchemaPIllegalAttrErr(pctxt,
7803 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7804 }
7805 attr = attr->next;
7806 }
7807 /* Attribute ID */
7808 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7809 /*
7810 * And now for the children...
7811 */
7812 child = node->children;
7813 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007814 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007815 child = child->next;
7816 }
7817 /*
7818 * Parse contained attribute decls/refs.
7819 */
7820 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7821 (xmlSchemaItemListPtr *) &(ret->attrUses),
7822 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7823 return(NULL);
7824 if (hasRefs)
7825 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7826 /*
7827 * Parse the attribute wildcard.
7828 */
7829 if (IS_SCHEMA(child, "anyAttribute")) {
7830 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7831 schema, child);
7832 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007833 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007834 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007835 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007836 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007837 NULL, node, child, NULL,
7838 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007839 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007840 return (ret);
7841}
7842
7843/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007844 * xmlSchemaPValAttrFormDefault:
7845 * @value: the value
7846 * @flags: the flags to be modified
7847 * @flagQualified: the specific flag for "qualified"
7848 *
7849 * Returns 0 if the value is valid, 1 otherwise.
7850 */
7851static int
7852xmlSchemaPValAttrFormDefault(const xmlChar *value,
7853 int *flags,
7854 int flagQualified)
7855{
7856 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7857 if ((*flags & flagQualified) == 0)
7858 *flags |= flagQualified;
7859 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007860 return (1);
7861
William M. Brack2f2a6632004-08-20 23:09:47 +00007862 return (0);
7863}
7864
7865/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007866 * xmlSchemaPValAttrBlockFinal:
7867 * @value: the value
7868 * @flags: the flags to be modified
7869 * @flagAll: the specific flag for "#all"
7870 * @flagExtension: the specific flag for "extension"
7871 * @flagRestriction: the specific flag for "restriction"
7872 * @flagSubstitution: the specific flag for "substitution"
7873 * @flagList: the specific flag for "list"
7874 * @flagUnion: the specific flag for "union"
7875 *
7876 * Validates the value of the attribute "final" and "block". The value
7877 * is converted into the specified flag values and returned in @flags.
7878 *
7879 * Returns 0 if the value is valid, 1 otherwise.
7880 */
7881
7882static int
7883xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007884 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007885 int flagAll,
7886 int flagExtension,
7887 int flagRestriction,
7888 int flagSubstitution,
7889 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007890 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007891{
7892 int ret = 0;
7893
7894 /*
7895 * TODO: This does not check for dublicate entries.
7896 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007897 if ((flags == NULL) || (value == NULL))
7898 return (-1);
7899 if (value[0] == 0)
7900 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 if (xmlStrEqual(value, BAD_CAST "#all")) {
7902 if (flagAll != -1)
7903 *flags |= flagAll;
7904 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007905 if (flagExtension != -1)
7906 *flags |= flagExtension;
7907 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007908 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007909 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007910 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007911 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007912 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007913 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007914 *flags |= flagUnion;
7915 }
7916 } else {
7917 const xmlChar *end, *cur = value;
7918 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007919
Daniel Veillardc0826a72004-08-10 14:17:33 +00007920 do {
7921 while (IS_BLANK_CH(*cur))
7922 cur++;
7923 end = cur;
7924 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7925 end++;
7926 if (end == cur)
7927 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007928 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007929 if (xmlStrEqual(item, BAD_CAST "extension")) {
7930 if (flagExtension != -1) {
7931 if ((*flags & flagExtension) == 0)
7932 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007933 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007934 ret = 1;
7935 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7936 if (flagRestriction != -1) {
7937 if ((*flags & flagRestriction) == 0)
7938 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007939 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007940 ret = 1;
7941 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7942 if (flagSubstitution != -1) {
7943 if ((*flags & flagSubstitution) == 0)
7944 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007945 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007946 ret = 1;
7947 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7948 if (flagList != -1) {
7949 if ((*flags & flagList) == 0)
7950 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007951 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007952 ret = 1;
7953 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7954 if (flagUnion != -1) {
7955 if ((*flags & flagUnion) == 0)
7956 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007957 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007958 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007959 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007960 ret = 1;
7961 if (item != NULL)
7962 xmlFree(item);
7963 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007964 } while ((ret == 0) && (*cur != 0));
7965 }
7966
Daniel Veillardc0826a72004-08-10 14:17:33 +00007967 return (ret);
7968}
7969
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007970static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007971xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007972 xmlSchemaIDCPtr idc,
7973 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007974 xmlAttrPtr attr,
7975 int isField)
7976{
7977 xmlNodePtr node;
7978
7979 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007980 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007981 * Schema Component Constraint: Selector Value OK
7982 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007983 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007984 * in [XPath].
7985 */
7986 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007987 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007988 XML_SCHEMAP_INTERNAL,
7989 "Internal error: xmlSchemaCheckCSelectorXPath, "
7990 "the selector is not specified.\n", NULL, NULL);
7991 return (-1);
7992 }
7993 if (attr == NULL)
7994 node = idc->node;
7995 else
7996 node = (xmlNodePtr) attr;
7997 if (selector->xpath == NULL) {
7998 xmlSchemaPCustomErr(ctxt,
7999 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008000 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008001 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008002 "The XPath expression of the selector is not valid", NULL);
8003 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
8004 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008005 const xmlChar **nsArray = NULL;
8006 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008007 /*
8008 * Compile the XPath expression.
8009 */
8010 /*
8011 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008012 * TODO: Call xmlPatterncompile with different options for selector/
8013 * field.
8014 */
Daniel Veillard14b56432006-03-09 18:41:40 +00008015 if (attr == NULL)
Daniel Veillardaac7c682006-03-10 13:40:16 +00008016 nsList = NULL;
Daniel Veillard14b56432006-03-09 18:41:40 +00008017 else
8018 nsList = xmlGetNsList(attr->doc, attr->parent);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008019 /*
8020 * Build an array of prefixes and namespaces.
8021 */
8022 if (nsList != NULL) {
8023 int i, count = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008024
8025 for (i = 0; nsList[i] != NULL; i++)
8026 count++;
8027
8028 nsArray = (const xmlChar **) xmlMalloc(
8029 (count * 2 + 1) * sizeof(const xmlChar *));
8030 if (nsArray == NULL) {
8031 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8032 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008033 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008034 return (-1);
8035 }
8036 for (i = 0; i < count; i++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008037 nsArray[2 * i] = nsList[i]->href;
8038 nsArray[2 * i + 1] = nsList[i]->prefix;
8039 }
8040 nsArray[count * 2] = NULL;
8041 xmlFree(nsList);
8042 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008043 /*
8044 * TODO: Differentiate between "selector" and "field".
8045 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008046 if (isField)
8047 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008048 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008049 else
8050 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008051 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008052 if (nsArray != NULL)
8053 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008054
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008055 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008056 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008057 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008058 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008059 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008060 "The XPath expression '%s' could not be "
8061 "compiled", selector->xpath);
8062 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008063 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008064 }
8065 return (0);
8066}
8067
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008068#define ADD_ANNOTATION(annot) \
8069 xmlSchemaAnnotPtr cur = item->annot; \
8070 if (item->annot == NULL) { \
8071 item->annot = annot; \
8072 return (annot); \
8073 } \
8074 cur = item->annot; \
8075 if (cur->next != NULL) { \
8076 cur = cur->next; \
8077 } \
8078 cur->next = annot;
8079
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008080/**
8081 * xmlSchemaAssignAnnotation:
8082 * @item: the schema component
8083 * @annot: the annotation
8084 *
8085 * Adds the annotation to the given schema component.
8086 *
8087 * Returns the given annotaion.
8088 */
8089static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008090xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8091 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008092{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008093 if ((annItem == NULL) || (annot == NULL))
8094 return (NULL);
8095 switch (annItem->type) {
8096 case XML_SCHEMA_TYPE_ELEMENT: {
8097 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8098 ADD_ANNOTATION(annot)
8099 }
8100 break;
8101 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8102 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8103 ADD_ANNOTATION(annot)
8104 }
8105 break;
8106 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8107 case XML_SCHEMA_TYPE_ANY: {
8108 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8109 ADD_ANNOTATION(annot)
8110 }
8111 break;
8112 case XML_SCHEMA_TYPE_PARTICLE:
8113 case XML_SCHEMA_TYPE_IDC_KEY:
8114 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008115 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008116 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8117 ADD_ANNOTATION(annot)
8118 }
8119 break;
8120 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8121 xmlSchemaAttributeGroupPtr item =
8122 (xmlSchemaAttributeGroupPtr) annItem;
8123 ADD_ANNOTATION(annot)
8124 }
8125 break;
8126 case XML_SCHEMA_TYPE_NOTATION: {
8127 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8128 ADD_ANNOTATION(annot)
8129 }
8130 break;
8131 case XML_SCHEMA_FACET_MININCLUSIVE:
8132 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8133 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8134 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8135 case XML_SCHEMA_FACET_TOTALDIGITS:
8136 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8137 case XML_SCHEMA_FACET_PATTERN:
8138 case XML_SCHEMA_FACET_ENUMERATION:
8139 case XML_SCHEMA_FACET_WHITESPACE:
8140 case XML_SCHEMA_FACET_LENGTH:
8141 case XML_SCHEMA_FACET_MAXLENGTH:
8142 case XML_SCHEMA_FACET_MINLENGTH: {
8143 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8144 ADD_ANNOTATION(annot)
8145 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008146 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008147 case XML_SCHEMA_TYPE_SIMPLE:
8148 case XML_SCHEMA_TYPE_COMPLEX: {
8149 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8150 ADD_ANNOTATION(annot)
8151 }
8152 break;
8153 case XML_SCHEMA_TYPE_GROUP: {
8154 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8155 ADD_ANNOTATION(annot)
8156 }
8157 break;
8158 case XML_SCHEMA_TYPE_SEQUENCE:
8159 case XML_SCHEMA_TYPE_CHOICE:
8160 case XML_SCHEMA_TYPE_ALL: {
8161 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8162 ADD_ANNOTATION(annot)
8163 }
8164 break;
8165 default:
8166 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008167 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008168 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008169 "Internal error: xmlSchemaAddAnnotation, "
8170 "The item is not a annotated schema component", NULL);
8171 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008172 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008173 return (annot);
8174}
8175
8176/**
8177 * xmlSchemaParseIDCSelectorAndField:
8178 * @ctxt: a schema validation context
8179 * @schema: the schema being built
8180 * @node: a subtree containing XML Schema informations
8181 *
8182 * Parses a XML Schema identity-contraint definition's
8183 * <selector> and <field> elements.
8184 *
8185 * Returns the parsed identity-constraint definition.
8186 */
8187static xmlSchemaIDCSelectPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00008188xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008189 xmlSchemaIDCPtr idc,
8190 xmlNodePtr node,
8191 int isField)
8192{
8193 xmlSchemaIDCSelectPtr item;
8194 xmlNodePtr child = NULL;
8195 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008197 /*
8198 * Check for illegal attributes.
8199 */
8200 attr = node->properties;
8201 while (attr != NULL) {
8202 if (attr->ns == NULL) {
8203 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8204 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008205 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008206 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008207 }
8208 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008209 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008210 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008211 }
8212 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008213 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008214 /*
8215 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008216 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008217 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8218 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008219 xmlSchemaPErrMemory(ctxt,
8220 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008221 NULL);
8222 return (NULL);
8223 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008224 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008225 /*
8226 * Attribute "xpath" (mandatory).
8227 */
8228 attr = xmlSchemaGetPropNode(node, "xpath");
8229 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008230 xmlSchemaPMissingAttrErr(ctxt,
8231 XML_SCHEMAP_S4S_ATTR_MISSING,
8232 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008233 "name", NULL);
8234 } else {
8235 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8236 /*
8237 * URGENT TODO: "field"s have an other syntax than "selector"s.
8238 */
8239
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008240 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8241 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008242 xmlSchemaPErr(ctxt,
8243 (xmlNodePtr) attr,
8244 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008245 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008246 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008247 NULL, NULL);
8248 }
8249
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008250 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008251 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008252 /*
8253 * And now for the children...
8254 */
8255 child = node->children;
8256 if (IS_SCHEMA(child, "annotation")) {
8257 /*
8258 * Add the annotation to the parent IDC.
8259 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008260 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008261 xmlSchemaParseAnnotation(ctxt, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008262 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008263 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008264 if (child != NULL) {
8265 xmlSchemaPContentErr(ctxt,
8266 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008267 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008268 NULL, "(annotation?)");
8269 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008270
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008271 return (item);
8272}
8273
8274/**
8275 * xmlSchemaParseIDC:
8276 * @ctxt: a schema validation context
8277 * @schema: the schema being built
8278 * @node: a subtree containing XML Schema informations
8279 *
8280 * Parses a XML Schema identity-contraint definition.
8281 *
8282 * Returns the parsed identity-constraint definition.
8283 */
8284static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008285xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008286 xmlSchemaPtr schema,
8287 xmlNodePtr node,
8288 xmlSchemaTypeType idcCategory,
8289 const xmlChar *targetNamespace)
8290{
8291 xmlSchemaIDCPtr item = NULL;
8292 xmlNodePtr child = NULL;
8293 xmlAttrPtr attr;
8294 const xmlChar *name = NULL;
8295 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008296
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008297 /*
8298 * Check for illegal attributes.
8299 */
8300 attr = node->properties;
8301 while (attr != NULL) {
8302 if (attr->ns == NULL) {
8303 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8304 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8305 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8306 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008307 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008308 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008309 }
8310 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008311 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008312 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008313 }
8314 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008315 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008316 /*
8317 * Attribute "name" (mandatory).
8318 */
8319 attr = xmlSchemaGetPropNode(node, "name");
8320 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008321 xmlSchemaPMissingAttrErr(ctxt,
8322 XML_SCHEMAP_S4S_ATTR_MISSING,
8323 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008324 "name", NULL);
8325 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008326 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008327 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008328 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8329 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008330 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008331 /* Create the component. */
8332 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8333 idcCategory, node);
8334 if (item == NULL)
8335 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008337 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008338 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8339 /*
8340 * Attribute "refer" (mandatory).
8341 */
8342 attr = xmlSchemaGetPropNode(node, "refer");
8343 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008344 xmlSchemaPMissingAttrErr(ctxt,
8345 XML_SCHEMAP_S4S_ATTR_MISSING,
8346 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008347 "refer", NULL);
8348 } else {
8349 /*
8350 * Create a reference item.
8351 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008352 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008353 NULL, NULL);
8354 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008355 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008356 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008357 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008358 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008359 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008360 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008361 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008362 }
8363 }
8364 /*
8365 * And now for the children...
8366 */
8367 child = node->children;
8368 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008369 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370 child = child->next;
8371 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008372 if (child == NULL) {
8373 xmlSchemaPContentErr(ctxt,
8374 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008375 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008376 "A child element is missing",
8377 "(annotation?, (selector, field+))");
8378 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008379 /*
8380 * Child element <selector>.
8381 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008382 if (IS_SCHEMA(child, "selector")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008383 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008384 item, child, 0);
8385 child = child->next;
8386 /*
8387 * Child elements <field>.
8388 */
8389 if (IS_SCHEMA(child, "field")) {
8390 do {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008391 field = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008392 item, child, 1);
8393 if (field != NULL) {
8394 field->index = item->nbFields;
8395 item->nbFields++;
8396 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008397 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008398 else
8399 item->fields = field;
8400 lastField = field;
8401 }
8402 child = child->next;
8403 } while (IS_SCHEMA(child, "field"));
8404 } else {
8405 xmlSchemaPContentErr(ctxt,
8406 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008407 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008408 NULL, "(annotation?, (selector, field+))");
8409 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008410 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008411 if (child != NULL) {
8412 xmlSchemaPContentErr(ctxt,
8413 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008414 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008415 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008416 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008417
8418 return (item);
8419}
8420
Daniel Veillardc0826a72004-08-10 14:17:33 +00008421/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008422 * xmlSchemaParseElement:
8423 * @ctxt: a schema validation context
8424 * @schema: the schema being built
8425 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008426 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008427 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008428 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008429 * *WARNING* this interface is highly subject to change
8430 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008431 * Returns the element declaration or a particle; NULL in case
8432 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008433 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008434static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008435xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008436 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008437{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008438 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008439 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008440 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008441 xmlNodePtr child = NULL;
8442 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008443 int min, max, isRef = 0;
8444 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008445
8446 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8447 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008448
Daniel Veillard4255d502002-04-16 15:50:10 +00008449 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008450 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008451
8452 if (isElemRef != NULL)
8453 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008454 /*
8455 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008456 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008457 * robust.
8458 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008459 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008460 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008461 if ((topLevel) || (attr == NULL)) {
8462 if (nameAttr == NULL) {
8463 xmlSchemaPMissingAttrErr(ctxt,
8464 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008465 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008466 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008467 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008468 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008469 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008470
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008471 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008472 child = node->children;
8473 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008474 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008475 child = child->next;
8476 }
8477 /*
8478 * Skip particle part if a global declaration.
8479 */
8480 if (topLevel)
8481 goto declaration_part;
8482 /*
8483 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008484 */
8485 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8486 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8487 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00008488 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008489 if (particle == NULL)
8490 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008491
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008492 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8493
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008494 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008495 const xmlChar *refNs = NULL, *ref = NULL;
8496 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008497 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008498 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008499 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008500 if (isElemRef != NULL)
8501 *isElemRef = 1;
8502
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008504 NULL, attr, &refNs, &ref);
8505 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008506 /*
8507 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008508 */
8509 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008510 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008511 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008512 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008513 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008514 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008515 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008516 attr = node->properties;
8517 while (attr != NULL) {
8518 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008519 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8520 xmlStrEqual(attr->name, BAD_CAST "name") ||
8521 xmlStrEqual(attr->name, BAD_CAST "id") ||
8522 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8523 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8524 {
8525 attr = attr->next;
8526 continue;
8527 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008528 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008529 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008530 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008531 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008532 "Only the attributes 'minOccurs', 'maxOccurs' and "
8533 "'id' are allowed in addition to 'ref'");
8534 break;
8535 }
8536 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8537 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008538 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008539 }
8540 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008541 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008542 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008543 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008544 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008545 if (child != NULL) {
8546 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008547 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008548 }
8549 if ((min == 0) && (max == 0))
8550 goto return_null;
8551 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008552 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008553 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008554 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008555 ref, refNs);
8556 if (refer == NULL)
8557 goto return_null;
8558 particle->children = (xmlSchemaTreeItemPtr) refer;
8559 particle->annot = annot;
8560 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008561 * Add the particle to pending components, since the reference
8562 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008563 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008564 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008565 return ((xmlSchemaBasicItemPtr) particle);
8566 }
8567 /*
8568 * The declaration part ===============================================
8569 */
8570declaration_part:
8571 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008572 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008573 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8574
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008575 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008576 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008577 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008578 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008579 * Evaluate the target namespace.
8580 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008581 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008582 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008583 } else {
8584 attr = xmlSchemaGetPropNode(node, "form");
8585 if (attr != NULL) {
8586 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8587 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008588 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008589 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008590 xmlSchemaPSimpleTypeErr(ctxt,
8591 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8592 NULL, (xmlNodePtr) attr,
8593 NULL, "(qualified | unqualified)",
8594 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008595 }
8596 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008597 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008599 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008600 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008601 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008602 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008603 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008604 * Check for illegal attributes.
8605 */
William M. Bracke7091952004-05-11 15:09:58 +00008606 attr = node->properties;
8607 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008608 if (attr->ns == NULL) {
8609 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8610 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008611 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008612 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008613 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008614 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008615 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8616 {
8617 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008618 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008619 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008620 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008621 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008622 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008623 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008624 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008625 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8626 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008627 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8628
8629 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008630 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008631 }
8632 }
8633 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008634
Daniel Veillardc0826a72004-08-10 14:17:33 +00008635 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008636 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008637 }
8638 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008639 }
William M. Bracke7091952004-05-11 15:09:58 +00008640 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008641 * Extract/validate attributes.
8642 */
8643 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008644 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 * Process top attributes of global element declarations here.
8646 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008647 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8648 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008649 xmlSchemaPValAttrQName(ctxt, schema,
8650 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008652 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008653 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008654 /*
8655 * Attribute "final".
8656 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008657 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008658 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008659 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8660 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8661 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8662 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008663 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008664 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8665 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008666 -1,
8667 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8668 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008669 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008670 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008671 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008672 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008673 attrValue, NULL, NULL, NULL);
8674 }
8675 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008676 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008677 /*
8678 * Attribute "block".
8679 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008680 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008681 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008682 /*
8683 * Apply default "block" values.
8684 */
8685 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8686 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8687 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8688 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8689 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8690 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008692 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8693 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008694 -1,
8695 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008696 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008697 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8698 xmlSchemaPSimpleTypeErr(ctxt,
8699 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008700 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008701 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008702 "restriction | substitution))", attrValue,
8703 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008704 }
8705 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008706 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008708
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008709 attr = xmlSchemaGetPropNode(node, "type");
8710 if (attr != NULL) {
8711 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008712 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008713 &(decl->namedTypeNs), &(decl->namedType));
8714 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008715 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008716 }
8717 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8718 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008719 if (attr != NULL) {
8720 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008721 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008722 /*
8723 * 3.3.3 : 1
8724 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008725 */
8726 xmlSchemaPMutualExclAttrErr(ctxt,
8727 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008728 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008729 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008730 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8731 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008732 }
William M. Bracke7091952004-05-11 15:09:58 +00008733 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008734 /*
8735 * And now for the children...
8736 */
William M. Bracke7091952004-05-11 15:09:58 +00008737 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008738 /*
8739 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008740 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008741 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008742 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008743 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008744 xmlSchemaPContentErr(ctxt,
8745 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008746 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008747 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008748 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008749 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008750 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008751 child = child->next;
8752 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008753 /*
8754 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008755 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008756 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008757 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008758 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008759 xmlSchemaPContentErr(ctxt,
8760 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008761 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008762 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008763 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008764 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008765 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008766 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008767 }
William M. Bracke7091952004-05-11 15:09:58 +00008768 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008769 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008770 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008771 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008772 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008773 } else if (IS_SCHEMA(child, "key")) {
8774 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008775 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008776 } else if (IS_SCHEMA(child, "keyref")) {
8777 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008778 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008779 }
8780 if (lastIDC != NULL)
8781 lastIDC->next = curIDC;
8782 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008783 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008784 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008785 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008786 }
8787 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008788 xmlSchemaPContentErr(ctxt,
8789 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008790 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008791 NULL, "(annotation?, ((simpleType | complexType)?, "
8792 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008793 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008794 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008795 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008796 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008797 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008798 * different layer.
8799 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008800 FREE_AND_NULL(des)
8801 if (topLevel)
8802 return ((xmlSchemaBasicItemPtr) decl);
8803 else {
8804 particle->children = (xmlSchemaTreeItemPtr) decl;
8805 return ((xmlSchemaBasicItemPtr) particle);
8806 }
8807
8808return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008809 FREE_AND_NULL(des);
8810 if (annot != NULL) {
8811 if (particle != NULL)
8812 particle->annot = NULL;
8813 if (decl != NULL)
8814 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008815 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008816 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008817 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008818}
8819
8820/**
8821 * xmlSchemaParseUnion:
8822 * @ctxt: a schema validation context
8823 * @schema: the schema being built
8824 * @node: a subtree containing XML Schema informations
8825 *
8826 * parse a XML schema Union definition
8827 * *WARNING* this interface is highly subject to change
8828 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008829 * Returns -1 in case of internal error, 0 in case of success and a positive
8830 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008831 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008832static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008833xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008834 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008835{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008836 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008837 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008838 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008839 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008840
8841 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008842 return (-1);
8843 /* Not a component, don't create it. */
8844 type = ctxt->ctxtType;
8845 /*
8846 * Mark the simple type as being of variety "union".
8847 */
8848 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008849 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008850 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Rob Richardsc6947bb2008-06-29 15:04:41 +00008851 * then the �simple ur-type definition�."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008852 */
8853 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008854 /*
8855 * Check for illegal attributes.
8856 */
8857 attr = node->properties;
8858 while (attr != NULL) {
8859 if (attr->ns == NULL) {
8860 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8861 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008862 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008863 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008864 }
8865 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008866 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008867 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008868 }
8869 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008870 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008871 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008872 /*
8873 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008874 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008875 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008876 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008877 if (attr != NULL) {
8878 const xmlChar *end;
8879 xmlChar *tmp;
8880 const xmlChar *localName, *nsName;
8881 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8882 xmlSchemaQNameRefPtr ref;
8883
8884 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008885 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008886 do {
8887 while (IS_BLANK_CH(*cur))
8888 cur++;
8889 end = cur;
8890 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8891 end++;
8892 if (end == cur)
8893 break;
8894 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008895 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008896 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008897 /*
8898 * Create the member type link.
8899 */
8900 link = (xmlSchemaTypeLinkPtr)
8901 xmlMalloc(sizeof(xmlSchemaTypeLink));
8902 if (link == NULL) {
8903 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8904 "allocating a type link", NULL);
8905 return (-1);
8906 }
8907 link->type = NULL;
8908 link->next = NULL;
8909 if (lastLink == NULL)
8910 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008911 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008912 lastLink->next = link;
8913 lastLink = link;
8914 /*
8915 * Create a reference item.
8916 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008917 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008918 localName, nsName);
8919 if (ref == NULL) {
8920 FREE_AND_NULL(tmp)
8921 return (-1);
8922 }
8923 /*
8924 * Assign the reference to the link, it will be resolved
8925 * later during fixup of the union simple type.
8926 */
8927 link->type = (xmlSchemaTypePtr) ref;
8928 }
8929 FREE_AND_NULL(tmp)
8930 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008931 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008932
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008933 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008934 /*
8935 * And now for the children...
8936 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008937 child = node->children;
8938 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008939 /*
8940 * Add the annotation to the simple type ancestor.
8941 */
8942 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008943 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008944 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008945 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008946 if (IS_SCHEMA(child, "simpleType")) {
8947 xmlSchemaTypePtr subtype, last = NULL;
8948
8949 /*
8950 * Anchor the member types in the "subtypes" field of the
8951 * simple type.
8952 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008953 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008954 subtype = (xmlSchemaTypePtr)
8955 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8956 if (subtype != NULL) {
8957 if (last == NULL) {
8958 type->subtypes = subtype;
8959 last = subtype;
8960 } else {
8961 last->next = subtype;
8962 last = subtype;
8963 }
8964 last->next = NULL;
8965 }
8966 child = child->next;
8967 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008968 }
8969 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008970 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008971 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008972 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008973 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008974 if ((attr == NULL) && (type->subtypes == NULL)) {
8975 /*
8976 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008977 * Either the memberTypes [attribute] of the <union> element must
8978 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008979 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008980 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008981 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008982 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008983 "Either the attribute 'memberTypes' or "
8984 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008985 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008986 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008987}
8988
8989/**
8990 * xmlSchemaParseList:
8991 * @ctxt: a schema validation context
8992 * @schema: the schema being built
8993 * @node: a subtree containing XML Schema informations
8994 *
8995 * parse a XML schema List definition
8996 * *WARNING* this interface is highly subject to change
8997 *
William M. Bracke7091952004-05-11 15:09:58 +00008998 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008999 * 1 in case of success.
9000 */
9001static xmlSchemaTypePtr
9002xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009003 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009004{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009005 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00009006 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009007 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00009008
9009 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9010 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009011 /* Not a component, don't create it. */
9012 type = ctxt->ctxtType;
9013 /*
9014 * Mark the type as being of variety "list".
9015 */
9016 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009018 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Rob Richardsc6947bb2008-06-29 15:04:41 +00009019 * then the �simple ur-type definition�."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009020 */
9021 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00009022 /*
9023 * Check for illegal attributes.
9024 */
9025 attr = node->properties;
9026 while (attr != NULL) {
9027 if (attr->ns == NULL) {
9028 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9029 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009030 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009031 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009032 }
9033 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009034 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009035 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009036 }
9037 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009038 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009039
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009040 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009041
William M. Brack2f2a6632004-08-20 23:09:47 +00009042 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009043 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9044 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009045 *
9046 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9047 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009048 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009049 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9050 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009051 /*
9052 * And now for the children...
9053 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009054 child = node->children;
9055 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009056 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009057 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009058 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009059 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009060 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009061 /*
9062 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009063 * Either the itemType [attribute] or the <simpleType> [child] of
9064 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009065 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009066 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009067 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009068 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009069 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009070 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009071 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009072 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009073 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009074 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009075 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009076 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009077 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009078 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009079 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009080 "Either the attribute 'itemType' or the <simpleType> child "
9081 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009082 }
9083 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009084 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009085 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009086 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009087 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009088 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009089 (type->subtypes == NULL) &&
9090 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009091 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009092 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009093 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009094 "Either the attribute 'itemType' or the <simpleType> child "
9095 "must be present", NULL);
9096 }
9097 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009098}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009099
Daniel Veillard4255d502002-04-16 15:50:10 +00009100/**
9101 * xmlSchemaParseSimpleType:
9102 * @ctxt: a schema validation context
9103 * @schema: the schema being built
9104 * @node: a subtree containing XML Schema informations
9105 *
9106 * parse a XML schema Simple Type definition
9107 * *WARNING* this interface is highly subject to change
9108 *
William M. Bracke7091952004-05-11 15:09:58 +00009109 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009110 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009111 */
9112static xmlSchemaTypePtr
9113xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009114 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009115{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009116 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009117 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009118 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009119 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009120 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009121
9122 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9123 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009124
Daniel Veillardc0826a72004-08-10 14:17:33 +00009125 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009126 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009127 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009128 xmlSchemaPMissingAttrErr(ctxt,
9129 XML_SCHEMAP_S4S_ATTR_MISSING,
9130 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009131 "name", NULL);
9132 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009133 } else {
9134 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009135 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009136 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9137 return (NULL);
9138 /*
9139 * Skip built-in types.
9140 */
9141 if (ctxt->isS4S) {
9142 xmlSchemaTypePtr biType;
9143
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009144 if (ctxt->isRedefine) {
9145 /*
9146 * REDEFINE: Disallow redefinition of built-in-types.
9147 * TODO: It seems that the spec does not say anything
9148 * about this case.
9149 */
9150 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009151 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009152 "Redefinition of built-in simple types is not "
9153 "supported", NULL);
9154 return(NULL);
9155 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009156 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9157 if (biType != NULL)
9158 return (biType);
9159 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009160 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009162 /*
9163 * TargetNamespace:
Rob Richardsc6947bb2008-06-29 15:04:41 +00009164 * SPEC "The �actual value� of the targetNamespace [attribute]
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009165 * of the <schema> ancestor element information item if present,
Rob Richardsc6947bb2008-06-29 15:04:41 +00009166 * otherwise �absent�.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009167 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009168 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009169#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009170 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009171#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009172 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009173 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009174 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009175#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009176 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009177 type = xmlSchemaAddType(ctxt, schema,
9178 XML_SCHEMA_TYPE_SIMPLE,
9179 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009180 ctxt->targetNamespace, node, 0);
9181#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009182 type = xmlSchemaAddType(ctxt, schema,
9183 XML_SCHEMA_TYPE_SIMPLE,
9184 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009185#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009186 if (type == NULL)
9187 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009188 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009189 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009190 /*
9191 * Check for illegal attributes.
9192 */
9193 attr = node->properties;
9194 while (attr != NULL) {
9195 if (attr->ns == NULL) {
9196 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009197 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00009198 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009199 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009200 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009201 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009202 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009203 }
9204 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009205 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009206 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009207 /*
9208 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009209 *
9210 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009211 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009212 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9213 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009214 if (type == NULL)
9215 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009216 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009217 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009218 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9219 /*
9220 * Check for illegal attributes.
9221 */
9222 attr = node->properties;
9223 while (attr != NULL) {
9224 if (attr->ns == NULL) {
9225 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9226 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009227 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009228 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009229 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009230 }
9231 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009232 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009233 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009234 }
9235 attr = attr->next;
9236 }
9237 /*
9238 * Attribute "final".
9239 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009240 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009241 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009242 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9243 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9244 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9245 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9246 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9247 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009248 } else {
9249 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009250 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9251 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009252 XML_SCHEMAS_TYPE_FINAL_LIST,
9253 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9254
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009255 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009256 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009257 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009258 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009259 attrValue, NULL, NULL, NULL);
9260 }
9261 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009262 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009263 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009264 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009265 /*
9266 * And now for the children...
9267 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009268 oldCtxtType = ctxt->ctxtType;
Daniel Veillarddee23482008-04-11 12:58:43 +00009269
Daniel Veillardc0826a72004-08-10 14:17:33 +00009270 ctxt->ctxtType = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00009271
Daniel Veillard4255d502002-04-16 15:50:10 +00009272 child = node->children;
9273 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009274 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009275 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009276 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009277 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009278 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009279 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009280 "(annotation?, (restriction | list | union))");
Daniel Veillarddee23482008-04-11 12:58:43 +00009281 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009282 xmlSchemaParseRestriction(ctxt, schema, child,
Daniel Veillarddee23482008-04-11 12:58:43 +00009283 XML_SCHEMA_TYPE_SIMPLE);
9284 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009285 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009286 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009287 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009288 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009289 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009290 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009291 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009292 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009293 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009294 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009295 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009296 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009297 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009298 /*
9299 * REDEFINE: SPEC src-redefine (5)
9300 * "Within the [children], each <simpleType> must have a
Rob Richardsc6947bb2008-06-29 15:04:41 +00009301 * <restriction> among its [children] ... the �actual value� of whose
9302 * base [attribute] must be the same as the �actual value� of its own
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009303 * name attribute plus target namespace;"
9304 */
9305 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9306 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009307 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009308 "<simpleType> must have a <restriction> child", NULL);
9309 }
Daniel Veillarddee23482008-04-11 12:58:43 +00009310
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009311 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009312 return (type);
9313}
9314
Daniel Veillard4255d502002-04-16 15:50:10 +00009315/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009316 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009317 * @ctxt: the parser context
9318 * @schema: the schema being built
9319 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009320 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009321 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009322 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009323 * We will return a particle component with a qname-component or
9324 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009325 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009326static xmlSchemaTreeItemPtr
9327xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9328 xmlSchemaPtr schema,
9329 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009330{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009331 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009332 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009333 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009334 const xmlChar *ref = NULL, *refNs = NULL;
9335 int min, max;
9336
9337 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009338 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009339
9340 attr = xmlSchemaGetPropNode(node, "ref");
9341 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009342 xmlSchemaPMissingAttrErr(ctxt,
9343 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009344 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009345 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009346 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009347 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009348 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009350 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009351 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009352 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009353 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009354 /*
9355 * Check for illegal attributes.
9356 */
9357 attr = node->properties;
9358 while (attr != NULL) {
9359 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009360 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009361 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9362 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9363 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009364 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009365 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009366 }
9367 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009368 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009369 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009370 }
9371 attr = attr->next;
9372 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009373 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00009374 item = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009375 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00009376 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009377 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009378 * Create a qname-reference and set as the term; it will be substituted
9379 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009380 */
9381 item->children = (xmlSchemaTreeItemPtr)
Daniel Veillarddee23482008-04-11 12:58:43 +00009382 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009383 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9384 /*
9385 * And now for the children...
9386 */
9387 child = node->children;
9388 /* TODO: Is annotation even allowed for a model group reference? */
9389 if (IS_SCHEMA(child, "annotation")) {
9390 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009391 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009392 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009393 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009394 child = child->next;
9395 }
9396 if (child != NULL) {
9397 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009398 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009399 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 "(annotation?)");
9401 }
9402 /*
9403 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9404 */
9405 if ((min == 0) && (max == 0))
9406 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009407
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009408 return ((xmlSchemaTreeItemPtr) item);
9409}
9410
9411/**
9412 * xmlSchemaParseModelGroupDefinition:
9413 * @ctxt: a schema validation context
9414 * @schema: the schema being built
9415 * @node: a subtree containing XML Schema informations
9416 *
9417 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009418 *
9419 * Note that the contraint src-redefine (6.2) can't be applied until
9420 * references have been resolved. So we will do this at the
9421 * component fixup level.
Daniel Veillarddee23482008-04-11 12:58:43 +00009422 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009423 * *WARNING* this interface is highly subject to change
9424 *
9425 * Returns -1 in case of error, 0 if the declaration is improper and
9426 * 1 in case of success.
9427 */
9428static xmlSchemaModelGroupDefPtr
9429xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9430 xmlSchemaPtr schema,
9431 xmlNodePtr node)
9432{
9433 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009434 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009435 xmlAttrPtr attr;
9436 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009437
9438 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009439 return (NULL);
9440
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009441 attr = xmlSchemaGetPropNode(node, "name");
9442 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009443 xmlSchemaPMissingAttrErr(ctxt,
9444 XML_SCHEMAP_S4S_ATTR_MISSING,
9445 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009446 "name", NULL);
9447 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009448 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009449 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9450 return (NULL);
9451 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009452 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9453 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009454 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009455 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009456 /*
9457 * Check for illegal attributes.
9458 */
9459 attr = node->properties;
9460 while (attr != NULL) {
9461 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009462 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009463 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009464 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009465 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009466 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009467 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009468 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009469 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009470 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009471 attr = attr->next;
9472 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009473 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009474 /*
9475 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009476 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009477 child = node->children;
9478 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009479 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009480 child = child->next;
9481 }
9482 if (IS_SCHEMA(child, "all")) {
9483 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9484 XML_SCHEMA_TYPE_ALL, 0);
9485 child = child->next;
9486 } else if (IS_SCHEMA(child, "choice")) {
9487 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9488 XML_SCHEMA_TYPE_CHOICE, 0);
9489 child = child->next;
9490 } else if (IS_SCHEMA(child, "sequence")) {
9491 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9492 XML_SCHEMA_TYPE_SEQUENCE, 0);
9493 child = child->next;
9494 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009495
Daniel Veillarddee23482008-04-11 12:58:43 +00009496
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009497
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009498 if (child != NULL) {
9499 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009500 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009501 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009502 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009503 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009504 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009505}
9506
9507/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009508 * xmlSchemaCleanupDoc:
9509 * @ctxt: a schema validation context
9510 * @node: the root of the document.
9511 *
9512 * removes unwanted nodes in a schemas document tree
9513 */
9514static void
9515xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9516{
9517 xmlNodePtr delete, cur;
9518
9519 if ((ctxt == NULL) || (root == NULL)) return;
9520
9521 /*
9522 * Remove all the blank text nodes
9523 */
9524 delete = NULL;
9525 cur = root;
9526 while (cur != NULL) {
9527 if (delete != NULL) {
9528 xmlUnlinkNode(delete);
9529 xmlFreeNode(delete);
9530 delete = NULL;
9531 }
9532 if (cur->type == XML_TEXT_NODE) {
9533 if (IS_BLANK_NODE(cur)) {
9534 if (xmlNodeGetSpacePreserve(cur) != 1) {
9535 delete = cur;
9536 }
9537 }
9538 } else if ((cur->type != XML_ELEMENT_NODE) &&
9539 (cur->type != XML_CDATA_SECTION_NODE)) {
9540 delete = cur;
9541 goto skip_children;
9542 }
9543
9544 /*
9545 * Skip to next node
9546 */
9547 if (cur->children != NULL) {
9548 if ((cur->children->type != XML_ENTITY_DECL) &&
9549 (cur->children->type != XML_ENTITY_REF_NODE) &&
9550 (cur->children->type != XML_ENTITY_NODE)) {
9551 cur = cur->children;
9552 continue;
9553 }
9554 }
9555 skip_children:
9556 if (cur->next != NULL) {
9557 cur = cur->next;
9558 continue;
9559 }
9560
9561 do {
9562 cur = cur->parent;
9563 if (cur == NULL)
9564 break;
9565 if (cur == root) {
9566 cur = NULL;
9567 break;
9568 }
9569 if (cur->next != NULL) {
9570 cur = cur->next;
9571 break;
9572 }
9573 } while (cur != NULL);
9574 }
9575 if (delete != NULL) {
9576 xmlUnlinkNode(delete);
9577 xmlFreeNode(delete);
9578 delete = NULL;
9579 }
9580}
9581
William M. Brack2f2a6632004-08-20 23:09:47 +00009582
William M. Brack2f2a6632004-08-20 23:09:47 +00009583static void
9584xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9585{
9586 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9587 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9588
9589 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9590 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9591
9592 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9593 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9594 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9595 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9596 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9597 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9598 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9599 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9600
9601 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9602 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9603 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9604 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9605 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9606 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9607}
9608
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009609static int
9610xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009611 xmlSchemaPtr schema,
9612 xmlNodePtr node)
9613{
9614 xmlAttrPtr attr;
9615 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009616 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009617
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009618 /*
9619 * Those flags should be moved to the parser context flags,
9620 * since they are not visible at the component level. I.e.
9621 * they are used if processing schema *documents* only.
9622 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009623 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009624 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009625
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009626 /*
9627 * Since the version is of type xs:token, we won't bother to
9628 * check it.
9629 */
9630 /* REMOVED:
9631 attr = xmlSchemaGetPropNode(node, "version");
9632 if (attr != NULL) {
9633 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
Daniel Veillarddee23482008-04-11 12:58:43 +00009634 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009635 HFAILURE;
9636 }
9637 */
9638 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9639 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009640 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009641 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9642 HFAILURE;
9643 if (res != 0) {
9644 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9645 goto exit;
9646 }
9647 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009648 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 if (attr != NULL) {
9650 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009651 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9652 XML_SCHEMAS_QUALIF_ELEM);
9653 HFAILURE;
9654 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009655 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009656 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009657 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009658 "(qualified | unqualified)", val, NULL, NULL, NULL);
9659 }
9660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009661 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009662 if (attr != NULL) {
9663 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009664 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9665 XML_SCHEMAS_QUALIF_ATTR);
9666 HFAILURE;
9667 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009668 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009669 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009670 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009671 "(qualified | unqualified)", val, NULL, NULL, NULL);
9672 }
9673 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009674 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009675 if (attr != NULL) {
9676 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009677 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009678 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9679 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9680 -1,
9681 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009682 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9683 HFAILURE;
9684 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009685 xmlSchemaPSimpleTypeErr(ctxt,
9686 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009687 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009688 "(#all | List of (extension | restriction | list | union))",
9689 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009690 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009691 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009692 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009693 if (attr != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00009694 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009695 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009696 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9697 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009698 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9699 HFAILURE;
9700 if (res != 0) {
9701 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009702 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009703 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009704 "(#all | List of (extension | restriction | substitution))",
9705 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009706 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009707 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009708
9709exit:
9710 if (oldErrs != ctxt->nberrors)
9711 res = ctxt->err;
9712 return(res);
9713exit_failure:
9714 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009715}
9716
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009717/**
9718 * xmlSchemaParseSchemaTopLevel:
9719 * @ctxt: a schema validation context
9720 * @schema: the schemas
9721 * @nodes: the list of top level nodes
9722 *
9723 * Returns the internal XML Schema structure built from the resource or
9724 * NULL in case of error
9725 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009726static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009727xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9728 xmlSchemaPtr schema, xmlNodePtr nodes)
9729{
9730 xmlNodePtr child;
9731 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009732 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009733
9734 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009735 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009736
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009737 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009738 child = nodes;
9739 while ((IS_SCHEMA(child, "include")) ||
9740 (IS_SCHEMA(child, "import")) ||
9741 (IS_SCHEMA(child, "redefine")) ||
9742 (IS_SCHEMA(child, "annotation"))) {
9743 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009744 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009745 if (schema->annot == NULL)
9746 schema->annot = annot;
9747 else
9748 xmlSchemaFreeAnnot(annot);
9749 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009750 tmpOldErrs = ctxt->nberrors;
9751 res = xmlSchemaParseImport(ctxt, schema, child);
9752 HFAILURE;
9753 HSTOP(ctxt);
9754 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009755 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009756 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009757 tmpOldErrs = ctxt->nberrors;
9758 res = xmlSchemaParseInclude(ctxt, schema, child);
9759 HFAILURE;
9760 HSTOP(ctxt);
9761 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009762 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009763 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009764 tmpOldErrs = ctxt->nberrors;
9765 res = xmlSchemaParseRedefine(ctxt, schema, child);
9766 HFAILURE;
9767 HSTOP(ctxt);
9768 if (tmpOldErrs != ctxt->nberrors)
9769 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009770 }
9771 child = child->next;
9772 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009773 /*
9774 * URGENT TODO: Change the functions to return int results.
9775 * We need especially to catch internal errors.
9776 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009777 while (child != NULL) {
9778 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009779 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009780 child = child->next;
9781 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009782 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009783 child = child->next;
9784 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009785 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009786 child = child->next;
9787 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009788 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009789 child = child->next;
9790 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009791 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009792 child = child->next;
9793 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009794 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009795 child = child->next;
9796 } else if (IS_SCHEMA(child, "notation")) {
9797 xmlSchemaParseNotation(ctxt, schema, child);
9798 child = child->next;
9799 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009800 xmlSchemaPContentErr(ctxt,
9801 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009802 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009803 NULL, "((include | import | redefine | annotation)*, "
9804 "(((simpleType | complexType | group | attributeGroup) "
9805 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009806 child = child->next;
9807 }
9808 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009809 /*
9810 * TODO: We should add all annotations.
9811 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009812 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009813 if (schema->annot == NULL)
9814 schema->annot = annot;
9815 else
9816 xmlSchemaFreeAnnot(annot);
9817 child = child->next;
9818 }
9819 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009820exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009821 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009822 if (oldErrs != ctxt->nberrors)
9823 res = ctxt->err;
9824 return(res);
9825exit_failure:
9826 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009827}
9828
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009829static xmlSchemaSchemaRelationPtr
9830xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009831{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009832 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009833
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009834 ret = (xmlSchemaSchemaRelationPtr)
9835 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009836 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009837 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009838 return(NULL);
9839 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009840 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009841 return(ret);
9842}
9843
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009844#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009845static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009846xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009847{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009848 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009849}
9850#endif
9851
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009852static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009853xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9854{
9855 xmlSchemaRedefPtr prev;
9856
9857 while (redef != NULL) {
9858 prev = redef;
9859 redef = redef->next;
9860 xmlFree(prev);
9861 }
9862}
9863
9864static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009865xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9866{
9867 /*
9868 * After the construction context has been freed, there will be
9869 * no schema graph available any more. Only the schema buckets
9870 * will stay alive, which are put into the "schemasImports" and
9871 * "includes" slots of the xmlSchema.
9872 */
9873 if (con->buckets != NULL)
9874 xmlSchemaItemListFree(con->buckets);
9875 if (con->pending != NULL)
9876 xmlSchemaItemListFree(con->pending);
9877 if (con->substGroups != NULL)
9878 xmlHashFree(con->substGroups,
9879 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009880 if (con->redefs != NULL)
9881 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009882 if (con->dict != NULL)
9883 xmlDictFree(con->dict);
9884 xmlFree(con);
9885}
9886
Daniel Veillarddee23482008-04-11 12:58:43 +00009887static xmlSchemaConstructionCtxtPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009888xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9889{
9890 xmlSchemaConstructionCtxtPtr ret;
9891
9892 ret = (xmlSchemaConstructionCtxtPtr)
9893 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9894 if (ret == NULL) {
9895 xmlSchemaPErrMemory(NULL,
9896 "allocating schema construction context", NULL);
9897 return (NULL);
9898 }
9899 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9900
9901 ret->buckets = xmlSchemaItemListCreate();
9902 if (ret->buckets == NULL) {
9903 xmlSchemaPErrMemory(NULL,
9904 "allocating list of schema buckets", NULL);
9905 xmlFree(ret);
9906 return (NULL);
9907 }
9908 ret->pending = xmlSchemaItemListCreate();
9909 if (ret->pending == NULL) {
9910 xmlSchemaPErrMemory(NULL,
9911 "allocating list of pending global components", NULL);
9912 xmlSchemaConstructionCtxtFree(ret);
9913 return (NULL);
9914 }
9915 ret->dict = dict;
9916 xmlDictReference(dict);
9917 return(ret);
9918}
9919
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009920static xmlSchemaParserCtxtPtr
9921xmlSchemaParserCtxtCreate(void)
9922{
9923 xmlSchemaParserCtxtPtr ret;
9924
9925 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9926 if (ret == NULL) {
9927 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9928 NULL);
9929 return (NULL);
9930 }
9931 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9932 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009933 ret->attrProhibs = xmlSchemaItemListCreate();
9934 if (ret->attrProhibs == NULL) {
9935 xmlFree(ret);
9936 return(NULL);
9937 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009938 return(ret);
9939}
9940
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009941/**
9942 * xmlSchemaNewParserCtxtUseDict:
9943 * @URL: the location of the schema
9944 * @dict: the dictionary to be used
9945 *
9946 * Create an XML Schemas parse context for that file/resource expected
9947 * to contain an XML Schemas file.
9948 *
9949 * Returns the parser context or NULL in case of error
9950 */
9951static xmlSchemaParserCtxtPtr
9952xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9953{
9954 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009955
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009956 ret = xmlSchemaParserCtxtCreate();
Daniel Veillarddee23482008-04-11 12:58:43 +00009957 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009958 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009959 ret->dict = dict;
Daniel Veillarddee23482008-04-11 12:58:43 +00009960 xmlDictReference(dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009961 if (URL != NULL)
9962 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009963 return (ret);
9964}
9965
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009966static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009967xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9968{
9969 if (vctxt->pctxt == NULL) {
9970 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009971 vctxt->pctxt =
9972 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009973 else
9974 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9975 if (vctxt->pctxt == NULL) {
9976 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9977 "failed to create a temp. parser context");
9978 return (-1);
9979 }
9980 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009981 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009982 vctxt->warning, vctxt->errCtxt);
9983 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9984 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009985 }
9986 return (0);
9987}
9988
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009989/**
9990 * xmlSchemaGetSchemaBucket:
9991 * @pctxt: the schema parser context
9992 * @schemaLocation: the URI of the schema document
9993 *
9994 * Returns a schema bucket if it was already parsed.
9995 *
9996 * Returns a schema bucket if it was already parsed from
9997 * @schemaLocation, NULL otherwise.
9998 */
9999static xmlSchemaBucketPtr
10000xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10001 const xmlChar *schemaLocation)
10002{
10003 xmlSchemaBucketPtr cur;
10004 xmlSchemaItemListPtr list;
10005
10006 list = pctxt->constructor->buckets;
10007 if (list->nbItems == 0)
10008 return(NULL);
10009 else {
10010 int i;
10011 for (i = 0; i < list->nbItems; i++) {
10012 cur = (xmlSchemaBucketPtr) list->items[i];
10013 /* Pointer comparison! */
10014 if (cur->schemaLocation == schemaLocation)
10015 return(cur);
10016 }
10017 }
10018 return(NULL);
10019}
10020
10021static xmlSchemaBucketPtr
10022xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10023 const xmlChar *schemaLocation,
10024 const xmlChar *targetNamespace)
10025{
10026 xmlSchemaBucketPtr cur;
10027 xmlSchemaItemListPtr list;
10028
10029 list = pctxt->constructor->buckets;
10030 if (list->nbItems == 0)
10031 return(NULL);
10032 else {
10033 int i;
10034 for (i = 0; i < list->nbItems; i++) {
10035 cur = (xmlSchemaBucketPtr) list->items[i];
10036 /* Pointer comparison! */
10037 if ((cur->origTargetNamespace == NULL) &&
10038 (cur->schemaLocation == schemaLocation) &&
10039 (cur->targetNamespace == targetNamespace))
10040 return(cur);
10041 }
10042 }
10043 return(NULL);
10044}
10045
10046
10047#define IS_BAD_SCHEMA_DOC(b) \
10048 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10049
10050static xmlSchemaBucketPtr
10051xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10052 const xmlChar *targetNamespace,
10053 int imported)
10054{
10055 xmlSchemaBucketPtr cur;
10056 xmlSchemaItemListPtr list;
10057
10058 list = pctxt->constructor->buckets;
10059 if (list->nbItems == 0)
10060 return(NULL);
10061 else {
10062 int i;
10063 for (i = 0; i < list->nbItems; i++) {
10064 cur = (xmlSchemaBucketPtr) list->items[i];
10065 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10066 (cur->origTargetNamespace == targetNamespace) &&
10067 ((imported && cur->imported) ||
10068 ((!imported) && (!cur->imported))))
10069 return(cur);
10070 }
10071 }
10072 return(NULL);
10073}
10074
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010075static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010076xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10077 xmlSchemaPtr schema,
10078 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010079{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010080 int oldFlags;
10081 xmlDocPtr oldDoc;
10082 xmlNodePtr node;
10083 int ret, oldErrs;
10084 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000010085
10086 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010087 * Save old values; reset the *main* schema.
10088 * URGENT TODO: This is not good; move the per-document information
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010089 * to the parser. Get rid of passing the main schema to the
10090 * parsing functions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010091 */
10092 oldFlags = schema->flags;
10093 oldDoc = schema->doc;
10094 if (schema->flags != 0)
10095 xmlSchemaClearSchemaDefaults(schema);
Daniel Veillarddee23482008-04-11 12:58:43 +000010096 schema->doc = bucket->doc;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010097 pctxt->schema = schema;
Daniel Veillarddee23482008-04-11 12:58:43 +000010098 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010099 * Keep the current target namespace on the parser *not* on the
10100 * main schema.
10101 */
10102 pctxt->targetNamespace = bucket->targetNamespace;
10103 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010104
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010105 if ((bucket->targetNamespace != NULL) &&
10106 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010107 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010108 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010109 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010110 pctxt->isS4S = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000010111 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010112 /* Mark it as parsed, even if parsing fails. */
10113 bucket->parsed++;
10114 /* Compile the schema doc. */
10115 node = xmlDocGetRootElement(bucket->doc);
10116 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10117 if (ret != 0)
10118 goto exit;
10119 /* An empty schema; just get out. */
10120 if (node->children == NULL)
10121 goto exit;
10122 oldErrs = pctxt->nberrors;
10123 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10124 if (ret != 0)
10125 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010126 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010127 * TODO: Not nice, but I'm not 100% sure we will get always an error
10128 * as a result of the obove functions; so better rely on pctxt->err
10129 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010130 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010131 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10132 ret = pctxt->err;
10133 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010134 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010135
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010136exit:
10137 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10138 /* Restore schema values. */
10139 schema->doc = oldDoc;
10140 schema->flags = oldFlags;
10141 return(ret);
10142}
10143
10144static int
10145xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10146 xmlSchemaPtr schema,
10147 xmlSchemaBucketPtr bucket)
10148{
10149 xmlSchemaParserCtxtPtr newpctxt;
10150 int res = 0;
10151
10152 if (bucket == NULL)
10153 return(0);
10154 if (bucket->parsed) {
10155 PERROR_INT("xmlSchemaParseNewDoc",
10156 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010157 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010158 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010159 if (bucket->doc == NULL) {
10160 PERROR_INT("xmlSchemaParseNewDoc",
10161 "parsing a schema doc, but there's no doc");
10162 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010163 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010164 if (pctxt->constructor == NULL) {
10165 PERROR_INT("xmlSchemaParseNewDoc",
10166 "no constructor");
10167 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000010168 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010169 /* Create and init the temporary parser context. */
10170 newpctxt = xmlSchemaNewParserCtxtUseDict(
10171 (const char *) bucket->schemaLocation, pctxt->dict);
10172 if (newpctxt == NULL)
10173 return(-1);
10174 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010175 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000010176 * TODO: Can we avoid that the parser knows about the main schema?
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010177 * It would be better if he knows about the current schema bucket
10178 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010179 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010180 newpctxt->schema = schema;
10181 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010182 pctxt->errCtxt);
10183 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10184 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010185 newpctxt->counter = pctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010186
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010187
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010188 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
Daniel Veillarddee23482008-04-11 12:58:43 +000010189
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010190 /* Channel back errors and cleanup the temporary parser context. */
10191 if (res != 0)
10192 pctxt->err = res;
10193 pctxt->nberrors += newpctxt->nberrors;
10194 pctxt->counter = newpctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010195 newpctxt->constructor = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010196 /* Free the parser context. */
10197 xmlSchemaFreeParserCtxt(newpctxt);
10198 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010199}
William M. Brack2f2a6632004-08-20 23:09:47 +000010200
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010201static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010202xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10203 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010204{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010205 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010206
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010207 if (cur == NULL) {
10208 bucket->relations = rel;
10209 return;
10210 }
10211 while (cur->next != NULL)
10212 cur = cur->next;
10213 cur->next = rel;
10214}
10215
10216
10217static const xmlChar *
10218xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10219 xmlNodePtr ctxtNode)
Daniel Veillarddee23482008-04-11 12:58:43 +000010220{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010221 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010222 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010223 */
Daniel Veillarddee23482008-04-11 12:58:43 +000010224 if (location != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010225 if (ctxtNode == NULL)
10226 return(location);
10227 else {
10228 xmlChar *base, *URI;
10229 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010230
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010231 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10232 if (base == NULL) {
10233 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10234 } else {
10235 URI = xmlBuildURI(location, base);
10236 xmlFree(base);
10237 }
10238 if (URI != NULL) {
10239 ret = xmlDictLookup(dict, URI, -1);
10240 xmlFree(URI);
10241 return(ret);
10242 }
10243 }
10244 }
10245 return(NULL);
10246}
Daniel Veillarddee23482008-04-11 12:58:43 +000010247
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010248
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010249
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010250/**
10251 * xmlSchemaAddSchemaDoc:
10252 * @pctxt: a schema validation context
10253 * @schema: the schema being built
10254 * @node: a subtree containing XML Schema informations
10255 *
10256 * Parse an included (and to-be-redefined) XML schema document.
10257 *
10258 * Returns 0 on success, a positive error code on errors and
10259 * -1 in case of an internal or API error.
10260 */
10261
10262static int
10263xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10264 int type, /* import or include or redefine */
10265 const xmlChar *schemaLocation,
10266 xmlDocPtr schemaDoc,
10267 const char *schemaBuffer,
10268 int schemaBufferLen,
10269 xmlNodePtr invokingNode,
Daniel Veillarddee23482008-04-11 12:58:43 +000010270 const xmlChar *sourceTargetNamespace,
10271 const xmlChar *importNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010272 xmlSchemaBucketPtr *bucket)
10273{
10274 const xmlChar *targetNamespace = NULL;
10275 xmlSchemaSchemaRelationPtr relation = NULL;
10276 xmlDocPtr doc = NULL;
10277 int res = 0, err = 0, located = 0, preserveDoc = 0;
10278 xmlSchemaBucketPtr bkt = NULL;
10279
10280 if (bucket != NULL)
10281 *bucket = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010282
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010283 switch (type) {
10284 case XML_SCHEMA_SCHEMA_IMPORT:
10285 case XML_SCHEMA_SCHEMA_MAIN:
10286 err = XML_SCHEMAP_SRC_IMPORT;
10287 break;
10288 case XML_SCHEMA_SCHEMA_INCLUDE:
10289 err = XML_SCHEMAP_SRC_INCLUDE;
10290 break;
10291 case XML_SCHEMA_SCHEMA_REDEFINE:
10292 err = XML_SCHEMAP_SRC_REDEFINE;
10293 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000010294 }
10295
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010296
10297 /* Special handling for the main schema:
10298 * skip the location and relation logic and just parse the doc.
10299 * We need just a bucket to be returned in this case.
Daniel Veillarddee23482008-04-11 12:58:43 +000010300 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010301 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
Daniel Veillarddee23482008-04-11 12:58:43 +000010302 goto doc_load;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010303
Daniel Veillarddee23482008-04-11 12:58:43 +000010304 /* Note that we expect the location to be an absulute URI. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010305 if (schemaLocation != NULL) {
10306 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10307 if ((bkt != NULL) &&
10308 (pctxt->constructor->bucket == bkt)) {
10309 /* Report self-imports/inclusions/redefinitions. */
Daniel Veillarddee23482008-04-11 12:58:43 +000010310
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010311 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10312 invokingNode, NULL,
10313 "The schema must not import/include/redefine itself",
10314 NULL, NULL);
10315 goto exit;
10316 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010317 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010318 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010319 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010320 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010321 relation = xmlSchemaSchemaRelationCreate();
10322 if (relation == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010323 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010324 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10325 relation);
10326 relation->type = type;
10327
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010328 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010329 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010330 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010331 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010332 relation->importNamespace = importNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010333 if (schemaLocation == NULL) {
10334 /*
10335 * No location; this is just an import of the namespace.
10336 * Note that we don't assign a bucket to the relation
10337 * in this case.
10338 */
10339 goto exit;
10340 }
10341 targetNamespace = importNamespace;
10342 }
10343
10344 /* Did we already fetch the doc? */
Daniel Veillarddee23482008-04-11 12:58:43 +000010345 if (bkt != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010346 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010347 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010348 /* We included/redefined and then try to import a schema. */
Daniel Veillard734e7662007-06-26 11:30:31 +000010349 if (schemaLocation == NULL)
10350 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010351 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10352 invokingNode, NULL,
10353 "The schema document '%s' cannot be imported, since "
10354 "it was already included or redefined",
10355 schemaLocation, NULL);
10356 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010357 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010358 /* We imported and then try to include/redefine a schema. */
Daniel Veillard734e7662007-06-26 11:30:31 +000010359 if (schemaLocation == NULL)
10360 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010361 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10362 invokingNode, NULL,
10363 "The schema document '%s' cannot be included or "
10364 "redefined, since it was already imported",
10365 schemaLocation, NULL);
10366 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000010367 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010368 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010369
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010370 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010371 /*
10372 * Given that the schemaLocation [attribute] is only a hint, it is open
10373 * to applications to ignore all but the first <import> for a given
Rob Richardsc6947bb2008-06-29 15:04:41 +000010374 * namespace, regardless of the �actual value� of schemaLocation, but
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010375 * such a strategy risks missing useful information when new
10376 * schemaLocations are offered.
10377 *
10378 * We will use the first <import> that comes with a location.
10379 * Further <import>s *with* a location, will result in an error.
10380 * TODO: Better would be to just report a warning here, but
10381 * we'll try it this way until someone complains.
10382 *
10383 * Schema Document Location Strategy:
10384 * 3 Based on the namespace name, identify an existing schema document,
10385 * either as a resource which is an XML document or a <schema> element
10386 * information item, in some local schema repository;
10387 * 5 Attempt to resolve the namespace name to locate such a resource.
10388 *
10389 * NOTE: (3) and (5) are not supported.
Daniel Veillarddee23482008-04-11 12:58:43 +000010390 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010391 if (bkt != NULL) {
10392 relation->bucket = bkt;
10393 goto exit;
10394 }
10395 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10396 importNamespace, 1);
10397
Daniel Veillarddee23482008-04-11 12:58:43 +000010398 if (bkt != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010399 relation->bucket = bkt;
10400 if (bkt->schemaLocation == NULL) {
10401 /* First given location of the schema; load the doc. */
10402 bkt->schemaLocation = schemaLocation;
10403 } else {
10404 if (!xmlStrEqual(schemaLocation,
10405 bkt->schemaLocation)) {
10406 /*
10407 * Additional location given; just skip it.
10408 * URGENT TODO: We should report a warning here.
10409 * res = XML_SCHEMAP_SRC_IMPORT;
10410 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010411 if (schemaLocation == NULL)
10412 schemaLocation = BAD_CAST "in_memory_buffer";
10413
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010414 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10415 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10416 invokingNode, NULL,
10417 "Skipping import of schema located at '%s' for the "
10418 "namespace '%s', since this namespace was already "
10419 "imported with the schema located at '%s'",
10420 schemaLocation, importNamespace, bkt->schemaLocation);
10421 }
10422 goto exit;
10423 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010424 }
10425 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010426 * No bucket + first location: load the doc and create a
10427 * bucket.
10428 */
10429 } else {
10430 /* <include> and <redefine> */
10431 if (bkt != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010432
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010433 if ((bkt->origTargetNamespace == NULL) &&
10434 (bkt->targetNamespace != sourceTargetNamespace)) {
10435 xmlSchemaBucketPtr chamel;
Daniel Veillarddee23482008-04-11 12:58:43 +000010436
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010437 /*
10438 * Chameleon include/redefine: skip loading only if it was
10439 * aleady build for the targetNamespace of the including
10440 * schema.
10441 */
10442 /*
10443 * URGENT TODO: If the schema is a chameleon-include then copy
10444 * the components into the including schema and modify the
10445 * targetNamespace of those components, do nothing otherwise.
10446 * NOTE: This is currently worked-around by compiling the
10447 * chameleon for every destinct including targetNamespace; thus
10448 * not performant at the moment.
10449 * TODO: Check when the namespace in wildcards for chameleons
10450 * needs to be converted: before we built wildcard intersections
10451 * or after.
10452 * Answer: after!
10453 */
10454 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10455 schemaLocation, sourceTargetNamespace);
10456 if (chamel != NULL) {
10457 /* A fitting chameleon was already parsed; NOP. */
10458 relation->bucket = chamel;
10459 goto exit;
10460 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010461 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010462 * We need to parse the chameleon again for a different
10463 * targetNamespace.
10464 * CHAMELEON TODO: Optimize this by only parsing the
10465 * chameleon once, and then copying the components to
10466 * the new targetNamespace.
10467 */
10468 bkt = NULL;
10469 } else {
10470 relation->bucket = bkt;
10471 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000010472 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010473 }
10474 }
10475 if ((bkt != NULL) && (bkt->doc != NULL)) {
10476 PERROR_INT("xmlSchemaAddSchemaDoc",
10477 "trying to load a schema doc, but a doc is already "
10478 "assigned to the schema bucket");
10479 goto exit_failure;
10480 }
10481
10482doc_load:
10483 /*
10484 * Load the document.
10485 */
10486 if (schemaDoc != NULL) {
10487 doc = schemaDoc;
10488 /* Don' free this one, since it was provided by the caller. */
10489 preserveDoc = 1;
10490 /* TODO: Does the context or the doc hold the location? */
10491 if (schemaDoc->URL != NULL)
10492 schemaLocation = xmlDictLookup(pctxt->dict,
10493 schemaDoc->URL, -1);
Daniel Veillard734e7662007-06-26 11:30:31 +000010494 else
10495 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010496 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10497 xmlParserCtxtPtr parserCtxt;
10498
10499 parserCtxt = xmlNewParserCtxt();
10500 if (parserCtxt == NULL) {
10501 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10502 "allocating a parser context", NULL);
10503 goto exit_failure;
10504 }
10505 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10506 /*
10507 * TODO: Do we have to burden the schema parser dict with all
10508 * the content of the schema doc?
10509 */
10510 xmlDictFree(parserCtxt->dict);
10511 parserCtxt->dict = pctxt->dict;
10512 xmlDictReference(parserCtxt->dict);
10513 }
10514 if (schemaLocation != NULL) {
10515 /* Parse from file. */
10516 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10517 NULL, SCHEMAS_PARSE_OPTIONS);
10518 } else if (schemaBuffer != NULL) {
10519 /* Parse from memory buffer. */
10520 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10521 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10522 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10523 if (doc != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010524 doc->URL = schemaLocation;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010525 }
10526 /*
10527 * For <import>:
10528 * 2.1 The referent is (a fragment of) a resource which is an
10529 * XML document (see clause 1.1), which in turn corresponds to
10530 * a <schema> element information item in a well-formed information
10531 * set, which in turn corresponds to a valid schema.
10532 * TODO: (2.1) fragments of XML documents are not supported.
10533 *
10534 * 2.2 The referent is a <schema> element information item in
10535 * a well-formed information set, which in turn corresponds
10536 * to a valid schema.
10537 * TODO: (2.2) is not supported.
10538 */
10539 if (doc == NULL) {
10540 xmlErrorPtr lerr;
10541 lerr = xmlGetLastError();
10542 /*
10543 * Check if this a parser error, or if the document could
10544 * just not be located.
10545 * TODO: Try to find specific error codes to react only on
10546 * localisation failures.
10547 */
10548 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10549 /*
10550 * We assume a parser error here.
10551 */
10552 located = 1;
10553 /* TODO: Error code ?? */
10554 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10555 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10556 invokingNode, NULL,
10557 "Failed to parse the XML resource '%s'",
Daniel Veillard734e7662007-06-26 11:30:31 +000010558 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010559 }
10560 }
10561 xmlFreeParserCtxt(parserCtxt);
10562 if ((doc == NULL) && located)
10563 goto exit_error;
10564 } else {
10565 xmlSchemaPErr(pctxt, NULL,
10566 XML_SCHEMAP_NOTHING_TO_PARSE,
10567 "No information for parsing was provided with the "
10568 "given schema parser context.\n",
10569 NULL, NULL);
10570 goto exit_failure;
10571 }
10572 /*
10573 * Preprocess the document.
10574 */
10575 if (doc != NULL) {
10576 xmlNodePtr docElem = NULL;
10577
Daniel Veillarddee23482008-04-11 12:58:43 +000010578 located = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010579 docElem = xmlDocGetRootElement(doc);
10580 if (docElem == NULL) {
10581 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
Daniel Veillarddee23482008-04-11 12:58:43 +000010582 invokingNode, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010583 "The document '%s' has no document element",
10584 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010585 goto exit_error;
10586 }
10587 /*
10588 * Remove all the blank text nodes.
10589 */
10590 xmlSchemaCleanupDoc(pctxt, docElem);
10591 /*
10592 * Check the schema's top level element.
10593 */
10594 if (!IS_SCHEMA(docElem, "schema")) {
10595 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10596 invokingNode, NULL,
10597 "The XML document '%s' is not a schema document",
10598 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010599 goto exit_error;
10600 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010601 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010602 * Note that we don't apply a type check for the
10603 * targetNamespace value here.
10604 */
10605 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10606 "targetNamespace");
10607 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010608
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010609/* after_doc_loading: */
10610 if ((bkt == NULL) && located) {
10611 /* Only create a bucket if the schema was located. */
10612 bkt = xmlSchemaBucketCreate(pctxt, type,
10613 targetNamespace);
10614 if (bkt == NULL)
10615 goto exit_failure;
10616 }
10617 if (bkt != NULL) {
10618 bkt->schemaLocation = schemaLocation;
10619 bkt->located = located;
10620 if (doc != NULL) {
10621 bkt->doc = doc;
10622 bkt->targetNamespace = targetNamespace;
10623 bkt->origTargetNamespace = targetNamespace;
10624 if (preserveDoc)
10625 bkt->preserveDoc = 1;
10626 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010627 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010628 bkt->imported++;
10629 /*
10630 * Add it to the graph of schemas.
10631 */
10632 if (relation != NULL)
10633 relation->bucket = bkt;
10634 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010635
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010636exit:
10637 /*
10638 * Return the bucket explicitely; this is needed for the
10639 * main schema.
10640 */
10641 if (bucket != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010642 *bucket = bkt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010643 return (0);
10644
10645exit_error:
10646 if ((doc != NULL) && (! preserveDoc)) {
10647 xmlFreeDoc(doc);
10648 if (bkt != NULL)
10649 bkt->doc = NULL;
10650 }
10651 return(pctxt->err);
10652
10653exit_failure:
10654 if ((doc != NULL) && (! preserveDoc)) {
10655 xmlFreeDoc(doc);
10656 if (bkt != NULL)
10657 bkt->doc = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010658 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010659 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010660}
10661
William M. Brack2f2a6632004-08-20 23:09:47 +000010662/**
10663 * xmlSchemaParseImport:
10664 * @ctxt: a schema validation context
10665 * @schema: the schema being built
10666 * @node: a subtree containing XML Schema informations
10667 *
10668 * parse a XML schema Import definition
10669 * *WARNING* this interface is highly subject to change
10670 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010671 * Returns 0 in case of success, a positive error code if
10672 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010673 */
10674static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010675xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010676 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010677{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010678 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010679 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10680 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010681 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010682 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010683 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010684
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010685 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010686 return (-1);
10687
10688 /*
10689 * Check for illegal attributes.
10690 */
10691 attr = node->properties;
10692 while (attr != NULL) {
10693 if (attr->ns == NULL) {
10694 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10695 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10696 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010697 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010698 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010699 }
10700 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010701 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010702 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010703 }
10704 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010705 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010706 /*
10707 * Extract and validate attributes.
10708 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010709 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010710 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010711 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010712 xmlSchemaPSimpleTypeErr(pctxt,
10713 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010714 NULL, node,
10715 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010716 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010717 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010718 }
10719
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010720 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010721 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010722 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010723 xmlSchemaPSimpleTypeErr(pctxt,
10724 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010725 NULL, node,
10726 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010727 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010728 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010729 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010730 /*
10731 * And now for the children...
10732 */
10733 child = node->children;
10734 if (IS_SCHEMA(child, "annotation")) {
10735 /*
10736 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010737 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010738 */
10739 child = child->next;
10740 }
10741 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010742 xmlSchemaPContentErr(pctxt,
10743 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010744 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010745 "(annotation?)");
10746 }
10747 /*
10748 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010749 *
10750 * Note that it is important to use the original @targetNamespace
10751 * (or none at all), to rule out imports of schemas _with_ a
10752 * @targetNamespace if the importing schema is a chameleon schema
10753 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010754 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010755 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010756 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010757 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000010758 * 1.1 If the namespace [attribute] is present, then its �actual value�
10759 * must not match the �actual value� of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010760 * targetNamespace [attribute].
10761 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010762 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010763 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010764 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010765 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010766 "The value of the attribute 'namespace' must not match "
10767 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010768 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010769 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010770 }
10771 } else {
10772 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010773 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010774 * <schema> must have a targetNamespace [attribute].
10775 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010776 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010777 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010778 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010779 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010780 "The attribute 'namespace' must be existent if "
10781 "the importing schema has no target namespace",
10782 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010783 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010784 }
10785 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010786 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010787 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010788 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010789 if (schemaLocation != NULL)
10790 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10791 schemaLocation, node);
10792 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010793 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010794 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010795
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010796 if (ret != 0)
10797 return(ret);
10798
10799 /*
10800 * For <import>: "It is *not* an error for the application
10801 * schema reference strategy to fail."
10802 * So just don't parse if no schema document was found.
10803 * Note that we will get no bucket if the schema could not be
10804 * located or if there was no schemaLocation.
10805 */
10806 if ((bucket == NULL) && (schemaLocation != NULL)) {
10807 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10808 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10809 node, NULL,
10810 "Failed to locate a schema at location '%s'. "
10811 "Skipping the import", schemaLocation, NULL, NULL);
10812 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010813
10814 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010815 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10816 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010817
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010818 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010819}
10820
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010821static int
10822xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10823 xmlSchemaPtr schema,
10824 xmlNodePtr node,
10825 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010826 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010827{
10828 xmlAttrPtr attr;
10829
10830 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10831 (schemaLocation == NULL))
10832 return (-1);
10833
10834 *schemaLocation = NULL;
10835 /*
10836 * Check for illegal attributes.
10837 * Applies for both <include> and <redefine>.
10838 */
10839 attr = node->properties;
10840 while (attr != NULL) {
10841 if (attr->ns == NULL) {
10842 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10843 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10844 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010845 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010846 }
10847 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10848 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010849 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010850 }
10851 attr = attr->next;
10852 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010853 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010854 /*
10855 * Preliminary step, extract the URI-Reference and make an URI
10856 * from the base.
10857 */
10858 /*
10859 * Attribute "schemaLocation" is mandatory.
10860 */
10861 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10862 if (attr != NULL) {
10863 xmlChar *base = NULL;
10864 xmlChar *uri = NULL;
10865
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010866 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010867 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10868 (const xmlChar **) schemaLocation) != 0)
10869 goto exit_error;
10870 base = xmlNodeGetBase(node->doc, node);
10871 if (base == NULL) {
10872 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10873 } else {
10874 uri = xmlBuildURI(*schemaLocation, base);
10875 xmlFree(base);
10876 }
10877 if (uri == NULL) {
10878 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10879 "could not build an URI from the schemaLocation")
10880 goto exit_failure;
10881 }
10882 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10883 xmlFree(uri);
10884 } else {
10885 xmlSchemaPMissingAttrErr(pctxt,
10886 XML_SCHEMAP_S4S_ATTR_MISSING,
10887 NULL, node, "schemaLocation", NULL);
10888 goto exit_error;
10889 }
10890 /*
10891 * Report self-inclusion and self-redefinition.
10892 */
10893 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010894 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010895 xmlSchemaPCustomErr(pctxt,
10896 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010897 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010898 "The schema document '%s' cannot redefine itself.",
Daniel Veillarddee23482008-04-11 12:58:43 +000010899 *schemaLocation);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010900 } else {
10901 xmlSchemaPCustomErr(pctxt,
10902 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010903 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010904 "The schema document '%s' cannot include itself.",
10905 *schemaLocation);
10906 }
10907 goto exit_error;
10908 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010909
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010910 return(0);
10911exit_error:
10912 return(pctxt->err);
10913exit_failure:
10914 return(-1);
10915}
10916
10917static int
10918xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10919 xmlSchemaPtr schema,
10920 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010921 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010922{
10923 xmlNodePtr child = NULL;
10924 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010925 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010926 int isChameleon = 0, wasChameleon = 0;
10927 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010928
10929 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10930 return (-1);
10931
10932 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010933 * Parse attributes. Note that the returned schemaLocation will
10934 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010935 */
10936 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010937 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010938 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010939 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010940 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010941 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010942 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010943 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10944 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010945 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010946 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010947 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010948 * If we get no schema bucket back, then this means that the schema
10949 * document could not be located or was broken XML or was not
10950 * a schema document.
Daniel Veillarddee23482008-04-11 12:58:43 +000010951 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010952 if ((bucket == NULL) || (bucket->doc == NULL)) {
10953 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10954 /*
10955 * WARNING for <include>:
10956 * We will raise an error if the schema cannot be located
10957 * for inclusions, since the that was the feedback from the
10958 * schema people. I.e. the following spec piece will *not* be
10959 * satisfied:
Rob Richardsc6947bb2008-06-29 15:04:41 +000010960 * SPEC src-include: "It is not an error for the �actual value� of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010961 * schemaLocation [attribute] to fail to resolve it all, in which
10962 * case no corresponding inclusion is performed.
10963 * So do we need a warning report here?"
10964 */
10965 res = XML_SCHEMAP_SRC_INCLUDE;
10966 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10967 node, NULL,
10968 "Failed to load the document '%s' for inclusion",
10969 schemaLocation, NULL);
10970 } else {
10971 /*
10972 * NOTE: This was changed to raise an error even if no redefinitions
10973 * are specified.
10974 *
10975 * SPEC src-redefine (1)
10976 * "If there are any element information items among the [children]
Rob Richardsc6947bb2008-06-29 15:04:41 +000010977 * other than <annotation> then the �actual value� of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010978 * schemaLocation [attribute] must successfully resolve."
10979 * TODO: Ask the WG if a the location has always to resolve
10980 * here as well!
10981 */
10982 res = XML_SCHEMAP_SRC_REDEFINE;
10983 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10984 node, NULL,
10985 "Failed to load the document '%s' for redefinition",
10986 schemaLocation, NULL);
10987 }
10988 } else {
10989 /*
10990 * Check targetNamespace sanity before parsing the new schema.
10991 * TODO: Note that we won't check further content if the
10992 * targetNamespace was bad.
Daniel Veillarddee23482008-04-11 12:58:43 +000010993 */
10994 if (bucket->origTargetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010995 /*
10996 * SPEC src-include (2.1)
Rob Richardsc6947bb2008-06-29 15:04:41 +000010997 * "SII has a targetNamespace [attribute], and its �actual
10998 * value� is identical to the �actual value� of the targetNamespace
10999 * [attribute] of SII� (which must have such an [attribute])."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011000 */
11001 if (pctxt->targetNamespace == NULL) {
11002 xmlSchemaCustomErr(ACTXT_CAST pctxt,
11003 XML_SCHEMAP_SRC_INCLUDE,
11004 node, NULL,
11005 "The target namespace of the included/redefined schema "
11006 "'%s' has to be absent, since the including/redefining "
11007 "schema has no target namespace",
11008 schemaLocation, NULL);
11009 goto exit_error;
11010 } else if (!xmlStrEqual(bucket->origTargetNamespace,
11011 pctxt->targetNamespace)) {
11012 /* TODO: Change error function. */
11013 xmlSchemaPCustomErrExt(pctxt,
11014 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011015 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011016 "The target namespace '%s' of the included/redefined "
11017 "schema '%s' differs from '%s' of the "
11018 "including/redefining schema",
11019 bucket->origTargetNamespace, schemaLocation,
11020 pctxt->targetNamespace);
11021 goto exit_error;
11022 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011023 } else if (pctxt->targetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011024 /*
11025 * Chameleons: the original target namespace will
11026 * differ from the resulting namespace.
11027 */
11028 isChameleon = 1;
11029 if (bucket->parsed &&
11030 (bucket->targetNamespace != pctxt->targetNamespace)) {
11031 /*
11032 * This is a sanity check, I dunno yet if this can happen.
11033 */
11034 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
11035 "trying to use an already parsed schema for a "
11036 "different targetNamespace");
11037 return(-1);
11038 }
11039 bucket->targetNamespace = pctxt->targetNamespace;
11040 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011041 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011042 /*
11043 * Parse the schema.
Daniel Veillarddee23482008-04-11 12:58:43 +000011044 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011045 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11046 if (isChameleon) {
11047 /* TODO: Get rid of this flag on the schema itself. */
11048 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11049 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11050 } else
11051 wasChameleon = 1;
11052 }
11053 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11054 /* Restore chameleon flag. */
11055 if (isChameleon && (!wasChameleon))
11056 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11057 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011058 /*
11059 * And now for the children...
11060 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011061 child = node->children;
11062 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011063 /*
11064 * Parse (simpleType | complexType | group | attributeGroup))*
11065 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011066 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011067 /*
11068 * How to proceed if the redefined schema was not located?
11069 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011070 pctxt->isRedefine = 1;
11071 while (IS_SCHEMA(child, "annotation") ||
11072 IS_SCHEMA(child, "simpleType") ||
11073 IS_SCHEMA(child, "complexType") ||
11074 IS_SCHEMA(child, "group") ||
11075 IS_SCHEMA(child, "attributeGroup")) {
11076 if (IS_SCHEMA(child, "annotation")) {
11077 /*
11078 * TODO: discard or not?
11079 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011080 } else if (IS_SCHEMA(child, "simpleType")) {
11081 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11082 } else if (IS_SCHEMA(child, "complexType")) {
11083 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011084 /* hasRedefinitions = 1; */
Daniel Veillarddee23482008-04-11 12:58:43 +000011085 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011086 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011087 xmlSchemaParseModelGroupDefinition(pctxt,
11088 schema, child);
11089 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011090 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011091 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11092 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011093 }
11094 child = child->next;
11095 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011096 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011097 pctxt->isRedefine = 0;
11098 } else {
11099 if (IS_SCHEMA(child, "annotation")) {
11100 /*
11101 * TODO: discard or not?
11102 */
11103 child = child->next;
11104 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011105 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011106 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011107 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011108 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11109 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011110 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011111 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11112 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011113 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011114 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011115 "(annotation?)");
Daniel Veillarddee23482008-04-11 12:58:43 +000011116 }
11117 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011118 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011119
11120exit_error:
11121 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011122}
11123
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011124static int
11125xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11126 xmlNodePtr node)
11127{
11128 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011129#ifndef ENABLE_REDEFINE
11130 TODO
11131 return(0);
11132#endif
11133 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11134 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011135 if (res != 0)
11136 return(res);
11137 return(0);
11138}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011139
11140static int
11141xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11142 xmlNodePtr node)
11143{
11144 int res;
11145
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011146 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11147 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011148 if (res != 0)
11149 return(res);
11150 return(0);
11151}
11152
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011153/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011154 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011155 * @ctxt: a schema validation context
11156 * @schema: the schema being built
11157 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011158 * @type: the "compositor" type
11159 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011160 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011161 * parse a XML schema Sequence definition.
11162 * Applies parts of:
11163 * Schema Representation Constraint:
11164 * Redefinition Constraints and Semantics (src-redefine)
11165 * (6.1), (6.1.1), (6.1.2)
11166 *
Daniel Veillarddee23482008-04-11 12:58:43 +000011167 * Schema Component Constraint:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011168 * All Group Limited (cos-all-limited) (2)
11169 * TODO: Actually this should go to component-level checks,
11170 * but is done here due to performance. Move it to an other layer
11171 * is schema construction via an API is implemented.
11172 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011173 * *WARNING* this interface is highly subject to change
11174 *
William M. Bracke7091952004-05-11 15:09:58 +000011175 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011176 * 1 in case of success.
11177 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011178static xmlSchemaTreeItemPtr
11179xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11180 xmlNodePtr node, xmlSchemaTypeType type,
11181 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011182{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011183 xmlSchemaModelGroupPtr item;
11184 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011185 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011186 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011187 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011188
11189 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011190 return (NULL);
11191 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011192 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011193 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011194 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011195 if (item == NULL)
11196 return (NULL);
11197
11198 if (withParticle) {
11199 if (type == XML_SCHEMA_TYPE_ALL) {
11200 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011201 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011202 } else {
11203 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011204 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11205 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11206 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011207 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011208 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11209 /*
11210 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011211 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000011212 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011213 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011214 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011215 particle->children = (xmlSchemaTreeItemPtr) item;
11216 /*
11217 * Check for illegal attributes.
11218 */
11219 attr = node->properties;
11220 while (attr != NULL) {
11221 if (attr->ns == NULL) {
11222 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11223 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11224 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011225 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011226 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011227 }
11228 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011229 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011230 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011231 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011232 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011233 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011234 } else {
11235 /*
11236 * Check for illegal attributes.
11237 */
11238 attr = node->properties;
11239 while (attr != NULL) {
11240 if (attr->ns == NULL) {
11241 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011242 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011243 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011244 }
11245 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011246 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011247 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011248 }
11249 attr = attr->next;
11250 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011251 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011252
William M. Brack2f2a6632004-08-20 23:09:47 +000011253 /*
11254 * Extract and validate attributes.
11255 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011256 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011257 /*
11258 * And now for the children...
11259 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011260 child = node->children;
11261 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011262 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011263 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011264 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011265 if (type == XML_SCHEMA_TYPE_ALL) {
11266 xmlSchemaParticlePtr part, last = NULL;
11267
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011268 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011269 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011270 schema, child, &isElemRef, 0);
11271 /*
11272 * SPEC cos-all-limited (2)
11273 * "The {max occurs} of all the particles in the {particles}
11274 * of the ('all') group must be 0 or 1.
11275 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011276 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011277 if (isElemRef)
11278 hasRefs++;
11279 if (part->minOccurs > 1) {
11280 xmlSchemaPCustomErr(ctxt,
11281 XML_SCHEMAP_COS_ALL_LIMITED,
11282 NULL, child,
11283 "Invalid value for minOccurs (must be 0 or 1)",
11284 NULL);
11285 /* Reset to 1. */
11286 part->minOccurs = 1;
11287 }
11288 if (part->maxOccurs > 1) {
11289 xmlSchemaPCustomErr(ctxt,
11290 XML_SCHEMAP_COS_ALL_LIMITED,
11291 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011292 "Invalid value for maxOccurs (must be 0 or 1)",
11293 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011294 /* Reset to 1. */
11295 part->maxOccurs = 1;
11296 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011297 if (last == NULL)
11298 item->children = (xmlSchemaTreeItemPtr) part;
11299 else
11300 last->next = (xmlSchemaTreeItemPtr) part;
11301 last = part;
11302 }
11303 child = child->next;
11304 }
11305 if (child != NULL) {
11306 xmlSchemaPContentErr(ctxt,
11307 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011308 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011309 "(annotation?, (annotation?, element*)");
11310 }
11311 } else {
11312 /* choice + sequence */
11313 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11314
11315 while ((IS_SCHEMA(child, "element")) ||
11316 (IS_SCHEMA(child, "group")) ||
11317 (IS_SCHEMA(child, "any")) ||
11318 (IS_SCHEMA(child, "choice")) ||
11319 (IS_SCHEMA(child, "sequence"))) {
11320
11321 if (IS_SCHEMA(child, "element")) {
11322 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011323 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11324 if (part && isElemRef)
11325 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011326 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011327 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011328 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011329 if (part != NULL)
11330 hasRefs++;
11331 /*
11332 * Handle redefinitions.
11333 */
11334 if (ctxt->isRedefine && ctxt->redef &&
11335 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11336 part && part->children)
11337 {
11338 if ((xmlSchemaGetQNameRefName(part->children) ==
11339 ctxt->redef->refName) &&
11340 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11341 ctxt->redef->refTargetNs))
11342 {
11343 /*
11344 * SPEC src-redefine:
11345 * (6.1) "If it has a <group> among its contents at
Rob Richardsc6947bb2008-06-29 15:04:41 +000011346 * some level the �actual value� of whose ref
11347 * [attribute] is the same as the �actual value� of
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011348 * its own name attribute plus target namespace, then
11349 * all of the following must be true:"
11350 * (6.1.1) "It must have exactly one such group."
11351 */
11352 if (ctxt->redefCounter != 0) {
11353 xmlChar *str = NULL;
11354
11355 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11356 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11357 "The redefining model group definition "
11358 "'%s' must not contain more than one "
11359 "reference to the redefined definition",
11360 xmlSchemaFormatQName(&str,
11361 ctxt->redef->refTargetNs,
11362 ctxt->redef->refName),
11363 NULL);
11364 FREE_AND_NULL(str)
11365 part = NULL;
11366 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11367 ((WXS_PARTICLE(part))->maxOccurs != 1))
11368 {
11369 xmlChar *str = NULL;
11370 /*
11371 * SPEC src-redefine:
Rob Richardsc6947bb2008-06-29 15:04:41 +000011372 * (6.1.2) "The �actual value� of both that
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011373 * group's minOccurs and maxOccurs [attribute]
Rob Richardsc6947bb2008-06-29 15:04:41 +000011374 * must be 1 (or �absent�).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011375 */
11376 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11377 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11378 "The redefining model group definition "
11379 "'%s' must not contain a reference to the "
11380 "redefined definition with a "
11381 "maxOccurs/minOccurs other than 1",
11382 xmlSchemaFormatQName(&str,
11383 ctxt->redef->refTargetNs,
11384 ctxt->redef->refName),
11385 NULL);
11386 FREE_AND_NULL(str)
11387 part = NULL;
11388 }
11389 ctxt->redef->reference = WXS_BASIC_CAST part;
11390 ctxt->redefCounter++;
Daniel Veillarddee23482008-04-11 12:58:43 +000011391 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011392 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011393 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011394 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011395 xmlSchemaParseAny(ctxt, schema, child);
11396 } else if (IS_SCHEMA(child, "choice")) {
11397 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11398 XML_SCHEMA_TYPE_CHOICE, 1);
11399 } else if (IS_SCHEMA(child, "sequence")) {
11400 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11401 XML_SCHEMA_TYPE_SEQUENCE, 1);
11402 }
11403 if (part != NULL) {
11404 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011405 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011406 else
11407 last->next = part;
11408 last = part;
11409 }
11410 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011411 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011412 if (child != NULL) {
11413 xmlSchemaPContentErr(ctxt,
11414 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011415 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011416 "(annotation?, (element | group | choice | sequence | any)*)");
11417 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011418 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011419 if ((max == 0) && (min == 0))
11420 return (NULL);
11421 if (hasRefs) {
11422 /*
11423 * We need to resolve references.
11424 */
11425 WXS_ADD_PENDING(ctxt, item);
11426 }
11427 if (withParticle)
Daniel Veillarddee23482008-04-11 12:58:43 +000011428 return ((xmlSchemaTreeItemPtr) particle);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011429 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011430 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011431}
11432
11433/**
11434 * xmlSchemaParseRestriction:
11435 * @ctxt: a schema validation context
11436 * @schema: the schema being built
11437 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011438 *
11439 * parse a XML schema Restriction definition
11440 * *WARNING* this interface is highly subject to change
11441 *
11442 * Returns the type definition or NULL in case of error
11443 */
11444static xmlSchemaTypePtr
11445xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011446 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011447{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011448 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011449 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011450 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011451
11452 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11453 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011454 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011455 type = ctxt->ctxtType;
11456 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011457
11458 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011459 * Check for illegal attributes.
11460 */
11461 attr = node->properties;
11462 while (attr != NULL) {
11463 if (attr->ns == NULL) {
11464 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11465 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011466 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011467 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011468 }
11469 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011470 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011471 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011472 }
11473 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011474 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011475 /*
11476 * Extract and validate attributes.
11477 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011478 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011479 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000011480 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011481 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011482 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011483 * Extract the base type. The "base" attribute is mandatory if inside
11484 * a complex type or if redefining.
11485 *
11486 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011487 * among its [children]), the simple type definition which is
Rob Richardsc6947bb2008-06-29 15:04:41 +000011488 * the {content type} of the type definition �resolved� to by
11489 * the �actual value� of the base [attribute]"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011490 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011491 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011492 &(type->baseNs), &(type->base)) == 0)
11493 {
11494 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11495 xmlSchemaPMissingAttrErr(ctxt,
11496 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011497 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011498 } else if ((ctxt->isRedefine) &&
11499 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11500 {
11501 if (type->base == NULL) {
11502 xmlSchemaPMissingAttrErr(ctxt,
11503 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011504 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011505 } else if ((! xmlStrEqual(type->base, type->name)) ||
11506 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11507 {
11508 xmlChar *str1 = NULL, *str2 = NULL;
11509 /*
11510 * REDEFINE: SPEC src-redefine (5)
11511 * "Within the [children], each <simpleType> must have a
Rob Richardsc6947bb2008-06-29 15:04:41 +000011512 * <restriction> among its [children] ... the �actual value� of
11513 * whose base [attribute] must be the same as the �actual value�
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011514 * of its own name attribute plus target namespace;"
11515 */
11516 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011517 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011518 "value '%s' of the 'base' attribute does not match the "
11519 "type's designation '%s'",
11520 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
Daniel Veillardfef73a52006-03-27 09:38:57 +000011521 xmlSchemaFormatQName(&str2, type->targetNamespace,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011522 type->name), NULL);
11523 FREE_AND_NULL(str1);
11524 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011525 /* Avoid confusion and erase the values. */
11526 type->base = NULL;
11527 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011528 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011529 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011530 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011531 /*
11532 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011533 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011534 child = node->children;
11535 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011536 /*
11537 * Add the annotation to the simple type ancestor.
11538 */
11539 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011540 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011541 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011542 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011543 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11544 /*
11545 * Corresponds to <simpleType><restriction><simpleType>.
11546 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011547 if (IS_SCHEMA(child, "simpleType")) {
11548 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011549 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011550 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011551 * Either the base [attribute] or the simpleType [child] of the
11552 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011553 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011554 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011555 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011556 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011557 "The attribute 'base' and the <simpleType> child are "
11558 "mutually exclusive", NULL);
11559 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011560 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011561 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011562 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011563 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011564 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011565 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011566 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011567 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011568 "Either the attribute 'base' or a <simpleType> child "
11569 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011570 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011571 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11572 /*
11573 * Corresponds to <complexType><complexContent><restriction>...
11574 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011575 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011576 * Model groups <all>, <choice> and <sequence>.
11577 */
11578 if (IS_SCHEMA(child, "all")) {
11579 type->subtypes = (xmlSchemaTypePtr)
11580 xmlSchemaParseModelGroup(ctxt, schema, child,
11581 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011582 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011583 } else if (IS_SCHEMA(child, "choice")) {
11584 type->subtypes = (xmlSchemaTypePtr)
11585 xmlSchemaParseModelGroup(ctxt,
11586 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11587 child = child->next;
11588 } else if (IS_SCHEMA(child, "sequence")) {
11589 type->subtypes = (xmlSchemaTypePtr)
11590 xmlSchemaParseModelGroup(ctxt, schema, child,
11591 XML_SCHEMA_TYPE_SEQUENCE, 1);
11592 child = child->next;
11593 /*
11594 * Model group reference <group>.
11595 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011596 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011597 type->subtypes = (xmlSchemaTypePtr)
11598 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011599 /*
11600 * Note that the reference will be resolved in
11601 * xmlSchemaResolveTypeReferences();
11602 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011603 child = child->next;
11604 }
11605 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011606 /*
11607 * Corresponds to <complexType><simpleContent><restriction>...
11608 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011609 * "1.1 the simple type definition corresponding to the <simpleType>
11610 * among the [children] of <restriction> if there is one;"
11611 */
11612 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011613 /*
11614 * We will store the to-be-restricted simple type in
11615 * type->contentTypeDef *temporarily*.
11616 */
11617 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011618 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011619 if ( type->contentTypeDef == NULL)
11620 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011621 child = child->next;
11622 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011623 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011624
11625 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011626 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011627 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011628 /*
11629 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011630 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011631 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011632
Daniel Veillard01fa6152004-06-29 17:04:39 +000011633 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011634 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011635 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011636 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011637 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11638 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011639 * *Single Facet Value*
11640 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011641 while ((IS_SCHEMA(child, "minInclusive")) ||
11642 (IS_SCHEMA(child, "minExclusive")) ||
11643 (IS_SCHEMA(child, "maxInclusive")) ||
11644 (IS_SCHEMA(child, "maxExclusive")) ||
11645 (IS_SCHEMA(child, "totalDigits")) ||
11646 (IS_SCHEMA(child, "fractionDigits")) ||
11647 (IS_SCHEMA(child, "pattern")) ||
11648 (IS_SCHEMA(child, "enumeration")) ||
11649 (IS_SCHEMA(child, "whiteSpace")) ||
11650 (IS_SCHEMA(child, "length")) ||
11651 (IS_SCHEMA(child, "maxLength")) ||
11652 (IS_SCHEMA(child, "minLength"))) {
11653 facet = xmlSchemaParseFacet(ctxt, schema, child);
11654 if (facet != NULL) {
11655 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011656 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011657 else
11658 lastfacet->next = facet;
11659 lastfacet = facet;
11660 lastfacet->next = NULL;
11661 }
11662 child = child->next;
11663 }
11664 /*
11665 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011666 */
11667 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011668 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11669
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011670 facet = type->facets;
11671 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011672 facetLink = (xmlSchemaFacetLinkPtr)
11673 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011674 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011675 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011676 xmlFree(facetLink);
11677 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011678 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011679 facetLink->facet = facet;
11680 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011681 if (lastFacetLink == NULL)
11682 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011683 else
11684 lastFacetLink->next = facetLink;
11685 lastFacetLink = facetLink;
11686 facet = facet->next;
11687 } while (facet != NULL);
11688 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011689 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011690 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11691 /*
11692 * Attribute uses/declarations.
11693 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011694 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11695 (xmlSchemaItemListPtr *) &(type->attrUses),
11696 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11697 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011698 /*
11699 * Attribute wildcard.
11700 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011701 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011702 type->attributeWildcard =
11703 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011704 child = child->next;
11705 }
11706 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011707 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011708 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11709 xmlSchemaPContentErr(ctxt,
11710 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011711 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011712 "annotation?, (group | all | choice | sequence)?, "
11713 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011714 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011715 xmlSchemaPContentErr(ctxt,
11716 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011717 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011718 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11719 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11720 "length | minLength | maxLength | enumeration | whiteSpace | "
11721 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11722 } else {
11723 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011724 xmlSchemaPContentErr(ctxt,
11725 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011726 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011727 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11728 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11729 "length | minLength | maxLength | enumeration | whiteSpace | "
11730 "pattern)*))");
11731 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011732 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011733 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011734}
11735
11736/**
11737 * xmlSchemaParseExtension:
11738 * @ctxt: a schema validation context
11739 * @schema: the schema being built
11740 * @node: a subtree containing XML Schema informations
11741 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011742 * Parses an <extension>, which is found inside a
11743 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011744 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011745 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011746 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011747 */
11748static xmlSchemaTypePtr
11749xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011750 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011751{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011752 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011753 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011754 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011755
11756 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11757 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011758 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011759 type = ctxt->ctxtType;
11760 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011761
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011762 /*
11763 * Check for illegal attributes.
11764 */
11765 attr = node->properties;
11766 while (attr != NULL) {
11767 if (attr->ns == NULL) {
11768 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11769 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011770 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011771 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011772 }
11773 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011774 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011775 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011776 }
11777 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011778 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011779
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011780 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011781
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011782 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011783 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011784 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011785 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11786 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011787 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011788 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011789 XML_SCHEMAP_S4S_ATTR_MISSING,
11790 NULL, node, "base", NULL);
11791 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011792 /*
11793 * And now for the children...
11794 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011795 child = node->children;
11796 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011797 /*
11798 * Add the annotation to the type ancestor.
11799 */
11800 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011801 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011802 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011803 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011804 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11805 /*
11806 * Corresponds to <complexType><complexContent><extension>... and:
11807 *
11808 * Model groups <all>, <choice>, <sequence> and <group>.
11809 */
11810 if (IS_SCHEMA(child, "all")) {
11811 type->subtypes = (xmlSchemaTypePtr)
11812 xmlSchemaParseModelGroup(ctxt, schema,
11813 child, XML_SCHEMA_TYPE_ALL, 1);
11814 child = child->next;
11815 } else if (IS_SCHEMA(child, "choice")) {
11816 type->subtypes = (xmlSchemaTypePtr)
11817 xmlSchemaParseModelGroup(ctxt, schema,
11818 child, XML_SCHEMA_TYPE_CHOICE, 1);
11819 child = child->next;
11820 } else if (IS_SCHEMA(child, "sequence")) {
11821 type->subtypes = (xmlSchemaTypePtr)
11822 xmlSchemaParseModelGroup(ctxt, schema,
11823 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11824 child = child->next;
11825 } else if (IS_SCHEMA(child, "group")) {
11826 type->subtypes = (xmlSchemaTypePtr)
11827 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011828 /*
11829 * Note that the reference will be resolved in
11830 * xmlSchemaResolveTypeReferences();
11831 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011832 child = child->next;
11833 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011834 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011835 if (child != NULL) {
11836 /*
11837 * Attribute uses/declarations.
11838 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011839 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11840 (xmlSchemaItemListPtr *) &(type->attrUses),
11841 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11842 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011843 /*
11844 * Attribute wildcard.
11845 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011846 if (IS_SCHEMA(child, "anyAttribute")) {
11847 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011848 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11849 child = child->next;
11850 }
11851 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011852 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011853 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11854 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011855 xmlSchemaPContentErr(ctxt,
11856 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011857 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011858 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011859 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011860 } else {
11861 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011862 xmlSchemaPContentErr(ctxt,
11863 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011864 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011865 "(annotation?, ((attribute | attributeGroup)*, "
11866 "anyAttribute?))");
11867 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011868 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011869 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011870}
11871
11872/**
11873 * xmlSchemaParseSimpleContent:
11874 * @ctxt: a schema validation context
11875 * @schema: the schema being built
11876 * @node: a subtree containing XML Schema informations
11877 *
11878 * parse a XML schema SimpleContent definition
11879 * *WARNING* this interface is highly subject to change
11880 *
11881 * Returns the type definition or NULL in case of error
11882 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011883static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011884xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011885 xmlSchemaPtr schema, xmlNodePtr node,
11886 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011887{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011888 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011889 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011890 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011891
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011892 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11893 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011894 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011895 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011896 /* Not a component, don't create it. */
11897 type = ctxt->ctxtType;
11898 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11899 /*
11900 * Check for illegal attributes.
11901 */
11902 attr = node->properties;
11903 while (attr != NULL) {
11904 if (attr->ns == NULL) {
11905 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011906 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011907 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011908 }
11909 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011910 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011911 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011912 }
11913 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011914 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011915
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011916 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011917
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011918 /*
11919 * And now for the children...
11920 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011921 child = node->children;
11922 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011923 /*
11924 * Add the annotation to the complex type ancestor.
11925 */
11926 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011927 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011928 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011929 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011930 if (child == NULL) {
11931 xmlSchemaPContentErr(ctxt,
11932 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011933 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011934 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011935 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011936 if (child == NULL) {
11937 xmlSchemaPContentErr(ctxt,
11938 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011939 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011940 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011941 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011942 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011943 xmlSchemaParseRestriction(ctxt, schema, child,
11944 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011945 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011946 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011947 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011948 xmlSchemaParseExtension(ctxt, schema, child,
11949 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011950 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011951 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011952 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011953 if (child != NULL) {
11954 xmlSchemaPContentErr(ctxt,
11955 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011956 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011957 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011958 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011959 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011960}
11961
11962/**
11963 * xmlSchemaParseComplexContent:
11964 * @ctxt: a schema validation context
11965 * @schema: the schema being built
11966 * @node: a subtree containing XML Schema informations
11967 *
11968 * parse a XML schema ComplexContent definition
11969 * *WARNING* this interface is highly subject to change
11970 *
11971 * Returns the type definition or NULL in case of error
11972 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011973static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011974xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011975 xmlSchemaPtr schema, xmlNodePtr node,
11976 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011977{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011978 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011979 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011980 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011981
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011982 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11983 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011984 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011985 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011986 /* Not a component, don't create it. */
11987 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011988 /*
11989 * Check for illegal attributes.
11990 */
11991 attr = node->properties;
11992 while (attr != NULL) {
11993 if (attr->ns == NULL) {
11994 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011995 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011996 {
11997 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011998 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011999 }
12000 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
12001 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012002 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012003 }
12004 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012005 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012006
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012007 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012008
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012009 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012010 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012011 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012012 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012013 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
12014 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012015 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012016 child = node->children;
12017 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012018 /*
12019 * Add the annotation to the complex type ancestor.
12020 */
12021 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012022 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012023 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012024 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012025 if (child == NULL) {
12026 xmlSchemaPContentErr(ctxt,
12027 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012028 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012029 NULL, "(annotation?, (restriction | extension))");
12030 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012031 if (child == NULL) {
12032 xmlSchemaPContentErr(ctxt,
12033 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012034 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012035 NULL, "(annotation?, (restriction | extension))");
12036 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012037 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012038 xmlSchemaParseRestriction(ctxt, schema, child,
12039 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012040 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012041 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012042 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012043 xmlSchemaParseExtension(ctxt, schema, child,
12044 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012045 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012046 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012047 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012048 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012049 xmlSchemaPContentErr(ctxt,
12050 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012051 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012052 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012053 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012054 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012055}
12056
12057/**
12058 * xmlSchemaParseComplexType:
12059 * @ctxt: a schema validation context
12060 * @schema: the schema being built
12061 * @node: a subtree containing XML Schema informations
12062 *
12063 * parse a XML schema Complex Type definition
12064 * *WARNING* this interface is highly subject to change
12065 *
12066 * Returns the type definition or NULL in case of error
12067 */
12068static xmlSchemaTypePtr
12069xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012070 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012071{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012072 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012073 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012074 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012075 xmlAttrPtr attr;
12076 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012077#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012078 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012079#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012080 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012081
Daniel Veillard4255d502002-04-16 15:50:10 +000012082
12083 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12084 return (NULL);
12085
Daniel Veillard01fa6152004-06-29 17:04:39 +000012086 ctxtType = ctxt->ctxtType;
12087
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012088 if (topLevel) {
12089 attr = xmlSchemaGetPropNode(node, "name");
12090 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012091 xmlSchemaPMissingAttrErr(ctxt,
12092 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012093 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012094 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012095 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12096 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012097 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012098 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012099
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012100 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012101 /*
12102 * Parse as local complex type definition.
12103 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012104#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012105 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012106 type = xmlSchemaAddType(ctxt, schema,
12107 XML_SCHEMA_TYPE_COMPLEX,
12108 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012109 ctxt->targetNamespace, node, 0);
12110#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012111 type = xmlSchemaAddType(ctxt, schema,
12112 XML_SCHEMA_TYPE_COMPLEX,
12113 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012114#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012115 if (type == NULL)
12116 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012117 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012118 type->node = node;
12119 type->type = XML_SCHEMA_TYPE_COMPLEX;
12120 /*
12121 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012122 */
12123 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012124 /*
12125 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012126 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012127 type = xmlSchemaAddType(ctxt, schema,
12128 XML_SCHEMA_TYPE_COMPLEX,
12129 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012130 if (type == NULL)
12131 return (NULL);
12132 type->node = node;
12133 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012134 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012135 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012136 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012137 /*
12138 * Handle attributes.
12139 */
12140 attr = node->properties;
12141 while (attr != NULL) {
12142 if (attr->ns == NULL) {
12143 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12144 /*
12145 * Attribute "id".
12146 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012147 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012148 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12149 /*
12150 * Attribute "mixed".
12151 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012152 if (xmlSchemaPGetBoolNodeValue(ctxt,
12153 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012154 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12155 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012156 /*
12157 * Attributes of global complex type definitions.
12158 */
12159 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12160 /* Pass. */
12161 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12162 /*
12163 * Attribute "abstract".
12164 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012165 if (xmlSchemaPGetBoolNodeValue(ctxt,
12166 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012167 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12168 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12169 /*
12170 * Attribute "final".
12171 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012172 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012173 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012174 if (xmlSchemaPValAttrBlockFinal(attrValue,
12175 &(type->flags),
12176 -1,
12177 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12178 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12179 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012180 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012181 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012182 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012183 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012184 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012185 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012186 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012187 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012188 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12189 /*
12190 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012191 */
12192 attrValue = xmlSchemaGetNodeContent(ctxt,
12193 (xmlNodePtr) attr);
12194 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012195 -1,
12196 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012197 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012198 -1, -1, -1) != 0) {
12199 xmlSchemaPSimpleTypeErr(ctxt,
12200 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012201 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012202 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012203 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012204 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012205 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012206 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012207 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012208 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012209 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012210 } else {
12211 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012212 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012213 }
12214 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012215 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012216 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012217 }
12218 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012219 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012220 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012221 /*
12222 * Apply default "block" values.
12223 */
12224 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12225 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12226 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12227 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12228 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012229 if (! final) {
12230 /*
12231 * Apply default "block" values.
12232 */
12233 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12234 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12235 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12236 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12237 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012238 /*
12239 * And now for the children...
12240 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012241 child = node->children;
12242 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012243 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012244 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012245 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012246 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012247 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012248 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012249 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012250 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012251 * Specifying mixed='true' when the <simpleContent>
12252 * alternative is chosen has no effect
12253 */
William M. Bracke7091952004-05-11 15:09:58 +000012254 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12255 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012256 xmlSchemaParseSimpleContent(ctxt, schema, child,
12257 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012258 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012259 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012260 /*
12261 * <complexType><complexContent>...
12262 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012263 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012264 xmlSchemaParseComplexContent(ctxt, schema, child,
12265 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012266 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012267 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012268 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012269 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12270 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012271 * SPEC
12272 * "...the third alternative (neither <simpleContent> nor
12273 * <complexContent>) is chosen. This case is understood as shorthand
Rob Richardsc6947bb2008-06-29 15:04:41 +000012274 * for complex content restricting the �ur-type definition�, and the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012275 * details of the mappings should be modified as necessary.
12276 */
12277 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12278 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012279 /*
12280 * Parse model groups.
12281 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012282 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012283 type->subtypes = (xmlSchemaTypePtr)
12284 xmlSchemaParseModelGroup(ctxt, schema, child,
12285 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012286 child = child->next;
12287 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012288 type->subtypes = (xmlSchemaTypePtr)
12289 xmlSchemaParseModelGroup(ctxt, schema, child,
12290 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012291 child = child->next;
12292 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012293 type->subtypes = (xmlSchemaTypePtr)
12294 xmlSchemaParseModelGroup(ctxt, schema, child,
12295 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012296 child = child->next;
12297 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012298 type->subtypes = (xmlSchemaTypePtr)
12299 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012300 /*
12301 * Note that the reference will be resolved in
12302 * xmlSchemaResolveTypeReferences();
12303 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012304 child = child->next;
12305 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012306 /*
12307 * Parse attribute decls/refs.
12308 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012309 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12310 (xmlSchemaItemListPtr *) &(type->attrUses),
12311 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12312 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012313 /*
12314 * Parse attribute wildcard.
12315 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012316 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012317 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12318 child = child->next;
12319 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012320 }
12321 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012322 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012323 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012324 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012325 NULL, "(annotation?, (simpleContent | complexContent | "
12326 "((group | all | choice | sequence)?, ((attribute | "
12327 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012328 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012329 /*
12330 * REDEFINE: SPEC src-redefine (5)
12331 */
12332 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12333 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012334 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012335 "<complexType> must have a <restriction> or <extension> "
12336 "grand-child", NULL);
12337 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012338 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012339 return (type);
12340}
12341
Daniel Veillard4255d502002-04-16 15:50:10 +000012342/************************************************************************
12343 * *
12344 * Validating using Schemas *
12345 * *
12346 ************************************************************************/
12347
12348/************************************************************************
12349 * *
12350 * Reading/Writing Schemas *
12351 * *
12352 ************************************************************************/
12353
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012354#if 0 /* Will be enabled if it is clear what options are needed. */
12355/**
12356 * xmlSchemaParserCtxtSetOptions:
12357 * @ctxt: a schema parser context
12358 * @options: a combination of xmlSchemaParserOption
12359 *
12360 * Sets the options to be used during the parse.
12361 *
12362 * Returns 0 in case of success, -1 in case of an
12363 * API error.
12364 */
12365static int
12366xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12367 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012368
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012369{
12370 int i;
12371
12372 if (ctxt == NULL)
12373 return (-1);
12374 /*
12375 * WARNING: Change the start value if adding to the
12376 * xmlSchemaParseOption.
12377 */
12378 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12379 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012380 return (-1);
12381 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012382 }
12383 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012384 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012385}
12386
12387/**
12388 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012389 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012390 *
12391 * Returns the option combination of the parser context.
12392 */
12393static int
12394xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012395
12396{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012397 if (ctxt == NULL)
12398 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012399 else
12400 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012401}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012402#endif
12403
Daniel Veillard4255d502002-04-16 15:50:10 +000012404/**
12405 * xmlSchemaNewParserCtxt:
12406 * @URL: the location of the schema
12407 *
12408 * Create an XML Schemas parse context for that file/resource expected
12409 * to contain an XML Schemas file.
12410 *
12411 * Returns the parser context or NULL in case of error
12412 */
12413xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012414xmlSchemaNewParserCtxt(const char *URL)
12415{
Daniel Veillard4255d502002-04-16 15:50:10 +000012416 xmlSchemaParserCtxtPtr ret;
12417
12418 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012419 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012420
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012421 ret = xmlSchemaParserCtxtCreate();
12422 if (ret == NULL)
12423 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012424 ret->dict = xmlDictCreate();
12425 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012426 return (ret);
12427}
12428
12429/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012430 * xmlSchemaNewMemParserCtxt:
12431 * @buffer: a pointer to a char array containing the schemas
12432 * @size: the size of the array
12433 *
12434 * Create an XML Schemas parse context for that memory buffer expected
12435 * to contain an XML Schemas file.
12436 *
12437 * Returns the parser context or NULL in case of error
12438 */
12439xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012440xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12441{
Daniel Veillard6045c902002-10-09 21:13:59 +000012442 xmlSchemaParserCtxtPtr ret;
12443
12444 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012445 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012446 ret = xmlSchemaParserCtxtCreate();
12447 if (ret == NULL)
12448 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012449 ret->buffer = buffer;
12450 ret->size = size;
Daniel Veillarddee23482008-04-11 12:58:43 +000012451 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012452 return (ret);
12453}
12454
12455/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012456 * xmlSchemaNewDocParserCtxt:
12457 * @doc: a preparsed document tree
12458 *
12459 * Create an XML Schemas parse context for that document.
12460 * NB. The document may be modified during the parsing process.
12461 *
12462 * Returns the parser context or NULL in case of error
12463 */
12464xmlSchemaParserCtxtPtr
12465xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12466{
12467 xmlSchemaParserCtxtPtr ret;
12468
12469 if (doc == NULL)
12470 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012471 ret = xmlSchemaParserCtxtCreate();
12472 if (ret == NULL)
12473 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012474 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012475 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012476 /* The application has responsibility for the document */
12477 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012478
12479 return (ret);
12480}
12481
12482/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012483 * xmlSchemaFreeParserCtxt:
12484 * @ctxt: the schema parser context
12485 *
12486 * Free the resources associated to the schema parser context
12487 */
12488void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012489xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12490{
Daniel Veillard4255d502002-04-16 15:50:10 +000012491 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012492 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012493 if (ctxt->doc != NULL && !ctxt->preserve)
Daniel Veillarddee23482008-04-11 12:58:43 +000012494 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012495 if (ctxt->vctxt != NULL) {
12496 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12497 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012498 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12499 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12500 ctxt->constructor = NULL;
12501 ctxt->ownsConstructor = 0;
12502 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012503 if (ctxt->attrProhibs != NULL)
12504 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012505 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012506 xmlFree(ctxt);
12507}
12508
12509/************************************************************************
12510 * *
12511 * Building the content models *
12512 * *
12513 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012514
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012515static void
12516xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012517 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012518{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012519 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012520 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012521 xmlSchemaSubstGroupPtr substGroup;
12522 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012523
12524 elemDecl = (xmlSchemaElementPtr) particle->children;
12525 /*
12526 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012527 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012528 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012529 if (end == NULL)
12530 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012531 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012532 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012533 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012534 XML_SCHEMAP_INTERNAL,
12535 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12536 "declaration is marked having a subst. group but none "
12537 "available.\n", elemDecl->name, NULL);
12538 return;
12539 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012540 if (counter >= 0) {
12541 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012542 * NOTE that we put the declaration in, even if it's abstract.
12543 * However, an error will be raised during *validation* if an element
12544 * information item shall be validated against an abstract element
12545 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012546 */
12547 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12548 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12549 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12550 /*
12551 * Add subst. group members.
12552 */
12553 for (i = 0; i < substGroup->members->nbItems; i++) {
12554 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12555 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12556 member->name, member->targetNamespace, member);
12557 }
12558 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012559 /*
12560 * NOTE that we put the declaration in, even if it's abstract,
12561 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012562 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012563 xmlAutomataNewTransition2(pctxt->am,
12564 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012565 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12566 /*
12567 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012568 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012569 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012570 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012571 /*
12572 * NOTE: This fixes bug #341150. xmlAutomataNewOnceTrans2()
12573 * was incorrectly used instead of xmlAutomataNewTransition2()
12574 * (seems like a copy&paste bug from the XML_SCHEMA_TYPE_ALL
12575 * section in xmlSchemaBuildAContentModel() ).
Daniel Veillarddee23482008-04-11 12:58:43 +000012576 * TODO: Check if xmlAutomataNewOnceTrans2() was instead
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012577 * intended for the above "counter" section originally. I.e.,
12578 * check xs:all with subst-groups.
12579 *
12580 * tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12581 * member->name, member->targetNamespace,
12582 * 1, 1, member);
12583 */
12584 tmp = xmlAutomataNewTransition2(pctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012585 member->name, member->targetNamespace, member);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012586 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012587 }
12588 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012589 xmlAutomataStatePtr hop;
12590 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12591 UNBOUNDED : particle->maxOccurs - 1;
12592 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12593
12594 counter =
12595 xmlAutomataNewCounter(pctxt->am, minOccurs,
12596 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012597 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012598
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012599 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012600 xmlAutomataNewTransition2(pctxt->am,
12601 start, NULL,
12602 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012603 hop);
12604 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012605 * Add subst. group members.
12606 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012607 for (i = 0; i < substGroup->members->nbItems; i++) {
12608 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12609 xmlAutomataNewEpsilon(pctxt->am,
12610 xmlAutomataNewTransition2(pctxt->am,
12611 start, NULL,
12612 member->name, member->targetNamespace, member),
12613 hop);
12614 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012615 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12616 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12617 }
12618 if (particle->minOccurs == 0)
12619 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012620 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012621}
12622
12623static void
12624xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12625 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012626{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012627 if (((xmlSchemaElementPtr) particle->children)->flags &
12628 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012629 /*
12630 * Substitution groups.
12631 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012632 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012633 } else {
12634 xmlSchemaElementPtr elemDecl;
12635 xmlAutomataStatePtr start;
12636
12637 elemDecl = (xmlSchemaElementPtr) particle->children;
12638
12639 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012640 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012641 if (particle->maxOccurs == 1) {
12642 start = ctxt->state;
12643 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012644 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12645 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12646 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012647 /* Special case. */
Daniel Veillarddee23482008-04-11 12:58:43 +000012648 start = ctxt->state;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012649 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012650 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012651 ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
Daniel Veillarddee23482008-04-11 12:58:43 +000012652 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012653 } else {
12654 int counter;
12655 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12656 UNBOUNDED : particle->maxOccurs - 1;
12657 int minOccurs = particle->minOccurs < 1 ?
12658 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012659
12660 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012661 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12662 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12663 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12664 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12665 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12666 NULL, counter);
12667 }
12668 if (particle->minOccurs == 0)
12669 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12670 }
12671}
12672
Daniel Veillard4255d502002-04-16 15:50:10 +000012673/**
12674 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012675 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012676 * @particle: the particle component
12677 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012678 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012679 * Create the automaton for the {content type} of a complex type.
12680 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012681 */
12682static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012683xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012684 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012685{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012686 if (particle == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012687 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012688 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012689 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012690 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012691 /*
12692 * Just return in this case. A missing "term" of the particle
12693 * might arise due to an invalid "term" component.
12694 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012695 return;
12696 }
12697
12698 switch (particle->children->type) {
12699 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012700 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012701 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012702 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012703
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012704 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012705
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 start = pctxt->state;
12707 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012708
12709 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012710 if (wild->any == 1) {
12711 /*
12712 * We need to add both transitions:
12713 *
12714 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012715 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012716 pctxt->state =
12717 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012718 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012719 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012720 /*
12721 * 2. the {"*"} for elements in no namespace.
12722 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012723 pctxt->state =
12724 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012725 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012726 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012727
12728 } else if (wild->nsSet != NULL) {
12729 ns = wild->nsSet;
12730 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012731 pctxt->state = start;
12732 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12733 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12734 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012735 ns = ns->next;
12736 } while (ns != NULL);
12737
12738 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012739 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12740 start, end, BAD_CAST "*", wild->negNsSet->value,
12741 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012742 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012743 } else {
12744 int counter;
12745 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012746 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012747 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012748 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012749 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012750
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012751 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12752 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012753 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012754 pctxt->state =
12755 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012756 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012757 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12758 pctxt->state =
12759 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012760 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012761 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012762 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012763 ns = wild->nsSet;
12764 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012765 pctxt->state =
12766 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012767 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012768 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012769 ns = ns->next;
12770 } while (ns != NULL);
12771
12772 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012773 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012774 start, hop, BAD_CAST "*", wild->negNsSet->value,
12775 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012776 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012777 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12778 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012779 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012780 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012781 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012782 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012783 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012784 break;
12785 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012786 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012787 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012788 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012789 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012790 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012791
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012792 /*
12793 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012794 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012795 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012796 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12797 sub = particle->children->children;
12798 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012799 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012800 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012801 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012802 }
12803 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012804 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012805
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012806 if (particle->maxOccurs >= UNBOUNDED) {
12807 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012808 xmlAutomataStatePtr tmp;
12809 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012810
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012811 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012812 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012813 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012814
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012815 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012816 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012817
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012818 sub = particle->children->children;
12819 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012820 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012821 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012822 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012823 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012824 tmp = pctxt->state;
12825 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012826 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012827 pctxt->state =
12828 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012829 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012830
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012831 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012832 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012833 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012834 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012835
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012836 sub = particle->children->children;
12837 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012838 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012839 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012840 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012841 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012842 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012843 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012844 /*
12845 * epsilon needed to block previous trans from
12846 * being allowed to enter back from another
12847 * construct
12848 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012849 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12850 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012851 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012852 xmlAutomataNewEpsilon(pctxt->am,
12853 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012854 }
12855 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012856 } else if ((particle->maxOccurs > 1)
12857 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012858 xmlAutomataStatePtr tmp;
12859 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012860
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012861 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012862 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012863 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012864
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012865 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012866 particle->minOccurs - 1,
12867 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012868
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012869 sub = particle->children->children;
12870 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012871 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012872 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012873 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012874 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012875 tmp = pctxt->state;
12876 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012877 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012878 pctxt->state =
12879 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012880 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012881 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012882 xmlAutomataNewEpsilon(pctxt->am,
12883 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012884 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012885 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012886 sub = particle->children->children;
12887 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012889 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012890 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012891 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012892 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012893 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12894 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012895 }
12896 }
12897 }
12898 break;
12899 }
12900 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012901 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012902 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012903
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012904 start = pctxt->state;
12905 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012906
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012907 /*
12908 * iterate over the subtypes and remerge the end with an
12909 * epsilon transition
12910 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012911 if (particle->maxOccurs == 1) {
12912 sub = particle->children->children;
12913 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012914 pctxt->state = start;
12915 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012916 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012917 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012918 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012919 }
12920 } else {
12921 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012922 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012923 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12924 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012925 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012926 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012927
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012928 /*
12929 * use a counter to keep track of the number of transtions
12930 * which went through the choice.
12931 */
12932 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012933 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12934 hop = xmlAutomataNewState(pctxt->am);
12935 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012936
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012937 sub = particle->children->children;
12938 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012939 pctxt->state = base;
12940 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012941 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012942 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012943 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012944 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012945 xmlAutomataNewEpsilon(pctxt->am, start, base);
12946 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12947 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012948 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012949 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012950 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012951 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012952 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012953 break;
12954 }
12955 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012956 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012957 xmlSchemaParticlePtr sub;
12958 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012959 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012960
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012961 sub = (xmlSchemaParticlePtr) particle->children->children;
12962 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012963 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012964 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012965 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012966 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012967
12968 elemDecl = (xmlSchemaElementPtr) sub->children;
12969 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012970 PERROR_INT("xmlSchemaBuildAContentModel",
12971 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012972 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012973 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012974 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012975 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012976 * {particles} of the group must be 0 or 1; this is
12977 * already ensured during the parse of the content of
12978 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012979 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012980 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12981 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012982
Daniel Veillarda980bef2005-07-18 21:34:03 +000012983 /*
12984 * This is an abstract group, we need to share
12985 * the same counter for all the element transitions
12986 * derived from the group
12987 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012988 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012989 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012990 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12991 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012992 } else {
12993 if ((sub->minOccurs == 1) &&
12994 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012995 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12996 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012997 elemDecl->name,
12998 elemDecl->targetNamespace,
12999 1, 1, elemDecl);
13000 } else if ((sub->minOccurs == 0) &&
13001 (sub->maxOccurs == 1)) {
13002
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013003 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
13004 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000013005 elemDecl->name,
13006 elemDecl->targetNamespace,
13007 0,
13008 1,
13009 elemDecl);
13010 }
13011 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013012 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013013 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013014 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013015 pctxt->state =
13016 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013017 break;
13018 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013019 case XML_SCHEMA_TYPE_GROUP:
13020 /*
13021 * If we hit a model group definition, then this means that
13022 * it was empty, thus was not substituted for the containing
13023 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013024 * TODO: But the group should be substituted and not occur at
13025 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013026 */
13027 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013028 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013029 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
13030 "xmlSchemaBuildAContentModel",
13031 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013032 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013033 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013034 }
13035}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013036
Daniel Veillard4255d502002-04-16 15:50:10 +000013037/**
13038 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000013039 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013040 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013041 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000013042 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013043 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000013044 */
13045static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013046xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013047 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013048{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013049 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13050 (type->contModel != NULL) ||
13051 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13052 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013053 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013054
13055#ifdef DEBUG_CONTENT
13056 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013057 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013058#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013059 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013060 ctxt->am = xmlNewAutomata();
13061 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013062 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013063 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013064 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013065 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013066 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013067 /*
13068 * Build the automaton.
13069 */
13070 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013071 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013072 type->contModel = xmlAutomataCompile(ctxt->am);
13073 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013074 xmlSchemaPCustomErr(ctxt,
13075 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013076 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013077 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013078 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013079 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013080 XML_SCHEMAP_NOT_DETERMINISTIC,
13081 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013082 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013083 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013084 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013085#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013086 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013087 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013088 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013089#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013090 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013091 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013092 xmlFreeAutomata(ctxt->am);
13093 ctxt->am = NULL;
13094}
13095
13096/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013097 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013098 * @elem: the schema element context
13099 * @ctxt: the schema parser context
13100 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013101 * Resolves the references of an element declaration
13102 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013103 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013104 */
13105static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013106xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13107 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013108{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013109 if ((ctxt == NULL) || (elemDecl == NULL) ||
13110 ((elemDecl != NULL) &&
13111 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013112 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013113 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013114
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013115 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013116 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013117
Rob Richardsc6947bb2008-06-29 15:04:41 +000013118 /* (type definition) ... otherwise the type definition �resolved�
13119 * to by the �actual value� of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013120 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013121 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013122 elemDecl->namedTypeNs);
13123 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013124 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013125 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013126 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013127 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013128 XML_SCHEMA_TYPE_BASIC, "type definition");
13129 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013130 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013131 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013132 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013133 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013134
Daniel Veillardc0826a72004-08-10 14:17:33 +000013135 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013136 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013137 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013138 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013139 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13140 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013141 if (substHead == NULL) {
13142 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013143 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013144 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013145 "substitutionGroup", elemDecl->substGroup,
13146 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013147 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013148 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013149 /*
13150 * Set the "substitution group affiliation".
13151 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013152 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013153 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013154 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013155 * The type definitions is set to:
13156 * SPEC "...the {type definition} of the element
Rob Richardsc6947bb2008-06-29 15:04:41 +000013157 * declaration �resolved� to by the �actual value�
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013158 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013159 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013160 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013161 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013162 }
13163 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013164 /*
13165 * SPEC "The definition of anyType serves as the default type definition
13166 * for element declarations whose XML representation does not specify one."
13167 */
13168 if ((elemDecl->subtypes == NULL) &&
13169 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013170 (elemDecl->substGroup == NULL))
13171 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013172}
13173
13174/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013175 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013176 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013177 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013178 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013179 * Checks and builds the "member type definitions" property of the union
13180 * simple type. This handles part (1), part (2) is done in
13181 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13182 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013183 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013184 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013185static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013186xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13187 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013188{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013189
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013190 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013191 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013192
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013193 /*
13194 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
Rob Richardsc6947bb2008-06-29 15:04:41 +000013195 * define the explicit members as the type definitions �resolved�
13196 * to by the items in the �actual value� of the memberTypes [attribute],
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013197 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013198 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013199 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013200 /*
13201 * Resolve references.
13202 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013203 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013204 lastLink = NULL;
13205 while (link != NULL) {
13206 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013207
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013208 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13209 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13210
13211 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013212 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013213 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013214 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013215 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13216 /*
13217 * Remove the member type link.
13218 */
13219 if (lastLink == NULL)
13220 type->memberTypes = link->next;
13221 else
13222 lastLink->next = link->next;
13223 newLink = link;
13224 link = link->next;
13225 xmlFree(newLink);
13226 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013227 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013228 lastLink = link;
13229 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013230 }
13231 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013232 /*
13233 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013234 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013235 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013236 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013237 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13238 if (link == NULL) {
13239 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13240 return (-1);
13241 }
13242 link->type = memberType;
13243 link->next = NULL;
13244 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013245 type->memberTypes = link;
13246 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013247 lastLink->next = link;
13248 lastLink = link;
13249 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013250 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013251 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013252}
13253
Daniel Veillard4255d502002-04-16 15:50:10 +000013254/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013255 * xmlSchemaIsDerivedFromBuiltInType:
13256 * @ctxt: the schema parser context
13257 * @type: the type definition
13258 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013259 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013260 *
13261 * Returns 1 if the type has the given value type, or
13262 * is derived from such a type.
13263 */
William M. Brack803812b2004-06-03 02:11:24 +000013264static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013265xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013266{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013267 if (type == NULL)
13268 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013269 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013270 return (0);
13271 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13272 if (type->builtInType == valType)
13273 return(1);
13274 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13275 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13276 return (0);
13277 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013278 }
13279 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013280}
13281
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013282#if 0
13283/**
13284 * xmlSchemaIsDerivedFromBuiltInType:
13285 * @ctxt: the schema parser context
13286 * @type: the type definition
13287 * @valType: the value type
13288 *
13289 *
13290 * Returns 1 if the type has the given value type, or
13291 * is derived from such a type.
13292 */
13293static int
13294xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13295{
13296 if (type == NULL)
13297 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013298 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013299 return (0);
13300 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13301 if (type->builtInType == valType)
13302 return(1);
13303 return (0);
13304 } else
13305 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13306
13307 return (0);
13308}
13309#endif
13310
13311static xmlSchemaTypePtr
13312xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13313{
13314 if (type == NULL)
13315 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013316 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013317 return (NULL);
13318 if (type->type == XML_SCHEMA_TYPE_BASIC)
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013319 return(type);
13320 return(xmlSchemaQueryBuiltInType(type->subtypes));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013321}
13322
Daniel Veillard3646d642004-06-02 19:19:14 +000013323/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013324 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013325 * @type: the simpleType definition
13326 *
13327 * Returns the primitive type of the given type or
13328 * NULL in case of error.
13329 */
13330static xmlSchemaTypePtr
13331xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13332{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013333
Daniel Veillard01fa6152004-06-29 17:04:39 +000013334 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013335 /*
13336 * Note that anySimpleType is actually not a primitive type
13337 * but we need that here.
13338 */
13339 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13340 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013341 return (type);
13342 type = type->baseType;
13343 }
13344
13345 return (NULL);
13346}
13347
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013348#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013349/**
13350 * xmlSchemaGetBuiltInTypeAncestor:
13351 * @type: the simpleType definition
13352 *
13353 * Returns the primitive type of the given type or
13354 * NULL in case of error.
13355 */
13356static xmlSchemaTypePtr
13357xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13358{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013359 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013360 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013361 while (type != NULL) {
13362 if (type->type == XML_SCHEMA_TYPE_BASIC)
13363 return (type);
13364 type = type->baseType;
13365 }
13366
13367 return (NULL);
13368}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013369#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013370
Daniel Veillard01fa6152004-06-29 17:04:39 +000013371/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013372 * xmlSchemaCloneWildcardNsConstraints:
13373 * @ctxt: the schema parser context
13374 * @dest: the destination wildcard
13375 * @source: the source wildcard
13376 *
13377 * Clones the namespace constraints of source
13378 * and assignes them to dest.
13379 * Returns -1 on internal error, 0 otherwise.
13380 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013381static int
13382xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013383 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013384 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013385{
13386 xmlSchemaWildcardNsPtr cur, tmp, last;
13387
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013388 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013389 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013390 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013391 cur = source->nsSet;
13392 last = NULL;
13393 while (cur != NULL) {
13394 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13395 if (tmp == NULL)
13396 return(-1);
13397 tmp->value = cur->value;
13398 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013399 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013400 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013401 last->next = tmp;
13402 last = tmp;
13403 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013404 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013405 if (dest->negNsSet != NULL)
13406 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013407 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013408 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13409 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013410 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013411 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013412 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013413 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013414 return(0);
13415}
13416
Daniel Veillard50355f02004-06-08 17:52:16 +000013417/**
13418 * xmlSchemaUnionWildcards:
13419 * @ctxt: the schema parser context
13420 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013421 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013422 *
13423 * Unions the namespace constraints of the given wildcards.
13424 * @completeWild will hold the resulting union.
13425 * Returns a positive error code on failure, -1 in case of an
13426 * internal error, 0 otherwise.
13427 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013428static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013429xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013430 xmlSchemaWildcardPtr completeWild,
13431 xmlSchemaWildcardPtr curWild)
13432{
13433 xmlSchemaWildcardNsPtr cur, curB, tmp;
13434
13435 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013436 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013437 * value.
13438 */
13439 if ((completeWild->any == curWild->any) &&
13440 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13441 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013442
Daniel Veillard3646d642004-06-02 19:19:14 +000013443 if ((completeWild->negNsSet == NULL) ||
13444 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013445
Daniel Veillard3646d642004-06-02 19:19:14 +000013446 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013447 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013448
13449 /*
13450 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013451 */
13452 cur = completeWild->nsSet;
13453 while (cur != NULL) {
13454 found = 0;
13455 curB = curWild->nsSet;
13456 while (curB != NULL) {
13457 if (cur->value == curB->value) {
13458 found = 1;
13459 break;
13460 }
13461 curB = curB->next;
13462 }
13463 if (!found)
13464 break;
13465 cur = cur->next;
13466 }
13467 if (found)
13468 return(0);
13469 } else
13470 return(0);
13471 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013472 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013473 /*
13474 * 2 If either O1 or O2 is any, then any must be the value
13475 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013476 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013477 if (completeWild->any == 0) {
13478 completeWild->any = 1;
13479 if (completeWild->nsSet != NULL) {
13480 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13481 completeWild->nsSet = NULL;
13482 }
13483 if (completeWild->negNsSet != NULL) {
13484 xmlFree(completeWild->negNsSet);
13485 completeWild->negNsSet = NULL;
13486 }
13487 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013488 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013489 }
13490 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013491 * 3 If both O1 and O2 are sets of (namespace names or �absent�),
Daniel Veillard3646d642004-06-02 19:19:14 +000013492 * then the union of those sets must be the value.
13493 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013494 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013495 int found;
13496 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013497
Daniel Veillard3646d642004-06-02 19:19:14 +000013498 cur = curWild->nsSet;
13499 start = completeWild->nsSet;
13500 while (cur != NULL) {
13501 found = 0;
13502 curB = start;
13503 while (curB != NULL) {
13504 if (cur->value == curB->value) {
13505 found = 1;
13506 break;
13507 }
13508 curB = curB->next;
13509 }
13510 if (!found) {
13511 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013512 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013513 return (-1);
13514 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013515 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013516 completeWild->nsSet = tmp;
13517 }
13518 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013519 }
13520
Daniel Veillard3646d642004-06-02 19:19:14 +000013521 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013522 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013523 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013524 * 4 If the two are negations of different values (namespace names
Rob Richardsc6947bb2008-06-29 15:04:41 +000013525 * or �absent�), then a pair of not and �absent� must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013526 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013527 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013528 (curWild->negNsSet != NULL) &&
13529 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13530 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013531
13532 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013533 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013534 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013535 * 5.
13536 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013537 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013538 (completeWild->negNsSet->value != NULL) &&
13539 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013540 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013541 (curWild->negNsSet->value != NULL) &&
13542 (completeWild->nsSet != NULL))) {
13543
13544 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013545
Daniel Veillard3646d642004-06-02 19:19:14 +000013546 if (completeWild->nsSet != NULL) {
13547 cur = completeWild->nsSet;
13548 curB = curWild->negNsSet;
13549 } else {
13550 cur = curWild->nsSet;
13551 curB = completeWild->negNsSet;
13552 }
13553 nsFound = 0;
13554 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013555 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013556 absentFound = 1;
13557 else if (cur->value == curB->value)
13558 nsFound = 1;
13559 if (nsFound && absentFound)
13560 break;
13561 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013562 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013563
13564 if (nsFound && absentFound) {
13565 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013566 * 5.1 If the set S includes both the negated namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013567 * name and �absent�, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013568 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013569 completeWild->any = 1;
13570 if (completeWild->nsSet != NULL) {
13571 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13572 completeWild->nsSet = NULL;
13573 }
13574 if (completeWild->negNsSet != NULL) {
13575 xmlFree(completeWild->negNsSet);
13576 completeWild->negNsSet = NULL;
13577 }
13578 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013579 /*
13580 * 5.2 If the set S includes the negated namespace name
Rob Richardsc6947bb2008-06-29 15:04:41 +000013581 * but not �absent�, then a pair of not and �absent� must
Daniel Veillard3646d642004-06-02 19:19:14 +000013582 * be the value.
13583 */
13584 if (completeWild->nsSet != NULL) {
13585 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13586 completeWild->nsSet = NULL;
13587 }
13588 if (completeWild->negNsSet == NULL) {
13589 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13590 if (completeWild->negNsSet == NULL)
13591 return (-1);
13592 }
13593 completeWild->negNsSet->value = NULL;
13594 } else if ((!nsFound) && absentFound) {
13595 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013596 * 5.3 If the set S includes �absent� but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013597 * namespace name, then the union is not expressible.
13598 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013599 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013600 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013601 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013602 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013603 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013604 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013605 /*
13606 * 5.4 If the set S does not include either the negated namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013607 * name or �absent�, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013608 * and a namespace name must be the value.
13609 */
13610 if (completeWild->negNsSet == NULL) {
13611 if (completeWild->nsSet != NULL) {
13612 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13613 completeWild->nsSet = NULL;
13614 }
13615 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13616 if (completeWild->negNsSet == NULL)
13617 return (-1);
13618 completeWild->negNsSet->value = curWild->negNsSet->value;
13619 }
13620 }
13621 return (0);
13622 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013623 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013624 * 6.
13625 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013626 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013627 (completeWild->negNsSet->value == NULL) &&
13628 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013629 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013630 (curWild->negNsSet->value == NULL) &&
13631 (completeWild->nsSet != NULL))) {
13632
13633 if (completeWild->nsSet != NULL) {
13634 cur = completeWild->nsSet;
13635 } else {
13636 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013637 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013638 while (cur != NULL) {
13639 if (cur->value == NULL) {
13640 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013641 * 6.1 If the set S includes �absent�, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013642 * value.
13643 */
13644 completeWild->any = 1;
13645 if (completeWild->nsSet != NULL) {
13646 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13647 completeWild->nsSet = NULL;
13648 }
13649 if (completeWild->negNsSet != NULL) {
13650 xmlFree(completeWild->negNsSet);
13651 completeWild->negNsSet = NULL;
13652 }
13653 return (0);
13654 }
13655 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013656 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013657 if (completeWild->negNsSet == NULL) {
13658 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013659 * 6.2 If the set S does not include �absent�, then a pair of not
13660 * and �absent� must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013661 */
13662 if (completeWild->nsSet != NULL) {
13663 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13664 completeWild->nsSet = NULL;
13665 }
13666 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13667 if (completeWild->negNsSet == NULL)
13668 return (-1);
13669 completeWild->negNsSet->value = NULL;
13670 }
13671 return (0);
13672 }
13673 return (0);
13674
13675}
13676
Daniel Veillard50355f02004-06-08 17:52:16 +000013677/**
13678 * xmlSchemaIntersectWildcards:
13679 * @ctxt: the schema parser context
13680 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013681 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013682 *
13683 * Intersects the namespace constraints of the given wildcards.
13684 * @completeWild will hold the resulting intersection.
13685 * Returns a positive error code on failure, -1 in case of an
13686 * internal error, 0 otherwise.
13687 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013688static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013689xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013690 xmlSchemaWildcardPtr completeWild,
13691 xmlSchemaWildcardPtr curWild)
13692{
William M. Brack803812b2004-06-03 02:11:24 +000013693 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013694
13695 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013696 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013697 * value.
13698 */
13699 if ((completeWild->any == curWild->any) &&
13700 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13701 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013702
Daniel Veillard3646d642004-06-02 19:19:14 +000013703 if ((completeWild->negNsSet == NULL) ||
13704 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013705
Daniel Veillard3646d642004-06-02 19:19:14 +000013706 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013707 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013708
13709 /*
13710 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013711 */
13712 cur = completeWild->nsSet;
13713 while (cur != NULL) {
13714 found = 0;
13715 curB = curWild->nsSet;
13716 while (curB != NULL) {
13717 if (cur->value == curB->value) {
13718 found = 1;
13719 break;
13720 }
13721 curB = curB->next;
13722 }
13723 if (!found)
13724 break;
13725 cur = cur->next;
13726 }
13727 if (found)
13728 return(0);
13729 } else
13730 return(0);
13731 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013732 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013733 /*
13734 * 2 If either O1 or O2 is any, then the other must be the value.
13735 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013736 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013737 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013738 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013739 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013740 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013741 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013742 * 3 If either O1 or O2 is a pair of not and a value (a namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013743 * name or �absent�) and the other is a set of (namespace names or
13744 * �absent�), then that set, minus the negated value if it was in
13745 * the set, minus �absent� if it was in the set, must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013746 */
13747 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13748 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13749 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013750
Daniel Veillard3646d642004-06-02 19:19:14 +000013751 if (completeWild->nsSet == NULL) {
13752 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013753 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013754 return(-1);
13755 } else
13756 neg = curWild->negNsSet->value;
13757 /*
13758 * Remove absent and negated.
13759 */
13760 prev = NULL;
13761 cur = completeWild->nsSet;
13762 while (cur != NULL) {
13763 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013764 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013765 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013766 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013767 prev->next = cur->next;
13768 xmlFree(cur);
13769 break;
13770 }
13771 prev = cur;
13772 cur = cur->next;
13773 }
13774 if (neg != NULL) {
13775 prev = NULL;
13776 cur = completeWild->nsSet;
13777 while (cur != NULL) {
13778 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013779 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013780 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013781 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013782 prev->next = cur->next;
13783 xmlFree(cur);
13784 break;
13785 }
13786 prev = cur;
13787 cur = cur->next;
13788 }
13789 }
13790
13791 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013792 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013793 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013794 * 4 If both O1 and O2 are sets of (namespace names or �absent�),
Daniel Veillard3646d642004-06-02 19:19:14 +000013795 * then the intersection of those sets must be the value.
13796 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013797 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013798 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013799
Daniel Veillard3646d642004-06-02 19:19:14 +000013800 cur = completeWild->nsSet;
13801 prev = NULL;
13802 while (cur != NULL) {
13803 found = 0;
13804 curB = curWild->nsSet;
13805 while (curB != NULL) {
13806 if (cur->value == curB->value) {
13807 found = 1;
13808 break;
13809 }
13810 curB = curB->next;
13811 }
13812 if (!found) {
13813 if (prev == NULL)
13814 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013815 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013816 prev->next = cur->next;
13817 tmp = cur->next;
13818 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013819 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013820 continue;
13821 }
13822 prev = cur;
13823 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013824 }
13825
Daniel Veillard3646d642004-06-02 19:19:14 +000013826 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013827 }
13828 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013829 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013830 */
13831 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013832 (curWild->negNsSet != NULL) &&
13833 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013834 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013835 (curWild->negNsSet->value != NULL)) {
13836
13837 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013838 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013839 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013840 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013841 }
13842 /*
13843 * 6 If the one is a negation of a namespace name and the other
Rob Richardsc6947bb2008-06-29 15:04:41 +000013844 * is a negation of �absent�, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013845 * of a namespace name must be the value.
13846 */
13847 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13848 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013849 (completeWild->negNsSet->value == NULL)) {
13850 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013851 }
13852 return(0);
13853}
13854
Daniel Veillard50355f02004-06-08 17:52:16 +000013855/**
13856 * xmlSchemaIsWildcardNsConstraintSubset:
13857 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013858 * @sub: the first wildcard
13859 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013860 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013861 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13862 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013863 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013864 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013865 */
13866static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013867xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13868 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013869{
Daniel Veillard50355f02004-06-08 17:52:16 +000013870 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013871 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013872 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013873 if (super->any)
13874 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013875 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013876 * 2.1 sub must be a pair of not and a namespace name or �absent�.
Daniel Veillard50355f02004-06-08 17:52:16 +000013877 * 2.2 super must be a pair of not and the same value.
13878 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013879 if ((sub->negNsSet != NULL) &&
13880 (super->negNsSet != NULL) &&
13881 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013882 return (0);
13883 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013884 * 3.1 sub must be a set whose members are either namespace names or �absent�.
Daniel Veillard50355f02004-06-08 17:52:16 +000013885 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013886 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013887 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013888 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013889 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013890 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013891 xmlSchemaWildcardNsPtr cur, curB;
13892 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013893
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013894 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013895 while (cur != NULL) {
13896 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013897 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013898 while (curB != NULL) {
13899 if (cur->value == curB->value) {
13900 found = 1;
13901 break;
13902 }
13903 curB = curB->next;
13904 }
13905 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013906 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013907 cur = cur->next;
13908 }
13909 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013910 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013911 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013912 xmlSchemaWildcardNsPtr cur;
13913 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013914 * 3.2.2 super must be a pair of not and a namespace name or
Rob Richardsc6947bb2008-06-29 15:04:41 +000013915 * �absent� and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013916 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013917 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013918 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013919 if (cur->value == super->negNsSet->value)
13920 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013921 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013922 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013923 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013924 }
13925 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013926 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013927}
13928
Daniel Veillard50355f02004-06-08 17:52:16 +000013929static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013930xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013931 int *fixed,
13932 const xmlChar **value,
13933 xmlSchemaValPtr *val)
13934{
13935 *fixed = 0;
13936 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013937 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013938 *val = NULL;
13939
Daniel Veillarddee23482008-04-11 12:58:43 +000013940 if (attruse->defValue != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013941 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013942 if (val != NULL)
13943 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013944 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013945 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013946 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013947 } else if ((attruse->attrDecl != NULL) &&
13948 (attruse->attrDecl->defValue != NULL)) {
13949 *value = attruse->attrDecl->defValue;
13950 if (val != NULL)
13951 *val = attruse->attrDecl->defVal;
13952 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013953 *fixed = 1;
13954 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013955 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013956 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013957}
Daniel Veillard3646d642004-06-02 19:19:14 +000013958/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013959 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013960 * @wild: the wildcard
13961 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013962 *
13963 * Validation Rule: Wildcard allows Namespace Name
13964 * (cvc-wildcard-namespace)
13965 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013966 * Returns 0 if the given namespace matches the wildcard,
13967 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013968 */
13969static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013970xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13971 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013972{
13973 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013974 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013975
13976 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013977 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013978 else if (wild->nsSet != NULL) {
13979 xmlSchemaWildcardNsPtr cur;
13980
13981 cur = wild->nsSet;
13982 while (cur != NULL) {
13983 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013984 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013985 cur = cur->next;
13986 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013987 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013988 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013989 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013990
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013991 return(1);
13992}
13993
13994#define XML_SCHEMA_ACTION_DERIVE 0
13995#define XML_SCHEMA_ACTION_REDEFINE 1
13996
13997#define WXS_ACTION_STR(a) \
13998((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13999
14000/*
14001* Schema Component Constraint:
14002* Derivation Valid (Restriction, Complex)
14003* derivation-ok-restriction (2) - (4)
14004*
14005* ATTENTION:
14006* In XML Schema 1.1 this will be:
14007* Validation Rule:
14008* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
14009*
14010*/
14011static int
14012xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
14013 int action,
14014 xmlSchemaBasicItemPtr item,
14015 xmlSchemaBasicItemPtr baseItem,
14016 xmlSchemaItemListPtr uses,
14017 xmlSchemaItemListPtr baseUses,
14018 xmlSchemaWildcardPtr wild,
14019 xmlSchemaWildcardPtr baseWild)
Daniel Veillarddee23482008-04-11 12:58:43 +000014020{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014021 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014022 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014023 const xmlChar *bEffValue;
14024 int effFixed;
Daniel Veillarddee23482008-04-11 12:58:43 +000014025
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014026 if (uses != NULL) {
14027 for (i = 0; i < uses->nbItems; i++) {
14028 cur = uses->items[i];
14029 found = 0;
14030 if (baseUses == NULL)
14031 goto not_found;
14032 for (j = 0; j < baseUses->nbItems; j++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014033 bcur = baseUses->items[j];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014034 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14035 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14036 (WXS_ATTRUSE_DECL_TNS(cur) ==
14037 WXS_ATTRUSE_DECL_TNS(bcur)))
14038 {
14039 /*
14040 * (2.1) "If there is an attribute use in the {attribute
14041 * uses} of the {base type definition} (call this B) whose
14042 * {attribute declaration} has the same {name} and {target
14043 * namespace}, then all of the following must be true:"
14044 */
14045 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000014046
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014047 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14048 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14049 {
14050 xmlChar *str = NULL;
14051 /*
14052 * (2.1.1) "one of the following must be true:"
14053 * (2.1.1.1) "B's {required} is false."
14054 * (2.1.1.2) "R's {required} is true."
14055 */
14056 xmlSchemaPAttrUseErr4(pctxt,
14057 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14058 WXS_ITEM_NODE(item), item, cur,
14059 "The 'optional' attribute use is inconsistent "
14060 "with the corresponding 'required' attribute use of "
14061 "the %s %s",
14062 WXS_ACTION_STR(action),
14063 xmlSchemaGetComponentDesignation(&str, baseItem),
14064 NULL, NULL);
14065 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014066 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014067 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014068 WXS_ATTRUSE_TYPEDEF(cur),
14069 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14070 {
14071 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014072
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014073 /*
14074 * SPEC (2.1.2) "R's {attribute declaration}'s
14075 * {type definition} must be validly derived from
14076 * B's {type definition} given the empty set as
Rob Richardsc6947bb2008-06-29 15:04:41 +000014077 * defined in Type Derivation OK (Simple) (�3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014078 */
14079 xmlSchemaPAttrUseErr4(pctxt,
14080 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14081 WXS_ITEM_NODE(item), item, cur,
14082 "The attribute declaration's %s "
14083 "is not validly derived from "
14084 "the corresponding %s of the "
14085 "attribute declaration in the %s %s",
14086 xmlSchemaGetComponentDesignation(&strA,
14087 WXS_ATTRUSE_TYPEDEF(cur)),
14088 xmlSchemaGetComponentDesignation(&strB,
14089 WXS_ATTRUSE_TYPEDEF(bcur)),
14090 WXS_ACTION_STR(action),
14091 xmlSchemaGetComponentDesignation(&strC, baseItem));
14092 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14093 FREE_AND_NULL(strA);
14094 FREE_AND_NULL(strB);
14095 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014096 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014097 } else {
14098 /*
14099 * 2.1.3 [Definition:] Let the effective value
14100 * constraint of an attribute use be its {value
14101 * constraint}, if present, otherwise its {attribute
14102 * declaration}'s {value constraint} .
14103 */
14104 xmlSchemaGetEffectiveValueConstraint(bcur,
14105 &effFixed, &bEffValue, NULL);
14106 /*
14107 * 2.1.3 ... one of the following must be true
14108 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000014109 * 2.1.3.1 B's �effective value constraint� is
14110 * �absent� or default.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014111 */
14112 if ((bEffValue != NULL) &&
14113 (effFixed == 1)) {
14114 const xmlChar *rEffValue = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014115
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014116 xmlSchemaGetEffectiveValueConstraint(bcur,
14117 &effFixed, &rEffValue, NULL);
14118 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014119 * 2.1.3.2 R's �effective value constraint� is
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014120 * fixed with the same string as B's.
14121 * MAYBE TODO: Compare the computed values.
14122 * Hmm, it says "same string" so
14123 * string-equality might really be sufficient.
14124 */
14125 if ((effFixed == 0) ||
14126 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14127 {
14128 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014129
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014130 xmlSchemaPAttrUseErr4(pctxt,
14131 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14132 WXS_ITEM_NODE(item), item, cur,
14133 "The effective value constraint of the "
14134 "attribute use is inconsistent with "
14135 "its correspondent in the %s %s",
14136 WXS_ACTION_STR(action),
14137 xmlSchemaGetComponentDesignation(&str,
14138 baseItem),
14139 NULL, NULL);
14140 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014141 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014142 }
14143 }
14144 }
14145 break;
14146 }
14147 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014148not_found:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014149 if (!found) {
14150 /*
14151 * (2.2) "otherwise the {base type definition} must have an
14152 * {attribute wildcard} and the {target namespace} of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000014153 * R's {attribute declaration} must be �valid� with respect
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014154 * to that wildcard, as defined in Wildcard allows Namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000014155 * Name (�3.10.4)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014156 */
14157 if ((baseWild == NULL) ||
14158 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14159 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14160 {
14161 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014162
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014163 xmlSchemaPAttrUseErr4(pctxt,
14164 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14165 WXS_ITEM_NODE(item), item, cur,
14166 "Neither a matching attribute use, "
14167 "nor a matching wildcard exists in the %s %s",
14168 WXS_ACTION_STR(action),
14169 xmlSchemaGetComponentDesignation(&str, baseItem),
14170 NULL, NULL);
14171 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014172 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014173 }
14174 }
14175 }
14176 }
14177 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000014178 * SPEC derivation-ok-restriction (3):
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014179 * (3) "For each attribute use in the {attribute uses} of the {base type
14180 * definition} whose {required} is true, there must be an attribute
14181 * use with an {attribute declaration} with the same {name} and
14182 * {target namespace} as its {attribute declaration} in the {attribute
14183 * uses} of the complex type definition itself whose {required} is true.
14184 */
14185 if (baseUses != NULL) {
14186 for (j = 0; j < baseUses->nbItems; j++) {
14187 bcur = baseUses->items[j];
14188 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14189 continue;
14190 found = 0;
14191 if (uses != NULL) {
14192 for (i = 0; i < uses->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014193 cur = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014194 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14195 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14196 (WXS_ATTRUSE_DECL_TNS(cur) ==
14197 WXS_ATTRUSE_DECL_TNS(bcur))) {
14198 found = 1;
14199 break;
14200 }
14201 }
14202 }
14203 if (!found) {
14204 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014205
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014206 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14207 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14208 NULL, item,
14209 "A matching attribute use for the "
14210 "'required' %s of the %s %s is missing",
14211 xmlSchemaGetComponentDesignation(&strA, bcur),
14212 WXS_ACTION_STR(action),
14213 xmlSchemaGetComponentDesignation(&strB, baseItem),
14214 NULL);
14215 FREE_AND_NULL(strA);
14216 FREE_AND_NULL(strB);
14217 }
14218 }
14219 }
14220 /*
14221 * derivation-ok-restriction (4)
14222 */
14223 if (wild != NULL) {
14224 /*
14225 * (4) "If there is an {attribute wildcard}, all of the
14226 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000014227 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014228 if (baseWild == NULL) {
14229 xmlChar *str = NULL;
14230
14231 /*
14232 * (4.1) "The {base type definition} must also have one."
Daniel Veillarddee23482008-04-11 12:58:43 +000014233 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014234 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14235 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14236 NULL, item,
14237 "The %s has an attribute wildcard, "
14238 "but the %s %s '%s' does not have one",
Daniel Veillarddee23482008-04-11 12:58:43 +000014239 WXS_ITEM_TYPE_NAME(item),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014240 WXS_ACTION_STR(action),
14241 WXS_ITEM_TYPE_NAME(baseItem),
14242 xmlSchemaGetComponentQName(&str, baseItem));
14243 FREE_AND_NULL(str);
14244 return(pctxt->err);
14245 } else if ((baseWild->any == 0) &&
14246 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14247 {
14248 xmlChar *str = NULL;
14249 /*
14250 * (4.2) "The complex type definition's {attribute wildcard}'s
14251 * {namespace constraint} must be a subset of the {base type
14252 * definition}'s {attribute wildcard}'s {namespace constraint},
Rob Richardsc6947bb2008-06-29 15:04:41 +000014253 * as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014254 */
14255 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14256 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14257 NULL, item,
14258 "The attribute wildcard is not a valid "
14259 "subset of the wildcard in the %s %s '%s'",
14260 WXS_ACTION_STR(action),
14261 WXS_ITEM_TYPE_NAME(baseItem),
14262 xmlSchemaGetComponentQName(&str, baseItem),
14263 NULL);
14264 FREE_AND_NULL(str);
14265 return(pctxt->err);
14266 }
Rob Richardsc6947bb2008-06-29 15:04:41 +000014267 /* 4.3 Unless the {base type definition} is the �ur-type
14268 * definition�, the complex type definition's {attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014269 * wildcard}'s {process contents} must be identical to or
14270 * stronger than the {base type definition}'s {attribute
14271 * wildcard}'s {process contents}, where strict is stronger
14272 * than lax is stronger than skip.
14273 */
14274 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14275 (wild->processContents < baseWild->processContents)) {
14276 xmlChar *str = NULL;
14277 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14278 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14279 NULL, baseItem,
14280 "The {process contents} of the attribute wildcard is "
14281 "weaker than the one in the %s %s '%s'",
14282 WXS_ACTION_STR(action),
14283 WXS_ITEM_TYPE_NAME(baseItem),
14284 xmlSchemaGetComponentQName(&str, baseItem),
14285 NULL);
14286 FREE_AND_NULL(str)
14287 return(pctxt->err);
14288 }
14289 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014290 return(0);
14291}
14292
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014293
14294static int
14295xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14296 xmlSchemaBasicItemPtr item,
14297 xmlSchemaWildcardPtr *completeWild,
14298 xmlSchemaItemListPtr list,
14299 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014300/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014301 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014302 * @ctxt: the schema parser context
14303 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014304 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014305 *
14306 * Builds the wildcard and the attribute uses on the given complex type.
14307 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014308 *
14309 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14310 * strings, so recheck this if we start to hardcode some schemata, since
14311 * they might not be in the same dict.
14312 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014313 */
14314static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014315xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014316 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014317{
14318 xmlSchemaTypePtr baseType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014319 xmlSchemaAttributeUsePtr use;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014320 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014321
Daniel Veillard01fa6152004-06-29 17:04:39 +000014322 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014323 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014324 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014325 return (-1);
14326 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014327 baseType = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014328 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014329 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014330 return(-1);
14331
14332 uses = type->attrUses;
14333 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014334 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014335 * Expand attribute group references. And build the 'complete'
14336 * wildcard, i.e. intersect multiple wildcards.
14337 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014338 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014339 if (uses != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014340 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014341 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014342 * This one will transfer all attr. prohibitions
14343 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014344 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014345 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14346 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14347 pctxt->attrProhibs) == -1)
14348 {
14349 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14350 "failed to expand attributes");
14351 }
14352 if (pctxt->attrProhibs->nbItems != 0)
14353 prohibs = pctxt->attrProhibs;
14354 } else {
14355 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14356 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14357 NULL) == -1)
14358 {
14359 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14360 "failed to expand attributes");
14361 }
14362 }
14363 }
14364 /*
14365 * Inherit the attribute uses of the base type.
14366 */
14367 if (baseUses != NULL) {
14368 int i, j;
14369 xmlSchemaAttributeUseProhibPtr pro;
14370
14371 if (WXS_IS_RESTRICTION(type)) {
14372 int usesCount;
Daniel Veillarddee23482008-04-11 12:58:43 +000014373 xmlSchemaAttributeUsePtr tmp;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014374
14375 if (uses != NULL)
14376 usesCount = uses->nbItems;
14377 else
14378 usesCount = 0;
14379
14380 /* Restriction. */
14381 for (i = 0; i < baseUses->nbItems; i++) {
14382 use = baseUses->items[i];
14383 if (prohibs) {
14384 /*
14385 * Filter out prohibited uses.
14386 */
14387 for (j = 0; j < prohibs->nbItems; j++) {
14388 pro = prohibs->items[j];
14389 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14390 (WXS_ATTRUSE_DECL_TNS(use) ==
14391 pro->targetNamespace))
14392 {
14393 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014394 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014395 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014396 }
14397 if (usesCount) {
14398 /*
14399 * Filter out existing uses.
14400 */
14401 for (j = 0; j < usesCount; j++) {
14402 tmp = uses->items[j];
14403 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14404 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14405 (WXS_ATTRUSE_DECL_TNS(use) ==
14406 WXS_ATTRUSE_DECL_TNS(tmp)))
14407 {
14408 goto inherit_next;
14409 }
14410 }
14411 }
14412 if (uses == NULL) {
14413 type->attrUses = xmlSchemaItemListCreate();
14414 if (type->attrUses == NULL)
14415 goto exit_failure;
14416 uses = type->attrUses;
14417 }
14418 xmlSchemaItemListAddSize(uses, 2, use);
14419inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014420 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014421 } else {
14422 /* Extension. */
Daniel Veillarddee23482008-04-11 12:58:43 +000014423 for (i = 0; i < baseUses->nbItems; i++) {
14424 use = baseUses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014425 if (uses == NULL) {
14426 type->attrUses = xmlSchemaItemListCreate();
14427 if (type->attrUses == NULL)
14428 goto exit_failure;
14429 uses = type->attrUses;
14430 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014431 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014432 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014433 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014434 }
14435 /*
14436 * Shrink attr. uses.
14437 */
14438 if (uses) {
14439 if (uses->nbItems == 0) {
14440 xmlSchemaItemListFree(uses);
14441 type->attrUses = NULL;
14442 }
14443 /*
14444 * TODO: We could shrink the size of the array
14445 * to fit the actual number of items.
14446 */
14447 }
14448 /*
14449 * Compute the complete wildcard.
14450 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014451 if (WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014452 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014453 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014454 * (3.2.2.1) "If the �base wildcard� is non-�absent�, then
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014455 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014456 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014457 if (type->attributeWildcard != NULL) {
14458 /*
14459 * Union the complete wildcard with the base wildcard.
14460 * SPEC {attribute wildcard}
14461 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
Rob Richardsc6947bb2008-06-29 15:04:41 +000014462 * and {annotation} are those of the �complete wildcard�,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014463 * and whose {namespace constraint} is the intensional union
Rob Richardsc6947bb2008-06-29 15:04:41 +000014464 * of the {namespace constraint} of the �complete wildcard�
14465 * and of the �base wildcard�, as defined in Attribute
14466 * Wildcard Union (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014467 */
14468 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14469 baseType->attributeWildcard) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000014470 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014471 } else {
14472 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014473 * (3.2.2.1.1) "If the �complete wildcard� is �absent�,
14474 * then the �base wildcard�."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014475 */
14476 type->attributeWildcard = baseType->attributeWildcard;
Daniel Veillarddee23482008-04-11 12:58:43 +000014477 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014478 } else {
14479 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014480 * (3.2.2.2) "otherwise (the �base wildcard� is �absent�) the
14481 * �complete wildcard"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014482 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014483 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014484 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014485 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014486 /*
14487 * SPEC {attribute wildcard}
14488 * (3.1) "If the <restriction> alternative is chosen, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000014489 * �complete wildcard�;"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014490 * NOOP
14491 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014492 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014493
Daniel Veillard3646d642004-06-02 19:19:14 +000014494 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014495
14496exit_failure:
14497 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014498}
14499
14500/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014501 * xmlSchemaTypeFinalContains:
14502 * @schema: the schema
14503 * @type: the type definition
14504 * @final: the final
14505 *
14506 * Evaluates if a type definition contains the given "final".
14507 * This does take "finalDefault" into account as well.
14508 *
14509 * Returns 1 if the type does containt the given "final",
14510 * 0 otherwise.
14511 */
14512static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014513xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014514{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014515 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014516 return (0);
14517 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014518 return (1);
14519 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014520 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014521}
14522
14523/**
14524 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14525 * @type: the Union Simple Type
14526 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014527 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014528 * returns NULL otherwise.
14529 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014530static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014531xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14532{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014533 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014534 if (type->memberTypes != NULL)
14535 return (type->memberTypes);
14536 else
14537 type = type->baseType;
14538 }
14539 return (NULL);
14540}
14541
14542/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014543 * xmlSchemaGetParticleTotalRangeMin:
14544 * @particle: the particle
14545 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014546 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014547 * (all and sequence) + (choice)
14548 *
14549 * Returns the minimun Effective Total Range.
14550 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014551static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014552xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014553{
14554 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014555 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014556 return (0);
14557 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014558 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014559 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014560 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014561
14562 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014563 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014564 while (part != NULL) {
14565 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14566 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014567 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014568 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014569 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014570 if (cur == 0)
14571 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014572 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014573 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014574 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014575 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014576 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014577 } else {
14578 /* <all> and <sequence> */
14579 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014580 xmlSchemaParticlePtr part =
14581 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014582
14583 if (part == NULL)
14584 return (0);
14585 do {
14586 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14587 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014588 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014589 else
14590 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014591 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014592 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014593 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014594 }
14595}
14596
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014597/**
14598 * xmlSchemaGetParticleTotalRangeMax:
14599 * @particle: the particle
14600 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014601 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014602 * (all and sequence) + (choice)
14603 *
14604 * Returns the maximum Effective Total Range.
14605 */
14606static int
14607xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14608{
14609 if ((particle->children == NULL) ||
14610 (particle->children->children == NULL))
14611 return (0);
14612 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14613 int max = -1, cur;
14614 xmlSchemaParticlePtr part =
14615 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014616
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014617 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14618 if (part->children == NULL)
14619 continue;
14620 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14621 (part->children->type == XML_SCHEMA_TYPE_ANY))
14622 cur = part->maxOccurs;
14623 else
14624 cur = xmlSchemaGetParticleTotalRangeMax(part);
14625 if (cur == UNBOUNDED)
14626 return (UNBOUNDED);
14627 if ((max < cur) || (max == -1))
14628 max = cur;
14629 }
14630 /* TODO: Handle overflows? */
14631 return (particle->maxOccurs * max);
14632 } else {
14633 /* <all> and <sequence> */
14634 int sum = 0, cur;
14635 xmlSchemaParticlePtr part =
14636 (xmlSchemaParticlePtr) particle->children->children;
14637
14638 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14639 if (part->children == NULL)
14640 continue;
14641 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14642 (part->children->type == XML_SCHEMA_TYPE_ANY))
14643 cur = part->maxOccurs;
14644 else
14645 cur = xmlSchemaGetParticleTotalRangeMax(part);
14646 if (cur == UNBOUNDED)
14647 return (UNBOUNDED);
14648 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14649 return (UNBOUNDED);
14650 sum += cur;
14651 }
14652 /* TODO: Handle overflows? */
14653 return (particle->maxOccurs * sum);
14654 }
14655}
14656
14657/**
14658 * xmlSchemaIsParticleEmptiable:
14659 * @particle: the particle
14660 *
14661 * Schema Component Constraint: Particle Emptiable
14662 * Checks whether the given particle is emptiable.
14663 *
14664 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014665 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014666static int
14667xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14668{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014669 /*
14670 * SPEC (1) "Its {min occurs} is 0."
14671 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014672 if ((particle == NULL) || (particle->minOccurs == 0) ||
14673 (particle->children == NULL))
14674 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014675 /*
14676 * SPEC (2) "Its {term} is a group and the minimum part of the
14677 * effective total range of that group, [...] is 0."
14678 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014679 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014680 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014681 return (1);
14682 }
14683 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014684}
14685
14686/**
14687 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014688 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014689 * @type: the derived simple type definition
14690 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014691 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014692 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014693 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014694 * Type Derivation OK (Simple) (cos-st-derived-OK)
14695 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014696 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014697 * derived from @baseType.
14698 *
14699 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014700 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014701static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014702xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014703 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014704 xmlSchemaTypePtr baseType,
14705 int subset)
14706{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014707 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014708 * 1 They are the same type definition.
14709 * TODO: The identy check might have to be more complex than this.
14710 */
14711 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014712 return (0);
14713 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014714 * 2.1 restriction is not in the subset, or in the {final}
14715 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014716 *
14717 * NOTE that this will be used also via "xsi:type".
14718 *
14719 * TODO: Revise this, it looks strange. How can the "type"
14720 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014721 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014722 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014723 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014724 return(-1);
14725 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014726 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014727 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014728 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014729 (xmlSchemaTypeFinalContains(type->baseType,
14730 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14731 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014732 }
14733 /* 2.2 */
14734 if (type->baseType == baseType) {
14735 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014736 * 2.2.1 D's �base type definition� is B.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014737 */
14738 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014739 }
14740 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014741 * 2.2.2 D's �base type definition� is not the �ur-type definition�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014742 * and is validly derived from B given the subset, as defined by this
14743 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014744 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014745 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014746 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014747 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014748 return (0);
14749 }
14750 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014751 * 2.2.3 D's {variety} is list or union and B is the �simple ur-type
14752 * definition�.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014753 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014754 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14755 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014756 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014757 }
14758 /*
14759 * 2.2.4 B's {variety} is union and D is validly derived from a type
14760 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014761 * defined by this constraint.
14762 *
14763 * NOTE: This seems not to involve built-in types, since there is no
14764 * built-in Union Simple Type.
14765 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014766 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014767 xmlSchemaTypeLinkPtr cur;
14768
14769 cur = baseType->memberTypes;
14770 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014771 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014772 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014773 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014774 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014775 type, cur->type, subset) == 0)
14776 {
14777 /*
14778 * It just has to be validly derived from at least one
14779 * member-type.
14780 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014781 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014782 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014783 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014784 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014785 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014786 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14787}
14788
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014789/**
14790 * xmlSchemaCheckTypeDefCircularInternal:
14791 * @pctxt: the schema parser context
14792 * @ctxtType: the type definition
14793 * @ancestor: an ancestor of @ctxtType
14794 *
14795 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014796 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014797 *
14798 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14799 * circular, 0 otherwise.
14800 */
14801static int
14802xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14803 xmlSchemaTypePtr ctxtType,
14804 xmlSchemaTypePtr ancestor)
14805{
14806 int ret;
14807
14808 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14809 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014810
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014811 if (ctxtType == ancestor) {
14812 xmlSchemaPCustomErr(pctxt,
14813 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014814 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014815 "The definition is circular", NULL);
14816 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14817 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014818 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14819 /*
14820 * Avoid inifinite recursion on circular types not yet checked.
14821 */
14822 return (0);
14823 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014824 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14825 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14826 ancestor->baseType);
14827 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14828 return (ret);
14829}
14830
14831/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014832 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014833 * @item: the complex/simple type definition
14834 * @ctxt: the parser context
14835 * @name: the name
14836 *
14837 * Checks for circular type definitions.
14838 */
14839static void
14840xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014841 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014842{
14843 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014844 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14845 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014846 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014847 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14848 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014849}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014850
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014851/*
14852* Simple Type Definition Representation OK (src-simple-type) 4
14853*
14854* "4 Circular union type definition is disallowed. That is, if the
14855* <union> alternative is chosen, there must not be any entries in the
14856* memberTypes [attribute] at any depth which resolve to the component
14857* corresponding to the <simpleType>."
14858*
14859* Note that this should work on the *representation* of a component,
14860* thus assumes any union types in the member types not being yet
14861* substituted. At this stage we need the variety of the types
14862* to be already computed.
14863*/
14864static int
14865xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14866 xmlSchemaTypePtr ctxType,
14867 xmlSchemaTypeLinkPtr members)
Daniel Veillarddee23482008-04-11 12:58:43 +000014868{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014869 xmlSchemaTypeLinkPtr member;
14870 xmlSchemaTypePtr memberType;
Daniel Veillarddee23482008-04-11 12:58:43 +000014871
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014872 member = members;
14873 while (member != NULL) {
14874 memberType = member->type;
14875 while ((memberType != NULL) &&
14876 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14877 if (memberType == ctxType) {
14878 xmlSchemaPCustomErr(pctxt,
14879 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014880 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014881 "The union type definition is circular", NULL);
14882 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14883 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014884 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014885 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14886 {
14887 int res;
14888 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14889 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14890 ctxType,
14891 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14892 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14893 if (res != 0)
14894 return(res);
14895 }
14896 memberType = memberType->baseType;
14897 }
14898 member = member->next;
14899 }
14900 return(0);
14901}
14902
14903static int
14904xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14905 xmlSchemaTypePtr type)
14906{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014907 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014908 return(0);
14909 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14910 type->memberTypes));
14911}
14912
Daniel Veillard01fa6152004-06-29 17:04:39 +000014913/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014914 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014915 * @item: the complex/simple type definition
14916 * @ctxt: the parser context
14917 * @name: the name
14918 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014919 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014920 */
14921static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014922xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014923 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014924{
14925 if (typeDef == NULL)
14926 return;
14927
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014928 /*
14929 * Resolve the base type.
14930 */
14931 if (typeDef->baseType == NULL) {
14932 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14933 typeDef->base, typeDef->baseNs);
14934 if (typeDef->baseType == NULL) {
14935 xmlSchemaPResCompAttrErr(ctxt,
14936 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014937 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014938 "base", typeDef->base, typeDef->baseNs,
14939 XML_SCHEMA_TYPE_SIMPLE, NULL);
14940 return;
14941 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014942 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014943 if (WXS_IS_SIMPLE(typeDef)) {
14944 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014945 /*
14946 * Resolve the memberTypes.
14947 */
14948 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14949 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014950 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014951 /*
14952 * Resolve the itemType.
14953 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014954 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14955
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014956 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014957 typeDef->base, typeDef->baseNs);
14958
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014959 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014960 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14961 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014962 typeDef->subtypes = NULL;
14963 xmlSchemaPResCompAttrErr(ctxt,
14964 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014965 WXS_BASIC_CAST typeDef, typeDef->node,
14966 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014967 XML_SCHEMA_TYPE_SIMPLE, NULL);
14968 }
14969 }
14970 return;
14971 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014972 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014973 /*
14974 * The ball of letters below means, that if we have a particle
14975 * which has a QName-helper component as its {term}, we want
14976 * to resolve it...
14977 */
14978 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14979 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14980 XML_SCHEMA_TYPE_PARTICLE) &&
14981 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14982 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14983 XML_SCHEMA_EXTRA_QNAMEREF))
14984 {
14985 xmlSchemaQNameRefPtr ref =
14986 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14987 xmlSchemaModelGroupDefPtr groupDef;
14988
14989 /*
14990 * URGENT TODO: Test this.
14991 */
14992 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14993 /*
14994 * Resolve the MG definition reference.
14995 */
14996 groupDef =
14997 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14998 ref->itemType, ref->name, ref->targetNamespace);
14999 if (groupDef == NULL) {
15000 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
15001 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
15002 "ref", ref->name, ref->targetNamespace, ref->itemType,
15003 NULL);
15004 /* Remove the particle. */
15005 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15006 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
15007 /* Remove the particle. */
15008 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15009 else {
15010 /*
15011 * Assign the MG definition's {model group} to the
15012 * particle's {term}.
15013 */
15014 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
Daniel Veillarddee23482008-04-11 12:58:43 +000015015
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015016 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
15017 /*
15018 * SPEC cos-all-limited (1.2)
15019 * "1.2 the {term} property of a particle with
15020 * {max occurs}=1 which is part of a pair which constitutes
15021 * the {content type} of a complex type definition."
15022 */
15023 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
15024 xmlSchemaCustomErr(ACTXT_CAST ctxt,
15025 /* TODO: error code */
15026 XML_SCHEMAP_COS_ALL_LIMITED,
15027 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
15028 "The particle's {max occurs} must be 1, since the "
15029 "reference resolves to an 'all' model group",
15030 NULL, NULL);
15031 }
15032 }
15033 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015034 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015035}
15036
15037
15038
15039/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015040 * xmlSchemaCheckSTPropsCorrect:
15041 * @ctxt: the schema parser context
15042 * @type: the simple type definition
15043 *
15044 * Checks st-props-correct.
15045 *
15046 * Returns 0 if the properties are correct,
15047 * if not, a positive error code and -1 on internal
15048 * errors.
15049 */
15050static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015051xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015052 xmlSchemaTypePtr type)
15053{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015054 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015055 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015056
Daniel Veillardc0826a72004-08-10 14:17:33 +000015057 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015058 /*
15059 * Schema Component Constraint: Simple Type Definition Properties Correct
15060 *
15061 * NOTE: This is somehow redundant, since we actually built a simple type
15062 * to have all the needed information; this acts as an self test.
15063 */
Rob Richardsc6947bb2008-06-29 15:04:41 +000015064 /* Base type: If the datatype has been �derived� by �restriction�
15065 * then the Simple Type Definition component from which it is �derived�,
15066 * otherwise the Simple Type Definition for anySimpleType (�4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015067 */
15068 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015069 /*
15070 * TODO: Think about: "modulo the impact of Missing
Rob Richardsc6947bb2008-06-29 15:04:41 +000015071 * Sub-components (�5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015072 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015073 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015074 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015075 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015076 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015077 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015078
Daniel Veillard01fa6152004-06-29 17:04:39 +000015079 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015080 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015081 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015082 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015083 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015084 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015085 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015086 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015087 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15088 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015089 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015090 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015091 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015092 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015093 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015094 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015095 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015096 "the simple ur-type definition as base type, not '%s'",
15097 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015098 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015099 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15100 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015101 /*
15102 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015103 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015104 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15105 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015106 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015107 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015108 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015109 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015110 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15111 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015112 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015113
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015114 /*
15115 * 3 The {final} of the {base type definition} must not contain restriction.
15116 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015117 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015118 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15119 xmlSchemaPCustomErr(ctxt,
15120 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015121 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015122 "The 'final' of its base type '%s' must not contain "
15123 "'restriction'",
15124 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015125 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015126 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015127 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015128
15129 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015130 * 2 All simple type definitions must be derived ultimately from the �simple
15131 * ur-type definition (so� circular definitions are disallowed). That is, it
15132 * must be possible to reach a built-in primitive datatype or the �simple
15133 * ur-type definition� by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015134 *
15135 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015136 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015137 return (0);
15138}
15139
15140/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015141 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015142 * @ctxt: the schema parser context
15143 * @type: the simple type definition
15144 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015145 * Schema Component Constraint:
15146 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15147
15148 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015149 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015150 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015151 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015152 * a positive error code otherwise.
15153 */
15154static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015155xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015156 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015157{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015158 xmlChar *str = NULL;
15159
Daniel Veillard01fa6152004-06-29 17:04:39 +000015160 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015161 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15162 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015163 return (-1);
15164 }
15165
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015166 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015167 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015168 /*
15169 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015170 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015171 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015172 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015173 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015174 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015175 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015176 "The base type '%s' is not an atomic simple type",
15177 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015178 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015179 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15180 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015181 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 * restriction.
15183 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015184 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015185 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015186 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015187 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015188 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015189 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015190 "The final of its base type '%s' must not contain 'restriction'",
15191 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015192 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015193 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15194 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015195
15196 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015197 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015198 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015199 * Primitive datatypes.
15200 */
15201 if (type->facets != NULL) {
15202 xmlSchemaFacetPtr facet;
15203 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015204
Daniel Veillard01fa6152004-06-29 17:04:39 +000015205 primitive = xmlSchemaGetPrimitiveType(type);
15206 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015207 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15208 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015209 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015210 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015211 facet = type->facets;
15212 do {
15213 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015214 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015215 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015216 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015217 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015218 }
15219 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015220 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015221 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015222 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015223 }
15224 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015225 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15226 * of the {base type definition} (call this BF),then the DF's {value}
15227 * must be a valid restriction of BF's {value} as defined in
15228 * [XML Schemas: Datatypes]."
15229 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015230 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015231 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015232 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015233 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015234 xmlSchemaTypePtr itemType = NULL;
15235
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015236 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015237 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015238 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15239 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015240 return (-1);
15241 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015242 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015243 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015244 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015245 * 2.1 The {item type definition} must have a {variety} of atomic or
15246 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015247 * must be atomic).
15248 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015249 if ((! WXS_IS_ATOMIC(itemType)) &&
15250 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015251 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015252 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015253 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015254 "The item type '%s' does not have a variety of atomic or union",
15255 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015256 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015257 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015258 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015259 xmlSchemaTypeLinkPtr member;
15260
15261 member = itemType->memberTypes;
15262 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015263 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015264 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015265 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015266 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015267 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015268 "member type '%s' of this item type is not atomic",
15269 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015270 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015271 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15272 }
15273 member = member->next;
15274 }
15275 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015277 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015278 xmlSchemaFacetPtr facet;
15279 /*
15280 * This is the case if we have: <simpleType><list ..
15281 */
15282 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015283 * 2.3.1
15284 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015285 * contain list.
15286 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 if (xmlSchemaTypeFinalContains(itemType,
15288 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15289 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015290 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015291 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015292 "The final of its item type '%s' must not contain 'list'",
15293 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015294 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015295 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15296 }
15297 /*
15298 * 2.3.1.2 The {facets} must only contain the whiteSpace
15299 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015300 * OPTIMIZE TODO: the S4S already disallows any facet
15301 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015302 */
15303 if (type->facets != NULL) {
15304 facet = type->facets;
15305 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015306 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015307 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015308 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015309 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015310 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15311 }
15312 facet = facet->next;
15313 } while (facet != NULL);
15314 }
15315 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015316 * MAYBE TODO: (Hmm, not really) Datatypes states:
Rob Richardsc6947bb2008-06-29 15:04:41 +000015317 * A �list� datatype can be �derived� from an �atomic� datatype
15318 * whose �lexical space� allows space (such as string or anyURI)or
15319 * a �union� datatype any of whose {member type definitions}'s
15320 * �lexical space� allows space.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015321 */
15322 } else {
15323 /*
15324 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015325 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015326 */
15327 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015328 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015329 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15330 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015331 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015332 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015333 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015334 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015335 "The base type '%s' must be a list type",
15336 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015337 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015338 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15339 }
15340 /*
15341 * 2.3.2.2 The {final} of the {base type definition} must not
15342 * contain restriction.
15343 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015344 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015345 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015346 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015347 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015348 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015349 "The 'final' of the base type '%s' must not contain 'restriction'",
15350 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015351 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015352 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15353 }
15354 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015355 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015356 * from the {base type definition}'s {item type definition} given
Rob Richardsc6947bb2008-06-29 15:04:41 +000015357 * the empty set, as defined in Type Derivation OK (Simple) (�3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015358 */
15359 {
15360 xmlSchemaTypePtr baseItemType;
15361
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015362 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015363 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015364 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15365 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015366 return (-1);
15367 }
15368 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015369 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015370 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015371 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015372 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015373 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015374 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015375 "The item type '%s' is not validly derived from "
15376 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015377 xmlSchemaGetComponentQName(&str, itemType),
15378 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15379 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015380
15381 FREE_AND_NULL(str)
15382 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015383 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015384 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15385 }
15386 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015387
Daniel Veillard01fa6152004-06-29 17:04:39 +000015388 if (type->facets != NULL) {
15389 xmlSchemaFacetPtr facet;
15390 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015391 /*
15392 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015393 * and enumeration facet components are allowed among the {facets}.
15394 */
15395 facet = type->facets;
15396 do {
15397 switch (facet->type) {
15398 case XML_SCHEMA_FACET_LENGTH:
15399 case XML_SCHEMA_FACET_MINLENGTH:
15400 case XML_SCHEMA_FACET_MAXLENGTH:
15401 case XML_SCHEMA_FACET_WHITESPACE:
15402 /*
15403 * TODO: 2.5.1.2 List datatypes
Rob Richardsc6947bb2008-06-29 15:04:41 +000015404 * The value of �whiteSpace� is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015405 */
15406 case XML_SCHEMA_FACET_PATTERN:
15407 case XML_SCHEMA_FACET_ENUMERATION:
15408 break;
15409 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015410 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015411 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015412 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015413 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015414 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015415 * invalid facets.
15416 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015417 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015418 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015419 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015420 facet = facet->next;
15421 } while (facet != NULL);
15422 if (ok == 0)
15423 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15424 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015425 * SPEC (2.3.2.5) (same as 1.3.2)
15426 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015427 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015428 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015429 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015430 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015431 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015432 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015433 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015434 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015435 * atomic or list.
15436 */
15437 xmlSchemaTypeLinkPtr member;
15438
15439 member = type->memberTypes;
15440 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015441 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015442 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015443
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015444 if ((! WXS_IS_ATOMIC(member->type)) &&
15445 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015446 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015447 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015448 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015449 "The member type '%s' is neither an atomic, nor a list type",
15450 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015451 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15453 }
15454 member = member->next;
15455 }
15456 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015457 * 3.3.1 If the {base type definition} is the �simple ur-type
15458 * definition�
Daniel Veillard01fa6152004-06-29 17:04:39 +000015459 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015460 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015461 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015462 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015463 * {final} which does not contain union.
15464 */
15465 member = type->memberTypes;
15466 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015467 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015468 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015469 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015470 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015471 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015472 "The 'final' of member type '%s' contains 'union'",
15473 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015474 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015475 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15476 }
15477 member = member->next;
15478 }
15479 /*
15480 * 3.3.1.2 The {facets} must be empty.
15481 */
15482 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015483 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015484 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015485 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015486 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015487 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15488 }
15489 } else {
15490 /*
15491 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015492 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015493 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015494 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015495 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015496 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015497 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015498 "The base type '%s' is not a union type",
15499 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015500 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015501 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15502 }
15503 /*
15504 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15505 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015506 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015507 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015508 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015509 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015510 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015511 "The 'final' of its base type '%s' must not contain 'restriction'",
15512 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015513 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015514 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15515 }
15516 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517 * 3.3.2.3 The {member type definitions}, in order, must be validly
15518 * derived from the corresponding type definitions in the {base
15519 * type definition}'s {member type definitions} given the empty set,
Rob Richardsc6947bb2008-06-29 15:04:41 +000015520 * as defined in Type Derivation OK (Simple) (�3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015521 */
15522 {
15523 xmlSchemaTypeLinkPtr baseMember;
15524
15525 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015526 * OPTIMIZE: if the type is restricting, it has no local defined
15527 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015528 * thus a check for equality can be skipped.
15529 */
15530 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015531 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015532 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015533 * types of it's base type. This check seems not necessary with
15534 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015535 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015536 */
15537 if (type->memberTypes != NULL) {
15538 member = type->memberTypes;
15539 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015540 if ((member == NULL) && (baseMember != NULL)) {
15541 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15542 "different number of member types in base");
15543 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015544 while (member != NULL) {
15545 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015546 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15547 "different number of member types in base");
Daniel Veillard14b56432006-03-09 18:41:40 +000015548 } else if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015549 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015550 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015551 xmlChar *strBMT = NULL, *strBT = NULL;
15552
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015553 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015554 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015555 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015556 "The member type %s is not validly "
15557 "derived from its corresponding member "
15558 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015559 xmlSchemaGetComponentQName(&str, member->type),
15560 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15561 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015562 FREE_AND_NULL(str)
15563 FREE_AND_NULL(strBMT)
15564 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015565 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015566 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015567 member = member->next;
15568 baseMember = baseMember->next;
15569 }
15570 }
15571 }
15572 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015573 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015574 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015575 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015576 if (type->facets != NULL) {
15577 xmlSchemaFacetPtr facet;
15578 int ok = 1;
15579
15580 facet = type->facets;
15581 do {
15582 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15583 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015584 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015585 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015586 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015587 ok = 0;
15588 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015589 facet = facet->next;
15590 } while (facet != NULL);
15591 if (ok == 0)
15592 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015593
Daniel Veillard01fa6152004-06-29 17:04:39 +000015594 }
15595 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015596 * SPEC (3.3.2.5) (same as 1.3.2)
15597 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015598 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015599 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015600 */
15601 }
15602 }
15603
15604 return (0);
15605}
15606
15607/**
15608 * xmlSchemaCheckSRCSimpleType:
15609 * @ctxt: the schema parser context
15610 * @type: the simple type definition
15611 *
Daniel Veillarddee23482008-04-11 12:58:43 +000015612 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015613 *
15614 * Returns 0 if the constraints are satisfied,
15615 * if not a positive error code and -1 on internal
15616 * errors.
15617 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015618#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015619static int
15620xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15621 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015622{
15623 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015624 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015625 * must satisfy the conditions set out in Constraints on Simple Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000015626 * Definition Schema Components (�3.14.6).
Daniel Veillarddee23482008-04-11 12:58:43 +000015627 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015628 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015629 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015630 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015631 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015632 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015633 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015634 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015635 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000015636 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015637 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015638 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015639 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015640 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015641 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015642 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015643 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015644 */
Daniel Veillarddee23482008-04-11 12:58:43 +000015645 } else if (WXS_IS_UNION(type)) {
15646 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015647 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015648 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015649 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015650 return (0);
15651}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015652#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015653
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015654static int
15655xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15656{
15657 if (ctxt->vctxt == NULL) {
15658 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15659 if (ctxt->vctxt == NULL) {
15660 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015661 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015662 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015663 "failed to create a temp. validation context.\n",
15664 NULL, NULL);
15665 return (-1);
15666 }
15667 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015668 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015669 ctxt->error, ctxt->warning, ctxt->errCtxt);
15670 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15671 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015672 }
15673 return (0);
15674}
15675
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015676static int
15677xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15678 xmlNodePtr node,
15679 xmlSchemaTypePtr type,
15680 const xmlChar *value,
15681 xmlSchemaValPtr *retVal,
15682 int fireErrors,
15683 int normalize,
15684 int isNormalized);
15685
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015686/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015687 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015688 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015689 * @type: the simple type definition
15690 * @value: the default value
15691 * @node: an optional node (the holder of the value)
15692 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015693 * Schema Component Constraint: Element Default Valid (Immediate)
15694 * (cos-valid-default)
15695 * This will be used by the parser only. For the validator there's
15696 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015697 *
15698 * Returns 0 if the constraints are satisfied,
15699 * if not, a positive error code and -1 on internal
15700 * errors.
15701 */
15702static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015703xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15704 xmlNodePtr node,
15705 xmlSchemaTypePtr type,
15706 const xmlChar *value,
15707 xmlSchemaValPtr *val)
15708{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015709 int ret = 0;
15710
15711 /*
15712 * cos-valid-default:
15713 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015714 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015715 * definition the appropriate case among the following must be true:
15716 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015717 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015718 /*
15719 * Complex type.
15720 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015721 * SPEC (2.1) "its {content type} must be a simple type definition
15722 * or mixed."
15723 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Rob Richardsc6947bb2008-06-29 15:04:41 +000015724 * type}'s particle must be �emptiable� as defined by
15725 * Particle Emptiable (�3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015726 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015727 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15728 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015729 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015730 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015731 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015732 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015733 "For a string to be a valid default, the type definition "
15734 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015735 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015736 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15737 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015738 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015739 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015740 * 1 If the type definition is a simple type definition, then the string
Rob Richardsc6947bb2008-06-29 15:04:41 +000015741 * must be �valid� with respect to that definition as defined by String
15742 * Valid (�3.14.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015743 *
15744 * AND
15745 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015746 * 2.2.1 If the {content type} is a simple type definition, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000015747 * string must be �valid� with respect to that simple type definition
15748 * as defined by String Valid (�3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015749 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015750 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015751 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015752 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015753 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015754 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015755 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015756 else
15757 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015758
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015759 if (ret < 0) {
15760 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15761 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015762 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015763
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015764 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015765}
15766
15767/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015768 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015769 * @ctxt: the schema parser context
15770 * @type: the complex type definition
15771 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015772 *.(4.6) Constraints on Complex Type Definition Schema Components
15773 * Schema Component Constraint:
15774 * Complex Type Definition Properties Correct (ct-props-correct)
15775 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015776 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015777 * Returns 0 if the constraints are satisfied, a positive
15778 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015779 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015780static int
15781xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15782 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015783{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015784 /*
15785 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15786 *
15787 * SPEC (1) "The values of the properties of a complex type definition must
15788 * be as described in the property tableau in The Complex Type Definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000015789 * Schema Component (�3.4.1), modulo the impact of Missing
15790 * Sub-components (�5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015791 */
15792 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015793 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015794 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015795 /*
15796 * SPEC (2) "If the {base type definition} is a simple type definition,
15797 * the {derivation method} must be extension."
15798 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015799 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015800 XML_SCHEMAP_SRC_CT_1,
Daniel Veillarddee23482008-04-11 12:58:43 +000015801 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015802 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015803 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015804 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015805 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015806 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015807 * SPEC (3) "Circular definitions are disallowed, except for the �ur-type
15808 * definition�. That is, it must be possible to reach the �ur-type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015809 * definition by repeatedly following the {base type definition}."
15810 *
15811 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015812 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015813 /*
15814 * NOTE that (4) and (5) need the following:
15815 * - attribute uses need to be already inherited (apply attr. prohibitions)
15816 * - attribute group references need to be expanded already
15817 * - simple types need to be typefixed already
Daniel Veillarddee23482008-04-11 12:58:43 +000015818 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015819 if (type->attrUses &&
15820 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15821 {
15822 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15823 xmlSchemaAttributeUsePtr use, tmp;
15824 int i, j, hasId = 0;
15825
15826 for (i = uses->nbItems -1; i >= 0; i--) {
15827 use = uses->items[i];
Daniel Veillarddee23482008-04-11 12:58:43 +000015828
15829 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015830 * SPEC ct-props-correct
15831 * (4) "Two distinct attribute declarations in the
15832 * {attribute uses} must not have identical {name}s and
15833 * {target namespace}s."
15834 */
15835 if (i > 0) {
15836 for (j = i -1; j >= 0; j--) {
15837 tmp = uses->items[j];
15838 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15839 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15840 (WXS_ATTRUSE_DECL_TNS(use) ==
15841 WXS_ATTRUSE_DECL_TNS(tmp)))
15842 {
15843 xmlChar *str = NULL;
15844
15845 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15846 XML_SCHEMAP_AG_PROPS_CORRECT,
15847 NULL, WXS_BASIC_CAST type,
15848 "Duplicate %s",
15849 xmlSchemaGetComponentDesignation(&str, use),
15850 NULL);
15851 FREE_AND_NULL(str);
15852 /*
15853 * Remove the duplicate.
15854 */
15855 if (xmlSchemaItemListRemove(uses, i) == -1)
15856 goto exit_failure;
15857 goto next_use;
15858 }
15859 }
15860 }
15861 /*
15862 * SPEC ct-props-correct
15863 * (5) "Two distinct attribute declarations in the
15864 * {attribute uses} must not have {type definition}s which
15865 * are or are derived from ID."
15866 */
15867 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15868 if (xmlSchemaIsDerivedFromBuiltInType(
15869 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000015870 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015871 if (hasId) {
15872 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000015873
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015874 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15875 XML_SCHEMAP_AG_PROPS_CORRECT,
15876 NULL, WXS_BASIC_CAST type,
15877 "There must not exist more than one attribute "
15878 "declaration of type 'xs:ID' "
15879 "(or derived from 'xs:ID'). The %s violates this "
15880 "constraint",
15881 xmlSchemaGetComponentDesignation(&str, use),
15882 NULL);
15883 FREE_AND_NULL(str);
15884 if (xmlSchemaItemListRemove(uses, i) == -1)
15885 goto exit_failure;
15886 }
Daniel Veillarddee23482008-04-11 12:58:43 +000015887
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015888 hasId = 1;
15889 }
15890 }
15891next_use: {}
15892 }
15893 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015894 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015895exit_failure:
15896 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015897}
15898
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015899static int
15900xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15901 xmlSchemaTypePtr typeB)
15902{
15903 /*
15904 * TODO: This should implement component-identity
15905 * in the future.
15906 */
15907 if ((typeA == NULL) || (typeB == NULL))
15908 return (0);
15909 return (typeA == typeB);
15910}
15911
15912/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015913 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015914 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015915 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015916 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015917 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015918 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015919 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015920 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15921 *
15922 * STATUS: completed
15923 *
15924 * Returns 0 if the constraints are satisfied, or 1
15925 * if not.
15926 */
15927static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015928xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015929 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015930 xmlSchemaTypePtr baseType,
15931 int set)
15932{
15933 int equal = xmlSchemaAreEqualTypes(type, baseType);
15934 /* TODO: Error codes. */
15935 /*
15936 * SPEC "For a complex type definition (call it D, for derived)
15937 * to be validly derived from a type definition (call this
15938 * B, for base) given a subset of {extension, restriction}
15939 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015940 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015941 if (! equal) {
15942 /*
15943 * SPEC (1) "If B and D are not the same type definition, then the
15944 * {derivation method} of D must not be in the subset."
15945 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015946 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15947 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015948 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015949 } else {
15950 /*
15951 * SPEC (2.1) "B and D must be the same type definition."
15952 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015953 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015954 }
15955 /*
15956 * SPEC (2.2) "B must be D's {base type definition}."
15957 */
15958 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015959 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015960 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015961 * SPEC (2.3.1) "D's {base type definition} must not be the �ur-type
15962 * definition�."
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015963 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015964 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015965 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015966
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015967 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015968 /*
15969 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15970 * must be validly derived from B given the subset as defined by this
15971 * constraint."
15972 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015973 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015974 baseType, set));
15975 } else {
15976 /*
15977 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15978 * must be validly derived from B given the subset as defined in Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000015979 * Derivation OK (Simple) (�3.14.6).
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015980 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015981 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015982 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015983 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015984}
15985
15986/**
15987 * xmlSchemaCheckCOSDerivedOK:
15988 * @type: the derived simple type definition
15989 * @baseType: the base type definition
15990 *
15991 * Calls:
15992 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015993 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015994 * Checks wheter @type can be validly derived from @baseType.
15995 *
15996 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015997 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015998static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015999xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016000 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016001 xmlSchemaTypePtr baseType,
16002 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016003{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016004 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016005 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016006 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016007 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016008}
16009
William M. Brack2f2a6632004-08-20 23:09:47 +000016010/**
16011 * xmlSchemaCheckCOSCTExtends:
16012 * @ctxt: the schema parser context
16013 * @type: the complex type definition
16014 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016015 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016016 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016017 * Derivation Valid (Extension) (cos-ct-extends)
16018 *
16019 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016020 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016021 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016022 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000016023 *
16024 * Returns 0 if the constraints are satisfied, a positive
16025 * error code if not and -1 if an internal error occured.
16026 */
16027static int
16028xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
16029 xmlSchemaTypePtr type)
16030{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016031 xmlSchemaTypePtr base = type->baseType;
16032 /*
16033 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
16034 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000016035 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016036 /*
16037 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016038 * then all of the following must be true:"
16039 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016040 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016041 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016042 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016043 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016044 */
16045 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16046 xmlSchemaPCustomErr(ctxt,
16047 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016048 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016049 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016050 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016051 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16052 }
Daniel Veillarddee23482008-04-11 12:58:43 +000016053
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016054 /*
16055 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16056 * since they are automatically satisfied through the
16057 * inheriting mechanism.
16058 * Note that even if redefining components, the inheriting mechanism
16059 * is used.
16060 */
16061#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016062 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016063 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016064 * uses}
16065 * of the complex type definition itself, that is, for every attribute
16066 * use in the {attribute uses} of the {base type definition}, there
16067 * must be an attribute use in the {attribute uses} of the complex
16068 * type definition itself whose {attribute declaration} has the same
16069 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016070 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016071 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016072 if (base->attrUses != NULL) {
16073 int i, j, found;
16074 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016075
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016076 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16077 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16078 found = 0;
16079 if (type->attrUses != NULL) {
16080 use = (WXS_LIST_CAST type->attrUses)->items[j];
16081 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16082 {
16083 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16084 WXS_ATTRUSE_DECL_NAME(buse)) &&
16085 (WXS_ATTRUSE_DECL_TNS(use) ==
16086 WXS_ATTRUSE_DECL_TNS(buse)) &&
16087 (WXS_ATTRUSE_TYPEDEF(use) ==
16088 WXS_ATTRUSE_TYPEDEF(buse))
16089 {
16090 found = 1;
16091 break;
16092 }
16093 }
16094 }
16095 if (! found) {
16096 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016097
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016098 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16099 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16100 NULL, WXS_BASIC_CAST type,
Daniel Veillarddee23482008-04-11 12:58:43 +000016101 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016102 * TODO: The report does not indicate that also the
16103 * type needs to be the same.
16104 */
16105 "This type is missing a matching correspondent "
16106 "for its {base type}'s %s in its {attribute uses}",
16107 xmlSchemaGetComponentDesignation(&str,
16108 buse->children),
16109 NULL);
16110 FREE_AND_NULL(str)
16111 }
16112 }
16113 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016114 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016115 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16116 * definition must also have one, and the base type definition's
16117 * {attribute wildcard}'s {namespace constraint} must be a subset
16118 * of the complex type definition's {attribute wildcard}'s {namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000016119 * constraint}, as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016120 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016121
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016122 /*
16123 * MAYBE TODO: Enable if ever needed. But this will be needed only
16124 * if created the type via a schema construction API.
16125 */
16126 if (base->attributeWildcard != NULL) {
16127 if (type->attributeWilcard == NULL) {
16128 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016129
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016130 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16131 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16132 NULL, type,
16133 "The base %s has an attribute wildcard, "
16134 "but this type is missing an attribute wildcard",
16135 xmlSchemaGetComponentDesignation(&str, base));
16136 FREE_AND_NULL(str)
16137
16138 } else if (xmlSchemaCheckCOSNSSubset(
16139 base->attributeWildcard, type->attributeWildcard))
16140 {
16141 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016142
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016143 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16144 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16145 NULL, type,
16146 "The attribute wildcard is not a valid "
16147 "superset of the one in the base %s",
16148 xmlSchemaGetComponentDesignation(&str, base));
16149 FREE_AND_NULL(str)
16150 }
16151 }
16152#endif
16153 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016154 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016155 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016156 if ((type->contentTypeDef != NULL) &&
16157 (type->contentTypeDef == base->contentTypeDef)) {
16158 /*
16159 * SPEC (1.4.1) "The {content type} of the {base type definition}
16160 * and the {content type} of the complex type definition itself
16161 * must be the same simple type definition"
16162 * PASS
16163 */
16164 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16165 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16166 /*
16167 * SPEC (1.4.2) "The {content type} of both the {base type
16168 * definition} and the complex type definition itself must
16169 * be empty."
16170 * PASS
16171 */
16172 } else {
16173 /*
16174 * SPEC (1.4.3) "All of the following must be true:"
16175 */
16176 if (type->subtypes == NULL) {
16177 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016178 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016179 * definition itself must specify a particle.
16180 */
16181 xmlSchemaPCustomErr(ctxt,
16182 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016183 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016184 "The content type must specify a particle", NULL);
16185 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16186 }
16187 /*
16188 * SPEC (1.4.3.2) "One of the following must be true:"
16189 */
16190 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16191 /*
16192 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16193 * definition} must be empty.
16194 * PASS
16195 */
16196 } else {
16197 /*
16198 * SPEC (1.4.3.2.2) "All of the following must be true:"
16199 */
16200 if ((type->contentType != base->contentType) ||
16201 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16202 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16203 /*
16204 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16205 * or both must be element-only."
16206 */
16207 xmlSchemaPCustomErr(ctxt,
16208 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016209 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016210 "The content type of both, the type and its base "
16211 "type, must either 'mixed' or 'element-only'", NULL);
16212 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016213 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016214 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016215 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000016216 * complex type definition must be a �valid extension�
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016217 * of the {base type definition}'s particle, as defined
Rob Richardsc6947bb2008-06-29 15:04:41 +000016218 * in Particle Valid (Extension) (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016219 *
16220 * NOTE that we won't check "Particle Valid (Extension)",
16221 * since it is ensured by the derivation process in
16222 * xmlSchemaTypeFixup(). We need to implement this when heading
16223 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016224 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016225 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016226 }
16227 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016228 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016229 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016230 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016231 } else {
16232 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016233 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016234 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016235 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016236 if (type->contentTypeDef != base) {
16237 /*
16238 * SPEC (2.1) "The {content type} must be the same simple type
16239 * definition."
16240 */
16241 xmlSchemaPCustomErr(ctxt,
16242 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016243 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016244 "The content type must be the simple base type", NULL);
16245 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16246 }
16247 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16248 /*
16249 * SPEC (2.2) "The {final} of the {base type definition} must not
16250 * contain extension"
16251 * NOTE that this is the same as (1.1).
16252 */
16253 xmlSchemaPCustomErr(ctxt,
16254 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016255 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016256 "The 'final' of the base type definition "
16257 "contains 'extension'", NULL);
16258 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016259 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016260 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016261 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016262}
16263
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016264/**
16265 * xmlSchemaCheckDerivationOKRestriction:
16266 * @ctxt: the schema parser context
16267 * @type: the complex type definition
16268 *
16269 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016270 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016271 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16272 *
16273 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016274 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016275 * (5.4.2) ???
16276 *
16277 * ATTENTION:
16278 * In XML Schema 1.1 this will be:
16279 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016280 *
16281 * Returns 0 if the constraints are satisfied, a positive
16282 * error code if not and -1 if an internal error occured.
16283 */
16284static int
16285xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16286 xmlSchemaTypePtr type)
16287{
16288 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016289
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016290 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016291 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016292 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016293 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016294 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016295 if (! WXS_IS_COMPLEX(base)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000016296 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016297 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16298 type->node, WXS_BASIC_CAST type,
16299 "The base type must be a complex type", NULL, NULL);
16300 return(ctxt->err);
16301 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016302 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16303 /*
16304 * SPEC (1) "The {base type definition} must be a complex type
16305 * definition whose {final} does not contain restriction."
16306 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016307 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016308 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16309 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016310 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016311 "contains 'restriction'", NULL, NULL);
16312 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016313 }
16314 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016315 * SPEC (2), (3) and (4)
16316 * Those are handled in a separate function, since the
16317 * same constraints are needed for redefinition of
16318 * attribute groups as well.
16319 */
16320 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16321 XML_SCHEMA_ACTION_DERIVE,
16322 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16323 type->attrUses, base->attrUses,
16324 type->attributeWildcard,
16325 base->attributeWildcard) == -1)
16326 {
16327 return(-1);
16328 }
16329 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016330 * SPEC (5) "One of the following must be true:"
16331 */
16332 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16333 /*
16334 * SPEC (5.1) "The {base type definition} must be the
Rob Richardsc6947bb2008-06-29 15:04:41 +000016335 * �ur-type definition�."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016336 * PASS
16337 */
16338 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16339 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16340 /*
16341 * SPEC (5.2.1) "The {content type} of the complex type definition
16342 * must be a simple type definition"
16343 *
16344 * SPEC (5.2.2) "One of the following must be true:"
16345 */
16346 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016347 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16348 {
16349 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016350 /*
16351 * SPEC (5.2.2.1) "The {content type} of the {base type
16352 * definition} must be a simple type definition from which
16353 * the {content type} is validly derived given the empty
Rob Richardsc6947bb2008-06-29 15:04:41 +000016354 * set as defined in Type Derivation OK (Simple) (�3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016355 *
16356 * ATTENTION TODO: This seems not needed if the type implicitely
16357 * derived from the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +000016358 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016359 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016360 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16361 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016362 if (err != 0) {
16363 xmlChar *strA = NULL, *strB = NULL;
16364
16365 if (err == -1)
16366 return(-1);
16367 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16368 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16369 NULL, WXS_BASIC_CAST type,
16370 "The {content type} %s is not validly derived from the "
16371 "base type's {content type} %s",
16372 xmlSchemaGetComponentDesignation(&strA,
16373 type->contentTypeDef),
16374 xmlSchemaGetComponentDesignation(&strB,
16375 base->contentTypeDef));
16376 FREE_AND_NULL(strA);
16377 FREE_AND_NULL(strB);
16378 return(ctxt->err);
16379 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016380 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16381 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016382 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016383 /*
16384 * SPEC (5.2.2.2) "The {base type definition} must be mixed
Rob Richardsc6947bb2008-06-29 15:04:41 +000016385 * and have a particle which is �emptiable� as defined in
16386 * Particle Emptiable (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016387 * PASS
16388 */
16389 } else {
16390 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016391 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16392 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016393 "The content type of the base type must be either "
16394 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016395 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016396 }
16397 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16398 /*
16399 * SPEC (5.3.1) "The {content type} of the complex type itself must
16400 * be empty"
16401 */
16402 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16403 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016404 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016405 * definition} must also be empty."
16406 * PASS
16407 */
16408 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16409 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16410 xmlSchemaIsParticleEmptiable(
16411 (xmlSchemaParticlePtr) base->subtypes)) {
16412 /*
16413 * SPEC (5.3.2.2) "The {content type} of the {base type
16414 * definition} must be elementOnly or mixed and have a particle
Rob Richardsc6947bb2008-06-29 15:04:41 +000016415 * which is �emptiable� as defined in Particle Emptiable (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016416 * PASS
16417 */
16418 } else {
16419 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016420 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16421 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016422 "The content type of the base type must be either "
16423 "empty or 'mixed' (or 'elements-only') and an emptiable "
16424 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016425 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016426 }
16427 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016428 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016429 /*
16430 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16431 * itself must be element-only"
Daniel Veillarddee23482008-04-11 12:58:43 +000016432 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016433 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016434 /*
16435 * SPEC (5.4.1.2) "The {content type} of the complex type
16436 * definition itself and of the {base type definition} must be
16437 * mixed"
16438 */
16439 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016440 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16441 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016442 "If the content type is 'mixed', then the content type of the "
16443 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016444 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016445 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016446 /*
16447 * SPEC (5.4.2) "The particle of the complex type definition itself
Rob Richardsc6947bb2008-06-29 15:04:41 +000016448 * must be a �valid restriction� of the particle of the {content
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016449 * type} of the {base type definition} as defined in Particle Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000016450 * (Restriction) (�3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016451 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016452 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016453 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016454 } else {
16455 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016456 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16457 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016458 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016459 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016460 }
16461 return (0);
16462}
16463
16464/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016465 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016466 * @ctxt: the schema parser context
16467 * @type: the complex type definition
16468 *
16469 * (3.4.6) Constraints on Complex Type Definition Schema Components
16470 *
16471 * Returns 0 if the constraints are satisfied, a positive
16472 * error code if not and -1 if an internal error occured.
16473 */
16474static int
16475xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16476 xmlSchemaTypePtr type)
16477{
16478 int ret;
16479 /*
16480 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016481 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016482 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16483 if (ret != 0)
16484 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016485 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016486 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16487 else
16488 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16489 return (ret);
16490}
16491
16492/**
16493 * xmlSchemaCheckSRCCT:
16494 * @ctxt: the schema parser context
16495 * @type: the complex type definition
16496 *
16497 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016498 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016499 * Complex Type Definition Representation OK (src-ct)
16500 *
16501 * Returns 0 if the constraints are satisfied, a positive
16502 * error code if not and -1 if an internal error occured.
16503 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016504static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016505xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016506 xmlSchemaTypePtr type)
16507{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016508 xmlSchemaTypePtr base;
16509 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016510
16511 /*
16512 * TODO: Adjust the error codes here, as I used
16513 * XML_SCHEMAP_SRC_CT_1 only yet.
16514 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016515 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016516 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016517 /*
16518 * 1 If the <complexContent> alternative is chosen, the type definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000016519 * �resolved� to by the �actual value� of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016520 * must be a complex type definition;
16521 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016522 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016523 xmlChar *str = NULL;
16524 xmlSchemaPCustomErr(ctxt,
16525 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016526 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016527 "If using <complexContent>, the base type is expected to be "
16528 "a complex type. The base type '%s' is a simple type",
16529 xmlSchemaFormatQName(&str, base->targetNamespace,
16530 base->name));
16531 FREE_AND_NULL(str)
16532 return (XML_SCHEMAP_SRC_CT_1);
16533 }
16534 } else {
16535 /*
16536 * SPEC
16537 * 2 If the <simpleContent> alternative is chosen, all of the
16538 * following must be true:
Rob Richardsc6947bb2008-06-29 15:04:41 +000016539 * 2.1 The type definition �resolved� to by the �actual value� of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016540 * base [attribute] must be one of the following:
16541 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016542 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016543 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016545 /*
16546 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016547 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016548 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016549 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016550 xmlSchemaPCustomErr(ctxt,
16551 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016552 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016553 "If using <simpleContent> and <restriction>, the base "
16554 "type must be a complex type. The base type '%s' is "
16555 "a simple type",
16556 xmlSchemaFormatQName(&str, base->targetNamespace,
16557 base->name));
16558 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016559 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016560 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016561 } else {
16562 /* Base type is a complex type. */
16563 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16564 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16565 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016566 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016567 * simple type definition;
16568 * PASS
16569 */
16570 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016571 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016572 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016573 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016574 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016575 type->name);
16576 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016577 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016578 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016579 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016580
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016581 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016582 * 2.1.2 only if the <restriction> alternative is also
16583 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016584 * is mixed and a particle emptiable.
16585 */
16586 if (! xmlSchemaIsParticleEmptiable(
16587 (xmlSchemaParticlePtr) base->subtypes)) {
16588 ret = XML_SCHEMAP_SRC_CT_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000016589 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016590 /*
16591 * Attention: at this point the <simpleType> child is in
16592 * ->contentTypeDef (put there during parsing).
Daniel Veillarddee23482008-04-11 12:58:43 +000016593 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016594 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016595 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016596 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016597 * 2.2 If clause 2.1.2 above is satisfied, then there
16598 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016599 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016600 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016601 /* TODO: Change error code to ..._SRC_CT_2_2. */
16602 xmlSchemaPCustomErr(ctxt,
16603 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016604 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016605 "A <simpleType> is expected among the children "
16606 "of <restriction>, if <simpleContent> is used and "
16607 "the base type '%s' is a complex type",
16608 xmlSchemaFormatQName(&str, base->targetNamespace,
16609 base->name));
16610 FREE_AND_NULL(str)
16611 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016612 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016613 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016614 ret = XML_SCHEMAP_SRC_CT_1;
16615 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016616 }
16617 if (ret > 0) {
16618 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016619 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016620 xmlSchemaPCustomErr(ctxt,
16621 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016622 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016623 "If <simpleContent> and <restriction> is used, the "
16624 "base type must be a simple type or a complex type with "
16625 "mixed content and particle emptiable. The base type "
16626 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016627 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016628 base->name));
16629 } else {
16630 xmlSchemaPCustomErr(ctxt,
16631 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016632 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016633 "If <simpleContent> and <extension> is used, the "
16634 "base type must be a simple type. The base type '%s' "
16635 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016636 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016637 base->name));
16638 }
16639 FREE_AND_NULL(str)
16640 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016641 }
16642 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016643 * SPEC (3) "The corresponding complex type definition component must
16644 * satisfy the conditions set out in Constraints on Complex Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000016645 * Definition Schema Components (�3.4.6);"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016646 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016647 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016648 /*
16649 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016650 * above for {attribute wildcard} is satisfied, the intensional
16651 * intersection must be expressible, as defined in Attribute Wildcard
Rob Richardsc6947bb2008-06-29 15:04:41 +000016652 * Intersection (�3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016653 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016654 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016655 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016656}
William M. Brack2f2a6632004-08-20 23:09:47 +000016657
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016658#ifdef ENABLE_PARTICLE_RESTRICTION
16659/**
16660 * xmlSchemaCheckParticleRangeOK:
16661 * @ctxt: the schema parser context
16662 * @type: the complex type definition
16663 *
16664 * (3.9.6) Constraints on Particle Schema Components
16665 * Schema Component Constraint:
16666 * Occurrence Range OK (range-ok)
16667 *
16668 * STATUS: complete
16669 *
16670 * Returns 0 if the constraints are satisfied, a positive
16671 * error code if not and -1 if an internal error occured.
16672 */
16673static int
16674xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16675 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016676{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016677 if (rmin < bmin)
16678 return (1);
16679 if ((bmax != UNBOUNDED) &&
16680 (rmax > bmax))
16681 return (1);
16682 return (0);
16683}
16684
16685/**
16686 * xmlSchemaCheckRCaseNameAndTypeOK:
16687 * @ctxt: the schema parser context
16688 * @r: the restricting element declaration particle
16689 * @b: the base element declaration particle
16690 *
16691 * (3.9.6) Constraints on Particle Schema Components
16692 * Schema Component Constraint:
16693 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16694 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016695 *
16696 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016697 * MISSING (3.2.3)
16698 * CLARIFY: (3.2.2)
16699 *
16700 * Returns 0 if the constraints are satisfied, a positive
16701 * error code if not and -1 if an internal error occured.
16702 */
16703static int
16704xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16705 xmlSchemaParticlePtr r,
16706 xmlSchemaParticlePtr b)
16707{
16708 xmlSchemaElementPtr elemR, elemB;
16709
16710 /* TODO: Error codes (rcase-NameAndTypeOK). */
16711 elemR = (xmlSchemaElementPtr) r->children;
16712 elemB = (xmlSchemaElementPtr) b->children;
16713 /*
16714 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16715 * the same."
16716 */
16717 if ((elemR != elemB) &&
16718 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16719 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16720 return (1);
16721 /*
16722 * SPEC (2) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016723 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016724 */
16725 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16726 b->minOccurs, b->maxOccurs) != 0)
16727 return (1);
16728 /*
16729 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16730 * {scope} are global."
16731 */
16732 if (elemR == elemB)
16733 return (0);
16734 /*
16735 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16736 */
16737 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16738 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16739 return (1);
16740 /*
16741 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16742 * or is not fixed, or R's declaration's {value constraint} is fixed
16743 * with the same value."
16744 */
16745 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16746 ((elemR->value == NULL) ||
16747 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16748 /* TODO: Equality of the initial value or normalized or canonical? */
16749 (! xmlStrEqual(elemR->value, elemB->value))))
16750 return (1);
16751 /*
16752 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16753 * definitions} is a subset of B's declaration's {identity-constraint
16754 * definitions}, if any."
16755 */
16756 if (elemB->idcs != NULL) {
16757 /* TODO */
16758 }
16759 /*
16760 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16761 * superset of B's declaration's {disallowed substitutions}."
16762 */
16763 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16764 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16765 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16766 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16767 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16768 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16769 return (1);
16770 /*
16771 * SPEC (3.2.5) "R's {type definition} is validly derived given
16772 * {extension, list, union} from B's {type definition}"
16773 *
16774 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16775 * set, if the corresponding constraints handle "restriction" and
16776 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016777 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016778 */
16779 {
16780 int set = 0;
16781
16782 set |= SUBSET_EXTENSION;
16783 set |= SUBSET_LIST;
16784 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016785 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016786 elemB->subtypes, set) != 0)
16787 return (1);
16788 }
16789 return (0);
16790}
16791
16792/**
16793 * xmlSchemaCheckRCaseNSCompat:
16794 * @ctxt: the schema parser context
16795 * @r: the restricting element declaration particle
16796 * @b: the base wildcard particle
16797 *
16798 * (3.9.6) Constraints on Particle Schema Components
16799 * Schema Component Constraint:
16800 * Particle Derivation OK (Elt:Any -- NSCompat)
16801 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016802 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016803 * STATUS: complete
16804 *
16805 * Returns 0 if the constraints are satisfied, a positive
16806 * error code if not and -1 if an internal error occured.
16807 */
16808static int
16809xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16810 xmlSchemaParticlePtr r,
16811 xmlSchemaParticlePtr b)
16812{
16813 /* TODO:Error codes (rcase-NSCompat). */
16814 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016815 * SPEC "For an element declaration particle to be a �valid restriction�
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016816 * of a wildcard particle all of the following must be true:"
16817 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000016818 * SPEC (1) "The element declaration's {target namespace} is �valid�
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016819 * with respect to the wildcard's {namespace constraint} as defined by
Rob Richardsc6947bb2008-06-29 15:04:41 +000016820 * Wildcard allows Namespace Name (�3.10.4)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016821 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016822 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016823 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16824 return (1);
16825 /*
16826 * SPEC (2) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016827 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016828 */
16829 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16830 b->minOccurs, b->maxOccurs) != 0)
16831 return (1);
16832
16833 return (0);
16834}
16835
16836/**
16837 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16838 * @ctxt: the schema parser context
16839 * @r: the restricting element declaration particle
16840 * @b: the base model group particle
16841 *
16842 * (3.9.6) Constraints on Particle Schema Components
16843 * Schema Component Constraint:
16844 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16845 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016846 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016847 * STATUS: TODO
16848 *
16849 * Returns 0 if the constraints are satisfied, a positive
16850 * error code if not and -1 if an internal error occured.
16851 */
16852static int
16853xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16854 xmlSchemaParticlePtr r,
16855 xmlSchemaParticlePtr b)
16856{
16857 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16858 TODO
16859 return (0);
16860}
16861
16862/**
16863 * xmlSchemaCheckRCaseNSSubset:
16864 * @ctxt: the schema parser context
16865 * @r: the restricting wildcard particle
16866 * @b: the base wildcard particle
16867 *
16868 * (3.9.6) Constraints on Particle Schema Components
16869 * Schema Component Constraint:
16870 * Particle Derivation OK (Any:Any -- NSSubset)
16871 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016872 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016873 * STATUS: complete
16874 *
16875 * Returns 0 if the constraints are satisfied, a positive
16876 * error code if not and -1 if an internal error occured.
16877 */
16878static int
16879xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16880 xmlSchemaParticlePtr r,
16881 xmlSchemaParticlePtr b,
16882 int isAnyTypeBase)
16883{
16884 /* TODO: Error codes (rcase-NSSubset). */
16885 /*
16886 * SPEC (1) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016887 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016888 */
16889 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16890 b->minOccurs, b->maxOccurs))
16891 return (1);
16892 /*
16893 * SPEC (2) "R's {namespace constraint} must be an intensional subset
Rob Richardsc6947bb2008-06-29 15:04:41 +000016894 * of B's {namespace constraint} as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016895 */
16896 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16897 (xmlSchemaWildcardPtr) b->children))
16898 return (1);
16899 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016900 * SPEC (3) "Unless B is the content model wildcard of the �ur-type
16901 * definition�, R's {process contents} must be identical to or stronger
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016902 * than B's {process contents}, where strict is stronger than lax is
16903 * stronger than skip."
16904 */
16905 if (! isAnyTypeBase) {
16906 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16907 ((xmlSchemaWildcardPtr) b->children)->processContents)
16908 return (1);
16909 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016910
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016911 return (0);
16912}
16913
16914/**
16915 * xmlSchemaCheckCOSParticleRestrict:
16916 * @ctxt: the schema parser context
16917 * @type: the complex type definition
16918 *
16919 * (3.9.6) Constraints on Particle Schema Components
16920 * Schema Component Constraint:
16921 * Particle Valid (Restriction) (cos-particle-restrict)
16922 *
16923 * STATUS: TODO
16924 *
16925 * Returns 0 if the constraints are satisfied, a positive
16926 * error code if not and -1 if an internal error occured.
16927 */
16928static int
16929xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16930 xmlSchemaParticlePtr r,
16931 xmlSchemaParticlePtr b)
16932{
16933 int ret = 0;
16934
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016935 /*part = WXS_TYPE_PARTICLE(type);
16936 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016937 */
16938
16939 TODO
16940
16941 /*
16942 * SPEC (1) "They are the same particle."
16943 */
16944 if (r == b)
16945 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016946
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016947
16948 return (0);
16949}
16950
16951/**
16952 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16953 * @ctxt: the schema parser context
16954 * @r: the model group particle
16955 * @b: the base wildcard particle
16956 *
16957 * (3.9.6) Constraints on Particle Schema Components
16958 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016959 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016960 * NSRecurseCheckCardinality)
16961 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016962 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016963 * STATUS: TODO: subst-groups
16964 *
16965 * Returns 0 if the constraints are satisfied, a positive
16966 * error code if not and -1 if an internal error occured.
16967 */
16968static int
16969xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16970 xmlSchemaParticlePtr r,
16971 xmlSchemaParticlePtr b)
16972{
16973 xmlSchemaParticlePtr part;
16974 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16975 if ((r->children == NULL) || (r->children->children == NULL))
16976 return (-1);
16977 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016978 * SPEC "For a group particle to be a �valid restriction� of a
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016979 * wildcard particle..."
16980 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000016981 * SPEC (1) "Every member of the {particles} of the group is a �valid
16982 * restriction� of the wildcard as defined by
16983 * Particle Valid (Restriction) (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016984 */
16985 part = (xmlSchemaParticlePtr) r->children->children;
16986 do {
16987 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16988 return (1);
16989 part = (xmlSchemaParticlePtr) part->next;
16990 } while (part != NULL);
16991 /*
16992 * SPEC (2) "The effective total range of the group [...] is a
16993 * valid restriction of B's occurrence range as defined by
Rob Richardsc6947bb2008-06-29 15:04:41 +000016994 * Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016995 */
16996 if (xmlSchemaCheckParticleRangeOK(
16997 xmlSchemaGetParticleTotalRangeMin(r),
16998 xmlSchemaGetParticleTotalRangeMax(r),
16999 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017000 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017001 return (0);
17002}
17003
17004/**
17005 * xmlSchemaCheckRCaseRecurse:
17006 * @ctxt: the schema parser context
17007 * @r: the <all> or <sequence> model group particle
17008 * @b: the base <all> or <sequence> model group particle
17009 *
17010 * (3.9.6) Constraints on Particle Schema Components
17011 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017012 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017013 Recurse)
17014 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017015 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017016 * STATUS: ?
17017 * TODO: subst-groups
17018 *
17019 * Returns 0 if the constraints are satisfied, a positive
17020 * error code if not and -1 if an internal error occured.
17021 */
17022static int
17023xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
17024 xmlSchemaParticlePtr r,
17025 xmlSchemaParticlePtr b)
17026{
17027 /* xmlSchemaParticlePtr part; */
17028 /* TODO: Error codes (rcase-Recurse). */
17029 if ((r->children == NULL) || (b->children == NULL) ||
17030 (r->children->type != b->children->type))
17031 return (-1);
17032 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000017033 * SPEC "For an all or sequence group particle to be a �valid
17034 * restriction� of another group particle with the same {compositor}..."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017035 *
17036 * SPEC (1) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000017037 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017038 */
17039 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17040 b->minOccurs, b->maxOccurs))
17041 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017042
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017043
17044 return (0);
17045}
17046
17047#endif
17048
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017049#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17050 xmlSchemaPCustomErrExt(pctxt, \
17051 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017052 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017053 "It is an error for both '%s' and '%s' to be specified on the "\
17054 "same type definition", \
17055 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17056 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17057
17058#define FACET_RESTR_ERR(fac1, msg) \
17059 xmlSchemaPCustomErr(pctxt, \
17060 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017061 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017062 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017063
17064#define FACET_RESTR_FIXED_ERR(fac) \
17065 xmlSchemaPCustomErr(pctxt, \
17066 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017067 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017068 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017069 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017070
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017071static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017072xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17073 xmlSchemaFacetPtr facet1,
17074 xmlSchemaFacetPtr facet2,
17075 int lessGreater,
17076 int orEqual,
17077 int ofBase)
17078{
17079 xmlChar *msg = NULL;
17080
17081 msg = xmlStrdup(BAD_CAST "'");
17082 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17083 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17084 if (lessGreater == 0)
17085 msg = xmlStrcat(msg, BAD_CAST " equal to");
17086 if (lessGreater == 1)
17087 msg = xmlStrcat(msg, BAD_CAST " greater than");
17088 else
17089 msg = xmlStrcat(msg, BAD_CAST " less than");
17090
17091 if (orEqual)
17092 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17093 msg = xmlStrcat(msg, BAD_CAST " '");
17094 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17095 if (ofBase)
17096 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17097 else
17098 msg = xmlStrcat(msg, BAD_CAST "'");
17099
17100 xmlSchemaPCustomErr(pctxt,
17101 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017102 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017103 (const char *) msg, NULL);
17104
17105 if (msg != NULL)
17106 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017107}
17108
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017109/*
17110* xmlSchemaDeriveAndValidateFacets:
17111*
17112* Schema Component Constraint: Simple Type Restriction (Facets)
17113* (st-restrict-facets)
17114*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017115static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017116xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17117 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017118{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017119 xmlSchemaTypePtr base = type->baseType;
17120 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017121 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017122 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17123 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17124 fmininc = NULL, fmaxinc = NULL,
17125 fminexc = NULL, fmaxexc = NULL,
17126 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17127 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17128 bfmininc = NULL, bfmaxinc = NULL,
17129 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017130 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017131
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017132 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017133 * SPEC st-restrict-facets 1:
Daniel Veillarddee23482008-04-11 12:58:43 +000017134 * "The {variety} of R is the same as that of B."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017135 */
17136 /*
17137 * SPEC st-restrict-facets 2:
17138 * "If {variety} is atomic, the {primitive type definition}
17139 * of R is the same as that of B."
17140 *
17141 * NOTE: we leave 1 & 2 out for now, since this will be
17142 * satisfied by the derivation process.
17143 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17144 */
17145 /*
17146 * SPEC st-restrict-facets 3:
17147 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017148 * of B, eliminating duplicates. To eliminate duplicates,
17149 * when a facet of the same kind occurs in both S and the
17150 * {facets} of B, the one in the {facets} of B is not
17151 * included, with the exception of enumeration and pattern
17152 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017153 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017154 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017155
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017156 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17157 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017158
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017159 last = type->facetSet;
17160 if (last != NULL)
17161 while (last->next != NULL)
17162 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017163
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017164 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17165 facet = cur->facet;
17166 switch (facet->type) {
17167 case XML_SCHEMA_FACET_LENGTH:
17168 flength = facet; break;
17169 case XML_SCHEMA_FACET_MINLENGTH:
17170 fminlen = facet; break;
17171 case XML_SCHEMA_FACET_MININCLUSIVE:
17172 fmininc = facet; break;
17173 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17174 fminexc = facet; break;
17175 case XML_SCHEMA_FACET_MAXLENGTH:
17176 fmaxlen = facet; break;
17177 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17178 fmaxinc = facet; break;
17179 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17180 fmaxexc = facet; break;
17181 case XML_SCHEMA_FACET_TOTALDIGITS:
17182 ftotdig = facet; break;
17183 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17184 ffracdig = facet; break;
17185 default:
17186 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017187 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017188 }
17189 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17190 facet = cur->facet;
17191 switch (facet->type) {
17192 case XML_SCHEMA_FACET_LENGTH:
17193 bflength = facet; break;
17194 case XML_SCHEMA_FACET_MINLENGTH:
17195 bfminlen = facet; break;
17196 case XML_SCHEMA_FACET_MININCLUSIVE:
17197 bfmininc = facet; break;
17198 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17199 bfminexc = facet; break;
17200 case XML_SCHEMA_FACET_MAXLENGTH:
17201 bfmaxlen = facet; break;
17202 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17203 bfmaxinc = facet; break;
17204 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17205 bfmaxexc = facet; break;
17206 case XML_SCHEMA_FACET_TOTALDIGITS:
17207 bftotdig = facet; break;
17208 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17209 bffracdig = facet; break;
17210 default:
17211 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017212 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017213 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017214 /*
17215 * length and minLength or maxLength (2.2) + (3.2)
17216 */
17217 if (flength && (fminlen || fmaxlen)) {
17218 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17219 "either of 'minLength' or 'maxLength' to be specified on "
17220 "the same type definition")
17221 }
17222 /*
17223 * Mutual exclusions in the same derivation step.
17224 */
17225 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017226 /*
17227 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017228 */
17229 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17230 }
17231 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017232 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017233 * SCC "minInclusive and minExclusive"
17234 */
17235 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017236 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017237
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017238 if (flength && bflength) {
17239 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017240 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017241 * The values have to be equal.
17242 */
17243 res = xmlSchemaCompareValues(flength->val, bflength->val);
17244 if (res == -2)
17245 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017246 if (res != 0)
17247 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17248 if ((res != 0) && (bflength->fixed)) {
17249 FACET_RESTR_FIXED_ERR(flength)
17250 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017251
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017252 }
17253 if (fminlen && bfminlen) {
17254 /*
17255 * SCC "minLength valid restriction"
17256 * minLength >= BASE minLength
17257 */
17258 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17259 if (res == -2)
17260 goto internal_error;
17261 if (res == -1)
17262 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17263 if ((res != 0) && (bfminlen->fixed)) {
17264 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017265 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017266 }
17267 if (fmaxlen && bfmaxlen) {
17268 /*
17269 * SCC "maxLength valid restriction"
17270 * maxLength <= BASE minLength
17271 */
17272 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17273 if (res == -2)
17274 goto internal_error;
17275 if (res == 1)
17276 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17277 if ((res != 0) && (bfmaxlen->fixed)) {
17278 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017279 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017280 }
17281 /*
17282 * SCC "length and minLength or maxLength"
17283 */
17284 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017285 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017286 if (flength) {
17287 if (! fminlen)
17288 flength = bflength;
17289 if (fminlen) {
17290 /* (1.1) length >= minLength */
17291 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17292 if (res == -2)
17293 goto internal_error;
17294 if (res == -1)
17295 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17296 }
17297 if (! fmaxlen)
17298 fmaxlen = bfmaxlen;
17299 if (fmaxlen) {
17300 /* (2.1) length <= maxLength */
17301 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17302 if (res == -2)
17303 goto internal_error;
17304 if (res == 1)
17305 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17306 }
17307 }
17308 if (fmaxinc) {
17309 /*
17310 * "maxInclusive"
17311 */
17312 if (fmininc) {
17313 /* SCC "maxInclusive >= minInclusive" */
17314 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17315 if (res == -2)
17316 goto internal_error;
17317 if (res == -1) {
17318 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17319 }
17320 }
17321 /*
17322 * SCC "maxInclusive valid restriction"
17323 */
17324 if (bfmaxinc) {
17325 /* maxInclusive <= BASE maxInclusive */
17326 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17327 if (res == -2)
17328 goto internal_error;
17329 if (res == 1)
17330 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17331 if ((res != 0) && (bfmaxinc->fixed)) {
17332 FACET_RESTR_FIXED_ERR(fmaxinc)
17333 }
17334 }
17335 if (bfmaxexc) {
17336 /* maxInclusive < BASE maxExclusive */
17337 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17338 if (res == -2)
17339 goto internal_error;
17340 if (res != -1) {
17341 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17342 }
17343 }
17344 if (bfmininc) {
17345 /* maxInclusive >= BASE minInclusive */
17346 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17347 if (res == -2)
17348 goto internal_error;
17349 if (res == -1) {
17350 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17351 }
17352 }
17353 if (bfminexc) {
17354 /* maxInclusive > BASE minExclusive */
17355 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17356 if (res == -2)
17357 goto internal_error;
17358 if (res != 1) {
17359 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17360 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017361 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017362 }
17363 if (fmaxexc) {
17364 /*
17365 * "maxExclusive >= minExclusive"
17366 */
17367 if (fminexc) {
17368 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17369 if (res == -2)
17370 goto internal_error;
17371 if (res == -1) {
17372 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17373 }
17374 }
17375 /*
17376 * "maxExclusive valid restriction"
17377 */
17378 if (bfmaxexc) {
17379 /* maxExclusive <= BASE maxExclusive */
17380 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17381 if (res == -2)
17382 goto internal_error;
17383 if (res == 1) {
17384 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17385 }
17386 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017387 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017388 }
17389 }
17390 if (bfmaxinc) {
17391 /* maxExclusive <= BASE maxInclusive */
17392 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17393 if (res == -2)
17394 goto internal_error;
17395 if (res == 1) {
17396 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17397 }
17398 }
17399 if (bfmininc) {
17400 /* maxExclusive > BASE minInclusive */
17401 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17402 if (res == -2)
17403 goto internal_error;
17404 if (res != 1) {
17405 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17406 }
17407 }
17408 if (bfminexc) {
17409 /* maxExclusive > BASE minExclusive */
17410 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17411 if (res == -2)
17412 goto internal_error;
17413 if (res != 1) {
17414 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17415 }
17416 }
17417 }
17418 if (fminexc) {
17419 /*
17420 * "minExclusive < maxInclusive"
17421 */
17422 if (fmaxinc) {
17423 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17424 if (res == -2)
17425 goto internal_error;
17426 if (res != -1) {
17427 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17428 }
17429 }
17430 /*
17431 * "minExclusive valid restriction"
17432 */
17433 if (bfminexc) {
17434 /* minExclusive >= BASE minExclusive */
17435 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17436 if (res == -2)
17437 goto internal_error;
17438 if (res == -1) {
17439 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17440 }
17441 if ((res != 0) && (bfminexc->fixed)) {
17442 FACET_RESTR_FIXED_ERR(fminexc)
17443 }
17444 }
17445 if (bfmaxinc) {
17446 /* minExclusive <= BASE maxInclusive */
17447 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17448 if (res == -2)
17449 goto internal_error;
17450 if (res == 1) {
17451 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17452 }
17453 }
17454 if (bfmininc) {
17455 /* minExclusive >= BASE minInclusive */
17456 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17457 if (res == -2)
17458 goto internal_error;
17459 if (res == -1) {
17460 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17461 }
17462 }
17463 if (bfmaxexc) {
17464 /* minExclusive < BASE maxExclusive */
17465 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17466 if (res == -2)
17467 goto internal_error;
17468 if (res != -1) {
17469 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17470 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017471 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017472 }
17473 if (fmininc) {
17474 /*
17475 * "minInclusive < maxExclusive"
17476 */
17477 if (fmaxexc) {
17478 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17479 if (res == -2)
17480 goto internal_error;
17481 if (res != -1) {
17482 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17483 }
17484 }
17485 /*
17486 * "minExclusive valid restriction"
17487 */
17488 if (bfmininc) {
17489 /* minInclusive >= BASE minInclusive */
17490 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17491 if (res == -2)
17492 goto internal_error;
17493 if (res == -1) {
17494 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17495 }
17496 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017497 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017498 }
17499 }
17500 if (bfmaxinc) {
17501 /* minInclusive <= BASE maxInclusive */
17502 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17503 if (res == -2)
17504 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017505 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017506 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17507 }
17508 }
17509 if (bfminexc) {
17510 /* minInclusive > BASE minExclusive */
17511 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17512 if (res == -2)
17513 goto internal_error;
17514 if (res != 1)
17515 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17516 }
17517 if (bfmaxexc) {
17518 /* minInclusive < BASE maxExclusive */
17519 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17520 if (res == -2)
17521 goto internal_error;
17522 if (res != -1)
17523 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17524 }
17525 }
17526 if (ftotdig && bftotdig) {
17527 /*
17528 * SCC " totalDigits valid restriction"
17529 * totalDigits <= BASE totalDigits
17530 */
17531 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17532 if (res == -2)
17533 goto internal_error;
17534 if (res == 1)
17535 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17536 -1, 1, 1);
17537 if ((res != 0) && (bftotdig->fixed)) {
17538 FACET_RESTR_FIXED_ERR(ftotdig)
17539 }
17540 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017541 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017542 /*
17543 * SCC "fractionDigits valid restriction"
17544 * fractionDigits <= BASE fractionDigits
17545 */
17546 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17547 if (res == -2)
17548 goto internal_error;
17549 if (res == 1)
17550 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17551 -1, 1, 1);
17552 if ((res != 0) && (bffracdig->fixed)) {
17553 FACET_RESTR_FIXED_ERR(ffracdig)
17554 }
17555 }
17556 /*
17557 * SCC "fractionDigits less than or equal to totalDigits"
17558 */
17559 if (! ftotdig)
17560 ftotdig = bftotdig;
17561 if (! ffracdig)
17562 ffracdig = bffracdig;
17563 if (ftotdig && ffracdig) {
17564 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17565 if (res == -2)
17566 goto internal_error;
17567 if (res == 1)
17568 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17569 -1, 1, 0);
17570 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017571 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 * *Enumerations* won' be added here, since only the first set
17573 * of enumerations in the ancestor-or-self axis is used
17574 * for validation, plus we need to use the base type of those
17575 * enumerations for whitespace.
17576 *
17577 * *Patterns*: won't be add here, since they are ORed at
17578 * type level and ANDed at ancestor level. This will
17579 * happed during validation by walking the base axis
17580 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017581 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017582 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17583 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017584 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017585 * Special handling of enumerations and patterns.
17586 * TODO: hmm, they should not appear in the set, so remove this.
17587 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017588 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017589 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017590 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017591 /*
17592 * Search for a duplicate facet in the current type.
17593 */
17594 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017595 /* err = 0; */
17596 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017597 while (link != NULL) {
17598 facet = link->facet;
17599 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017600 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017601 case XML_SCHEMA_FACET_WHITESPACE:
17602 /*
17603 * The whitespace must be stronger.
17604 */
17605 if (facet->whitespace < bfacet->whitespace) {
Rob Richardsc6947bb2008-06-29 15:04:41 +000017606 FACET_RESTR_ERR(facet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017607 "The 'whitespace' value has to be equal to "
17608 "or stronger than the 'whitespace' value of "
17609 "the base type")
17610 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017611 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017612 (facet->whitespace != bfacet->whitespace)) {
17613 FACET_RESTR_FIXED_ERR(facet)
17614 }
17615 break;
17616 default:
17617 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017618 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017619 /* Duplicate found. */
17620 break;
17621 }
17622 link = link->next;
17623 }
17624 /*
17625 * If no duplicate was found: add the base types's facet
17626 * to the set.
17627 */
17628 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017629 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017630 xmlMalloc(sizeof(xmlSchemaFacetLink));
17631 if (link == NULL) {
17632 xmlSchemaPErrMemory(pctxt,
17633 "deriving facets, creating a facet link", NULL);
17634 return (-1);
17635 }
17636 link->facet = cur->facet;
17637 link->next = NULL;
17638 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017639 type->facetSet = link;
17640 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017641 last->next = link;
17642 last = link;
17643 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017644
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017645 }
17646
17647 return (0);
17648internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017649 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17650 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017651 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017652}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017653
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017654static int
17655xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17656 xmlSchemaTypePtr type)
17657{
17658 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17659 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017660 * The actual value is then formed by replacing any union type
Rob Richardsc6947bb2008-06-29 15:04:41 +000017661 * definition in the �explicit members� with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017662 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017663 *
17664 * TODO: There's a bug entry at
17665 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17666 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017667 */
17668 link = type->memberTypes;
17669 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017670
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017671 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017672 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017673
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017674 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017675 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017676 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017677 link->type = subLink->type;
17678 if (subLink->next != NULL) {
17679 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017680 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017681 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017682 while (subLink != NULL) {
17683 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017684 xmlMalloc(sizeof(xmlSchemaTypeLink));
17685 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017686 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017687 NULL);
17688 return (-1);
17689 }
17690 newLink->type = subLink->type;
17691 prevLink->next = newLink;
17692 prevLink = newLink;
17693 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017694
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017695 subLink = subLink->next;
17696 }
17697 }
17698 }
17699 }
17700 link = link->next;
17701 }
17702 return (0);
17703}
17704
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017705static void
17706xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
Daniel Veillarddee23482008-04-11 12:58:43 +000017707{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017708 int has = 0, needVal = 0, normVal = 0;
17709
17710 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17711 if (has) {
17712 needVal = (type->baseType->flags &
17713 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17714 normVal = (type->baseType->flags &
17715 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17716 }
17717 if (type->facets != NULL) {
17718 xmlSchemaFacetPtr fac;
Daniel Veillarddee23482008-04-11 12:58:43 +000017719
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017720 for (fac = type->facets; fac != NULL; fac = fac->next) {
17721 switch (fac->type) {
17722 case XML_SCHEMA_FACET_WHITESPACE:
17723 break;
17724 case XML_SCHEMA_FACET_PATTERN:
17725 normVal = 1;
17726 has = 1;
17727 break;
17728 case XML_SCHEMA_FACET_ENUMERATION:
17729 needVal = 1;
17730 normVal = 1;
17731 has = 1;
17732 break;
17733 default:
17734 has = 1;
17735 break;
17736 }
Daniel Veillarddee23482008-04-11 12:58:43 +000017737 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017738 }
17739 if (normVal)
17740 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17741 if (needVal)
17742 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17743 if (has)
17744 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17745
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017746 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017747 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17748 /*
17749 * OPTIMIZE VAL TODO: Some facets need a computed value.
17750 */
17751 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17752 (prim->builtInType != XML_SCHEMAS_STRING)) {
17753 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
Daniel Veillarddee23482008-04-11 12:58:43 +000017754 }
17755 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017756}
17757
17758static int
17759xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17760{
Daniel Veillarddee23482008-04-11 12:58:43 +000017761
17762
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017763 /*
17764 * Evaluate the whitespace-facet value.
Daniel Veillarddee23482008-04-11 12:58:43 +000017765 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017766 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017767 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17768 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017769 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017770 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000017771
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017772 if (type->facetSet != NULL) {
17773 xmlSchemaFacetLinkPtr lin;
17774
17775 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17776 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17777 switch (lin->facet->whitespace) {
17778 case XML_SCHEMAS_FACET_PRESERVE:
17779 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17780 break;
17781 case XML_SCHEMAS_FACET_REPLACE:
17782 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17783 break;
17784 case XML_SCHEMAS_FACET_COLLAPSE:
17785 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17786 break;
17787 default:
17788 return (-1);
17789 }
17790 return (0);
17791 }
17792 }
17793 }
17794 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000017795 * For all �atomic� datatypes other than string (and types �derived�
17796 * by �restriction� from it) the value of whiteSpace is fixed to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017797 * collapse
17798 */
17799 {
17800 xmlSchemaTypePtr anc;
17801
Daniel Veillarddee23482008-04-11 12:58:43 +000017802 for (anc = type->baseType; anc != NULL &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017803 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17804 anc = anc->baseType) {
17805
17806 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
Daniel Veillarddee23482008-04-11 12:58:43 +000017807 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017808 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17809
17810 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
Daniel Veillarddee23482008-04-11 12:58:43 +000017811 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017812 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17813
17814 } else
17815 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17816 break;
17817 }
17818 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017819 }
17820 return (0);
17821}
17822
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017823static int
17824xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17825 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017826{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017827 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17828 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017829 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017830 return(0);
17831 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017832
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017833 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017834 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017835 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017836 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017837 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017838 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017839 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017840 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017841 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017842 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017843 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017844 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017845 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017846 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017847 * Corresponds to <simpleType><union>...
Daniel Veillarddee23482008-04-11 12:58:43 +000017848 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017849 if (type->memberTypes == NULL) {
17850 /*
17851 * This one is really needed, so get out.
17852 */
17853 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17854 "union type has no member-types assigned");
17855 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000017856 }
17857 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017858 /*
17859 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017860 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017861 if (type->baseType == NULL) {
17862 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17863 "type has no base-type assigned");
17864 return(-1);
17865 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017866 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017867 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17868 return(-1);
17869 /*
17870 * Variety
17871 * If the <restriction> alternative is chosen, then the
17872 * {variety} of the {base type definition}.
17873 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017874 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017875 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017876 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017877 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017878 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017879 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017880 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017881 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017882 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017883 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017884 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017885 * NOTE that we won't assign the memberTypes of the base,
17886 * since this will make trouble when freeing them; we will
17887 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017888 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017889 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017890 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017891 return(0);
17892}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017893
Daniel Veillard8651f532002-04-17 09:06:27 +000017894#ifdef DEBUG_TYPE
Daniel Veillard67952602006-01-05 15:29:44 +000017895static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017896xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17897 xmlSchemaTypePtr type)
17898{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017899 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017900 xmlGenericError(xmlGenericErrorContext,
17901 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017902 type->node->doc->URL,
17903 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017904 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017905 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017906 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017907 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017908 switch (type->contentType) {
17909 case XML_SCHEMA_CONTENT_SIMPLE:
17910 xmlGenericError(xmlGenericErrorContext, "simple\n");
17911 break;
17912 case XML_SCHEMA_CONTENT_ELEMENTS:
17913 xmlGenericError(xmlGenericErrorContext, "elements\n");
17914 break;
17915 case XML_SCHEMA_CONTENT_UNKNOWN:
17916 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17917 break;
17918 case XML_SCHEMA_CONTENT_EMPTY:
17919 xmlGenericError(xmlGenericErrorContext, "empty\n");
17920 break;
17921 case XML_SCHEMA_CONTENT_MIXED:
17922 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017923 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017924 xmlGenericError(xmlGenericErrorContext,
17925 "mixed as emptiable particle\n");
17926 else
17927 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17928 break;
17929 /* Removed, since not used. */
17930 /*
17931 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17932 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17933 break;
17934 */
17935 case XML_SCHEMA_CONTENT_BASIC:
17936 xmlGenericError(xmlGenericErrorContext, "basic\n");
17937 break;
17938 default:
17939 xmlGenericError(xmlGenericErrorContext,
17940 "not registered !!!\n");
17941 break;
17942 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017943 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017944}
Daniel Veillard8651f532002-04-17 09:06:27 +000017945#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017946
17947/*
17948* 3.14.6 Constraints on Simple Type Definition Schema Components
17949*/
17950static int
17951xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17952 xmlSchemaTypePtr type)
17953{
17954 int res, olderrs = pctxt->nberrors;
17955
17956 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17957 return(-1);
17958
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017959 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017960 return(0);
17961
17962 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17963 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17964
17965 if (type->baseType == NULL) {
17966 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17967 "missing baseType");
17968 goto exit_failure;
17969 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017970 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017971 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000017972 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017973 * If a member type of a union is a union itself, we need to substitute
17974 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017975 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17976 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017977 */
17978 if ((type->memberTypes != NULL) &&
17979 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
Daniel Veillarddee23482008-04-11 12:58:43 +000017980 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017981 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000017982 * SPEC src-simple-type 1
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017983 * "The corresponding simple type definition, if any, must satisfy
17984 * the conditions set out in Constraints on Simple Type Definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000017985 * Schema Components (�3.14.6)."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017986 */
17987 /*
17988 * Schema Component Constraint: Simple Type Definition Properties Correct
17989 * (st-props-correct)
17990 */
17991 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17992 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000017993 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017994 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17995 * (cos-st-restricts)
17996 */
17997 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17998 HFAILURE HERROR
17999 /*
18000 * TODO: Removed the error report, since it got annoying to get an
18001 * extra error report, if anything failed until now.
18002 * Enable this if needed.
18003 *
18004 * xmlSchemaPErr(ctxt, type->node,
18005 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
18006 * "Simple type '%s' does not satisfy the constraints "
18007 * "on simple type definitions.\n",
18008 * type->name, NULL);
18009 */
18010 /*
18011 * Schema Component Constraint: Simple Type Restriction (Facets)
18012 * (st-restrict-facets)
18013 */
18014 res = xmlSchemaCheckFacetValues(type, pctxt);
18015 HFAILURE HERROR
18016 if ((type->facetSet != NULL) ||
18017 (type->baseType->facetSet != NULL)) {
18018 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
18019 HFAILURE HERROR
18020 }
18021 /*
18022 * Whitespace value.
18023 */
18024 res = xmlSchemaTypeFixupWhitespace(type);
18025 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000018026 xmlSchemaTypeFixupOptimFacets(type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018027
18028exit_error:
18029#ifdef DEBUG_TYPE
18030 xmlSchemaDebugFixedType(pctxt, type);
18031#endif
18032 if (olderrs != pctxt->nberrors)
18033 return(pctxt->err);
18034 return(0);
18035
18036exit_failure:
18037#ifdef DEBUG_TYPE
18038 xmlSchemaDebugFixedType(pctxt, type);
18039#endif
18040 return(-1);
18041}
18042
18043static int
18044xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18045 xmlSchemaTypePtr type)
18046{
18047 int res = 0, olderrs = pctxt->nberrors;
18048 xmlSchemaTypePtr baseType = type->baseType;
18049
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018050 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018051 return(0);
18052 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18053 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018054 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018055 "missing baseType");
18056 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018057 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018058 /*
18059 * Fixup the base type.
18060 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018061 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018062 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018063 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18064 /*
18065 * Skip fixup if the base type is invalid.
18066 * TODO: Generate a warning!
18067 */
18068 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000018069 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018070 /*
18071 * This basically checks if the base type can be derived.
18072 */
18073 res = xmlSchemaCheckSRCCT(pctxt, type);
Daniel Veillarddee23482008-04-11 12:58:43 +000018074 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018075 /*
18076 * Fixup the content type.
18077 */
18078 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18079 /*
18080 * Corresponds to <complexType><simpleContent>...
18081 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018082 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018083 (baseType->contentTypeDef != NULL) &&
18084 (WXS_IS_RESTRICTION(type))) {
18085 xmlSchemaTypePtr contentBase, content;
18086#ifdef ENABLE_NAMED_LOCALS
18087 char buf[30];
18088 const xmlChar *tmpname;
18089#endif
18090 /*
18091 * SPEC (1) If <restriction> + base type is <complexType>,
18092 * "whose own {content type} is a simple type..."
18093 */
18094 if (type->contentTypeDef != NULL) {
18095 /*
18096 * SPEC (1.1) "the simple type definition corresponding to the
18097 * <simpleType> among the [children] of <restriction> if there
18098 * is one;"
18099 * Note that this "<simpleType> among the [children]" was put
18100 * into ->contentTypeDef during parsing.
18101 */
18102 contentBase = type->contentTypeDef;
18103 type->contentTypeDef = NULL;
18104 } else {
18105 /*
18106 * (1.2) "...otherwise (<restriction> has no <simpleType>
18107 * among its [children]), the simple type definition which
18108 * is the {content type} of the ... base type."
18109 */
18110 contentBase = baseType->contentTypeDef;
18111 }
18112 /*
18113 * SPEC
18114 * "... a simple type definition which restricts the simple
18115 * type definition identified in clause 1.1 or clause 1.2
18116 * with a set of facet components"
18117 *
18118 * Create the anonymous simple type, which will be the content
18119 * type of the complex type.
18120 */
18121#ifdef ENABLE_NAMED_LOCALS
18122 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18123 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018124 content = xmlSchemaAddType(pctxt, pctxt->schema,
18125 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018126 type->node, 0);
18127#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018128 content = xmlSchemaAddType(pctxt, pctxt->schema,
18129 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018130 type->node, 0);
18131#endif
18132 if (content == NULL)
18133 goto exit_failure;
18134 /*
18135 * We will use the same node as for the <complexType>
18136 * to have it somehow anchored in the schema doc.
18137 */
18138 content->type = XML_SCHEMA_TYPE_SIMPLE;
18139 content->baseType = contentBase;
18140 /*
18141 * Move the facets, previously anchored on the
18142 * complexType during parsing.
18143 */
18144 content->facets = type->facets;
18145 type->facets = NULL;
18146 content->facetSet = type->facetSet;
18147 type->facetSet = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000018148
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018149 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018150 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018151 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018152 /*
18153 * Fixup the newly created type. We don't need to check
18154 * for circularity here.
18155 */
18156 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018157 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018158 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018159 HFAILURE HERROR
18160
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018161 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018162 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18163 (WXS_IS_RESTRICTION(type))) {
18164 /*
18165 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18166 * an emptiable particle, then a simple type definition which
18167 * restricts the <restriction>'s <simpleType> child.
18168 */
18169 if ((type->contentTypeDef == NULL) ||
18170 (type->contentTypeDef->baseType == NULL)) {
18171 /*
18172 * TODO: Check if this ever happens.
18173 */
18174 xmlSchemaPCustomErr(pctxt,
18175 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018176 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018177 "Internal error: xmlSchemaTypeFixup, "
18178 "complex type '%s': the <simpleContent><restriction> "
18179 "is missing a <simpleType> child, but was not catched "
18180 "by xmlSchemaCheckSRCCT()", type->name);
18181 goto exit_failure;
18182 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018183 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018184 /*
18185 * SPEC (3) If <extension> + base is <complexType> with
18186 * <simpleType> content, "...then the {content type} of that
18187 * complex type definition"
18188 */
18189 if (baseType->contentTypeDef == NULL) {
18190 /*
18191 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18192 * should have catched this already.
18193 */
18194 xmlSchemaPCustomErr(pctxt,
18195 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018196 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018197 "Internal error: xmlSchemaTypeFixup, "
18198 "complex type '%s': the <extension>ed base type is "
18199 "a complex type with no simple content type",
18200 type->name);
18201 goto exit_failure;
18202 }
18203 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018204 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018205 /*
18206 * SPEC (4) <extension> + base is <simpleType>
18207 * "... then that simple type definition"
18208 */
18209 type->contentTypeDef = baseType;
18210 } else {
18211 /*
18212 * TODO: Check if this ever happens.
18213 */
18214 xmlSchemaPCustomErr(pctxt,
18215 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018216 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018217 "Internal error: xmlSchemaTypeFixup, "
18218 "complex type '%s' with <simpleContent>: unhandled "
18219 "derivation case", type->name);
18220 goto exit_failure;
18221 }
18222 } else {
18223 int dummySequence = 0;
18224 xmlSchemaParticlePtr particle =
18225 (xmlSchemaParticlePtr) type->subtypes;
18226 /*
18227 * Corresponds to <complexType><complexContent>...
18228 *
18229 * NOTE that the effective mixed was already set during parsing of
18230 * <complexType> and <complexContent>; its flag value is
18231 * XML_SCHEMAS_TYPE_MIXED.
18232 *
18233 * Compute the "effective content":
18234 * (2.1.1) + (2.1.2) + (2.1.3)
18235 */
18236 if ((particle == NULL) ||
18237 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18238 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18239 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18240 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18241 (particle->minOccurs == 0))) &&
18242 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18243 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18244 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000018245 * SPEC (2.1.4) "If the �effective mixed� is true, then
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018246 * a particle whose properties are as follows:..."
18247 *
18248 * Empty sequence model group with
18249 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18250 * NOTE that we sill assign it the <complexType> node to
18251 * somehow anchor it in the doc.
18252 */
18253 if ((particle == NULL) ||
18254 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18255 /*
18256 * Create the particle.
18257 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018258 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018259 type->node, 1, 1);
18260 if (particle == NULL)
18261 goto exit_failure;
18262 /*
18263 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018264 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018265 particle->children = (xmlSchemaTreeItemPtr)
18266 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18267 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18268 if (particle->children == NULL)
18269 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018270
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018271 type->subtypes = (xmlSchemaTypePtr) particle;
18272 }
18273 dummySequence = 1;
18274 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18275 } else {
18276 /*
18277 * SPEC (2.1.5) "otherwise empty"
18278 */
18279 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18280 }
18281 } else {
18282 /*
18283 * SPEC (2.2) "otherwise the particle corresponding to the
18284 * <all>, <choice>, <group> or <sequence> among the
18285 * [children]."
18286 */
18287 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18288 }
18289 /*
18290 * Compute the "content type".
18291 */
18292 if (WXS_IS_RESTRICTION(type)) {
18293 /*
18294 * SPEC (3.1) "If <restriction>..."
18295 * (3.1.1) + (3.1.2) */
18296 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18297 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18298 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18299 }
18300 } else {
18301 /*
18302 * SPEC (3.2) "If <extension>..."
18303 */
18304 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18305 /*
18306 * SPEC (3.2.1)
Rob Richardsc6947bb2008-06-29 15:04:41 +000018307 * "If the �effective content� is empty, then the
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018308 * {content type} of the [...] base ..."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018309 */
18310 type->contentType = baseType->contentType;
18311 type->subtypes = baseType->subtypes;
18312 /*
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018313 * Fixes bug #347316:
18314 * This is the case when the base type has a simple
18315 * type definition as content.
18316 */
18317 type->contentTypeDef = baseType->contentTypeDef;
18318 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018319 * NOTE that the effective mixed is ignored here.
18320 */
18321 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18322 /*
18323 * SPEC (3.2.2)
18324 */
18325 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18326 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18327 } else {
18328 /*
18329 * SPEC (3.2.3)
18330 */
18331 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18332 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18333 /*
18334 * "A model group whose {compositor} is sequence and whose
18335 * {particles} are..."
18336 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018337 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18338 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18339 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18340 XML_SCHEMA_TYPE_ALL))
18341 {
18342 /*
18343 * SPEC cos-all-limited (1)
18344 */
18345 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18346 /* TODO: error code */
18347 XML_SCHEMAP_COS_ALL_LIMITED,
18348 WXS_ITEM_NODE(type), NULL,
18349 "The type has an 'all' model group in its "
18350 "{content type} and thus cannot be derived from "
18351 "a non-empty type, since this would produce a "
18352 "'sequence' model group containing the 'all' "
18353 "model group; 'all' model groups are not "
18354 "allowed to appear inside other model groups",
18355 NULL, NULL);
18356
18357 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18358 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18359 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18360 XML_SCHEMA_TYPE_ALL))
18361 {
18362 /*
18363 * SPEC cos-all-limited (1)
18364 */
18365 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18366 /* TODO: error code */
18367 XML_SCHEMAP_COS_ALL_LIMITED,
18368 WXS_ITEM_NODE(type), NULL,
18369 "A type cannot be derived by extension from a type "
18370 "which has an 'all' model group in its "
18371 "{content type}, since this would produce a "
18372 "'sequence' model group containing the 'all' "
18373 "model group; 'all' model groups are not "
18374 "allowed to appear inside other model groups",
18375 NULL, NULL);
18376
18377 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018378 xmlSchemaTreeItemPtr effectiveContent =
18379 (xmlSchemaTreeItemPtr) type->subtypes;
18380 /*
18381 * Create the particle.
18382 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018383 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018384 type->node, 1, 1);
18385 if (particle == NULL)
18386 goto exit_failure;
18387 /*
18388 * Create the "sequence" model group.
18389 */
18390 particle->children = (xmlSchemaTreeItemPtr)
18391 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18392 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18393 if (particle->children == NULL)
18394 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018395 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018396 /*
18397 * SPEC "the particle of the {content type} of
18398 * the ... base ..."
18399 * Create a duplicate of the base type's particle
18400 * and assign its "term" to it.
18401 */
18402 particle->children->children =
18403 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018404 type->node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018405 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18406 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18407 if (particle->children->children == NULL)
18408 goto exit_failure;
18409 particle = (xmlSchemaParticlePtr)
18410 particle->children->children;
18411 particle->children =
18412 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18413 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000018414 * SPEC "followed by the �effective content�."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018415 */
18416 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018417 /*
18418 * This all will result in:
18419 * new-particle
18420 * --> new-sequence(
18421 * new-particle
18422 * --> base-model,
18423 * this-particle
18424 * --> this-model
18425 * )
18426 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018427 } else {
18428 /*
18429 * This is the case when there is already an empty
18430 * <sequence> with minOccurs==maxOccurs==1.
18431 * Just add the base types's content type.
18432 * NOTE that, although we miss to add an intermediate
18433 * <sequence>, this should produce no difference to
18434 * neither the regex compilation of the content model,
18435 * nor to the complex type contraints.
18436 */
18437 particle->children->children =
18438 (xmlSchemaTreeItemPtr) baseType->subtypes;
18439 }
18440 }
18441 }
18442 }
18443 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018444 * Now fixup attribute uses:
18445 * - expand attr. group references
18446 * - intersect attribute wildcards
18447 * - inherit attribute uses of the base type
18448 * - inherit or union attr. wildcards if extending
18449 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018450 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018451 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018452 HFAILURE HERROR
18453 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018454 * Apply the complex type component constraints; this will not
18455 * check attributes, since this is done in
18456 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018457 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018458 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018459 HFAILURE HERROR
18460
18461#ifdef DEBUG_TYPE
18462 xmlSchemaDebugFixedType(pctxt, type);
18463#endif
18464 if (olderrs != pctxt->nberrors)
18465 return(pctxt->err);
18466 else
18467 return(0);
18468
18469exit_error:
18470 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18471#ifdef DEBUG_TYPE
18472 xmlSchemaDebugFixedType(pctxt, type);
18473#endif
18474 return(pctxt->err);
18475
18476exit_failure:
18477 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18478#ifdef DEBUG_TYPE
18479 xmlSchemaDebugFixedType(pctxt, type);
18480#endif
18481 return(-1);
18482}
18483
18484
18485/**
18486 * xmlSchemaTypeFixup:
18487 * @typeDecl: the schema type definition
18488 * @ctxt: the schema parser context
18489 *
18490 * Fixes the content model of the type.
18491 * URGENT TODO: We need an int result!
18492 */
18493static int
18494xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018495 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018496{
18497 if (type == NULL)
18498 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018499 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18500 AERROR_INT("xmlSchemaTypeFixup",
18501 "this function needs a parser context");
18502 return(-1);
18503 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018504 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018505 return(0);
18506 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018507 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018508 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018509 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018510 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018511}
18512
18513/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018514 * xmlSchemaCheckFacet:
18515 * @facet: the facet
18516 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018517 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018518 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018519 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018520 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018521 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018522 * Returns 0 if valid, a positive error code if not valid and
18523 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018524 */
18525int
18526xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018527 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018528 xmlSchemaParserCtxtPtr pctxt,
18529 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018530{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018531 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018532
Daniel Veillardce682bc2004-11-05 17:22:25 +000018533 if ((facet == NULL) || (typeDecl == NULL))
18534 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018536 * TODO: will the parser context be given if used from
18537 * the relaxNG module?
18538 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018539 if (pctxt == NULL)
18540 ctxtGiven = 0;
18541 else
18542 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018543
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018544 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018545 case XML_SCHEMA_FACET_MININCLUSIVE:
18546 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18547 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018548 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18549 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018550 /*
18551 * Okay we need to validate the value
18552 * at that point.
18553 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018554 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018555
18556 /* 4.3.5.5 Constraints on enumeration Schema Components
18557 * Schema Component Constraint: enumeration valid restriction
Rob Richardsc6947bb2008-06-29 15:04:41 +000018558 * It is an �error� if any member of {value} is not in the
18559 * �value space� of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018560 *
18561 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Rob Richardsc6947bb2008-06-29 15:04:41 +000018562 * The value �must� be in the
18563 * �value space� of the �base type�.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018564 */
18565 /*
18566 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018567 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018568 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018569 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018570 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018571 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018572 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018573 */
18574 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18575 base = typeDecl->baseType;
18576 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018577 PERROR_INT("xmlSchemaCheckFacet",
18578 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018579 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018580 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018581 } else
18582 base = typeDecl;
Daniel Veillarddee23482008-04-11 12:58:43 +000018583
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018584 if (! ctxtGiven) {
18585 /*
18586 * A context is needed if called from RelaxNG.
Daniel Veillarddee23482008-04-11 12:58:43 +000018587 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018588 pctxt = xmlSchemaNewParserCtxt("*");
18589 if (pctxt == NULL)
18590 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018591 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018592 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018593 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018594 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018595 * facet->node is just the node holding the facet
18596 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018597 * of the facet.
Daniel Veillarddee23482008-04-11 12:58:43 +000018598 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018599 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018600 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018601 facet->value, &(facet->val), 1, 1, 0);
18602 if (ret != 0) {
18603 if (ret < 0) {
18604 /* No error message for RelaxNG. */
Daniel Veillarddee23482008-04-11 12:58:43 +000018605 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018606 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018607 XML_SCHEMAP_INTERNAL, facet->node, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000018608 "Internal error: xmlSchemaCheckFacet, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018609 "failed to validate the value '%s' of the "
18610 "facet '%s' against the base type",
18611 facet->value, xmlSchemaFacetTypeToString(facet->type));
18612 }
18613 goto internal_error;
18614 }
18615 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18616 /* No error message for RelaxNG. */
18617 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018618 xmlChar *str = NULL;
18619
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018620 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018621 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018622 "The value '%s' of the facet does not validate "
18623 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018624 facet->value,
18625 xmlSchemaFormatQName(&str,
18626 base->targetNamespace, base->name));
18627 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018628 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018629 goto exit;
18630 } else if (facet->val == NULL) {
18631 if (ctxtGiven) {
18632 PERROR_INT("xmlSchemaCheckFacet",
18633 "value was not computed");
18634 }
18635 TODO
18636 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018637 break;
18638 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018639 case XML_SCHEMA_FACET_PATTERN:
18640 facet->regexp = xmlRegexpCompile(facet->value);
18641 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018642 ret = XML_SCHEMAP_REGEXP_INVALID;
18643 /* No error message for RelaxNG. */
18644 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018645 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018646 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018647 "The value '%s' of the facet 'pattern' is not a "
18648 "valid regular expression",
18649 facet->value, NULL);
18650 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018651 }
18652 break;
18653 case XML_SCHEMA_FACET_TOTALDIGITS:
18654 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18655 case XML_SCHEMA_FACET_LENGTH:
18656 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018657 case XML_SCHEMA_FACET_MINLENGTH:
18658
18659 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18660 ret = xmlSchemaValidatePredefinedType(
18661 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18662 facet->value, &(facet->val));
18663 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018664 ret = xmlSchemaValidatePredefinedType(
18665 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18666 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018667 }
18668 if (ret != 0) {
18669 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018670 /* No error message for RelaxNG. */
18671 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018672 PERROR_INT("xmlSchemaCheckFacet",
18673 "validating facet value");
18674 }
18675 goto internal_error;
18676 }
18677 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18678 /* No error message for RelaxNG. */
18679 if (ctxtGiven) {
18680 /* error code */
18681 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18682 ret, facet->node, WXS_BASIC_CAST typeDecl,
Daniel Veillarddee23482008-04-11 12:58:43 +000018683 "The value '%s' of the facet '%s' is not a valid '%s'",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018684 facet->value,
18685 xmlSchemaFacetTypeToString(facet->type),
Daniel Veillarddee23482008-04-11 12:58:43 +000018686 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018687 BAD_CAST "nonNegativeInteger" :
18688 BAD_CAST "positiveInteger",
18689 NULL);
18690 }
18691 }
18692 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000018693
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018694 case XML_SCHEMA_FACET_WHITESPACE:{
18695 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18696 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18697 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18698 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18699 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18700 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18701 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018702 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18703 /* No error message for RelaxNG. */
18704 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018705 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018706 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018707 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018708 "The value '%s' of the facet 'whitespace' is not "
18709 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018710 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018711 }
18712 }
18713 default:
18714 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018715 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018716exit:
18717 if ((! ctxtGiven) && (pctxt != NULL))
18718 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018719 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018720internal_error:
18721 if ((! ctxtGiven) && (pctxt != NULL))
18722 xmlSchemaFreeParserCtxt(pctxt);
18723 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018724}
18725
18726/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018727 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018728 * @typeDecl: the schema type definition
18729 * @ctxt: the schema parser context
18730 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018731 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018732 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018733static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018734xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018735 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018736{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018737 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018738 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018739 /*
18740 * NOTE: It is intended to use the facets list, instead
18741 * of facetSet.
18742 */
18743 if (typeDecl->facets != NULL) {
18744 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018745
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018746 /*
18747 * Temporarily assign the "schema" to the validation context
18748 * of the parser context. This is needed for NOTATION validation.
18749 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018750 if (pctxt->vctxt == NULL) {
18751 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18752 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018753 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018754 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018755 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018756 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18757 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018758 facet = facet->next;
18759 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018760 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018761 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018762 if (olderrs != pctxt->nberrors)
18763 return(pctxt->err);
18764 return(0);
18765exit_failure:
18766 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018767}
18768
18769/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018770 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018771 * @ctxtMGroup: the searched model group
18772 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018773 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018774 *
18775 * This one is intended to be used by
18776 * xmlSchemaCheckGroupDefCircular only.
18777 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018778 * Returns the particle with the circular model group definition reference,
18779 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018780 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018781static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018782xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018783 xmlSchemaTreeItemPtr particle)
18784{
18785 xmlSchemaTreeItemPtr circ = NULL;
18786 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018787 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018788
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018789 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018790 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018791 if (term == NULL)
18792 continue;
18793 switch (term->type) {
18794 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018795 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018796 if (gdef == groupDef)
18797 return (particle);
18798 /*
18799 * Mark this model group definition to avoid infinite
18800 * recursion on circular references not yet examined.
18801 */
18802 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18803 continue;
18804 if (gdef->children != NULL) {
18805 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18806 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18807 gdef->children->children);
18808 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18809 if (circ != NULL)
18810 return (circ);
18811 }
18812 break;
18813 case XML_SCHEMA_TYPE_SEQUENCE:
18814 case XML_SCHEMA_TYPE_CHOICE:
18815 case XML_SCHEMA_TYPE_ALL:
18816 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18817 if (circ != NULL)
18818 return (circ);
18819 break;
18820 default:
18821 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018822 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018823 }
18824 return (NULL);
18825}
18826
18827/**
18828 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018829 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018830 * @ctxt: the parser context
18831 * @name: the name
18832 *
18833 * Checks for circular references to model group definitions.
18834 */
18835static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018836xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018837 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018838{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018839 /*
18840 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018841 * 2 Circular groups are disallowed. That is, within the {particles}
18842 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018843 * is the group itself.
18844 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018845 if ((item == NULL) ||
18846 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18847 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018848 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018849 {
18850 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018851
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018852 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018853 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018854 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018855 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018856 * TODO: The error report is not adequate: this constraint
18857 * is defined for model groups but not definitions, but since
18858 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018859 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018860 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018861 */
18862 xmlSchemaPCustomErr(ctxt,
18863 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018864 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018865 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018866 "defined", xmlSchemaFormatQName(&str,
18867 item->targetNamespace, item->name));
18868 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018869 /*
18870 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018871 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018872 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018873 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018874 }
18875 }
18876}
18877
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018878/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018879 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018880 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018881 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018882 *
18883 * Assigns the model group of model group definitions to the "term"
18884 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018885 * In xmlSchemaResolveModelGroupParticleReferences the model group
18886 * definitions were assigned to the "term", since needed for the
18887 * circularity check.
18888 *
18889 * Schema Component Constraint:
18890 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018891 */
18892static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018893xmlSchemaModelGroupToModelGroupDefFixup(
18894 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18895 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018896{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018897 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18898
18899 while (particle != NULL) {
18900 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18901 ((WXS_PARTICLE_TERM(particle))->type !=
18902 XML_SCHEMA_TYPE_GROUP))
18903 {
18904 particle = WXS_PTC_CAST particle->next;
18905 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000018906 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018907 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18908 /*
18909 * TODO: Remove the particle.
18910 */
18911 WXS_PARTICLE_TERM(particle) = NULL;
18912 particle = WXS_PTC_CAST particle->next;
18913 continue;
18914 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018915 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018916 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018917 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018918 WXS_PARTICLE_TERM(particle) =
18919 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18920
18921 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018922 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018923}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018924
18925/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018926 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018927 * @ctxtGr: the searched attribute group
18928 * @attr: the current attribute list to be processed
18929 *
18930 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018931 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018932 *
18933 * Returns the circular attribute grou reference, otherwise NULL.
18934 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018935static xmlSchemaQNameRefPtr
18936xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18937 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018938{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018939 xmlSchemaAttributeGroupPtr gr;
18940 xmlSchemaQNameRefPtr ref, circ;
18941 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018942 /*
18943 * We will search for an attribute group reference which
18944 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018945 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018946 for (i = 0; i < list->nbItems; i++) {
18947 ref = list->items[i];
18948 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18949 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18950 (ref->item != NULL))
18951 {
18952 gr = WXS_ATTR_GROUP_CAST ref->item;
18953 if (gr == ctxtGr)
18954 return(ref);
18955 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
Daniel Veillarddee23482008-04-11 12:58:43 +000018956 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018957 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018958 * Mark as visited to avoid infinite recursion on
18959 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018960 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018961 if ((gr->attrUses) &&
18962 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18963 {
18964 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18965 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000018966 (xmlSchemaItemListPtr) gr->attrUses);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018967 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18968 if (circ != NULL)
18969 return (circ);
18970 }
Daniel Veillarddee23482008-04-11 12:58:43 +000018971
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018972 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018973 }
18974 return (NULL);
18975}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018976
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018977/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018978 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018979 * attrGr: the attribute group definition
18980 * @ctxt: the parser context
18981 * @name: the name
18982 *
18983 * Checks for circular references of attribute groups.
18984 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018985static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018986xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018987 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018988{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018989 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018990 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018991 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018992 * 3 Circular group reference is disallowed outside <redefine>.
18993 * That is, unless this element information item's parent is
18994 * <redefine>, then among the [children], if any, there must
18995 * not be an <attributeGroup> with ref [attribute] which resolves
18996 * to the component corresponding to this <attributeGroup>. Indirect
18997 * circularity is also ruled out. That is, when QName resolution
Rob Richardsc6947bb2008-06-29 15:04:41 +000018998 * (Schema Document) (�3.15.3) is applied to a �QName� arising from
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018999 * any <attributeGroup>s with a ref [attribute] among the [children],
Rob Richardsc6947bb2008-06-29 15:04:41 +000019000 * it must not be the case that a �QName� is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019001 * which resolves to the component corresponding to this <attributeGroup>.
19002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019003 if (attrGr->attrUses == NULL)
19004 return(0);
19005 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
19006 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019007 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019008 xmlSchemaQNameRefPtr circ;
Daniel Veillarddee23482008-04-11 12:58:43 +000019009
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019010 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000019011 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019012 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019013 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019014 /*
19015 * TODO: Report the referenced attr group as QName.
19016 */
19017 xmlSchemaPCustomErr(ctxt,
19018 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019019 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019020 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019021 "defined", xmlSchemaGetComponentQName(&str, attrGr));
19022 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019023 /*
19024 * NOTE: We will cut the reference to avoid further
19025 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019026 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019027 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019028 circ->item = NULL;
19029 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019030 }
19031 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019032 return(0);
19033}
19034
19035static int
19036xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19037 xmlSchemaAttributeGroupPtr attrGr);
19038
19039/**
19040 * xmlSchemaExpandAttributeGroupRefs:
19041 * @pctxt: the parser context
19042 * @node: the node of the component holding the attribute uses
Daniel Veillarddee23482008-04-11 12:58:43 +000019043 * @completeWild: the intersected wildcard to be returned
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019044 * @list: the attribute uses
19045 *
19046 * Substitutes contained attribute group references
19047 * for their attribute uses. Wilcards are intersected.
19048 * Attribute use prohibitions are removed from the list
19049 * and returned via the @prohibs list.
19050 * Pointlessness of attr. prohibs, if a matching attr. decl
19051 * is existent a well, are checked.
19052 */
19053static int
19054xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19055 xmlSchemaBasicItemPtr item,
19056 xmlSchemaWildcardPtr *completeWild,
19057 xmlSchemaItemListPtr list,
19058 xmlSchemaItemListPtr prohibs)
19059{
19060 xmlSchemaAttributeGroupPtr gr;
19061 xmlSchemaAttributeUsePtr use;
19062 xmlSchemaItemListPtr sublist;
19063 int i, j;
19064 int created = (*completeWild == NULL) ? 0 : 1;
19065
19066 if (prohibs)
19067 prohibs->nbItems = 0;
19068
19069 for (i = 0; i < list->nbItems; i++) {
19070 use = list->items[i];
19071
Daniel Veillarddee23482008-04-11 12:58:43 +000019072 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019073 if (prohibs == NULL) {
19074 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19075 "unexpected attr prohibition found");
19076 return(-1);
19077 }
19078 /*
19079 * Remove from attribute uses.
19080 */
19081 if (xmlSchemaItemListRemove(list, i) == -1)
19082 return(-1);
19083 i--;
19084 /*
19085 * Note that duplicate prohibitions were already
19086 * handled at parsing time.
Daniel Veillarddee23482008-04-11 12:58:43 +000019087 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019088 /*
19089 * Add to list of prohibitions.
19090 */
19091 xmlSchemaItemListAddSize(prohibs, 2, use);
19092 continue;
19093 }
19094 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19095 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19096 {
19097 if ((WXS_QNAME_CAST use)->item == NULL)
19098 return(-1);
19099 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19100 /*
19101 * Expand the referenced attr. group.
19102 * TODO: remove this, this is done in a previous step, so
19103 * already done here.
19104 */
19105 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19106 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19107 return(-1);
19108 }
19109 /*
19110 * Build the 'complete' wildcard; i.e. intersect multiple
19111 * wildcards.
19112 */
19113 if (gr->attributeWildcard != NULL) {
19114 if (*completeWild == NULL) {
19115 *completeWild = gr->attributeWildcard;
19116 } else {
19117 if (! created) {
19118 xmlSchemaWildcardPtr tmpWild;
19119
19120 /*
19121 * Copy the first encountered wildcard as context,
19122 * except for the annotation.
19123 *
19124 * Although the complete wildcard might not correspond
19125 * to any node in the schema, we will anchor it on
19126 * the node of the owner component.
19127 */
19128 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19129 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19130 WXS_ITEM_NODE(item));
19131 if (tmpWild == NULL)
19132 return(-1);
19133 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19134 tmpWild, *completeWild) == -1)
19135 return (-1);
19136 tmpWild->processContents = (*completeWild)->processContents;
19137 *completeWild = tmpWild;
19138 created = 1;
19139 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019140
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019141 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19142 gr->attributeWildcard) == -1)
19143 return(-1);
19144 }
19145 }
19146 /*
19147 * Just remove the reference if the referenced group does not
19148 * contain any attribute uses.
19149 */
William M. Brack06559b32007-03-14 09:34:15 +000019150 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19151 if ((sublist == NULL) || sublist->nbItems == 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019152 if (xmlSchemaItemListRemove(list, i) == -1)
19153 return(-1);
19154 i--;
19155 continue;
19156 }
19157 /*
19158 * Add the attribute uses.
19159 */
William M. Brack06559b32007-03-14 09:34:15 +000019160 list->items[i] = sublist->items[0];
19161 if (sublist->nbItems != 1) {
19162 for (j = 1; j < sublist->nbItems; j++) {
19163 i++;
19164 if (xmlSchemaItemListInsert(list,
19165 sublist->items[j], i) == -1)
19166 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019167 }
William M. Brack06559b32007-03-14 09:34:15 +000019168 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019169 }
19170
19171 }
19172 /*
19173 * Handle pointless prohibitions of declared attributes.
19174 */
19175 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19176 xmlSchemaAttributeUseProhibPtr prohib;
19177
19178 for (i = prohibs->nbItems -1; i >= 0; i--) {
19179 prohib = prohibs->items[i];
19180 for (j = 0; j < list->nbItems; j++) {
19181 use = list->items[j];
19182
19183 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19184 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19185 {
19186 xmlChar *str = NULL;
19187
19188 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19189 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19190 prohib->node, NULL,
19191 "Skipping pointless attribute use prohibition "
19192 "'%s', since a corresponding attribute use "
19193 "exists already in the type definition",
19194 xmlSchemaFormatQName(&str,
19195 prohib->targetNamespace, prohib->name),
19196 NULL, NULL);
19197 FREE_AND_NULL(str);
19198 /*
19199 * Remove the prohibition.
19200 */
19201 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19202 return(-1);
19203 break;
19204 }
19205 }
19206 }
19207 }
19208 return(0);
19209}
19210
19211/**
19212 * xmlSchemaAttributeGroupExpandRefs:
19213 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019214 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019215 *
19216 * Computation of:
19217 * {attribute uses} property
19218 * {attribute wildcard} property
19219 *
19220 * Substitutes contained attribute group references
19221 * for their attribute uses. Wilcards are intersected.
19222 */
19223static int
19224xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19225 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019226{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019227 if ((attrGr->attrUses == NULL) ||
19228 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19229 return(0);
19230
19231 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19232 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19233 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000019234 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019235 return(0);
19236}
19237
19238/**
19239 * xmlSchemaAttributeGroupExpandRefs:
19240 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019241 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019242 *
19243 * Substitutes contained attribute group references
19244 * for their attribute uses. Wilcards are intersected.
Daniel Veillarddee23482008-04-11 12:58:43 +000019245 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019246 * Schema Component Constraint:
Daniel Veillarddee23482008-04-11 12:58:43 +000019247 * Attribute Group Definition Properties Correct (ag-props-correct)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019248 */
19249static int
19250xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19251 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019252{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019253 /*
19254 * SPEC ag-props-correct
19255 * (1) "The values of the properties of an attribute group definition
19256 * must be as described in the property tableau in The Attribute
Rob Richardsc6947bb2008-06-29 15:04:41 +000019257 * Group Definition Schema Component (�3.6.1), modulo the impact of
19258 * Missing Sub-components (�5.3);"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019259 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019260
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019261 if ((attrGr->attrUses != NULL) &&
19262 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19263 {
19264 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19265 xmlSchemaAttributeUsePtr use, tmp;
19266 int i, j, hasId = 0;
19267
19268 for (i = uses->nbItems -1; i >= 0; i--) {
Daniel Veillarddee23482008-04-11 12:58:43 +000019269 use = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019270 /*
19271 * SPEC ag-props-correct
19272 * (2) "Two distinct members of the {attribute uses} must not have
19273 * {attribute declaration}s both of whose {name}s match and whose
19274 * {target namespace}s are identical."
19275 */
19276 if (i > 0) {
19277 for (j = i -1; j >= 0; j--) {
19278 tmp = uses->items[j];
19279 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19280 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19281 (WXS_ATTRUSE_DECL_TNS(use) ==
19282 WXS_ATTRUSE_DECL_TNS(tmp)))
19283 {
19284 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019285
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019286 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19287 XML_SCHEMAP_AG_PROPS_CORRECT,
19288 attrGr->node, WXS_BASIC_CAST attrGr,
19289 "Duplicate %s",
19290 xmlSchemaGetComponentDesignation(&str, use),
19291 NULL);
19292 FREE_AND_NULL(str);
19293 /*
19294 * Remove the duplicate.
19295 */
19296 if (xmlSchemaItemListRemove(uses, i) == -1)
19297 return(-1);
19298 goto next_use;
19299 }
19300 }
19301 }
19302 /*
19303 * SPEC ag-props-correct
19304 * (3) "Two distinct members of the {attribute uses} must not have
19305 * {attribute declaration}s both of whose {type definition}s are or
19306 * are derived from ID."
19307 * TODO: Does 'derived' include member-types of unions?
19308 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019309 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019310 if (xmlSchemaIsDerivedFromBuiltInType(
19311 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000019312 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019313 if (hasId) {
19314 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019315
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019316 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19317 XML_SCHEMAP_AG_PROPS_CORRECT,
19318 attrGr->node, WXS_BASIC_CAST attrGr,
19319 "There must not exist more than one attribute "
19320 "declaration of type 'xs:ID' "
19321 "(or derived from 'xs:ID'). The %s violates this "
19322 "constraint",
19323 xmlSchemaGetComponentDesignation(&str, use),
19324 NULL);
19325 FREE_AND_NULL(str);
19326 if (xmlSchemaItemListRemove(uses, i) == -1)
19327 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000019328 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019329 hasId = 1;
19330 }
19331 }
19332next_use: {}
19333 }
19334 }
19335 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019336}
19337
19338/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019339 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019340 * @attrgrpDecl: the schema attribute definition
19341 * @ctxt: the schema parser context
19342 * @name: the attribute name
19343 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019345 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019346static int
19347xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19348 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019349{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019350 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019351
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019352 if (ref->item != NULL)
19353 return(0);
19354 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19355 ref->name,
19356 ref->targetNamespace);
19357 if (group == NULL) {
19358 xmlSchemaPResCompAttrErr(ctxt,
19359 XML_SCHEMAP_SRC_RESOLVE,
19360 NULL, ref->node,
19361 "ref", ref->name, ref->targetNamespace,
19362 ref->itemType, NULL);
19363 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019364 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019365 ref->item = WXS_BASIC_CAST group;
19366 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019367}
19368
19369/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019370 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019371 * @item: an schema attribute declaration/use
19372 * @ctxt: a schema parser context
19373 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019374 *
19375 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019376 * Schema Component Constraint:
19377 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019378 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019379 * Validates the value constraints of an attribute declaration/use.
19380 * NOTE that this needs the simle type definitions to be already
19381 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019382 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019383static int
19384xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19385 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019386{
19387
19388 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019389 * SPEC a-props-correct (1)
19390 * "The values of the properties of an attribute declaration must
19391 * be as described in the property tableau in The Attribute
Rob Richardsc6947bb2008-06-29 15:04:41 +000019392 * Declaration Schema Component (�3.2.1), modulo the impact of
19393 * Missing Sub-components (�5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019394 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019395
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019396 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19397 return(0);
19398
19399 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019400 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019401
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019402 /*
19403 * SPEC a-props-correct (3)
19404 * "If the {type definition} is or is derived from ID then there
19405 * must not be a {value constraint}."
19406 */
19407 if (xmlSchemaIsDerivedFromBuiltInType(
19408 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19409 {
19410 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19411 XML_SCHEMAP_A_PROPS_CORRECT_3,
19412 NULL, WXS_BASIC_CAST attr,
19413 "Value constraints are not allowed if the type definition "
19414 "is or is derived from xs:ID",
19415 NULL, NULL);
19416 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019417 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019418 /*
19419 * SPEC a-props-correct (2)
19420 * "if there is a {value constraint}, the canonical lexical
Rob Richardsc6947bb2008-06-29 15:04:41 +000019421 * representation of its value must be �valid� with respect
19422 * to the {type definition} as defined in String Valid (�3.14.4)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019423 * TODO: Don't care about the *cononical* stuff here, this requirement
19424 * will be removed in WXS 1.1 anyway.
19425 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019426 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019427 attr->node, WXS_ATTR_TYPEDEF(attr),
19428 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019429 1, 1, 0);
19430 if (ret != 0) {
19431 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019432 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019433 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019434 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019435 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019436 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019437 XML_SCHEMAP_A_PROPS_CORRECT_2,
19438 NULL, WXS_BASIC_CAST attr,
19439 "The value of the value constraint is not valid",
19440 NULL, NULL);
19441 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019442 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019443 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019444
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019445 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019446}
19447
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019448static xmlSchemaElementPtr
19449xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19450 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019451{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019452 xmlSchemaElementPtr ret;
19453
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019454 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019455 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019456 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019457 return (ancestor);
19458
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019459 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019460 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019461 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019462 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019463 WXS_SUBST_HEAD(ancestor));
19464 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019465
19466 return (ret);
19467}
19468
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019469/**
19470 * xmlSchemaCheckElemPropsCorrect:
19471 * @ctxt: a schema parser context
19472 * @decl: the element declaration
19473 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019474 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019475 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019476 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019477 *
19478 * STATUS:
19479 * missing: (6)
19480 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019481static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019482xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19483 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019484{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019485 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019486 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019487 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019488 * SPEC (1) "The values of the properties of an element declaration
19489 * must be as described in the property tableau in The Element
Rob Richardsc6947bb2008-06-29 15:04:41 +000019490 * Declaration Schema Component (�3.3.1), modulo the impact of Missing
19491 * Sub-components (�5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019492 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019493 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19494 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019495
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019496 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019497 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000019498 * SPEC (3) "If there is a non-�absent� {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019499 * affiliation}, then {scope} must be global."
19500 */
19501 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19502 xmlSchemaPCustomErr(pctxt,
19503 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019504 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019505 "Only global element declarations can have a "
19506 "substitution group affiliation", NULL);
19507 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019508 }
19509 /*
19510 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19511 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019512 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019513 * property."
19514 */
19515 if (head == elemDecl)
19516 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019517 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019518 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19519 else
19520 circ = NULL;
19521 if (circ != NULL) {
19522 xmlChar *strA = NULL, *strB = NULL;
19523
19524 xmlSchemaPCustomErrExt(pctxt,
19525 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019526 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019527 "The element declaration '%s' defines a circular "
19528 "substitution group to element declaration '%s'",
19529 xmlSchemaGetComponentQName(&strA, circ),
19530 xmlSchemaGetComponentQName(&strB, head),
19531 NULL);
19532 FREE_AND_NULL(strA)
19533 FREE_AND_NULL(strB)
19534 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19535 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019536 /*
19537 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019538 * the {type definition}
19539 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019540 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019541 * of the {substitution group exclusions} of the {substitution group
Rob Richardsc6947bb2008-06-29 15:04:41 +000019542 * affiliation}, as defined in Type Derivation OK (Complex) (�3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019543 * (if the {type definition} is complex) or as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000019544 * Type Derivation OK (Simple) (�3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019545 * simple)."
19546 *
19547 * NOTE: {substitution group exclusions} means the values of the
19548 * attribute "final".
19549 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019550
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019551 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019552 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019553
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019554 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19555 set |= SUBSET_EXTENSION;
19556 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19557 set |= SUBSET_RESTRICTION;
19558
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019559 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019560 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019561 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19562
19563 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019564 xmlSchemaPCustomErrExt(pctxt,
19565 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019566 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019567 "The type definition '%s' was "
19568 "either rejected by the substitution group "
19569 "affiliation '%s', or not validly derived from its type "
19570 "definition '%s'",
19571 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019572 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019573 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019574 FREE_AND_NULL(strA)
19575 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019576 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019577 }
19578 }
19579 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019580 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019581 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019582 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019583 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019584 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019585 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019586 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019587 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019588 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019589 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019590 (WXS_IS_COMPLEX(typeDef) &&
19591 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019592 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19593 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019594
19595 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19596 xmlSchemaPCustomErr(pctxt,
19597 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019598 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019599 "The type definition (or type definition's content type) is or "
19600 "is derived from ID; value constraints are not allowed in "
19601 "conjunction with such a type definition", NULL);
19602 } else if (elemDecl->value != NULL) {
19603 int vcret;
19604 xmlNodePtr node = NULL;
19605
19606 /*
19607 * SPEC (2) "If there is a {value constraint}, the canonical lexical
Rob Richardsc6947bb2008-06-29 15:04:41 +000019608 * representation of its value must be �valid� with respect to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019609 * {type definition} as defined in Element Default Valid (Immediate)
Rob Richardsc6947bb2008-06-29 15:04:41 +000019610 * (�3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019611 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019612 if (typeDef == NULL) {
19613 xmlSchemaPErr(pctxt, elemDecl->node,
19614 XML_SCHEMAP_INTERNAL,
19615 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19616 "type is missing... skipping validation of "
19617 "the value constraint", NULL, NULL);
19618 return (-1);
19619 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019620 if (elemDecl->node != NULL) {
19621 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19622 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19623 BAD_CAST "fixed");
19624 else
19625 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19626 BAD_CAST "default");
19627 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019628 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19629 typeDef, elemDecl->value, &(elemDecl->defVal));
19630 if (vcret != 0) {
19631 if (vcret < 0) {
19632 PERROR_INT("xmlSchemaElemCheckValConstr",
19633 "failed to validate the value constraint of an "
19634 "element declaration");
19635 return (-1);
19636 }
19637 return (vcret);
19638 }
19639 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019640
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019641 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019642}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019643
19644/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019645 * xmlSchemaCheckElemSubstGroup:
19646 * @ctxt: a schema parser context
19647 * @decl: the element declaration
19648 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019649 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019650 * Schema Component Constraint:
19651 * Substitution Group (cos-equiv-class)
19652 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019653 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019654 * a list will be built for each subst. group head, holding all direct
19655 * referents to this head.
19656 * NOTE that this function needs:
19657 * 1. circular subst. groups to be checked beforehand
19658 * 2. the declaration's type to be derived from the head's type
19659 *
19660 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019661 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019662 */
19663static void
19664xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19665 xmlSchemaElementPtr elemDecl)
19666{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019667 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019668 /* SPEC (1) "Its {abstract} is false." */
19669 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19670 return;
19671 {
19672 xmlSchemaElementPtr head;
19673 xmlSchemaTypePtr headType, type;
19674 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019675 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019676 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19677 * {disallowed substitutions} as the blocking constraint, as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000019678 * Substitution Group OK (Transitive) (�3.3.6)."
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019679 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019680 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19681 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019682 set = 0;
19683 methSet = 0;
19684 /*
19685 * The blocking constraints.
19686 */
19687 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19688 continue;
19689 headType = head->subtypes;
19690 type = elemDecl->subtypes;
19691 if (headType == type)
19692 goto add_member;
19693 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19694 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19695 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19696 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19697 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019698 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019699 * "The set of all {derivation method}s involved in the
19700 * derivation of D's {type definition} from C's {type definition}
19701 * does not intersect with the union of the blocking constraint,
19702 * C's {prohibited substitutions} (if C is complex, otherwise the
19703 * empty set) and the {prohibited substitutions} (respectively the
19704 * empty set) of any intermediate {type definition}s in the
19705 * derivation of D's {type definition} from C's {type definition}."
19706 */
19707 /*
19708 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19709 * subst.head axis, the methSet does not need to be computed for
19710 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019711 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019712 /*
19713 * The set of all {derivation method}s involved in the derivation
19714 */
19715 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019716 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019717 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19718 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019719
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019720 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019721 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19722 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19723
19724 type = type->baseType;
19725 }
19726 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019727 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019728 * the head's type.
19729 */
19730 type = elemDecl->subtypes->baseType;
19731 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019732 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019733 if ((type->flags &
19734 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19735 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19736 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19737 if ((type->flags &
19738 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19739 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19740 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19741 } else
19742 break;
19743 if (type == headType)
19744 break;
19745 type = type->baseType;
19746 }
19747 if ((set != 0) &&
19748 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19749 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19750 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19751 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19752 continue;
19753 }
19754add_member:
19755 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19756 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19757 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19758 }
19759 }
19760}
19761
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019762#ifdef WXS_ELEM_DECL_CONS_ENABLED /* enable when finished */
19763/**
19764 * xmlSchemaCheckElementDeclComponent
19765 * @pctxt: the schema parser context
19766 * @ctxtComponent: the context component (an element declaration)
19767 * @ctxtParticle: the first particle of the context component
19768 * @searchParticle: the element declaration particle to be analysed
19769 *
Daniel Veillarddee23482008-04-11 12:58:43 +000019770 * Schema Component Constraint: Element Declarations Consistent
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019771 */
19772static int
19773xmlSchemaCheckElementDeclConsistent(xmlSchemaParserCtxtPtr pctxt,
19774 xmlSchemaBasicItemPtr ctxtComponent,
Daniel Veillarddee23482008-04-11 12:58:43 +000019775 xmlSchemaParticlePtr ctxtParticle,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019776 xmlSchemaParticlePtr searchParticle,
19777 xmlSchemaParticlePtr curParticle,
19778 int search)
19779{
19780 return(0);
19781
19782 int ret = 0;
19783 xmlSchemaParticlePtr cur = curParticle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019784 if (curParticle == NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019785 return(0);
19786 }
19787 if (WXS_PARTICLE_TERM(curParticle) == NULL) {
19788 /*
19789 * Just return in this case. A missing "term" of the particle
19790 * might arise due to an invalid "term" component.
19791 */
19792 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000019793 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019794 while (cur != NULL) {
19795 switch (WXS_PARTICLE_TERM(cur)->type) {
19796 case XML_SCHEMA_TYPE_ANY:
19797 break;
19798 case XML_SCHEMA_TYPE_ELEMENT:
19799 if (search == 0) {
19800 ret = xmlSchemaCheckElementDeclConsistent(pctxt,
19801 ctxtComponent, ctxtParticle, cur, ctxtParticle, 1);
19802 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000019803 return(ret);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019804 } else {
19805 xmlSchemaElementPtr elem =
19806 WXS_ELEM_CAST(WXS_PARTICLE_TERM(cur));
19807 /*
19808 * SPEC Element Declarations Consistent:
19809 * "If the {particles} contains, either directly,
19810 * indirectly (that is, within the {particles} of a
Rob Richardsc6947bb2008-06-29 15:04:41 +000019811 * contained model group, recursively) or �implicitly�
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019812 * two or more element declaration particles with
19813 * the same {name} and {target namespace}, then
19814 * all their type definitions must be the same
19815 * top-level definition [...]"
19816 */
19817 if (xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->name,
19818 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->name) &&
19819 xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19820 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->targetNamespace))
19821 {
19822 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019823
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019824 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19825 /* TODO: error code */
19826 XML_SCHEMAP_COS_NONAMBIG,
19827 WXS_ITEM_NODE(cur), NULL,
19828 "In the content model of %s, there are multiple "
19829 "element declarations for '%s' with different "
19830 "type definitions",
19831 xmlSchemaGetComponentDesignation(&strA,
19832 ctxtComponent),
19833 xmlSchemaFormatQName(&strB,
19834 WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19835 WXS_PARTICLE_TERM_AS_ELEM(cur)->name));
19836 FREE_AND_NULL(strA);
19837 FREE_AND_NULL(strB);
19838 return(XML_SCHEMAP_COS_NONAMBIG);
19839 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019840 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019841 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000019842 case XML_SCHEMA_TYPE_SEQUENCE: {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019843 break;
19844 }
19845 case XML_SCHEMA_TYPE_CHOICE:{
19846 /*
19847 xmlSchemaTreeItemPtr sub;
Daniel Veillarddee23482008-04-11 12:58:43 +000019848
19849 sub = WXS_PARTICLE_TERM(particle)->children; (xmlSchemaParticlePtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019850 while (sub != NULL) {
19851 ret = xmlSchemaCheckElementDeclConsistent(pctxt, ctxtComponent,
19852 ctxtParticle, ctxtElem);
19853 if (ret != 0)
19854 return(ret);
19855 sub = sub->next;
19856 }
19857 */
19858 break;
19859 }
19860 case XML_SCHEMA_TYPE_ALL:
19861 break;
19862 case XML_SCHEMA_TYPE_GROUP:
19863 break;
19864 default:
19865 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
19866 "xmlSchemaCheckElementDeclConsistent",
19867 "found unexpected term of type '%s' in content model",
19868 WXS_ITEM_TYPE_NAME(WXS_PARTICLE_TERM(cur)), NULL);
19869 return(-1);
19870 }
19871 cur = (xmlSchemaParticlePtr) cur->next;
19872 }
19873
19874exit:
19875 return(ret);
19876}
19877#endif
19878
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019879/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019880 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019881 * @item: an schema element declaration/particle
19882 * @ctxt: a schema parser context
19883 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019884 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019885 * Validates the value constraints of an element declaration.
Daniel Veillarddee23482008-04-11 12:58:43 +000019886 * Adds substitution group members.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019887 */
19888static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019889xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019890 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019891{
19892 if (elemDecl == NULL)
19893 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019894 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19895 return;
19896 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019897 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0) {
19898 /*
19899 * Adds substitution group members.
19900 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019901 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019902 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019903}
19904
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019905/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019906 * xmlSchemaResolveModelGroupParticleReferences:
19907 * @particle: a particle component
19908 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019909 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019910 * Resolves references of a model group's {particles} to
19911 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019912 */
19913static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019914xmlSchemaResolveModelGroupParticleReferences(
19915 xmlSchemaParserCtxtPtr ctxt,
19916 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019917{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019918 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19919 xmlSchemaQNameRefPtr ref;
19920 xmlSchemaBasicItemPtr refItem;
19921
19922 /*
19923 * URGENT TODO: Test this.
19924 */
19925 while (particle != NULL) {
19926 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19927 ((WXS_PARTICLE_TERM(particle))->type !=
19928 XML_SCHEMA_EXTRA_QNAMEREF))
19929 {
19930 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019931 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019932 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019933 /*
19934 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019935 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019936 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019937 particle->children = NULL;
19938
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019939 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19940 ref->itemType, ref->name, ref->targetNamespace);
19941 if (refItem == NULL) {
19942 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019943 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019944 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019945 /* TODO: remove the particle. */
19946 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019947 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019948 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19949 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19950 /* TODO: remove the particle. */
19951 goto next_particle;
19952 /*
19953 * NOTE that we will assign the model group definition
19954 * itself to the "term" of the particle. This will ease
19955 * the check for circular model group definitions. After
19956 * that the "term" will be assigned the model group of the
19957 * model group definition.
19958 */
19959 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19960 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019961 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019962 * SPEC cos-all-limited (1)
19963 * SPEC cos-all-limited (1.2)
19964 * "It appears only as the value of one or both of the
19965 * following properties:"
19966 * (1.1) "the {model group} property of a model group
19967 * definition."
19968 * (1.2) "the {term} property of a particle [... of] the "
19969 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019970 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019971 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19972 /* TODO: error code */
19973 XML_SCHEMAP_COS_ALL_LIMITED,
19974 WXS_ITEM_NODE(particle), NULL,
19975 "A model group definition is referenced, but "
19976 "it contains an 'all' model group, which "
19977 "cannot be contained by model groups",
19978 NULL, NULL);
19979 /* TODO: remove the particle. */
19980 goto next_particle;
19981 }
19982 particle->children = (xmlSchemaTreeItemPtr) refItem;
19983 } else {
19984 /*
19985 * TODO: Are referenced element declarations the only
19986 * other components we expect here?
19987 */
19988 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019989 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019990next_particle:
19991 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019992 }
19993}
19994
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019995static int
19996xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
Daniel Veillarddee23482008-04-11 12:58:43 +000019997 xmlSchemaValPtr y)
19998{
19999 xmlSchemaTypePtr tx, ty, ptx, pty;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020000 int ret;
20001
20002 while (x != NULL) {
20003 /* Same types. */
20004 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
20005 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
20006 ptx = xmlSchemaGetPrimitiveType(tx);
20007 pty = xmlSchemaGetPrimitiveType(ty);
20008 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000020009 * (1) if a datatype T' is �derived� by �restriction� from an
20010 * atomic datatype T then the �value space� of T' is a subset of
20011 * the �value space� of T. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020012 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000020013 * (2) if datatypes T' and T'' are �derived� by �restriction�
20014 * from a common atomic ancestor T then the �value space�s of T'
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020015 * and T'' may overlap.
20016 */
20017 if (ptx != pty)
20018 return(0);
20019 /*
20020 * We assume computed values to be normalized, so do a fast
20021 * string comparison for string based types.
20022 */
20023 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020024 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020025 if (! xmlStrEqual(
20026 xmlSchemaValueGetAsString(x),
20027 xmlSchemaValueGetAsString(y)))
20028 return (0);
20029 } else {
20030 ret = xmlSchemaCompareValuesWhtsp(
20031 x, XML_SCHEMA_WHITESPACE_PRESERVE,
20032 y, XML_SCHEMA_WHITESPACE_PRESERVE);
20033 if (ret == -2)
20034 return(-1);
20035 if (ret != 0)
20036 return(0);
20037 }
20038 /*
20039 * Lists.
20040 */
20041 x = xmlSchemaValueGetNext(x);
20042 if (x != NULL) {
20043 y = xmlSchemaValueGetNext(y);
20044 if (y == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020045 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020046 } else if (xmlSchemaValueGetNext(y) != NULL)
20047 return (0);
20048 else
20049 return (1);
20050 }
20051 return (0);
20052}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020053
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020054/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020055 * xmlSchemaResolveAttrUseReferences:
20056 * @item: an attribute use
20057 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020058 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020059 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020060 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020061static int
20062xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
20063 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000020064{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020065 if ((ctxt == NULL) || (ause == NULL))
20066 return(-1);
20067 if ((ause->attrDecl == NULL) ||
20068 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
20069 return(0);
20070
20071 {
20072 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
20073
20074 /*
20075 * TODO: Evaluate, what errors could occur if the declaration is not
20076 * found.
20077 */
20078 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
20079 ref->name, ref->targetNamespace);
20080 if (ause->attrDecl == NULL) {
20081 xmlSchemaPResCompAttrErr(ctxt,
20082 XML_SCHEMAP_SRC_RESOLVE,
20083 WXS_BASIC_CAST ause, ause->node,
20084 "ref", ref->name, ref->targetNamespace,
20085 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20086 return(ctxt->err);;
20087 }
20088 }
20089 return(0);
20090}
20091
20092/**
20093 * xmlSchemaCheckAttrUsePropsCorrect:
20094 * @ctxt: a parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000020095 * @use: an attribute use
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020096 *
20097 * Schema Component Constraint:
20098 * Attribute Use Correct (au-props-correct)
Daniel Veillarddee23482008-04-11 12:58:43 +000020099 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020100 */
20101static int
20102xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
20103 xmlSchemaAttributeUsePtr use)
20104{
20105 if ((ctxt == NULL) || (use == NULL))
20106 return(-1);
20107 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
20108 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
Daniel Veillarddee23482008-04-11 12:58:43 +000020109 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020110
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020111 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020112 * SPEC au-props-correct (1)
20113 * "The values of the properties of an attribute use must be as
20114 * described in the property tableau in The Attribute Use Schema
Rob Richardsc6947bb2008-06-29 15:04:41 +000020115 * Component (�3.5.1), modulo the impact of Missing
20116 * Sub-components (�5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000020117 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020118
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020119 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
20120 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
20121 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20122 {
20123 xmlSchemaPCustomErr(ctxt,
20124 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20125 WXS_BASIC_CAST use, NULL,
20126 "The attribute declaration has a 'fixed' value constraint "
20127 ", thus the attribute use must also have a 'fixed' value "
20128 "constraint",
20129 NULL);
20130 return(ctxt->err);
20131 }
20132 /*
20133 * Compute and check the value constraint's value.
20134 */
20135 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
20136 int ret;
20137 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000020138 * TODO: The spec seems to be missing a check of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020139 * value constraint of the attribute use. We will do it here.
20140 */
20141 /*
20142 * SPEC a-props-correct (3)
20143 */
20144 if (xmlSchemaIsDerivedFromBuiltInType(
20145 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
20146 {
20147 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20148 XML_SCHEMAP_AU_PROPS_CORRECT,
20149 NULL, WXS_BASIC_CAST use,
20150 "Value constraints are not allowed if the type definition "
20151 "is or is derived from xs:ID",
20152 NULL, NULL);
20153 return(ctxt->err);
20154 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020155
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020156 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
20157 use->node, WXS_ATTRUSE_TYPEDEF(use),
20158 use->defValue, &(use->defVal),
20159 1, 1, 0);
20160 if (ret != 0) {
20161 if (ret < 0) {
20162 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
20163 "calling xmlSchemaVCheckCVCSimpleType()");
20164 return(-1);
20165 }
20166 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20167 XML_SCHEMAP_AU_PROPS_CORRECT,
20168 NULL, WXS_BASIC_CAST use,
20169 "The value of the value constraint is not valid",
20170 NULL, NULL);
20171 return(ctxt->err);
20172 }
20173 }
20174 /*
20175 * SPEC au-props-correct (2)
20176 * "If the {attribute declaration} has a fixed
20177 * {value constraint}, then if the attribute use itself has a
20178 * {value constraint}, it must also be fixed and its value must match
20179 * that of the {attribute declaration}'s {value constraint}."
20180 */
20181 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20182 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20183 {
20184 if (! xmlSchemaAreValuesEqual(use->defVal,
20185 (WXS_ATTRUSE_DECL(use))->defVal))
20186 {
20187 xmlSchemaPCustomErr(ctxt,
20188 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20189 WXS_BASIC_CAST use, NULL,
20190 "The 'fixed' value constraint of the attribute use "
20191 "must match the attribute declaration's value "
20192 "constraint '%s'",
20193 (WXS_ATTRUSE_DECL(use))->defValue);
20194 }
20195 return(ctxt->err);
20196 }
20197 return(0);
20198}
20199
20200
20201
20202
20203/**
20204 * xmlSchemaResolveAttrTypeReferences:
20205 * @item: an attribute declaration
Daniel Veillarddee23482008-04-11 12:58:43 +000020206 * @ctxt: a parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020207 *
20208 * Resolves the referenced type definition component.
20209 */
20210static int
20211xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20212 xmlSchemaParserCtxtPtr ctxt)
20213{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020214 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020215 * The simple type definition corresponding to the <simpleType> element
20216 * information item in the [children], if present, otherwise the simple
Rob Richardsc6947bb2008-06-29 15:04:41 +000020217 * type definition �resolved� to by the �actual value� of the type
20218 * [attribute], if present, otherwise the �simple ur-type definition�.
Daniel Veillard01fa6152004-06-29 17:04:39 +000020219 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020220 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020221 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020222 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20223 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020224 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020225 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020226 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020227
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020228 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20229 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020230 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020231 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020232 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020233 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020234 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020235 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020236 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020237 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020238 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020239
Daniel Veillard3646d642004-06-02 19:19:14 +000020240 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020241 /*
20242 * The type defaults to the xs:anySimpleType.
20243 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020244 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20245 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020246 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020247}
20248
20249/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020250 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020251 * @idc: the identity-constraint definition
20252 * @ctxt: the schema parser context
20253 * @name: the attribute name
20254 *
20255 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020256 * Schema Component Constraint:
20257 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020258 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020259static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020260xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020261 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020262{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020263 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020264 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020265 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020266 idc->ref->item = (xmlSchemaBasicItemPtr)
20267 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20268 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020269 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020270 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020271 * TODO: It is actually not an error to fail to resolve
20272 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020273 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020274 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020275 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020276 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020277 "refer", idc->ref->name,
20278 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020279 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020280 return(pctxt->err);
20281 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20282 /*
20283 * SPEC c-props-correct (1)
20284 */
20285 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20286 XML_SCHEMAP_C_PROPS_CORRECT,
20287 NULL, WXS_BASIC_CAST idc,
20288 "The keyref references a keyref",
20289 NULL, NULL);
20290 idc->ref->item = NULL;
20291 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020292 } else {
20293 if (idc->nbFields !=
20294 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20295 xmlChar *str = NULL;
20296 xmlSchemaIDCPtr refer;
Daniel Veillarddee23482008-04-11 12:58:43 +000020297
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020298 refer = (xmlSchemaIDCPtr) idc->ref->item;
20299 /*
20300 * SPEC c-props-correct(2)
20301 * "If the {identity-constraint category} is keyref,
20302 * the cardinality of the {fields} must equal that of
20303 * the {fields} of the {referenced key}.
20304 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020305 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020306 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020307 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020308 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020309 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020310 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020311 refer->name),
20312 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020313 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020314 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020315 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020316 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020317 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020318 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020319}
20320
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020321static int
20322xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20323 xmlSchemaParserCtxtPtr pctxt)
20324{
20325 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20326 prohib->targetNamespace) == NULL) {
20327
20328 xmlSchemaPResCompAttrErr(pctxt,
20329 XML_SCHEMAP_SRC_RESOLVE,
20330 NULL, prohib->node,
20331 "ref", prohib->name, prohib->targetNamespace,
20332 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20333 return(XML_SCHEMAP_SRC_RESOLVE);
20334 }
20335 return(0);
20336}
20337
20338#define WXS_REDEFINED_TYPE(c) \
20339(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20340
20341#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20342(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20343
20344#define WXS_REDEFINED_ATTR_GROUP(c) \
20345(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20346
20347static int
20348xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20349{
20350 int err = 0;
20351 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20352 xmlSchemaBasicItemPtr prev, item;
20353 int wasRedefined;
20354
20355 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020356 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020357
20358 do {
20359 item = redef->item;
20360 /*
20361 * First try to locate the redefined component in the
20362 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020363 * NOTE: According to this schema bug entry:
20364 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20365 * it's not clear if the referenced component needs to originate
20366 * from the <redefine>d schema _document_ or the schema; the latter
20367 * would include all imported and included sub-schemas of the
20368 * <redefine>d schema. Currenlty we latter approach is used.
20369 * SUPPLEMENT: It seems that the WG moves towards the latter
20370 * approach, so we are doing it right.
Daniel Veillarddee23482008-04-11 12:58:43 +000020371 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020372 */
20373 prev = xmlSchemaFindRedefCompInGraph(
20374 redef->targetBucket, item->type,
20375 redef->refName, redef->refTargetNs);
20376 if (prev == NULL) {
20377 xmlChar *str = NULL;
20378 xmlNodePtr node;
20379
20380 /*
20381 * SPEC src-redefine:
Rob Richardsc6947bb2008-06-29 15:04:41 +000020382 * (6.2.1) "The �actual value� of its own name attribute plus
20383 * target namespace must successfully �resolve� to a model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020384 * group definition in I."
Rob Richardsc6947bb2008-06-29 15:04:41 +000020385 * (7.2.1) "The �actual value� of its own name attribute plus
20386 * target namespace must successfully �resolve� to an attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020387 * group definition in I."
20388
20389 *
20390 * Note that, if we are redefining with the use of references
20391 * to components, the spec assumes the src-resolve to be used;
20392 * but this won't assure that we search only *inside* the
20393 * redefined schema.
20394 */
20395 if (redef->reference)
20396 node = WXS_ITEM_NODE(redef->reference);
20397 else
20398 node = WXS_ITEM_NODE(item);
20399 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20400 /*
20401 * TODO: error code.
20402 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
Daniel Veillarddee23482008-04-11 12:58:43 +000020403 * reference kind.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020404 */
20405 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020406 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020407 "the redefined schema",
20408 WXS_ITEM_TYPE_NAME(item),
20409 xmlSchemaFormatQName(&str, redef->refTargetNs,
20410 redef->refName));
Daniel Veillarddee23482008-04-11 12:58:43 +000020411 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020412 err = pctxt->err;
20413 redef = redef->next;
20414 continue;
20415 }
20416 /*
20417 * TODO: Obtaining and setting the redefinition state is really
20418 * clumsy.
20419 */
20420 wasRedefined = 0;
20421 switch (item->type) {
20422 case XML_SCHEMA_TYPE_COMPLEX:
20423 case XML_SCHEMA_TYPE_SIMPLE:
20424 if ((WXS_TYPE_CAST prev)->flags &
20425 XML_SCHEMAS_TYPE_REDEFINED)
20426 {
20427 wasRedefined = 1;
20428 break;
20429 }
20430 /* Mark it as redefined. */
20431 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20432 /*
20433 * Assign the redefined type to the
20434 * base type of the redefining type.
20435 * TODO: How
20436 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020437 ((xmlSchemaTypePtr) item)->baseType =
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020438 (xmlSchemaTypePtr) prev;
20439 break;
20440 case XML_SCHEMA_TYPE_GROUP:
20441 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20442 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20443 {
20444 wasRedefined = 1;
20445 break;
20446 }
20447 /* Mark it as redefined. */
20448 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20449 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20450 if (redef->reference != NULL) {
20451 /*
20452 * Overwrite the QName-reference with the
20453 * referenced model group def.
20454 */
20455 (WXS_PTC_CAST redef->reference)->children =
20456 WXS_TREE_CAST prev;
20457 }
20458 redef->target = prev;
20459 break;
20460 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20461 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20462 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20463 {
20464 wasRedefined = 1;
20465 break;
20466 }
20467 (WXS_ATTR_GROUP_CAST prev)->flags |=
20468 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20469 if (redef->reference != NULL) {
20470 /*
20471 * Assign the redefined attribute group to the
20472 * QName-reference component.
20473 * This is the easy case, since we will just
20474 * expand the redefined group.
20475 */
20476 (WXS_QNAME_CAST redef->reference)->item = prev;
20477 redef->target = NULL;
20478 } else {
20479 /*
20480 * This is the complicated case: we need
20481 * to apply src-redefine (7.2.2) at a later
20482 * stage, i.e. when attribute group references
20483 * have beed expanded and simple types have
20484 * beed fixed.
20485 */
20486 redef->target = prev;
20487 }
20488 break;
20489 default:
20490 PERROR_INT("xmlSchemaResolveRedefReferences",
20491 "Unexpected redefined component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020492 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020493 }
20494 if (wasRedefined) {
20495 xmlChar *str = NULL;
20496 xmlNodePtr node;
20497
20498 if (redef->reference)
20499 node = WXS_ITEM_NODE(redef->reference);
20500 else
20501 node = WXS_ITEM_NODE(redef->item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020502
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020503 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20504 /* TODO: error code. */
20505 XML_SCHEMAP_SRC_REDEFINE,
20506 node, NULL,
20507 "The referenced %s was already redefined. Multiple "
20508 "redefinition of the same component is not supported",
20509 xmlSchemaGetComponentDesignation(&str, prev),
20510 NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000020511 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020512 err = pctxt->err;
20513 redef = redef->next;
20514 continue;
20515 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020516 redef = redef->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020517 } while (redef != NULL);
20518
20519 return(err);
20520}
20521
20522static int
20523xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20524{
20525 int err = 0;
20526 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20527 xmlSchemaBasicItemPtr item;
20528
20529 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020530 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020531
20532 do {
20533 if (redef->target == NULL) {
20534 redef = redef->next;
20535 continue;
20536 }
20537 item = redef->item;
Daniel Veillarddee23482008-04-11 12:58:43 +000020538
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020539 switch (item->type) {
20540 case XML_SCHEMA_TYPE_SIMPLE:
20541 case XML_SCHEMA_TYPE_COMPLEX:
20542 /*
20543 * Since the spec wants the {name} of the redefined
20544 * type to be 'absent', we'll NULL it.
20545 */
20546 (WXS_TYPE_CAST redef->target)->name = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020547
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020548 /*
20549 * TODO: Seems like there's nothing more to do. The normal
20550 * inheritance mechanism is used. But not 100% sure.
20551 */
20552 break;
20553 case XML_SCHEMA_TYPE_GROUP:
20554 /*
20555 * URGENT TODO:
20556 * SPEC src-redefine:
20557 * (6.2.2) "The {model group} of the model group definition
20558 * which corresponds to it per XML Representation of Model
Rob Richardsc6947bb2008-06-29 15:04:41 +000020559 * Group Definition Schema Components (�3.7.2) must be a
20560 * �valid restriction� of the {model group} of that model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020561 * group definition in I, as defined in Particle Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000020562 * (Restriction) (�3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020563 */
20564 break;
20565 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20566 /*
20567 * SPEC src-redefine:
20568 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20569 * the attribute group definition which corresponds to it
20570 * per XML Representation of Attribute Group Definition Schema
Rob Richardsc6947bb2008-06-29 15:04:41 +000020571 * Components (�3.6.2) must be �valid restrictions� of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020572 * {attribute uses} and {attribute wildcard} of that attribute
20573 * group definition in I, as defined in clause 2, clause 3 and
20574 * clause 4 of Derivation Valid (Restriction, Complex)
Rob Richardsc6947bb2008-06-29 15:04:41 +000020575 * (�3.4.6) (where references to the base type definition are
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020576 * understood as references to the attribute group definition
20577 * in I)."
20578 */
20579 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20580 XML_SCHEMA_ACTION_REDEFINE,
20581 item, redef->target,
20582 (WXS_ATTR_GROUP_CAST item)->attrUses,
20583 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20584 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20585 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20586 if (err == -1)
20587 return(-1);
20588 break;
20589 default:
20590 break;
20591 }
20592 redef = redef->next;
20593 } while (redef != NULL);
20594 return(0);
20595}
Daniel Veillarddee23482008-04-11 12:58:43 +000020596
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020597
20598static int
20599xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20600 xmlSchemaBucketPtr bucket)
20601{
20602 xmlSchemaBasicItemPtr item;
20603 int err;
20604 xmlHashTablePtr *table;
20605 const xmlChar *name;
20606 int i;
20607
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020608#define WXS_GET_GLOBAL_HASH(c, slot) { \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020609 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20610 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20611 else \
20612 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20613
20614 /*
20615 * Add global components to the schema's hash tables.
20616 * This is the place where duplicate components will be
20617 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020618 * TODO: I think normally we should support imports of the
20619 * same namespace from multiple locations. We don't do currently,
20620 * but if we do then according to:
20621 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20622 * we would need, if imported directly, to import redefined
20623 * components as well to be able to catch clashing components.
20624 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020625 */
20626 if (bucket == NULL)
20627 return(-1);
20628 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20629 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000020630 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20631
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020632 for (i = 0; i < bucket->globals->nbItems; i++) {
20633 item = bucket->globals->items[i];
20634 table = NULL;
20635 switch (item->type) {
20636 case XML_SCHEMA_TYPE_COMPLEX:
20637 case XML_SCHEMA_TYPE_SIMPLE:
20638 if (WXS_REDEFINED_TYPE(item))
20639 continue;
20640 name = (WXS_TYPE_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020641 WXS_GET_GLOBAL_HASH(bucket, typeDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020642 break;
20643 case XML_SCHEMA_TYPE_ELEMENT:
20644 name = (WXS_ELEM_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020645 WXS_GET_GLOBAL_HASH(bucket, elemDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020646 break;
20647 case XML_SCHEMA_TYPE_ATTRIBUTE:
20648 name = (WXS_ATTR_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020649 WXS_GET_GLOBAL_HASH(bucket, attrDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020650 break;
20651 case XML_SCHEMA_TYPE_GROUP:
20652 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20653 continue;
20654 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020655 WXS_GET_GLOBAL_HASH(bucket, groupDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020656 break;
20657 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20658 if (WXS_REDEFINED_ATTR_GROUP(item))
20659 continue;
20660 name = (WXS_ATTR_GROUP_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020661 WXS_GET_GLOBAL_HASH(bucket, attrgrpDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020662 break;
20663 case XML_SCHEMA_TYPE_IDC_KEY:
20664 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20665 case XML_SCHEMA_TYPE_IDC_KEYREF:
20666 name = (WXS_IDC_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020667 WXS_GET_GLOBAL_HASH(bucket, idcDef)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020668 break;
20669 case XML_SCHEMA_TYPE_NOTATION:
20670 name = ((xmlSchemaNotationPtr) item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020671 WXS_GET_GLOBAL_HASH(bucket, notaDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +000020672 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020673 default:
20674 PERROR_INT("xmlSchemaAddComponents",
20675 "Unexpected global component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020676 continue;
20677 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020678 if (*table == NULL) {
20679 *table = xmlHashCreateDict(10, pctxt->dict);
20680 if (*table == NULL) {
20681 PERROR_INT("xmlSchemaAddComponents",
20682 "failed to create a component hash table");
20683 return(-1);
20684 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020685 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020686 err = xmlHashAddEntry(*table, name, item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020687 if (err != 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020688 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020689
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020690 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20691 XML_SCHEMAP_REDEFINED_TYPE,
20692 WXS_ITEM_NODE(item),
20693 WXS_BASIC_CAST item,
20694 "A global %s '%s' does already exist",
20695 WXS_ITEM_TYPE_NAME(item),
20696 xmlSchemaGetComponentQName(&str, item));
Daniel Veillarddee23482008-04-11 12:58:43 +000020697 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020698 }
20699 }
20700 /*
20701 * Process imported/included schemas.
20702 */
20703 if (bucket->relations != NULL) {
20704 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20705 do {
20706 if ((rel->bucket != NULL) &&
20707 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20708 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20709 return(-1);
20710 }
20711 rel = rel->next;
20712 } while (rel != NULL);
20713 }
20714 return(0);
20715}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020716
20717static int
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020718xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt,
20719 xmlSchemaBucketPtr rootBucket)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020720{
20721 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20722 xmlSchemaTreeItemPtr item, *items;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020723 int nbItems, i, ret = 0;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020724 xmlSchemaBucketPtr oldbucket = con->bucket;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020725 xmlSchemaElementPtr elemDecl;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020726
20727#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20728
20729 if ((con->pending == NULL) ||
20730 (con->pending->nbItems == 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000020731 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020732
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020733 /*
20734 * Since xmlSchemaFixupComplexType() will create new particles
20735 * (local components), and those particle components need a bucket
20736 * on the constructor, we'll assure here that the constructor has
20737 * a bucket.
Daniel Veillarddee23482008-04-11 12:58:43 +000020738 * TODO: Think about storing locals _only_ on the main bucket.
20739 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020740 if (con->bucket == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020741 con->bucket = rootBucket;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020742
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020743 /* TODO:
20744 * SPEC (src-redefine):
20745 * (6.2) "If it has no such self-reference, then all of the
20746 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000020747
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020748 * (6.2.2) The {model group} of the model group definition which
20749 * corresponds to it per XML Representation of Model Group
Rob Richardsc6947bb2008-06-29 15:04:41 +000020750 * Definition Schema Components (�3.7.2) must be a �valid
20751 * restriction� of the {model group} of that model group definition
20752 * in I, as defined in Particle Valid (Restriction) (�3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020753 */
20754 xmlSchemaCheckSRCRedefineFirst(pctxt);
20755
20756 /*
20757 * Add global components to the schemata's hash tables.
Daniel Veillarddee23482008-04-11 12:58:43 +000020758 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020759 xmlSchemaAddComponents(pctxt, rootBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020760
20761 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020762 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20763 nbItems = con->pending->nbItems;
20764 /*
20765 * Now that we have parsed *all* the schema document(s) and converted
20766 * them to schema components, we can resolve references, apply component
20767 * constraints, create the FSA from the content model, etc.
Daniel Veillarddee23482008-04-11 12:58:43 +000020768 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020769 /*
20770 * Resolve references of..
20771 *
20772 * 1. element declarations:
20773 * - the type definition
20774 * - the substitution group affiliation
20775 * 2. simple/complex types:
20776 * - the base type definition
20777 * - the memberTypes of union types
20778 * - the itemType of list types
20779 * 3. attributes declarations and attribute uses:
20780 * - the type definition
20781 * - if an attribute use, then the attribute declaration
20782 * 4. attribute group references:
20783 * - the attribute group definition
20784 * 5. particles:
20785 * - the term of the particle (e.g. a model group)
20786 * 6. IDC key-references:
20787 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020788 * 7. Attribute prohibitions which had a "ref" attribute.
Daniel Veillarddee23482008-04-11 12:58:43 +000020789 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020790 for (i = 0; i < nbItems; i++) {
20791 item = items[i];
20792 switch (item->type) {
20793 case XML_SCHEMA_TYPE_ELEMENT:
20794 xmlSchemaResolveElementReferences(
20795 (xmlSchemaElementPtr) item, pctxt);
20796 FIXHFAILURE;
20797 break;
20798 case XML_SCHEMA_TYPE_COMPLEX:
20799 case XML_SCHEMA_TYPE_SIMPLE:
20800 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020801 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020802 FIXHFAILURE;
20803 break;
20804 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020805 xmlSchemaResolveAttrTypeReferences(
20806 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020807 FIXHFAILURE;
20808 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020809 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20810 xmlSchemaResolveAttrUseReferences(
20811 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020812 FIXHFAILURE;
20813 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020814 case XML_SCHEMA_EXTRA_QNAMEREF:
Daniel Veillarddee23482008-04-11 12:58:43 +000020815 if ((WXS_QNAME_CAST item)->itemType ==
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020816 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20817 {
20818 xmlSchemaResolveAttrGroupReferences(
20819 WXS_QNAME_CAST item, pctxt);
20820 }
20821 FIXHFAILURE;
20822 break;
20823 case XML_SCHEMA_TYPE_SEQUENCE:
20824 case XML_SCHEMA_TYPE_CHOICE:
20825 case XML_SCHEMA_TYPE_ALL:
20826 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20827 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020828 FIXHFAILURE;
20829 break;
20830 case XML_SCHEMA_TYPE_IDC_KEY:
20831 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20832 case XML_SCHEMA_TYPE_IDC_KEYREF:
20833 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020834 (xmlSchemaIDCPtr) item, pctxt);
20835 FIXHFAILURE;
20836 break;
20837 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20838 /*
20839 * Handle attribue prohibition which had a
20840 * "ref" attribute.
20841 */
20842 xmlSchemaResolveAttrUseProhibReferences(
20843 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020844 FIXHFAILURE;
20845 break;
20846 default:
20847 break;
20848 }
20849 }
20850 if (pctxt->nberrors != 0)
20851 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020852
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020853 /*
20854 * Now that all references are resolved we
20855 * can check for circularity of...
Daniel Veillarddee23482008-04-11 12:58:43 +000020856 * 1. the base axis of type definitions
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020857 * 2. nested model group definitions
20858 * 3. nested attribute group definitions
20859 * TODO: check for circual substitution groups.
20860 */
20861 for (i = 0; i < nbItems; i++) {
20862 item = items[i];
20863 /*
20864 * Let's better stop on the first error here.
20865 */
20866 switch (item->type) {
20867 case XML_SCHEMA_TYPE_COMPLEX:
20868 case XML_SCHEMA_TYPE_SIMPLE:
20869 xmlSchemaCheckTypeDefCircular(
20870 (xmlSchemaTypePtr) item, pctxt);
20871 FIXHFAILURE;
20872 if (pctxt->nberrors != 0)
20873 goto exit_error;
20874 break;
20875 case XML_SCHEMA_TYPE_GROUP:
20876 xmlSchemaCheckGroupDefCircular(
20877 (xmlSchemaModelGroupDefPtr) item, pctxt);
20878 FIXHFAILURE;
20879 if (pctxt->nberrors != 0)
20880 goto exit_error;
20881 break;
20882 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20883 xmlSchemaCheckAttrGroupCircular(
20884 (xmlSchemaAttributeGroupPtr) item, pctxt);
20885 FIXHFAILURE;
20886 if (pctxt->nberrors != 0)
20887 goto exit_error;
20888 break;
20889 default:
20890 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000020891 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020892 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020893 if (pctxt->nberrors != 0)
20894 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020895 /*
20896 * Model group definition references:
20897 * Such a reference is reflected by a particle at the component
20898 * level. Until now the 'term' of such particles pointed
20899 * to the model group definition; this was done, in order to
20900 * ease circularity checks. Now we need to set the 'term' of
20901 * such particles to the model group of the model group definition.
20902 */
20903 for (i = 0; i < nbItems; i++) {
20904 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020905 switch (item->type) {
20906 case XML_SCHEMA_TYPE_SEQUENCE:
Daniel Veillarddee23482008-04-11 12:58:43 +000020907 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020908 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20909 WXS_MODEL_GROUP_CAST item);
20910 break;
20911 default:
20912 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020913 }
20914 }
20915 if (pctxt->nberrors != 0)
20916 goto exit_error;
20917 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020918 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020919 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020920 for (i = 0; i < nbItems; i++) {
20921 item = items[i];
20922 switch (item->type) {
20923 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20924 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20925 WXS_ATTR_GROUP_HAS_REFS(item))
20926 {
20927 xmlSchemaAttributeGroupExpandRefs(pctxt,
20928 WXS_ATTR_GROUP_CAST item);
20929 FIXHFAILURE;
20930 }
20931 break;
20932 default:
20933 break;
20934 }
20935 }
20936 if (pctxt->nberrors != 0)
20937 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020938 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020939 * First compute the variety of simple types. This is needed as
20940 * a seperate step, since otherwise we won't be able to detect
20941 * circular union types in all cases.
20942 */
20943 for (i = 0; i < nbItems; i++) {
20944 item = items[i];
20945 switch (item->type) {
20946 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020947 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020948 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20949 (xmlSchemaTypePtr) item);
20950 FIXHFAILURE;
20951 }
20952 break;
20953 default:
20954 break;
20955 }
20956 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020957 if (pctxt->nberrors != 0)
20958 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020959 /*
20960 * Detect circular union types. Note that this needs the variety to
20961 * be already computed.
20962 */
20963 for (i = 0; i < nbItems; i++) {
20964 item = items[i];
20965 switch (item->type) {
20966 case XML_SCHEMA_TYPE_SIMPLE:
20967 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000020968 xmlSchemaCheckUnionTypeDefCircular(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020969 (xmlSchemaTypePtr) item);
20970 FIXHFAILURE;
20971 }
20972 break;
20973 default:
20974 break;
20975 }
20976 }
20977 if (pctxt->nberrors != 0)
20978 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020979
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020980 /*
20981 * Do the complete type fixup for simple types.
20982 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020983 for (i = 0; i < nbItems; i++) {
20984 item = items[i];
20985 switch (item->type) {
20986 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020987 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20988 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20989 FIXHFAILURE;
20990 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020991 break;
20992 default:
20993 break;
20994 }
20995 }
20996 if (pctxt->nberrors != 0)
20997 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020998 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020999 * At this point we need build and check all simple types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021000 */
21001 /*
21002 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021003 */
21004 for (i = 0; i < nbItems; i++) {
21005 item = items[i];
21006 switch (item->type) {
Daniel Veillarddee23482008-04-11 12:58:43 +000021007 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021008 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
21009 FIXHFAILURE;
Daniel Veillarddee23482008-04-11 12:58:43 +000021010 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021011 default:
21012 break;
21013 }
21014 }
21015 if (pctxt->nberrors != 0)
21016 goto exit_error;
21017 /*
21018 * Apply constraints for attribute uses.
Daniel Veillarddee23482008-04-11 12:58:43 +000021019 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021020 for (i = 0; i < nbItems; i++) {
21021 item = items[i];
21022 switch (item->type) {
21023 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
21024 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
21025 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
21026 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021027 FIXHFAILURE;
21028 }
21029 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021030 default:
21031 break;
21032 }
21033 }
21034 if (pctxt->nberrors != 0)
21035 goto exit_error;
21036
21037 /*
21038 * Apply constraints for attribute group definitions.
21039 */
21040 for (i = 0; i < nbItems; i++) {
21041 item = items[i];
21042 switch (item->type) {
21043 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
21044 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
21045 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
21046 {
21047 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
21048 FIXHFAILURE;
21049 }
21050 break;
21051 default:
21052 break;
21053 }
21054 }
21055 if (pctxt->nberrors != 0)
21056 goto exit_error;
21057
21058 /*
21059 * Apply constraints for redefinitions.
21060 */
21061 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
21062 xmlSchemaCheckSRCRedefineSecond(pctxt);
21063 if (pctxt->nberrors != 0)
21064 goto exit_error;
21065
Daniel Veillarddee23482008-04-11 12:58:43 +000021066 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021067 * Complex types are builded and checked.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021068 */
21069 for (i = 0; i < nbItems; i++) {
21070 item = con->pending->items[i];
21071 switch (item->type) {
21072 case XML_SCHEMA_TYPE_COMPLEX:
21073 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
21074 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
21075 FIXHFAILURE;
21076 }
21077 break;
21078 default:
21079 break;
21080 }
21081 }
21082 if (pctxt->nberrors != 0)
21083 goto exit_error;
21084
21085 /*
21086 * The list could have changed, since xmlSchemaFixupComplexType()
21087 * will create particles and model groups in some cases.
21088 */
21089 items = (xmlSchemaTreeItemPtr *) con->pending->items;
Daniel Veillarddee23482008-04-11 12:58:43 +000021090 nbItems = con->pending->nbItems;
21091
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021092 /*
21093 * Apply some constraints for element declarations.
21094 */
21095 for (i = 0; i < nbItems; i++) {
21096 item = items[i];
21097 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021098 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021099 elemDecl = (xmlSchemaElementPtr) item;
Daniel Veillarddee23482008-04-11 12:58:43 +000021100
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021101 if ((elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0)
21102 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021103 xmlSchemaCheckElementDeclComponent(
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021104 (xmlSchemaElementPtr) elemDecl, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021105 FIXHFAILURE;
21106 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021107
21108#ifdef WXS_ELEM_DECL_CONS_ENABLED
21109 /*
21110 * Schema Component Constraint: Element Declarations Consistent
21111 * Apply this constraint to local types of element declarations.
21112 */
21113 if ((WXS_ELEM_TYPEDEF(elemDecl) != NULL) &&
21114 (WXS_IS_COMPLEX(WXS_ELEM_TYPEDEF(elemDecl))) &&
21115 (WXS_TYPE_IS_LOCAL(WXS_ELEM_TYPEDEF(elemDecl))))
21116 {
21117 xmlSchemaCheckElementDeclConsistent(pctxt,
21118 WXS_BASIC_CAST elemDecl,
21119 WXS_TYPE_PARTICLE(WXS_ELEM_TYPEDEF(elemDecl)),
21120 NULL, NULL, 0);
21121 }
21122#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021123 break;
21124 default:
21125 break;
21126 }
21127 }
21128 if (pctxt->nberrors != 0)
21129 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021130
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021131 /*
21132 * Finally we can build the automaton from the content model of
21133 * complex types.
21134 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021135
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021136 for (i = 0; i < nbItems; i++) {
21137 item = items[i];
21138 switch (item->type) {
21139 case XML_SCHEMA_TYPE_COMPLEX:
21140 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021141 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021142 break;
21143 default:
21144 break;
21145 }
21146 }
21147 if (pctxt->nberrors != 0)
21148 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021149 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021150 * URGENT TODO: cos-element-consistent
Daniel Veillarddee23482008-04-11 12:58:43 +000021151 */
21152 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021153
Daniel Veillarddee23482008-04-11 12:58:43 +000021154exit_error:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021155 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021156 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021157
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021158exit_failure:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021159 ret = -1;
21160
21161exit:
21162 /*
21163 * Reset the constructor. This is needed for XSI acquisition, since
21164 * those items will be processed over and over again for every XSI
21165 * if not cleared here.
21166 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021167 con->bucket = oldbucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000021168 con->pending->nbItems = 0;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021169 if (con->substGroups != NULL) {
21170 xmlHashFree(con->substGroups,
21171 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
21172 con->substGroups = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000021173 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021174 if (con->redefs != NULL) {
21175 xmlSchemaRedefListFree(con->redefs);
21176 con->redefs = NULL;
21177 }
21178 return(ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021179}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021180/**
Daniel Veillard4255d502002-04-16 15:50:10 +000021181 * xmlSchemaParse:
21182 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021183 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021184 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000021185 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000021186 *
21187 * Returns the internal XML Schema structure built from the resource or
21188 * NULL in case of error
21189 */
21190xmlSchemaPtr
21191xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
21192{
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021193 xmlSchemaPtr mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021194 xmlSchemaBucketPtr bucket = NULL;
21195 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000021196
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021197 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021198 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021199 * the API; i.e. not automatically by the validated instance document.
21200 */
21201
Daniel Veillard4255d502002-04-16 15:50:10 +000021202 xmlSchemaInitTypes();
21203
Daniel Veillard6045c902002-10-09 21:13:59 +000021204 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000021205 return (NULL);
21206
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021207 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021208 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021209 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021210 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021211
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021212 /* Create the *main* schema. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021213 mainSchema = xmlSchemaNewSchema(ctxt);
21214 if (mainSchema == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021215 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021216 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021217 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021218 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021219 if (ctxt->constructor == NULL) {
21220 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21221 if (ctxt->constructor == NULL)
21222 return(NULL);
21223 /* Take ownership of the constructor to be able to free it. */
21224 ctxt->ownsConstructor = 1;
21225 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021226 ctxt->constructor->mainSchema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021227 /*
21228 * Locate and add the schema document.
21229 */
21230 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21231 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21232 NULL, NULL, &bucket);
21233 if (res == -1)
21234 goto exit_failure;
21235 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021236 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021237
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021238 if (bucket == NULL) {
21239 /* TODO: Error code, actually we failed to *locate* the schema. */
Daniel Veillarddee23482008-04-11 12:58:43 +000021240 if (ctxt->URL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021241 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21242 NULL, NULL,
21243 "Failed to locate the main schema resource at '%s'",
21244 ctxt->URL, NULL);
21245 else
21246 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21247 NULL, NULL,
21248 "Failed to locate the main schema resource",
21249 NULL, NULL);
21250 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021251 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021252 /* Then do the parsing for good. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021253 if (xmlSchemaParseNewDocWithContext(ctxt, mainSchema, bucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021254 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021255 if (ctxt->nberrors != 0)
21256 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021257
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021258 mainSchema->doc = bucket->doc;
21259 mainSchema->preserve = ctxt->preserve;
Daniel Veillarddee23482008-04-11 12:58:43 +000021260
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021261 ctxt->schema = mainSchema;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021262
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021263 if (xmlSchemaFixupComponents(ctxt, WXS_CONSTRUCTOR(ctxt)->mainBucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021264 goto exit_failure;
21265
21266 /*
21267 * TODO: This is not nice, since we cannot distinguish from the
21268 * result if there was an internal error or not.
21269 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021270exit:
21271 if (ctxt->nberrors != 0) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021272 if (mainSchema) {
21273 xmlSchemaFree(mainSchema);
21274 mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021275 }
21276 if (ctxt->constructor) {
21277 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21278 ctxt->constructor = NULL;
21279 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021280 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021281 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021282 ctxt->schema = NULL;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021283 return(mainSchema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021284exit_failure:
Daniel Veillarddee23482008-04-11 12:58:43 +000021285 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021286 * Quite verbose, but should catch internal errors, which were
21287 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021288 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021289 if (mainSchema) {
21290 xmlSchemaFree(mainSchema);
21291 mainSchema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021292 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021293 if (ctxt->constructor) {
21294 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21295 ctxt->constructor = NULL;
21296 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021297 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021298 PERROR_INT2("xmlSchemaParse",
Daniel Veillarddee23482008-04-11 12:58:43 +000021299 "An internal error occured");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021300 ctxt->schema = NULL;
21301 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021302}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021303
Daniel Veillard4255d502002-04-16 15:50:10 +000021304/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021305 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021306 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021307 * @err: the error callback
21308 * @warn: the warning callback
21309 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021310 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021311 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021312 */
21313void
21314xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021315 xmlSchemaValidityErrorFunc err,
21316 xmlSchemaValidityWarningFunc warn, void *ctx)
21317{
Daniel Veillard4255d502002-04-16 15:50:10 +000021318 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021319 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021320 ctxt->error = err;
21321 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021322 ctxt->errCtxt = ctx;
21323 if (ctxt->vctxt != NULL)
21324 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21325}
21326
21327/**
21328 * xmlSchemaSetParserStructuredErrors:
21329 * @ctxt: a schema parser context
21330 * @serror: the structured error function
21331 * @ctx: the functions context
21332 *
21333 * Set the structured error callback
21334 */
21335void
21336xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21337 xmlStructuredErrorFunc serror,
21338 void *ctx)
21339{
21340 if (ctxt == NULL)
21341 return;
21342 ctxt->serror = serror;
21343 ctxt->errCtxt = ctx;
21344 if (ctxt->vctxt != NULL)
21345 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021346}
21347
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021348/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021349 * xmlSchemaGetParserErrors:
21350 * @ctxt: a XMl-Schema parser context
21351 * @err: the error callback result
21352 * @warn: the warning callback result
21353 * @ctx: contextual data for the callbacks result
21354 *
21355 * Get the callback information used to handle errors for a parser context
21356 *
21357 * Returns -1 in case of failure, 0 otherwise
21358 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021359int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021360xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021361 xmlSchemaValidityErrorFunc * err,
21362 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021363{
21364 if (ctxt == NULL)
21365 return(-1);
21366 if (err != NULL)
21367 *err = ctxt->error;
21368 if (warn != NULL)
21369 *warn = ctxt->warning;
21370 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021371 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021372 return(0);
21373}
21374
21375/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021376 * xmlSchemaFacetTypeToString:
21377 * @type: the facet type
21378 *
21379 * Convert the xmlSchemaTypeType to a char string.
21380 *
21381 * Returns the char string representation of the facet type if the
21382 * type is a facet and an "Internal Error" string otherwise.
21383 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021384static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021385xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21386{
21387 switch (type) {
21388 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021389 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021390 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021391 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021392 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021393 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021394 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021395 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021396 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021397 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021398 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021399 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021400 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021401 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021402 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021403 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021404 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021405 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021406 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021407 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021408 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021409 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021410 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021411 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021412 default:
21413 break;
21414 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021415 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021416}
21417
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021418static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021419xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21420{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021421 /*
21422 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021423 * from xsd:string.
21424 */
21425 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021426 /*
21427 * Note that we assume a whitespace of preserve for anySimpleType.
21428 */
21429 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21430 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21431 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021432 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021433 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021434 else {
21435 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000021436 * For all �atomic� datatypes other than string (and types �derived�
21437 * by �restriction� from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021438 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021439 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021440 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021441 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021442 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021443 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021444 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021445 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021446 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021447 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021448 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021449 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021450 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021451 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21452 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21453 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21454 return (XML_SCHEMA_WHITESPACE_REPLACE);
21455 else
21456 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021457 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021458 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021459}
21460
Daniel Veillard4255d502002-04-16 15:50:10 +000021461/************************************************************************
21462 * *
21463 * Simple type validation *
21464 * *
21465 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021466
Daniel Veillard4255d502002-04-16 15:50:10 +000021467
21468/************************************************************************
21469 * *
21470 * DOM Validation code *
21471 * *
21472 ************************************************************************/
21473
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021474/**
21475 * xmlSchemaAssembleByLocation:
21476 * @pctxt: a schema parser context
21477 * @vctxt: a schema validation context
21478 * @schema: the existing schema
21479 * @node: the node that fired the assembling
21480 * @nsName: the namespace name of the new schema
21481 * @location: the location of the schema
21482 *
21483 * Expands an existing schema by an additional schema.
21484 *
21485 * Returns 0 if the new schema is correct, a positive error code
21486 * number otherwise and -1 in case of an internal or API error.
21487 */
21488static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021489xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021490 xmlSchemaPtr schema,
21491 xmlNodePtr node,
21492 const xmlChar *nsName,
21493 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021494{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021495 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021496 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021497 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021498
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021499 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021500 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021501
21502 if (vctxt->pctxt == NULL) {
21503 VERROR_INT("xmlSchemaAssembleByLocation",
21504 "no parser context available");
21505 return(-1);
21506 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021507 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021508 if (pctxt->constructor == NULL) {
21509 PERROR_INT("xmlSchemaAssembleByLocation",
21510 "no constructor");
21511 return(-1);
21512 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021513 /*
21514 * Acquire the schema document.
21515 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021516 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21517 location, node);
21518 /*
21519 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21520 * the process will automatically change this to
21521 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21522 */
21523 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Daniel Veillarddee23482008-04-11 12:58:43 +000021524 location, NULL, NULL, 0, node, NULL, nsName,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021525 &bucket);
21526 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021527 return(ret);
21528 if (bucket == NULL) {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021529 /*
21530 * Generate a warning that the document could not be located.
21531 */
21532 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21533 node, NULL,
21534 "The document at location '%s' could not be acquired",
21535 location, NULL, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021536 return(ret);
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021537 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021538 /*
21539 * The first located schema will be handled as if all other
21540 * schemas imported by XSI were imported by this first schema.
21541 */
21542 if ((bucket != NULL) &&
21543 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21544 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21545 /*
21546 * TODO: Is this handled like an import? I.e. is it not an error
21547 * if the schema cannot be located?
21548 */
21549 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21550 return(0);
21551 /*
21552 * We will reuse the parser context for every schema imported
21553 * directly via XSI. So reset the context.
21554 */
21555 pctxt->nberrors = 0;
21556 pctxt->err = 0;
21557 pctxt->doc = bucket->doc;
Daniel Veillarddee23482008-04-11 12:58:43 +000021558
21559 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021560 if (ret == -1) {
21561 pctxt->doc = NULL;
21562 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021563 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021564 /* Paranoid error channelling. */
21565 if ((ret == 0) && (pctxt->nberrors != 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000021566 ret = pctxt->err;
21567 if (pctxt->nberrors == 0) {
21568 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021569 * Only bother to fixup pending components, if there was
21570 * no error yet.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021571 * For every XSI acquired schema (and its sub-schemata) we will
21572 * fixup the components.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021573 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021574 xmlSchemaFixupComponents(pctxt, bucket);
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000021575 ret = pctxt->err;
21576 /*
21577 * Not nice, but we need somehow to channel the schema parser
21578 * error to the validation context.
21579 */
21580 if ((ret != 0) && (vctxt->err == 0))
21581 vctxt->err = ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021582 vctxt->nberrors += pctxt->nberrors;
21583 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000021584 /* Add to validation error sum. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021585 vctxt->nberrors += pctxt->nberrors;
21586 }
21587 pctxt->doc = NULL;
21588 return(ret);
21589exit_failure:
21590 pctxt->doc = NULL;
21591 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021592}
21593
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021594static xmlSchemaAttrInfoPtr
Daniel Veillarddee23482008-04-11 12:58:43 +000021595xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021596 int metaType)
21597{
21598 if (vctxt->nbAttrInfos == 0)
21599 return (NULL);
21600 {
21601 int i;
21602 xmlSchemaAttrInfoPtr iattr;
21603
21604 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21605 iattr = vctxt->attrInfos[i];
21606 if (iattr->metaType == metaType)
21607 return (iattr);
21608 }
21609
21610 }
21611 return (NULL);
21612}
21613
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021614/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021615 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021616 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021617 *
21618 * Expands an existing schema by an additional schema using
21619 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21620 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21621 * must be set to 1.
21622 *
21623 * Returns 0 if the new schema is correct, a positive error code
21624 * number otherwise and -1 in case of an internal or API error.
21625 */
21626static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021627xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021628{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021629 const xmlChar *cur, *end;
21630 const xmlChar *nsname = NULL, *location;
21631 int count = 0;
21632 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021633 xmlSchemaAttrInfoPtr iattr;
21634
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021635 /*
21636 * Parse the value; we will assume an even number of values
21637 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021638 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021639 * URGENT TODO: !! This needs to work for both
21640 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21641 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021642 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021643 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21644 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21645 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021646 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021647 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21648 if (iattr == NULL)
21649 return (0);
21650 cur = iattr->value;
21651 do {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021652 /*
21653 * TODO: Move the string parsing mechanism away from here.
21654 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021655 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021656 /*
21657 * Get the namespace name.
21658 */
21659 while (IS_BLANK_CH(*cur))
21660 cur++;
21661 end = cur;
21662 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21663 end++;
21664 if (end == cur)
21665 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021666 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021667 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021668 cur = end;
21669 }
21670 /*
21671 * Get the URI.
21672 */
21673 while (IS_BLANK_CH(*cur))
21674 cur++;
21675 end = cur;
21676 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21677 end++;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021678 if (end == cur) {
21679 if (iattr->metaType ==
21680 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC)
21681 {
21682 /*
21683 * If using @schemaLocation then tuples are expected.
21684 * I.e. the namespace name *and* the document's URI.
Daniel Veillarddee23482008-04-11 12:58:43 +000021685 */
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021686 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21687 iattr->node, NULL,
21688 "The value must consist of tuples: the target namespace "
21689 "name and the document's URI", NULL, NULL, NULL);
21690 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021691 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021692 }
21693 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021694 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021695 cur = end;
21696 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21697 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021698 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021699 VERROR_INT("xmlSchemaAssembleByXSI",
21700 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021701 return (-1);
21702 }
21703 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021704 return (ret);
21705}
21706
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021707static const xmlChar *
21708xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21709 const xmlChar *prefix)
21710{
21711 if (vctxt->sax != NULL) {
21712 int i, j;
21713 xmlSchemaNodeInfoPtr inode;
Daniel Veillarddee23482008-04-11 12:58:43 +000021714
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021715 for (i = vctxt->depth; i >= 0; i--) {
21716 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21717 inode = vctxt->elemInfos[i];
21718 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21719 if (((prefix == NULL) &&
21720 (inode->nsBindings[j] == NULL)) ||
21721 ((prefix != NULL) && xmlStrEqual(prefix,
21722 inode->nsBindings[j]))) {
21723
21724 /*
21725 * Note that the namespace bindings are already
21726 * in a string dict.
21727 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021728 return (inode->nsBindings[j+1]);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021729 }
21730 }
21731 }
21732 }
21733 return (NULL);
Daniel Veillard438ebbd2008-05-12 12:58:46 +000021734#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021735 } else if (vctxt->reader != NULL) {
21736 xmlChar *nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +000021737
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021738 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21739 if (nsName != NULL) {
21740 const xmlChar *ret;
21741
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021742 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21743 xmlFree(nsName);
21744 return (ret);
21745 } else
21746 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021747#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021748 } else {
21749 xmlNsPtr ns;
21750
21751 if ((vctxt->inode->node == NULL) ||
21752 (vctxt->inode->node->doc == NULL)) {
21753 VERROR_INT("xmlSchemaLookupNamespace",
21754 "no node or node's doc avaliable");
21755 return (NULL);
21756 }
21757 ns = xmlSearchNs(vctxt->inode->node->doc,
21758 vctxt->inode->node, prefix);
21759 if (ns != NULL)
21760 return (ns->href);
21761 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021762 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021763}
21764
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021765/*
21766* This one works on the schema of the validation context.
21767*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021768static int
Daniel Veillarddee23482008-04-11 12:58:43 +000021769xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021770 xmlSchemaPtr schema,
21771 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021772 const xmlChar *value,
21773 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021774 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021775{
21776 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021777
21778 if (vctxt && (vctxt->schema == NULL)) {
21779 VERROR_INT("xmlSchemaValidateNotation",
21780 "a schema is needed on the validation context");
21781 return (-1);
21782 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021783 ret = xmlValidateQName(value, 1);
21784 if (ret != 0)
21785 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021786 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021787 xmlChar *localName = NULL;
21788 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021789
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021790 localName = xmlSplitQName2(value, &prefix);
21791 if (prefix != NULL) {
21792 const xmlChar *nsName = NULL;
21793
Daniel Veillarddee23482008-04-11 12:58:43 +000021794 if (vctxt != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021795 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21796 else if (node != NULL) {
21797 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21798 if (ns != NULL)
21799 nsName = ns->href;
21800 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021801 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021802 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021803 return (1);
21804 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021805 if (nsName == NULL) {
21806 xmlFree(prefix);
21807 xmlFree(localName);
21808 return (1);
21809 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021810 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Daniel Veillard77fbc3e2008-03-24 14:06:19 +000021811 if ((valNeeded) && (val != NULL)) {
21812 (*val) = xmlSchemaNewNOTATIONValue(xmlStrdup(localName),
21813 xmlStrdup(nsName));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021814 if (*val == NULL)
21815 ret = -1;
21816 }
21817 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021818 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021819 xmlFree(prefix);
21820 xmlFree(localName);
21821 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021822 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021823 if (valNeeded && (val != NULL)) {
21824 (*val) = xmlSchemaNewNOTATIONValue(
21825 BAD_CAST xmlStrdup(value), NULL);
21826 if (*val == NULL)
21827 ret = -1;
21828 }
21829 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021830 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021831 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021832 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021833 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021834}
21835
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021836static int
21837xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21838 const xmlChar* lname,
21839 const xmlChar* nsname)
21840{
21841 int i;
21842
21843 lname = xmlDictLookup(vctxt->dict, lname, -1);
21844 if (lname == NULL)
21845 return(-1);
21846 if (nsname != NULL) {
21847 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21848 if (nsname == NULL)
21849 return(-1);
21850 }
21851 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21852 if ((vctxt->nodeQNames->items [i] == lname) &&
21853 (vctxt->nodeQNames->items[i +1] == nsname))
21854 /* Already there */
21855 return(i);
21856 }
21857 /* Add new entry. */
21858 i = vctxt->nodeQNames->nbItems;
21859 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21860 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21861 return(i);
21862}
21863
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021864/************************************************************************
21865 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021866 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021867 * *
21868 ************************************************************************/
21869
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021870/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021871 * xmlSchemaAugmentIDC:
21872 * @idcDef: the IDC definition
21873 *
21874 * Creates an augmented IDC definition item.
21875 *
21876 * Returns the item, or NULL on internal errors.
21877 */
21878static void
21879xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21880 xmlSchemaValidCtxtPtr vctxt)
21881{
21882 xmlSchemaIDCAugPtr aidc;
21883
21884 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21885 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021886 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021887 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21888 NULL);
21889 return;
21890 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021891 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021892 aidc->def = idcDef;
21893 aidc->next = NULL;
21894 if (vctxt->aidcs == NULL)
21895 vctxt->aidcs = aidc;
21896 else {
21897 aidc->next = vctxt->aidcs;
21898 vctxt->aidcs = aidc;
21899 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021900 /*
21901 * Save if we have keyrefs at all.
21902 */
21903 if ((vctxt->hasKeyrefs == 0) &&
21904 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21905 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021906}
21907
21908/**
Daniel Veillard3888f472007-08-23 09:29:03 +000021909 * xmlSchemaAugmentImportedIDC:
21910 * @imported: the imported schema
21911 *
21912 * Creates an augmented IDC definition for the imported schema.
21913 */
21914static void
21915xmlSchemaAugmentImportedIDC(xmlSchemaImportPtr imported, xmlSchemaValidCtxtPtr vctxt) {
21916 if (imported->schema->idcDef != NULL) {
21917 xmlHashScan(imported->schema->idcDef ,
21918 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
21919 }
21920}
21921
21922/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021923 * xmlSchemaIDCNewBinding:
21924 * @idcDef: the IDC definition of this binding
21925 *
21926 * Creates a new IDC binding.
21927 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021928 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021929 */
21930static xmlSchemaPSVIIDCBindingPtr
21931xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21932{
21933 xmlSchemaPSVIIDCBindingPtr ret;
21934
21935 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21936 sizeof(xmlSchemaPSVIIDCBinding));
21937 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021938 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021939 "allocating a PSVI IDC binding item", NULL);
21940 return (NULL);
21941 }
21942 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21943 ret->definition = idcDef;
21944 return (ret);
21945}
21946
21947/**
21948 * xmlSchemaIDCStoreNodeTableItem:
21949 * @vctxt: the WXS validation context
21950 * @item: the IDC node table item
21951 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021952 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021953 * They are stored to avoid copying them if IDC node-tables are merged
21954 * with corresponding parent IDC node-tables (bubbling).
21955 *
21956 * Returns 0 if succeeded, -1 on internal errors.
21957 */
21958static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021959xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021960 xmlSchemaPSVIIDCNodePtr item)
21961{
21962 /*
21963 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021964 */
21965 if (vctxt->idcNodes == NULL) {
21966 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021967 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21968 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021969 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021970 "allocating the IDC node table item list", NULL);
21971 return (-1);
21972 }
21973 vctxt->sizeIdcNodes = 20;
21974 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21975 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021976 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21977 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021978 sizeof(xmlSchemaPSVIIDCNodePtr));
21979 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021980 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021981 "re-allocating the IDC node table item list", NULL);
21982 return (-1);
21983 }
21984 }
21985 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021986
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021987 return (0);
21988}
21989
21990/**
21991 * xmlSchemaIDCStoreKey:
21992 * @vctxt: the WXS validation context
21993 * @item: the IDC key
21994 *
21995 * The validation context is used to store an IDC key.
21996 *
21997 * Returns 0 if succeeded, -1 on internal errors.
21998 */
21999static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022000xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022001 xmlSchemaPSVIIDCKeyPtr key)
22002{
22003 /*
22004 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022005 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022006 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022007 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022008 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
22009 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022010 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022011 "allocating the IDC key storage list", NULL);
22012 return (-1);
22013 }
22014 vctxt->sizeIdcKeys = 40;
22015 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
22016 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022017 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
22018 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022019 sizeof(xmlSchemaPSVIIDCKeyPtr));
22020 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022021 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022022 "re-allocating the IDC key storage list", NULL);
22023 return (-1);
22024 }
22025 }
22026 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022027
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022028 return (0);
22029}
22030
22031/**
22032 * xmlSchemaIDCAppendNodeTableItem:
22033 * @bind: the IDC binding
22034 * @ntItem: the node-table item
22035 *
22036 * Appends the IDC node-table item to the binding.
22037 *
22038 * Returns 0 on success and -1 on internal errors.
22039 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022040static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022041xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
22042 xmlSchemaPSVIIDCNodePtr ntItem)
22043{
22044 if (bind->nodeTable == NULL) {
22045 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022046 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022047 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
22048 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022049 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022050 "allocating an array of IDC node-table items", NULL);
22051 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022052 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022053 } else if (bind->sizeNodes <= bind->nbNodes) {
22054 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022055 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
22056 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022057 sizeof(xmlSchemaPSVIIDCNodePtr));
22058 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022059 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022060 "re-allocating an array of IDC node-table items", NULL);
22061 return(-1);
22062 }
22063 }
22064 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022065 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022066}
22067
22068/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022069 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022070 * @vctxt: the WXS validation context
22071 * @matcher: the IDC matcher
22072 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022073 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022074 * of the given matcher. If none found, a new one is created
22075 * and added to the IDC table.
22076 *
22077 * Returns an IDC binding or NULL on internal errors.
22078 */
22079static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022080xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022081 xmlSchemaIDCMatcherPtr matcher)
22082{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022083 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022084
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022085 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022086
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022087 if (ielem->idcTable == NULL) {
22088 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
22089 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022090 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022091 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022092 } else {
22093 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022094
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022095 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022096 do {
22097 if (bind->definition == matcher->aidc->def)
22098 return(bind);
22099 if (bind->next == NULL) {
22100 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
22101 if (bind->next == NULL)
22102 return (NULL);
22103 return(bind->next);
22104 }
22105 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022106 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022107 }
22108 return (NULL);
22109}
22110
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022111static xmlSchemaItemListPtr
22112xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
22113 xmlSchemaIDCMatcherPtr matcher)
22114{
22115 if (matcher->targets == NULL)
22116 matcher->targets = xmlSchemaItemListCreate();
22117 return(matcher->targets);
22118}
22119
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022120/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022121 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022122 * @key: the IDC key
22123 *
22124 * Frees an IDC key together with its compiled value.
22125 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022126static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022127xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
22128{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022129 if (key->val != NULL)
22130 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022131 xmlFree(key);
22132}
22133
22134/**
22135 * xmlSchemaIDCFreeBinding:
22136 *
22137 * Frees an IDC binding. Note that the node table-items
22138 * are not freed.
22139 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022140static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022141xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
22142{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022143 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022144 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022145 if (bind->dupls != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000022146 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022147 xmlFree(bind);
22148}
22149
22150/**
22151 * xmlSchemaIDCFreeIDCTable:
22152 * @bind: the first IDC binding in the list
22153 *
22154 * Frees an IDC table, i.e. all the IDC bindings in the list.
22155 */
22156static void
22157xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
22158{
22159 xmlSchemaPSVIIDCBindingPtr prev;
22160
22161 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022162 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022163 bind = bind->next;
22164 xmlSchemaIDCFreeBinding(prev);
22165 }
22166}
22167
22168/**
22169 * xmlSchemaIDCFreeMatcherList:
22170 * @matcher: the first IDC matcher in the list
22171 *
22172 * Frees a list of IDC matchers.
22173 */
22174static void
22175xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
22176{
22177 xmlSchemaIDCMatcherPtr next;
22178
22179 while (matcher != NULL) {
22180 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022181 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022182 int i;
22183 for (i = 0; i < matcher->sizeKeySeqs; i++)
22184 if (matcher->keySeqs[i] != NULL)
22185 xmlFree(matcher->keySeqs[i]);
Daniel Veillarddee23482008-04-11 12:58:43 +000022186 xmlFree(matcher->keySeqs);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022187 }
22188 if (matcher->targets != NULL) {
22189 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22190 int i;
22191 xmlSchemaPSVIIDCNodePtr idcNode;
22192 /*
22193 * Node-table items for keyrefs are not stored globally
22194 * to the validation context, since they are not bubbled.
22195 * We need to free them here.
22196 */
22197 for (i = 0; i < matcher->targets->nbItems; i++) {
22198 idcNode =
22199 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22200 xmlFree(idcNode->keys);
22201 xmlFree(idcNode);
22202 }
22203 }
22204 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022205 }
22206 xmlFree(matcher);
22207 matcher = next;
22208 }
22209}
22210
22211/**
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022212 * xmlSchemaIDCReleaseMatcherList:
22213 * @vctxt: the WXS validation context
22214 * @matcher: the first IDC matcher in the list
22215 *
22216 * Caches a list of IDC matchers for reuse.
22217 */
22218static void
22219xmlSchemaIDCReleaseMatcherList(xmlSchemaValidCtxtPtr vctxt,
22220 xmlSchemaIDCMatcherPtr matcher)
22221{
22222 xmlSchemaIDCMatcherPtr next;
22223
22224 while (matcher != NULL) {
22225 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022226 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022227 int i;
22228 /*
22229 * Don't free the array, but only the content.
22230 */
22231 for (i = 0; i < matcher->sizeKeySeqs; i++)
22232 if (matcher->keySeqs[i] != NULL) {
22233 xmlFree(matcher->keySeqs[i]);
22234 matcher->keySeqs[i] = NULL;
22235 }
22236 }
22237 if (matcher->targets) {
22238 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22239 int i;
22240 xmlSchemaPSVIIDCNodePtr idcNode;
22241 /*
22242 * Node-table items for keyrefs are not stored globally
22243 * to the validation context, since they are not bubbled.
22244 * We need to free them here.
22245 */
22246 for (i = 0; i < matcher->targets->nbItems; i++) {
22247 idcNode =
22248 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22249 xmlFree(idcNode->keys);
22250 xmlFree(idcNode);
22251 }
22252 }
22253 xmlSchemaItemListFree(matcher->targets);
22254 matcher->targets = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022255 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022256 matcher->next = NULL;
22257 /*
22258 * Cache the matcher.
22259 */
22260 if (vctxt->idcMatcherCache != NULL)
22261 matcher->nextCached = vctxt->idcMatcherCache;
22262 vctxt->idcMatcherCache = matcher;
22263
22264 matcher = next;
22265 }
22266}
22267
22268/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022269 * xmlSchemaIDCAddStateObject:
22270 * @vctxt: the WXS validation context
22271 * @matcher: the IDC matcher
22272 * @sel: the XPath information
22273 * @parent: the parent "selector" state object if any
22274 * @type: "selector" or "field"
22275 *
22276 * Creates/reuses and activates state objects for the given
22277 * XPath information; if the XPath expression consists of unions,
22278 * multiple state objects are created for every unioned expression.
22279 *
22280 * Returns 0 on success and -1 on internal errors.
22281 */
22282static int
22283xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
22284 xmlSchemaIDCMatcherPtr matcher,
22285 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022286 int type)
22287{
22288 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022289
22290 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022291 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022292 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022293 if (vctxt->xpathStatePool != NULL) {
22294 sto = vctxt->xpathStatePool;
22295 vctxt->xpathStatePool = sto->next;
22296 sto->next = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022297 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022298 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022299 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022300 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022301 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
22302 if (sto == NULL) {
22303 xmlSchemaVErrMemory(NULL,
22304 "allocating an IDC state object", NULL);
22305 return (-1);
22306 }
22307 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
Daniel Veillarddee23482008-04-11 12:58:43 +000022308 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022309 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000022310 * Add to global list.
22311 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022312 if (vctxt->xpathStates != NULL)
22313 sto->next = vctxt->xpathStates;
22314 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022315
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022316 /*
22317 * Free the old xpath validation context.
22318 */
22319 if (sto->xpathCtxt != NULL)
22320 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22321
22322 /*
22323 * Create a new XPath (pattern) validation context.
22324 */
22325 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22326 (xmlPatternPtr) sel->xpathComp);
22327 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022328 VERROR_INT("xmlSchemaIDCAddStateObject",
22329 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022330 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000022331 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022332 sto->type = type;
22333 sto->depth = vctxt->depth;
22334 sto->matcher = matcher;
22335 sto->sel = sel;
22336 sto->nbHistory = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000022337
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022338#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022339 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22340 sto->sel->xpath);
22341#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022342 return (0);
22343}
22344
22345/**
22346 * xmlSchemaXPathEvaluate:
22347 * @vctxt: the WXS validation context
22348 * @nodeType: the nodeType of the current node
22349 *
22350 * Evaluates all active XPath state objects.
22351 *
22352 * Returns the number of IC "field" state objects which resolved to
22353 * this node, 0 if none resolved and -1 on internal errors.
22354 */
22355static int
22356xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022357 xmlElementType nodeType)
22358{
22359 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022360 int res, resolved = 0, depth = vctxt->depth;
Daniel Veillarddee23482008-04-11 12:58:43 +000022361
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022362 if (vctxt->xpathStates == NULL)
22363 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022364
22365 if (nodeType == XML_ATTRIBUTE_NODE)
22366 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022367#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022368 {
22369 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022370 xmlGenericError(xmlGenericErrorContext,
22371 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022372 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22373 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022374 FREE_AND_NULL(str)
22375 }
22376#endif
22377 /*
22378 * Process all active XPath state objects.
22379 */
22380 first = vctxt->xpathStates;
22381 sto = first;
22382 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022383#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022384 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Daniel Veillarddee23482008-04-11 12:58:43 +000022385 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022386 sto->matcher->aidc->def->name, sto->sel->xpath);
22387 else
Daniel Veillarddee23482008-04-11 12:58:43 +000022388 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022389 sto->matcher->aidc->def->name, sto->sel->xpath);
22390#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022391 if (nodeType == XML_ELEMENT_NODE)
22392 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022393 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022394 else
22395 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022396 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022397
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022398 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022399 VERROR_INT("xmlSchemaXPathEvaluate",
22400 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022401 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022402 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022403 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022404 goto next_sto;
22405 /*
22406 * Full match.
22407 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022408#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022409 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022410 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022411#endif
22412 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022413 * Register a match in the state object history.
22414 */
22415 if (sto->history == NULL) {
22416 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22417 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022418 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022419 "allocating the state object history", NULL);
22420 return(-1);
22421 }
Daniel Veillard6f9b0872006-08-12 14:09:01 +000022422 sto->sizeHistory = 5;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022423 } else if (sto->sizeHistory <= sto->nbHistory) {
22424 sto->sizeHistory *= 2;
22425 sto->history = (int *) xmlRealloc(sto->history,
22426 sto->sizeHistory * sizeof(int));
22427 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022428 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022429 "re-allocating the state object history", NULL);
22430 return(-1);
22431 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022432 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022433 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022434
22435#ifdef DEBUG_IDC
22436 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22437 vctxt->depth);
22438#endif
22439
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022440 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22441 xmlSchemaIDCSelectPtr sel;
22442 /*
22443 * Activate state objects for the IDC fields of
22444 * the IDC selector.
22445 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022446#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022447 xmlGenericError(xmlGenericErrorContext, "IDC: "
22448 "activating field states\n");
22449#endif
22450 sel = sto->matcher->aidc->def->fields;
22451 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022452 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22453 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22454 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022455 sel = sel->next;
22456 }
22457 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22458 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022459 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022460 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022461#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022462 xmlGenericError(xmlGenericErrorContext,
22463 "IDC: key found\n");
22464#endif
22465 /*
22466 * Notify that the character value of this node is
22467 * needed.
22468 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022469 if (resolved == 0) {
22470 if ((vctxt->inode->flags &
22471 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22472 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22473 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022474 resolved++;
22475 }
22476next_sto:
22477 if (sto->next == NULL) {
22478 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022479 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022480 */
22481 head = first;
22482 sto = vctxt->xpathStates;
22483 } else
22484 sto = sto->next;
22485 }
22486 return (resolved);
22487}
22488
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022489static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022490xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022491 xmlChar **buf,
22492 xmlSchemaPSVIIDCKeyPtr *seq,
22493 int count)
22494{
22495 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022496 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022497
22498 *buf = xmlStrdup(BAD_CAST "[");
22499 for (i = 0; i < count; i++) {
22500 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +000022501 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022502 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22503 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022504 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022505 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022506 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022507 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22508 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022509 *buf = xmlStrcat(*buf, BAD_CAST "???");
22510 }
22511 if (i < count -1)
22512 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22513 else
22514 *buf = xmlStrcat(*buf, BAD_CAST "'");
22515 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022516 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022517 value = NULL;
22518 }
22519 }
22520 *buf = xmlStrcat(*buf, BAD_CAST "]");
22521
22522 return (BAD_CAST *buf);
22523}
22524
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022525/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022526 * xmlSchemaXPathPop:
22527 * @vctxt: the WXS validation context
22528 *
22529 * Pops all XPath states.
22530 *
22531 * Returns 0 on success and -1 on internal errors.
22532 */
22533static int
22534xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22535{
22536 xmlSchemaIDCStateObjPtr sto;
22537 int res;
22538
22539 if (vctxt->xpathStates == NULL)
22540 return(0);
22541 sto = vctxt->xpathStates;
22542 do {
22543 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22544 if (res == -1)
22545 return (-1);
22546 sto = sto->next;
22547 } while (sto != NULL);
22548 return(0);
22549}
22550
22551/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022552 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022553 * @vctxt: the WXS validation context
22554 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022555 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022556 *
22557 * Processes and pops the history items of the IDC state objects.
22558 * IDC key-sequences are validated/created on IDC bindings.
Daniel Veillarddee23482008-04-11 12:58:43 +000022559 *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022560 * Returns 0 on success and -1 on internal errors.
22561 */
22562static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022563xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022564 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022565{
22566 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022567 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022568 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022569 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022570
22571 if (vctxt->xpathStates == NULL)
22572 return (0);
22573 sto = vctxt->xpathStates;
22574
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022575#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022576 {
22577 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022578 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022579 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022580 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22581 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022582 FREE_AND_NULL(str)
22583 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022584#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022585 /*
22586 * Evaluate the state objects.
22587 */
22588 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022589 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22590 if (res == -1) {
22591 VERROR_INT("xmlSchemaXPathProcessHistory",
22592 "calling xmlStreamPop()");
22593 return (-1);
22594 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022595#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022596 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22597 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022598#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022599 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022600 goto deregister_check;
22601
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022602 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022603
22604 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022605 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022606 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022607 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022608 sto = sto->next;
22609 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000022610 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022611 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022612 /*
22613 * NOTE: According to
22614 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22615 * ... the simple-content of complex types is also allowed.
22616 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022617
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022618 if (WXS_IS_COMPLEX(type)) {
22619 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22620 /*
22621 * Sanity check for complex types with simple content.
22622 */
22623 simpleType = type->contentTypeDef;
22624 if (simpleType == NULL) {
22625 VERROR_INT("xmlSchemaXPathProcessHistory",
22626 "field resolves to a CT with simple content "
22627 "but the CT is missing the ST definition");
22628 return (-1);
22629 }
22630 } else
22631 simpleType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022632 } else
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022633 simpleType = type;
22634 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022635 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022636
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022637 /*
22638 * Not qualified if the field resolves to a node of non
22639 * simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022640 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022641 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +000022642 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022643 WXS_BASIC_CAST sto->matcher->aidc->def,
22644 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022645 "non-simple type",
22646 sto->sel->xpath,
22647 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22648 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022649 sto->nbHistory--;
22650 goto deregister_check;
22651 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022652
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022653 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022654 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022655 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022656 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022657 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022658 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022659 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022660 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022661 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022662 sto->nbHistory--;
22663 goto deregister_check;
22664 } else {
22665 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22666 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022667 int pos, idx;
Daniel Veillarddee23482008-04-11 12:58:43 +000022668
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022669 /*
22670 * The key will be anchored on the matcher's list of
22671 * key-sequences. The position in this list is determined
22672 * by the target node's depth relative to the matcher's
22673 * depth of creation (i.e. the depth of the scope element).
Daniel Veillarddee23482008-04-11 12:58:43 +000022674 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022675 * Element Depth Pos List-entries
22676 * <scope> 0 NULL
22677 * <bar> 1 NULL
22678 * <target/> 2 2 target
22679 * <bar>
22680 * </scope>
22681 *
22682 * The size of the list is only dependant on the depth of
22683 * the tree.
22684 * An entry will be NULLed in selector_leave, i.e. when
Daniel Veillarddee23482008-04-11 12:58:43 +000022685 * we hit the target's
22686 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022687 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022688 idx = sto->sel->index;
Daniel Veillarddee23482008-04-11 12:58:43 +000022689
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022690 /*
22691 * Create/grow the array of key-sequences.
22692 */
22693 if (matcher->keySeqs == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022694 if (pos > 9)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022695 matcher->sizeKeySeqs = pos * 2;
22696 else
22697 matcher->sizeKeySeqs = 10;
Daniel Veillarddee23482008-04-11 12:58:43 +000022698 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022699 xmlMalloc(matcher->sizeKeySeqs *
Daniel Veillarddee23482008-04-11 12:58:43 +000022700 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22701 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022702 xmlSchemaVErrMemory(NULL,
22703 "allocating an array of key-sequences",
22704 NULL);
22705 return(-1);
22706 }
22707 memset(matcher->keySeqs, 0,
22708 matcher->sizeKeySeqs *
22709 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Daniel Veillarddee23482008-04-11 12:58:43 +000022710 } else if (pos >= matcher->sizeKeySeqs) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022711 int i = matcher->sizeKeySeqs;
Daniel Veillarddee23482008-04-11 12:58:43 +000022712
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022713 matcher->sizeKeySeqs *= 2;
22714 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22715 xmlRealloc(matcher->keySeqs,
22716 matcher->sizeKeySeqs *
22717 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022718 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022719 xmlSchemaVErrMemory(NULL,
22720 "reallocating an array of key-sequences",
22721 NULL);
22722 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022723 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022724 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022725 * The array needs to be NULLed.
22726 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022727 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022728 for (; i < matcher->sizeKeySeqs; i++)
22729 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022730 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022731
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022732 /*
22733 * Get/create the key-sequence.
22734 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022735 keySeq = matcher->keySeqs[pos];
22736 if (keySeq == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022737 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022738 } else if (keySeq[idx] != NULL) {
22739 xmlChar *str = NULL;
22740 /*
22741 * cvc-identity-constraint:
Rob Richardsc6947bb2008-06-29 15:04:41 +000022742 * 3 For each node in the �target node set� all
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022743 * of the {fields}, with that node as the context
22744 * node, evaluate to either an empty node-set or
22745 * a node-set with exactly one member, which must
22746 * have a simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022747 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022748 * The key was already set; report an error.
22749 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022750 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022751 XML_SCHEMAV_CVC_IDC, NULL,
22752 WXS_BASIC_CAST matcher->aidc->def,
22753 "The XPath '%s' of a field of %s evaluates to a "
22754 "node-set with more than one member",
22755 sto->sel->xpath,
22756 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22757 FREE_AND_NULL(str);
22758 sto->nbHistory--;
22759 goto deregister_check;
22760 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000022761 goto create_key;
22762
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022763create_sequence:
22764 /*
22765 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022766 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022767 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
Daniel Veillarddee23482008-04-11 12:58:43 +000022768 matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022769 sizeof(xmlSchemaPSVIIDCKeyPtr));
22770 if (keySeq == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022771 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022772 "allocating an IDC key-sequence", NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000022773 return(-1);
22774 }
22775 memset(keySeq, 0, matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022776 sizeof(xmlSchemaPSVIIDCKeyPtr));
22777 matcher->keySeqs[pos] = keySeq;
22778create_key:
22779 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022780 * Create a key once per node only.
Daniel Veillarddee23482008-04-11 12:58:43 +000022781 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022782 if (key == NULL) {
22783 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22784 sizeof(xmlSchemaPSVIIDCKey));
22785 if (key == NULL) {
22786 xmlSchemaVErrMemory(NULL,
22787 "allocating a IDC key", NULL);
22788 xmlFree(keySeq);
22789 matcher->keySeqs[pos] = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022790 return(-1);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022791 }
22792 /*
22793 * Consume the compiled value.
22794 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022795 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022796 key->val = vctxt->inode->val;
22797 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022798 /*
22799 * Store the key in a global list.
22800 */
22801 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22802 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022803 return (-1);
22804 }
22805 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022806 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022807 }
22808 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022809
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022810 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022811 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022812 xmlSchemaPSVIIDCNodePtr ntItem;
22813 xmlSchemaIDCMatcherPtr matcher;
22814 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022815 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022816 int pos, i, j, nbKeys;
22817 /*
22818 * Here we have the following scenario:
22819 * An IDC 'selector' state object resolved to a target node,
Daniel Veillarddee23482008-04-11 12:58:43 +000022820 * during the time this target node was in the
22821 * ancestor-or-self axis, the 'field' state object(s) looked
22822 * out for matching nodes to create a key-sequence for this
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022823 * target node. Now we are back to this target node and need
Daniel Veillarddee23482008-04-11 12:58:43 +000022824 * to put the key-sequence, together with the target node
22825 * itself, into the node-table of the corresponding IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022826 * binding.
22827 */
22828 matcher = sto->matcher;
22829 idc = matcher->aidc->def;
22830 nbKeys = idc->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000022831 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022832 /*
22833 * Check if the matcher has any key-sequences at all, plus
22834 * if it has a key-sequence for the current target node.
Daniel Veillarddee23482008-04-11 12:58:43 +000022835 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022836 if ((matcher->keySeqs == NULL) ||
22837 (matcher->sizeKeySeqs <= pos)) {
22838 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22839 goto selector_key_error;
22840 else
22841 goto selector_leave;
22842 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022843
22844 keySeq = &(matcher->keySeqs[pos]);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022845 if (*keySeq == NULL) {
22846 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22847 goto selector_key_error;
22848 else
22849 goto selector_leave;
22850 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022851
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022852 for (i = 0; i < nbKeys; i++) {
22853 if ((*keySeq)[i] == NULL) {
22854 /*
22855 * Not qualified, if not all fields did resolve.
22856 */
22857 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22858 /*
22859 * All fields of a "key" IDC must resolve.
22860 */
22861 goto selector_key_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000022862 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022863 goto selector_leave;
22864 }
22865 }
22866 /*
22867 * All fields did resolve.
22868 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022869
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022870 /*
22871 * 4.1 If the {identity-constraint category} is unique(/key),
Rob Richardsc6947bb2008-06-29 15:04:41 +000022872 * then no two members of the �qualified node set� have
22873 * �key-sequences� whose members are pairwise equal, as
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022874 * defined by Equal in [XML Schemas: Datatypes].
22875 *
22876 * Get the IDC binding from the matcher and check for
22877 * duplicate key-sequences.
22878 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022879#if 0
22880 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22881#endif
22882 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Daniel Veillarddee23482008-04-11 12:58:43 +000022883 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022884 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022885 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Daniel Veillarddee23482008-04-11 12:58:43 +000022886
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022887 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022888 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022889 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022890 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022891 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022892 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022893 bkeySeq =
22894 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022895 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022896 ckey = (*keySeq)[j];
Daniel Veillarddee23482008-04-11 12:58:43 +000022897 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022898 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022899 if (res == -1) {
22900 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022901 } else if (res == 0) {
22902 /*
22903 * One of the keys differs, so the key-sequence
22904 * won't be equal; get out.
22905 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022906 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022907 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022908 }
22909 if (res == 1) {
22910 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022911 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022912 */
22913 break;
22914 }
22915 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022916 } while (i < targets->nbItems);
22917 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022918 xmlChar *str = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022919 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022920 * TODO: Try to report the key-sequence.
22921 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022922 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022923 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022924 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022925 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022926 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022927 (*keySeq), nbKeys),
22928 xmlSchemaGetIDCDesignation(&strB, idc));
22929 FREE_AND_NULL(str);
22930 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022931 goto selector_leave;
22932 }
22933 }
22934 /*
22935 * Add a node-table item to the IDC binding.
22936 */
22937 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22938 sizeof(xmlSchemaPSVIIDCNode));
22939 if (ntItem == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022940 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022941 "allocating an IDC node-table item", NULL);
22942 xmlFree(*keySeq);
22943 *keySeq = NULL;
22944 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000022945 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022946 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Daniel Veillarddee23482008-04-11 12:58:43 +000022947
22948 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022949 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022950 */
22951 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22952 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22953 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022954 xmlFree(*keySeq);
22955 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022956 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022957 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022958 ntItem->nodeQNameID = -1;
22959 } else {
22960 /*
22961 * Save a cached QName for this node on the IDC node, to be
22962 * able to report it, even if the node is not saved.
22963 */
22964 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22965 vctxt->inode->localName, vctxt->inode->nsName);
22966 if (ntItem->nodeQNameID == -1) {
22967 xmlFree(ntItem);
22968 xmlFree(*keySeq);
22969 *keySeq = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022970 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022971 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022972 }
22973 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022974 * Init the node-table item: Save the node, position and
22975 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022976 */
22977 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022978 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022979 ntItem->keys = *keySeq;
22980 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022981#if 0
Daniel Veillarddee23482008-04-11 12:58:43 +000022982 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022983#endif
22984 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022985 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022986 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022987 * Free the item, since keyref items won't be
22988 * put on a global list.
22989 */
22990 xmlFree(ntItem->keys);
22991 xmlFree(ntItem);
22992 }
22993 return (-1);
22994 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022995
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022996 goto selector_leave;
22997selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022998 {
22999 xmlChar *str = NULL;
23000 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000023001 * 4.2.1 (KEY) The �target node set� and the
23002 * �qualified node set� are equal, that is, every
23003 * member of the �target node set� is also a member
23004 * of the �qualified node set� and vice versa.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023005 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023006 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023007 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023008 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023009 "Not all fields of %s evaluate to a node",
23010 xmlSchemaGetIDCDesignation(&str, idc), NULL);
23011 FREE_AND_NULL(str);
23012 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023013selector_leave:
23014 /*
23015 * Free the key-sequence if not added to the IDC table.
23016 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023017 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023018 xmlFree(*keySeq);
23019 *keySeq = NULL;
23020 }
23021 } /* if selector */
Daniel Veillarddee23482008-04-11 12:58:43 +000023022
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023023 sto->nbHistory--;
23024
23025deregister_check:
23026 /*
23027 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023028 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023029 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023030#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023031 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
23032 sto->sel->xpath);
23033#endif
23034 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023035 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023036 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023037 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023038 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023039 nextsto = sto->next;
23040 /*
23041 * Unlink from the list of active XPath state objects.
23042 */
23043 vctxt->xpathStates = sto->next;
23044 sto->next = vctxt->xpathStatePool;
23045 /*
23046 * Link it to the pool of reusable state objects.
23047 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023048 vctxt->xpathStatePool = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023049 sto = nextsto;
23050 } else
23051 sto = sto->next;
23052 } /* while (sto != NULL) */
23053 return (0);
23054}
23055
23056/**
23057 * xmlSchemaIDCRegisterMatchers:
23058 * @vctxt: the WXS validation context
23059 * @elemDecl: the element declaration
23060 *
23061 * Creates helper objects to evaluate IDC selectors/fields
23062 * successively.
23063 *
23064 * Returns 0 if OK and -1 on internal errors.
23065 */
23066static int
23067xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
23068 xmlSchemaElementPtr elemDecl)
23069{
23070 xmlSchemaIDCMatcherPtr matcher, last = NULL;
23071 xmlSchemaIDCPtr idc, refIdc;
23072 xmlSchemaIDCAugPtr aidc;
Daniel Veillarddee23482008-04-11 12:58:43 +000023073
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023074 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
23075 if (idc == NULL)
23076 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000023077
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023078#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023079 {
23080 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023081 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023082 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023083 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
23084 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023085 FREE_AND_NULL(str)
23086 }
23087#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023088 if (vctxt->inode->idcMatchers != NULL) {
23089 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23090 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023091 return (-1);
23092 }
23093 do {
23094 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
23095 /*
23096 * Since IDCs bubbles are expensive we need to know the
23097 * depth at which the bubbles should stop; this will be
23098 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023099 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023100 * be -1, indicating that no bubbles are needed.
23101 */
23102 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
23103 if (refIdc != NULL) {
23104 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023105 * Remember that we have keyrefs on this node.
23106 */
23107 vctxt->inode->hasKeyrefs = 1;
23108 /*
23109 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023110 */
23111 aidc = vctxt->aidcs;
23112 while (aidc != NULL) {
23113 if (aidc->def == refIdc)
23114 break;
23115 aidc = aidc->next;
23116 }
23117 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023118 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023119 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023120 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023121 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000023122 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023123 if ((aidc->keyrefDepth == -1) ||
23124 (vctxt->depth < aidc->keyrefDepth))
23125 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023126 }
23127 }
23128 /*
23129 * Lookup the augmented IDC item for the IDC definition.
23130 */
23131 aidc = vctxt->aidcs;
23132 while (aidc != NULL) {
23133 if (aidc->def == idc)
23134 break;
23135 aidc = aidc->next;
23136 }
23137 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023138 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23139 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023140 return (-1);
23141 }
23142 /*
23143 * Create an IDC matcher for every IDC definition.
23144 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023145 if (vctxt->idcMatcherCache != NULL) {
23146 /*
23147 * Reuse a cached matcher.
23148 */
23149 matcher = vctxt->idcMatcherCache;
23150 vctxt->idcMatcherCache = matcher->nextCached;
23151 matcher->nextCached = NULL;
23152 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000023153 matcher = (xmlSchemaIDCMatcherPtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023154 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
23155 if (matcher == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023156 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023157 "allocating an IDC matcher", NULL);
23158 return (-1);
23159 }
23160 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023161 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023162 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023163 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023164 else
23165 last->next = matcher;
23166 last = matcher;
23167
23168 matcher->type = IDC_MATCHER;
Daniel Veillarddee23482008-04-11 12:58:43 +000023169 matcher->depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023170 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023171 matcher->idcType = aidc->def->type;
Daniel Veillarddee23482008-04-11 12:58:43 +000023172#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023173 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
Daniel Veillarddee23482008-04-11 12:58:43 +000023174#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023175 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023176 * Init the automaton state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023177 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023178 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023179 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023180 return (-1);
23181
23182 idc = idc->next;
23183 } while (idc != NULL);
23184 return (0);
23185}
23186
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023187static int
23188xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
23189 xmlSchemaNodeInfoPtr ielem)
23190{
23191 xmlSchemaPSVIIDCBindingPtr bind;
23192 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
23193 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
23194 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023195
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023196 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
23197 /* vctxt->createIDCNodeTables */
23198 while (matcher != NULL) {
23199 /*
23200 * Skip keyref IDCs and empty IDC target-lists.
23201 */
23202 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
23203 WXS_ILIST_IS_EMPTY(matcher->targets))
23204 {
23205 matcher = matcher->next;
23206 continue;
23207 }
23208 /*
23209 * If we _want_ the IDC node-table to be created in any case
23210 * then do so. Otherwise create them only if keyrefs need them.
23211 */
23212 if ((! vctxt->createIDCNodeTables) &&
23213 ((matcher->aidc->keyrefDepth == -1) ||
23214 (matcher->aidc->keyrefDepth > vctxt->depth)))
23215 {
23216 matcher = matcher->next;
23217 continue;
23218 }
23219 /*
23220 * Get/create the IDC binding on this element for the IDC definition.
23221 */
23222 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
23223
23224 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
23225 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
23226 nbDupls = bind->dupls->nbItems;
23227 } else {
23228 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023229 nbDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023230 }
23231 if (bind->nodeTable != NULL) {
23232 nbNodeTable = bind->nbNodes;
23233 } else {
23234 nbNodeTable = 0;
23235 }
23236
23237 if ((nbNodeTable == 0) && (nbDupls == 0)) {
23238 /*
23239 * Transfer all IDC target-nodes to the IDC node-table.
23240 */
23241 bind->nodeTable =
Daniel Veillarddee23482008-04-11 12:58:43 +000023242 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023243 bind->sizeNodes = matcher->targets->sizeItems;
23244 bind->nbNodes = matcher->targets->nbItems;
23245
23246 matcher->targets->items = NULL;
23247 matcher->targets->sizeItems = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023248 matcher->targets->nbItems = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023249 } else {
23250 /*
23251 * Compare the key-sequences and add to the IDC node-table.
23252 */
23253 nbTargets = matcher->targets->nbItems;
Daniel Veillarddee23482008-04-11 12:58:43 +000023254 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023255 nbFields = matcher->aidc->def->nbFields;
23256 i = 0;
23257 do {
23258 keys = targets[i]->keys;
Daniel Veillarddee23482008-04-11 12:58:43 +000023259 if (nbDupls) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023260 /*
23261 * Search in already found duplicates first.
23262 */
23263 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023264 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023265 if (nbFields == 1) {
23266 res = xmlSchemaAreValuesEqual(keys[0]->val,
23267 dupls[j]->keys[0]->val);
23268 if (res == -1)
23269 goto internal_error;
23270 if (res == 1) {
23271 /*
23272 * Equal key-sequence.
23273 */
23274 goto next_target;
23275 }
23276 } else {
23277 res = 0;
23278 ntkeys = dupls[j]->keys;
23279 for (k = 0; k < nbFields; k++) {
23280 res = xmlSchemaAreValuesEqual(keys[k]->val,
23281 ntkeys[k]->val);
23282 if (res == -1)
23283 goto internal_error;
23284 if (res == 0) {
23285 /*
23286 * One of the keys differs.
23287 */
23288 break;
23289 }
23290 }
23291 if (res == 1) {
23292 /*
23293 * Equal key-sequence found.
23294 */
23295 goto next_target;
23296 }
23297 }
23298 j++;
Daniel Veillarddee23482008-04-11 12:58:43 +000023299 } while (j < nbDupls);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023300 }
23301 if (nbNodeTable) {
23302 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023303 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023304 if (nbFields == 1) {
23305 res = xmlSchemaAreValuesEqual(keys[0]->val,
23306 bind->nodeTable[j]->keys[0]->val);
23307 if (res == -1)
23308 goto internal_error;
23309 if (res == 0) {
23310 /*
23311 * The key-sequence differs.
23312 */
23313 goto next_node_table_entry;
23314 }
23315 } else {
23316 res = 0;
23317 ntkeys = bind->nodeTable[j]->keys;
23318 for (k = 0; k < nbFields; k++) {
23319 res = xmlSchemaAreValuesEqual(keys[k]->val,
23320 ntkeys[k]->val);
23321 if (res == -1)
23322 goto internal_error;
23323 if (res == 0) {
23324 /*
23325 * One of the keys differs.
23326 */
23327 goto next_node_table_entry;
23328 }
23329 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023330 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023331 /*
23332 * Add the duplicate to the list of duplicates.
23333 */
23334 if (bind->dupls == NULL) {
23335 bind->dupls = xmlSchemaItemListCreate();
23336 if (bind->dupls == NULL)
23337 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023338 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023339 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23340 goto internal_error;
23341 /*
23342 * Remove the duplicate entry from the IDC node-table.
23343 */
23344 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23345 bind->nbNodes--;
23346
23347 goto next_target;
23348
23349next_node_table_entry:
23350 j++;
23351 } while (j < nbNodeTable);
23352 }
23353 /*
23354 * If everything is fine, then add the IDC target-node to
23355 * the IDC node-table.
23356 */
23357 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23358 goto internal_error;
23359
23360next_target:
23361 i++;
23362 } while (i < nbTargets);
23363 }
23364 matcher = matcher->next;
23365 }
23366 return(0);
23367
23368internal_error:
23369 return(-1);
23370}
23371
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023372/**
Daniel Veillarddee23482008-04-11 12:58:43 +000023373 * xmlSchemaBubbleIDCNodeTables:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023374 * @depth: the current tree depth
23375 *
Daniel Veillarddee23482008-04-11 12:58:43 +000023376 * Merges IDC bindings of an element at @depth into the corresponding IDC
23377 * bindings of its parent element. If a duplicate note-table entry is found,
23378 * both, the parent node-table entry and child entry are discarded from the
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023379 * node-table of the parent.
23380 *
23381 * Returns 0 if OK and -1 on internal errors.
23382 */
23383static int
23384xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23385{
23386 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023387 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23388 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023389 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023390 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023391
Daniel Veillarddee23482008-04-11 12:58:43 +000023392 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023393 if (bind == NULL) {
23394 /* Fine, no table, no bubbles. */
23395 return (0);
23396 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023397
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023398 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23399 /*
23400 * Walk all bindings; create new or add to existing bindings.
23401 * Remove duplicate key-sequences.
23402 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023403 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023404
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023405 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23406 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023407 /*
23408 * Check if the key/unique IDC table needs to be bubbled.
23409 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023410 if (! vctxt->createIDCNodeTables) {
23411 aidc = vctxt->aidcs;
23412 do {
23413 if (aidc->def == bind->definition) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023414 if ((aidc->keyrefDepth == -1) ||
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023415 (aidc->keyrefDepth >= vctxt->depth)) {
23416 goto next_binding;
23417 }
23418 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023419 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023420 aidc = aidc->next;
23421 } while (aidc != NULL);
23422 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023423
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023424 if (parTable != NULL)
23425 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023426 /*
23427 * Search a matching parent binding for the
23428 * IDC definition.
23429 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023430 while (parBind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023431 if (parBind->definition == bind->definition)
23432 break;
23433 parBind = parBind->next;
23434 }
23435
23436 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023437 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023438 * Compare every node-table entry of the child node,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023439 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023440 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023441 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023442
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023443 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23444 oldDupls = parBind->dupls->nbItems;
23445 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23446 } else {
23447 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023448 oldDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023449 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023450
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023451 parNodes = parBind->nodeTable;
23452 nbFields = bind->definition->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000023453
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023454 for (i = 0; i < bind->nbNodes; i++) {
23455 node = bind->nodeTable[i];
23456 if (node == NULL)
23457 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023458 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023459 * ...with every key-sequence of the parent node, already
23460 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023461 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023462 if (oldDupls) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023463 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023464 while (j < oldDupls) {
23465 if (nbFields == 1) {
23466 ret = xmlSchemaAreValuesEqual(
23467 node->keys[0]->val,
23468 dupls[j]->keys[0]->val);
23469 if (ret == -1)
23470 goto internal_error;
23471 if (ret == 0) {
23472 j++;
23473 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023474 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023475 } else {
23476 parNode = dupls[j];
23477 for (k = 0; k < nbFields; k++) {
23478 ret = xmlSchemaAreValuesEqual(
23479 node->keys[k]->val,
23480 parNode->keys[k]->val);
23481 if (ret == -1)
23482 goto internal_error;
23483 if (ret == 0)
23484 break;
23485 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023486 }
23487 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023488 /* Duplicate found. */
23489 break;
23490 j++;
23491 }
23492 if (j != oldDupls) {
23493 /* Duplicate found. Skip this entry. */
23494 continue;
23495 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023496 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023497 /*
23498 * ... and with every key-sequence of the parent node.
23499 */
23500 if (oldNum) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023501 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023502 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023503 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023504 if (nbFields == 1) {
23505 ret = xmlSchemaAreValuesEqual(
23506 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023507 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023508 if (ret == -1)
23509 goto internal_error;
23510 if (ret == 0) {
23511 j++;
23512 continue;
23513 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023514 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023515 for (k = 0; k < nbFields; k++) {
23516 ret = xmlSchemaAreValuesEqual(
23517 node->keys[k]->val,
23518 parNode->keys[k]->val);
23519 if (ret == -1)
23520 goto internal_error;
23521 if (ret == 0)
23522 break;
23523 }
23524 }
23525 if (ret == 1)
23526 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023527 break;
23528 j++;
23529 }
23530 if (j != oldNum) {
23531 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023532 * Handle duplicates. Move the duplicate in
23533 * the parent's node-table to the list of
23534 * duplicates.
Daniel Veillarddee23482008-04-11 12:58:43 +000023535 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023536 oldNum--;
23537 parBind->nbNodes--;
23538 /*
23539 * Move last old item to pos of duplicate.
23540 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023541 parNodes[j] = parNodes[oldNum];
Daniel Veillarddee23482008-04-11 12:58:43 +000023542
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023543 if (parBind->nbNodes != oldNum) {
23544 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023545 * If new items exist, move last new item to
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023546 * last of old items.
23547 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023548 parNodes[oldNum] =
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023549 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023550 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023551 if (parBind->dupls == NULL) {
23552 parBind->dupls = xmlSchemaItemListCreate();
23553 if (parBind->dupls == NULL)
23554 goto internal_error;
23555 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023556 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023557 } else {
23558 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023559 * Add the node-table entry (node and key-sequence) of
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023560 * the child node to the node table of the parent node.
23561 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023562 if (parBind->nodeTable == NULL) {
23563 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023564 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023565 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023566 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023567 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023568 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023569 }
23570 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023571 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023572 parBind->sizeNodes *= 2;
Daniel Veillarddee23482008-04-11 12:58:43 +000023573 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23574 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023575 sizeof(xmlSchemaPSVIIDCNodePtr));
23576 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023577 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023578 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023579 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023580 }
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023581 }
23582 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023583 /*
23584 * Append the new node-table entry to the 'new node-table
23585 * entries' section.
23586 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023587 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023588 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023589
Daniel Veillarddee23482008-04-11 12:58:43 +000023590 }
23591
23592 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023593 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023594 /*
23595 * No binding for the IDC was found: create a new one and
23596 * copy all node-tables.
23597 */
23598 parBind = xmlSchemaIDCNewBinding(bind->definition);
23599 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023600 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023601
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023602 /*
23603 * TODO: Hmm, how to optimize the initial number of
23604 * allocated entries?
23605 */
23606 if (bind->nbNodes != 0) {
23607 /*
23608 * Add all IDC node-table entries.
23609 */
23610 if (! vctxt->psviExposeIDCNodeTables) {
23611 /*
23612 * Just move the entries.
23613 * NOTE: this is quite save here, since
23614 * all the keyref lookups have already been
23615 * performed.
23616 */
23617 parBind->nodeTable = bind->nodeTable;
23618 bind->nodeTable = NULL;
23619 parBind->sizeNodes = bind->sizeNodes;
23620 bind->sizeNodes = 0;
23621 parBind->nbNodes = bind->nbNodes;
23622 bind->nbNodes = 0;
23623 } else {
23624 /*
23625 * Copy the entries.
23626 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023627 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023628 xmlMalloc(bind->nbNodes *
23629 sizeof(xmlSchemaPSVIIDCNodePtr));
23630 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023631 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023632 "allocating an array of IDC node-table "
23633 "items", NULL);
23634 xmlSchemaIDCFreeBinding(parBind);
23635 goto internal_error;
23636 }
23637 parBind->sizeNodes = bind->nbNodes;
23638 parBind->nbNodes = bind->nbNodes;
23639 memcpy(parBind->nodeTable, bind->nodeTable,
23640 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23641 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023642 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023643 if (bind->dupls) {
23644 /*
23645 * Move the duplicates.
23646 */
23647 if (parBind->dupls != NULL)
23648 xmlSchemaItemListFree(parBind->dupls);
23649 parBind->dupls = bind->dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023650 bind->dupls = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023651 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023652 if (*parTable == NULL)
23653 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023654 else {
23655 parBind->next = *parTable;
23656 *parTable = parBind;
Daniel Veillarddee23482008-04-11 12:58:43 +000023657 }
23658 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023659
23660next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023661 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023662 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023663 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023664
23665internal_error:
23666 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023667}
23668
23669/**
23670 * xmlSchemaCheckCVCIDCKeyRef:
23671 * @vctxt: the WXS validation context
23672 * @elemDecl: the element declaration
23673 *
23674 * Check the cvc-idc-keyref constraints.
23675 */
23676static int
23677xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23678{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023679 xmlSchemaIDCMatcherPtr matcher;
23680 xmlSchemaPSVIIDCBindingPtr bind;
Daniel Veillarddee23482008-04-11 12:58:43 +000023681
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023682 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023683 /*
23684 * Find a keyref.
23685 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023686 while (matcher != NULL) {
23687 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23688 matcher->targets &&
23689 matcher->targets->nbItems)
23690 {
23691 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023692 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023693 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023694
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023695 nbFields = matcher->aidc->def->nbFields;
23696
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023697 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023698 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023699 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023700 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023701 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023702 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023703 bind->definition)
23704 break;
23705 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023706 }
23707 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023708 /*
23709 * Search for a matching key-sequences.
23710 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023711 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023712 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023713 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023714 if (bind != NULL) {
23715 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023716 for (j = 0; j < bind->nbNodes; j++) {
23717 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023718 for (k = 0; k < nbFields; k++) {
23719 res = xmlSchemaAreValuesEqual(keys[k]->val,
23720 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023721 if (res == 0)
23722 break;
23723 else if (res == -1) {
23724 return (-1);
23725 }
23726 }
23727 if (res == 1) {
23728 /*
23729 * Match found.
23730 */
23731 break;
23732 }
23733 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023734 if ((res == 0) && hasDupls) {
23735 /*
23736 * Search in duplicates
23737 */
23738 for (j = 0; j < bind->dupls->nbItems; j++) {
23739 keys = ((xmlSchemaPSVIIDCNodePtr)
23740 bind->dupls->items[j])->keys;
23741 for (k = 0; k < nbFields; k++) {
23742 res = xmlSchemaAreValuesEqual(keys[k]->val,
23743 refKeys[k]->val);
23744 if (res == 0)
23745 break;
23746 else if (res == -1) {
23747 return (-1);
23748 }
23749 }
23750 if (res == 1) {
23751 /*
23752 * Match in duplicates found.
23753 */
23754 xmlChar *str = NULL, *strB = NULL;
23755 xmlSchemaKeyrefErr(vctxt,
23756 XML_SCHEMAV_CVC_IDC, refNode,
23757 (xmlSchemaTypePtr) matcher->aidc->def,
23758 "More than one match found for "
23759 "key-sequence %s of keyref '%s'",
23760 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23761 refNode->keys, nbFields),
23762 xmlSchemaGetComponentQName(&strB,
23763 matcher->aidc->def));
23764 FREE_AND_NULL(str);
23765 FREE_AND_NULL(strB);
23766 break;
23767 }
23768 }
23769 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023770 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023771
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023772 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023773 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023774 xmlSchemaKeyrefErr(vctxt,
23775 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023776 (xmlSchemaTypePtr) matcher->aidc->def,
23777 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023778 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023779 refNode->keys, nbFields),
23780 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023781 FREE_AND_NULL(str);
23782 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023783 }
23784 }
23785 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023786 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023787 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023788 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023789 return (0);
23790}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023791
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023792/************************************************************************
23793 * *
23794 * XML Reader validation code *
23795 * *
23796 ************************************************************************/
23797
23798static xmlSchemaAttrInfoPtr
23799xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023800{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023801 xmlSchemaAttrInfoPtr iattr;
23802 /*
23803 * Grow/create list of attribute infos.
23804 */
23805 if (vctxt->attrInfos == NULL) {
23806 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23807 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23808 vctxt->sizeAttrInfos = 1;
23809 if (vctxt->attrInfos == NULL) {
23810 xmlSchemaVErrMemory(vctxt,
23811 "allocating attribute info list", NULL);
23812 return (NULL);
23813 }
23814 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23815 vctxt->sizeAttrInfos++;
23816 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23817 xmlRealloc(vctxt->attrInfos,
23818 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23819 if (vctxt->attrInfos == NULL) {
23820 xmlSchemaVErrMemory(vctxt,
23821 "re-allocating attribute info list", NULL);
23822 return (NULL);
23823 }
23824 } else {
23825 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23826 if (iattr->localName != NULL) {
23827 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23828 "attr info not cleared");
23829 return (NULL);
23830 }
23831 iattr->nodeType = XML_ATTRIBUTE_NODE;
23832 return (iattr);
23833 }
23834 /*
23835 * Create an attribute info.
23836 */
23837 iattr = (xmlSchemaAttrInfoPtr)
23838 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23839 if (iattr == NULL) {
23840 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23841 return (NULL);
23842 }
23843 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23844 iattr->nodeType = XML_ATTRIBUTE_NODE;
23845 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23846
23847 return (iattr);
23848}
23849
23850static int
23851xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23852 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023853 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023854 const xmlChar *localName,
Daniel Veillarddee23482008-04-11 12:58:43 +000023855 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023856 int ownedNames,
23857 xmlChar *value,
23858 int ownedValue)
23859{
23860 xmlSchemaAttrInfoPtr attr;
23861
23862 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23863 if (attr == NULL) {
23864 VERROR_INT("xmlSchemaPushAttribute",
23865 "calling xmlSchemaGetFreshAttrInfo()");
23866 return (-1);
23867 }
23868 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023869 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023870 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23871 attr->localName = localName;
23872 attr->nsName = nsName;
23873 if (ownedNames)
23874 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23875 /*
23876 * Evaluate if it's an XSI attribute.
23877 */
23878 if (nsName != NULL) {
23879 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23880 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023881 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023882 }
23883 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23884 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23885 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23886 }
23887 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23888 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23889 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23890 }
23891 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23892 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23893 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23894 }
23895 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23896 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23897 }
23898 }
23899 attr->value = value;
23900 if (ownedValue)
23901 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23902 if (attr->metaType != 0)
23903 attr->state = XML_SCHEMAS_ATTR_META;
23904 return (0);
23905}
23906
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023907/**
23908 * xmlSchemaClearElemInfo:
23909 * @vctxt: the WXS validation context
23910 * @ielem: the element information item
23911 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023912static void
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023913xmlSchemaClearElemInfo(xmlSchemaValidCtxtPtr vctxt,
23914 xmlSchemaNodeInfoPtr ielem)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023915{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023916 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023917 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023918 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23919 FREE_AND_NULL(ielem->localName);
23920 FREE_AND_NULL(ielem->nsName);
23921 } else {
23922 ielem->localName = NULL;
23923 ielem->nsName = NULL;
23924 }
23925 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23926 FREE_AND_NULL(ielem->value);
23927 } else {
23928 ielem->value = NULL;
23929 }
23930 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023931 /*
23932 * PSVI TODO: Be careful not to free it when the value is
23933 * exposed via PSVI.
23934 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023935 xmlSchemaFreeValue(ielem->val);
23936 ielem->val = NULL;
23937 }
23938 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023939 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023940 * REVISIT OPTIMIZE TODO: Use a pool of IDC matchers.
23941 * Does it work?
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023942 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023943 xmlSchemaIDCReleaseMatcherList(vctxt, ielem->idcMatchers);
23944#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023945 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023946#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023947 ielem->idcMatchers = NULL;
23948 }
23949 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023950 /*
23951 * OPTIMIZE TODO: Use a pool of IDC tables??.
23952 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023953 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23954 ielem->idcTable = NULL;
23955 }
23956 if (ielem->regexCtxt != NULL) {
23957 xmlRegFreeExecCtxt(ielem->regexCtxt);
23958 ielem->regexCtxt = NULL;
23959 }
23960 if (ielem->nsBindings != NULL) {
23961 xmlFree((xmlChar **)ielem->nsBindings);
23962 ielem->nsBindings = NULL;
23963 ielem->nbNsBindings = 0;
23964 ielem->sizeNsBindings = 0;
23965 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023966}
23967
23968/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023969 * xmlSchemaGetFreshElemInfo:
23970 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023971 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023972 * Creates/reuses and initializes the element info item for
23973 * the currect tree depth.
23974 *
23975 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023976 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023977static xmlSchemaNodeInfoPtr
23978xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023979{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023980 xmlSchemaNodeInfoPtr info = NULL;
23981
23982 if (vctxt->depth > vctxt->sizeElemInfos) {
23983 VERROR_INT("xmlSchemaGetFreshElemInfo",
23984 "inconsistent depth encountered");
23985 return (NULL);
23986 }
23987 if (vctxt->elemInfos == NULL) {
23988 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23989 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23990 if (vctxt->elemInfos == NULL) {
23991 xmlSchemaVErrMemory(vctxt,
23992 "allocating the element info array", NULL);
23993 return (NULL);
23994 }
23995 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23996 vctxt->sizeElemInfos = 10;
23997 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23998 int i = vctxt->sizeElemInfos;
23999
24000 vctxt->sizeElemInfos *= 2;
24001 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
24002 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
24003 sizeof(xmlSchemaNodeInfoPtr));
24004 if (vctxt->elemInfos == NULL) {
24005 xmlSchemaVErrMemory(vctxt,
24006 "re-allocating the element info array", NULL);
24007 return (NULL);
24008 }
24009 /*
24010 * We need the new memory to be NULLed.
24011 * TODO: Use memset instead?
24012 */
24013 for (; i < vctxt->sizeElemInfos; i++)
24014 vctxt->elemInfos[i] = NULL;
24015 } else
24016 info = vctxt->elemInfos[vctxt->depth];
24017
24018 if (info == NULL) {
24019 info = (xmlSchemaNodeInfoPtr)
24020 xmlMalloc(sizeof(xmlSchemaNodeInfo));
24021 if (info == NULL) {
24022 xmlSchemaVErrMemory(vctxt,
24023 "allocating an element info", NULL);
24024 return (NULL);
24025 }
24026 vctxt->elemInfos[vctxt->depth] = info;
24027 } else {
24028 if (info->localName != NULL) {
24029 VERROR_INT("xmlSchemaGetFreshElemInfo",
24030 "elem info has not been cleared");
24031 return (NULL);
24032 }
24033 }
24034 memset(info, 0, sizeof(xmlSchemaNodeInfo));
24035 info->nodeType = XML_ELEMENT_NODE;
24036 info->depth = vctxt->depth;
24037
24038 return (info);
24039}
24040
24041#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
24042#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
24043#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
24044
24045static int
24046xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
24047 xmlNodePtr node,
24048 xmlSchemaTypePtr type,
24049 xmlSchemaValType valType,
24050 const xmlChar * value,
24051 xmlSchemaValPtr val,
24052 unsigned long length,
24053 int fireErrors)
24054{
24055 int ret, error = 0;
24056
24057 xmlSchemaTypePtr tmpType;
24058 xmlSchemaFacetLinkPtr facetLink;
24059 xmlSchemaFacetPtr facet;
24060 unsigned long len = 0;
24061 xmlSchemaWhitespaceValueType ws;
24062
24063 /*
24064 * In Libxml2, derived built-in types have currently no explicit facets.
24065 */
24066 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000024067 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024068
24069 /*
24070 * NOTE: Do not jump away, if the facetSet of the given type is
24071 * empty: until now, "pattern" and "enumeration" facets of the
24072 * *base types* need to be checked as well.
24073 */
24074 if (type->facetSet == NULL)
24075 goto pattern_and_enum;
24076
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024077 if (! WXS_IS_ATOMIC(type)) {
24078 if (WXS_IS_LIST(type))
24079 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024080 else
24081 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024082 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024083 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024084 * Whitespace handling is only of importance for string-based
24085 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024086 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024087 tmpType = xmlSchemaGetPrimitiveType(type);
24088 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024089 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024090 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
24091 } else
24092 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
24093 /*
24094 * If the value was not computed (for string or
24095 * anySimpleType based types), then use the provided
24096 * type.
24097 */
24098 if (val == NULL)
24099 valType = valType;
24100 else
24101 valType = xmlSchemaGetValType(val);
Daniel Veillarddee23482008-04-11 12:58:43 +000024102
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024103 ret = 0;
24104 for (facetLink = type->facetSet; facetLink != NULL;
24105 facetLink = facetLink->next) {
24106 /*
24107 * Skip the pattern "whiteSpace": it is used to
24108 * format the character content beforehand.
24109 */
24110 switch (facetLink->facet->type) {
24111 case XML_SCHEMA_FACET_WHITESPACE:
24112 case XML_SCHEMA_FACET_PATTERN:
24113 case XML_SCHEMA_FACET_ENUMERATION:
24114 continue;
24115 case XML_SCHEMA_FACET_LENGTH:
24116 case XML_SCHEMA_FACET_MINLENGTH:
24117 case XML_SCHEMA_FACET_MAXLENGTH:
24118 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
24119 valType, value, val, &len, ws);
24120 break;
24121 default:
24122 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
24123 valType, value, val, ws);
24124 break;
24125 }
24126 if (ret < 0) {
24127 AERROR_INT("xmlSchemaValidateFacets",
24128 "validating against a atomic type facet");
24129 return (-1);
24130 } else if (ret > 0) {
24131 if (fireErrors)
24132 xmlSchemaFacetErr(actxt, ret, node,
24133 value, len, type, facetLink->facet, NULL, NULL, NULL);
24134 else
24135 return (ret);
24136 if (error == 0)
24137 error = ret;
24138 }
24139 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024140 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024141
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024142WXS_IS_LIST:
24143 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024144 goto pattern_and_enum;
24145 /*
24146 * "length", "minLength" and "maxLength" of list types.
24147 */
24148 ret = 0;
24149 for (facetLink = type->facetSet; facetLink != NULL;
24150 facetLink = facetLink->next) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024151
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024152 switch (facetLink->facet->type) {
24153 case XML_SCHEMA_FACET_LENGTH:
24154 case XML_SCHEMA_FACET_MINLENGTH:
Daniel Veillarddee23482008-04-11 12:58:43 +000024155 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024156 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
24157 value, length, NULL);
24158 break;
24159 default:
24160 continue;
24161 }
24162 if (ret < 0) {
24163 AERROR_INT("xmlSchemaValidateFacets",
24164 "validating against a list type facet");
24165 return (-1);
24166 } else if (ret > 0) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024167 if (fireErrors)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024168 xmlSchemaFacetErr(actxt, ret, node,
24169 value, length, type, facetLink->facet, NULL, NULL, NULL);
24170 else
24171 return (ret);
24172 if (error == 0)
24173 error = ret;
24174 }
24175 ret = 0;
24176 }
24177
24178pattern_and_enum:
24179 if (error >= 0) {
24180 int found = 0;
24181 /*
24182 * Process enumerations. Facet values are in the value space
24183 * of the defining type's base type. This seems to be a bug in the
24184 * XML Schema 1.0 spec. Use the whitespace type of the base type.
24185 * Only the first set of enumerations in the ancestor-or-self axis
24186 * is used for validation.
24187 */
24188 ret = 0;
24189 tmpType = type;
24190 do {
24191 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
24192 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
24193 continue;
24194 found = 1;
24195 ret = xmlSchemaAreValuesEqual(facet->val, val);
24196 if (ret == 1)
24197 break;
24198 else if (ret < 0) {
24199 AERROR_INT("xmlSchemaValidateFacets",
24200 "validating against an enumeration facet");
24201 return (-1);
24202 }
24203 }
24204 if (ret != 0)
24205 break;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +000024206 /*
24207 * Break on the first set of enumerations. Any additional
24208 * enumerations which might be existent on the ancestors
24209 * of the current type are restricted by this set; thus
24210 * *must* *not* be taken into account.
24211 */
24212 if (found)
24213 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024214 tmpType = tmpType->baseType;
24215 } while ((tmpType != NULL) &&
24216 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24217 if (found && (ret == 0)) {
24218 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
24219 if (fireErrors) {
24220 xmlSchemaFacetErr(actxt, ret, node,
24221 value, 0, type, NULL, NULL, NULL, NULL);
24222 } else
24223 return (ret);
24224 if (error == 0)
24225 error = ret;
24226 }
24227 }
24228
24229 if (error >= 0) {
24230 int found;
24231 /*
24232 * Process patters. Pattern facets are ORed at type level
24233 * and ANDed if derived. Walk the base type axis.
24234 */
24235 tmpType = type;
24236 facet = NULL;
24237 do {
24238 found = 0;
24239 for (facetLink = tmpType->facetSet; facetLink != NULL;
24240 facetLink = facetLink->next) {
24241 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
24242 continue;
24243 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000024244 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024245 * NOTE that for patterns, @value needs to be the
24246 * normalized vaule.
24247 */
24248 ret = xmlRegexpExec(facetLink->facet->regexp, value);
24249 if (ret == 1)
24250 break;
24251 else if (ret < 0) {
24252 AERROR_INT("xmlSchemaValidateFacets",
24253 "validating against a pattern facet");
24254 return (-1);
24255 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000024256 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024257 * Save the last non-validating facet.
24258 */
24259 facet = facetLink->facet;
24260 }
24261 }
24262 if (found && (ret != 1)) {
24263 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
24264 if (fireErrors) {
24265 xmlSchemaFacetErr(actxt, ret, node,
24266 value, 0, type, facet, NULL, NULL, NULL);
24267 } else
24268 return (ret);
24269 if (error == 0)
24270 error = ret;
24271 break;
24272 }
24273 tmpType = tmpType->baseType;
24274 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24275 }
24276
24277 return (error);
24278}
Daniel Veillarddee23482008-04-11 12:58:43 +000024279
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024280static xmlChar *
24281xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
24282 const xmlChar *value)
24283{
Daniel Veillarddee23482008-04-11 12:58:43 +000024284 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024285 case XML_SCHEMA_WHITESPACE_COLLAPSE:
24286 return (xmlSchemaCollapseString(value));
24287 case XML_SCHEMA_WHITESPACE_REPLACE:
24288 return (xmlSchemaWhiteSpaceReplace(value));
24289 default:
24290 return (NULL);
24291 }
24292}
24293
24294static int
24295xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
24296 const xmlChar *value,
24297 xmlSchemaValPtr *val,
24298 int valNeeded)
24299{
24300 int ret;
24301 const xmlChar *nsName;
24302 xmlChar *local, *prefix = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000024303
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024304 ret = xmlValidateQName(value, 1);
24305 if (ret != 0) {
24306 if (ret == -1) {
24307 VERROR_INT("xmlSchemaValidateQName",
24308 "calling xmlValidateQName()");
24309 return (-1);
24310 }
24311 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
24312 }
24313 /*
24314 * NOTE: xmlSplitQName2 will always return a duplicated
24315 * strings.
24316 */
24317 local = xmlSplitQName2(value, &prefix);
24318 if (local == NULL)
24319 local = xmlStrdup(value);
24320 /*
24321 * OPTIMIZE TODO: Use flags for:
24322 * - is there any namespace binding?
24323 * - is there a default namespace?
24324 */
24325 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
Daniel Veillarddee23482008-04-11 12:58:43 +000024326
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024327 if (prefix != NULL) {
24328 xmlFree(prefix);
24329 /*
24330 * A namespace must be found if the prefix is
24331 * NOT NULL.
24332 */
24333 if (nsName == NULL) {
24334 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024335 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024336 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024337 "The QName value '%s' has no "
24338 "corresponding namespace declaration in "
24339 "scope", value, NULL);
24340 if (local != NULL)
24341 xmlFree(local);
24342 return (ret);
24343 }
24344 }
24345 if (valNeeded && val) {
24346 if (nsName != NULL)
24347 *val = xmlSchemaNewQNameValue(
24348 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24349 else
24350 *val = xmlSchemaNewQNameValue(NULL,
24351 BAD_CAST local);
24352 } else
24353 xmlFree(local);
24354 return (0);
24355}
24356
24357/*
24358* cvc-simple-type
24359*/
24360static int
24361xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24362 xmlNodePtr node,
24363 xmlSchemaTypePtr type,
24364 const xmlChar *value,
24365 xmlSchemaValPtr *retVal,
24366 int fireErrors,
24367 int normalize,
24368 int isNormalized)
24369{
24370 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24371 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024372 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024373 xmlChar *normValue = NULL;
24374
24375#define NORMALIZE(atype) \
24376 if ((! isNormalized) && \
24377 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24378 normValue = xmlSchemaNormalizeValue(atype, value); \
24379 if (normValue != NULL) \
24380 value = normValue; \
24381 isNormalized = 1; \
24382 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024383
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024384 if ((retVal != NULL) && (*retVal != NULL)) {
24385 xmlSchemaFreeValue(*retVal);
24386 *retVal = NULL;
24387 }
24388 /*
24389 * 3.14.4 Simple Type Definition Validation Rules
24390 * Validation Rule: String Valid
24391 */
24392 /*
24393 * 1 It is schema-valid with respect to that definition as defined
24394 * by Datatype Valid in [XML Schemas: Datatypes].
24395 */
24396 /*
24397 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
Rob Richardsc6947bb2008-06-29 15:04:41 +000024398 * the empty set, as defined in Type Derivation OK (Simple) (�3.14.6), then
24399 * the string must be a �declared entity name�.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024400 */
24401 /*
24402 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
Rob Richardsc6947bb2008-06-29 15:04:41 +000024403 * given the empty set, as defined in Type Derivation OK (Simple) (�3.14.6),
24404 * then every whitespace-delimited substring of the string must be a �declared
24405 * entity name�.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024406 */
24407 /*
24408 * 2.3 otherwise no further condition applies.
24409 */
24410 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24411 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024412 if (value == NULL)
24413 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024414 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024415 xmlSchemaTypePtr biType; /* The built-in type. */
24416 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024417 * SPEC (1.2.1) "if {variety} is �atomic� then the string must �match�
24418 * a literal in the �lexical space� of {base type definition}"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024419 */
24420 /*
24421 * Whitespace-normalize.
24422 */
24423 NORMALIZE(type);
24424 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24425 /*
24426 * Get the built-in type.
24427 */
24428 biType = type->baseType;
24429 while ((biType != NULL) &&
24430 (biType->type != XML_SCHEMA_TYPE_BASIC))
24431 biType = biType->baseType;
24432
24433 if (biType == NULL) {
24434 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24435 "could not get the built-in type");
24436 goto internal_error;
24437 }
24438 } else
24439 biType = type;
24440 /*
24441 * NOTATIONs need to be processed here, since they need
24442 * to lookup in the hashtable of NOTATION declarations of the schema.
24443 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024444 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24445 switch (biType->builtInType) {
24446 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024447 ret = xmlSchemaValidateNotation(
24448 (xmlSchemaValidCtxtPtr) actxt,
24449 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24450 NULL, value, &val, valNeeded);
24451 break;
24452 case XML_SCHEMAS_QNAME:
24453 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24454 value, &val, valNeeded);
24455 break;
24456 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024457 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024458 if (valNeeded)
24459 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24460 value, &val, NULL);
24461 else
24462 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24463 value, NULL, NULL);
24464 break;
24465 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024466 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24467 switch (biType->builtInType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024468 case XML_SCHEMAS_NOTATION:
24469 ret = xmlSchemaValidateNotation(NULL,
24470 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24471 value, &val, valNeeded);
24472 break;
24473 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024474 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024475 if (valNeeded)
24476 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24477 value, &val, node);
24478 else
24479 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24480 value, NULL, node);
24481 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024482 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024483 } else {
24484 /*
24485 * Validation via a public API is not implemented yet.
24486 */
24487 TODO
24488 goto internal_error;
24489 }
24490 if (ret != 0) {
24491 if (ret < 0) {
24492 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24493 "validating against a built-in type");
24494 goto internal_error;
24495 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024496 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024497 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24498 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024499 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024500 }
24501 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24502 /*
24503 * Check facets.
24504 */
24505 ret = xmlSchemaValidateFacets(actxt, node, type,
24506 (xmlSchemaValType) biType->builtInType, value, val,
24507 0, fireErrors);
24508 if (ret != 0) {
24509 if (ret < 0) {
24510 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24511 "validating facets of atomic simple type");
24512 goto internal_error;
24513 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024514 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024515 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24516 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024517 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024518 }
24519 }
24520 if (fireErrors && (ret > 0))
24521 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024522 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024523
24524 xmlSchemaTypePtr itemType;
24525 const xmlChar *cur, *end;
24526 xmlChar *tmpValue = NULL;
24527 unsigned long len = 0;
24528 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
Rob Richardsc6947bb2008-06-29 15:04:41 +000024529 /* 1.2.2 if {variety} is �list� then the string must be a sequence
24530 * of white space separated tokens, each of which �match�es a literal
24531 * in the �lexical space� of {item type definition}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024532 */
24533 /*
24534 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24535 * the list type has an enum or pattern facet.
24536 */
24537 NORMALIZE(type);
24538 /*
24539 * VAL TODO: Optimize validation of empty values.
24540 * VAL TODO: We do not have computed values for lists.
24541 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024542 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024543 cur = value;
24544 do {
24545 while (IS_BLANK_CH(*cur))
24546 cur++;
24547 end = cur;
24548 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24549 end++;
24550 if (end == cur)
24551 break;
24552 tmpValue = xmlStrndup(cur, end - cur);
24553 len++;
24554
24555 if (valNeeded)
24556 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24557 tmpValue, &curVal, fireErrors, 0, 1);
24558 else
24559 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24560 tmpValue, NULL, fireErrors, 0, 1);
24561 FREE_AND_NULL(tmpValue);
24562 if (curVal != NULL) {
24563 /*
24564 * Add to list of computed values.
24565 */
24566 if (val == NULL)
24567 val = curVal;
24568 else
24569 xmlSchemaValueAppend(prevVal, curVal);
24570 prevVal = curVal;
24571 curVal = NULL;
24572 }
24573 if (ret != 0) {
24574 if (ret < 0) {
24575 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24576 "validating an item of list simple type");
24577 goto internal_error;
24578 }
24579 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24580 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024582 cur = end;
24583 } while (*cur != 0);
24584 FREE_AND_NULL(tmpValue);
24585 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24586 /*
24587 * Apply facets (pattern, enumeration).
24588 */
24589 ret = xmlSchemaValidateFacets(actxt, node, type,
24590 XML_SCHEMAS_UNKNOWN, value, val,
24591 len, fireErrors);
24592 if (ret != 0) {
24593 if (ret < 0) {
24594 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24595 "validating facets of list simple type");
24596 goto internal_error;
24597 }
24598 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24599 }
24600 }
24601 if (fireErrors && (ret > 0)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024602 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024603 * Report the normalized value.
24604 */
24605 normalize = 1;
24606 NORMALIZE(type);
24607 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24608 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024609 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024610 xmlSchemaTypeLinkPtr memberLink;
24611 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024612 * TODO: For all datatypes �derived� by �union� whiteSpace does
24613 * not apply directly; however, the normalization behavior of �union�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024614 * types is controlled by the value of whiteSpace on that one of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000024615 * �memberTypes� against which the �union� is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024616 *
24617 * This means that the value is normalized by the first validating
24618 * member type, then the facets of the union type are applied. This
24619 * needs changing of the value!
24620 */
24621
24622 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024623 * 1.2.3 if {variety} is �union� then the string must �match� a
24624 * literal in the �lexical space� of at least one member of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024625 * {member type definitions}
24626 */
24627 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24628 if (memberLink == NULL) {
24629 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24630 "union simple type has no member types");
24631 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000024632 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024633 /*
24634 * Always normalize union type values, since we currently
24635 * cannot store the whitespace information with the value
24636 * itself; otherwise a later value-comparison would be
24637 * not possible.
24638 */
24639 while (memberLink != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024640 if (valNeeded)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024641 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24642 memberLink->type, value, &val, 0, 1, 0);
24643 else
24644 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24645 memberLink->type, value, NULL, 0, 1, 0);
24646 if (ret <= 0)
24647 break;
24648 memberLink = memberLink->next;
24649 }
24650 if (ret != 0) {
24651 if (ret < 0) {
24652 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24653 "validating members of union simple type");
24654 goto internal_error;
24655 }
24656 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24657 }
24658 /*
24659 * Apply facets (pattern, enumeration).
24660 */
24661 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24662 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024663 * The normalization behavior of �union� types is controlled by
24664 * the value of whiteSpace on that one of the �memberTypes�
24665 * against which the �union� is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024666 */
24667 NORMALIZE(memberLink->type);
24668 ret = xmlSchemaValidateFacets(actxt, node, type,
24669 XML_SCHEMAS_UNKNOWN, value, val,
24670 0, fireErrors);
24671 if (ret != 0) {
24672 if (ret < 0) {
24673 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24674 "validating facets of union simple type");
24675 goto internal_error;
24676 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024677 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024678 }
24679 }
24680 if (fireErrors && (ret > 0))
24681 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24682 }
24683
24684 if (normValue != NULL)
24685 xmlFree(normValue);
24686 if (ret == 0) {
24687 if (retVal != NULL)
24688 *retVal = val;
24689 else if (val != NULL)
24690 xmlSchemaFreeValue(val);
24691 } else if (val != NULL)
24692 xmlSchemaFreeValue(val);
24693 return (ret);
24694internal_error:
24695 if (normValue != NULL)
24696 xmlFree(normValue);
24697 if (val != NULL)
24698 xmlSchemaFreeValue(val);
24699 return (-1);
24700}
24701
24702static int
24703xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24704 const xmlChar *value,
24705 const xmlChar **nsName,
24706 const xmlChar **localName)
24707{
24708 int ret = 0;
24709
24710 if ((nsName == NULL) || (localName == NULL))
24711 return (-1);
24712 *nsName = NULL;
24713 *localName = NULL;
24714
24715 ret = xmlValidateQName(value, 1);
24716 if (ret == -1)
24717 return (-1);
24718 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024719 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024720 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24721 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24722 return (1);
24723 }
24724 {
24725 xmlChar *local = NULL;
24726 xmlChar *prefix;
24727
24728 /*
24729 * NOTE: xmlSplitQName2 will return a duplicated
24730 * string.
24731 */
24732 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024733 if (local == NULL)
24734 *localName = xmlDictLookup(vctxt->dict, value, -1);
24735 else {
24736 *localName = xmlDictLookup(vctxt->dict, local, -1);
24737 xmlFree(local);
24738 }
24739
24740 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24741
24742 if (prefix != NULL) {
24743 xmlFree(prefix);
24744 /*
24745 * A namespace must be found if the prefix is NOT NULL.
24746 */
24747 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024748 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024749 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024750 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024751 "The QName value '%s' has no "
24752 "corresponding namespace declaration in scope",
24753 value, NULL);
24754 return (2);
24755 }
24756 }
24757 }
24758 return (0);
24759}
24760
24761static int
24762xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24763 xmlSchemaAttrInfoPtr iattr,
24764 xmlSchemaTypePtr *localType,
24765 xmlSchemaElementPtr elemDecl)
24766{
24767 int ret = 0;
24768 /*
24769 * cvc-elt (3.3.4) : (4)
24770 * AND
24771 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24772 * (1.2.1.2.1) - (1.2.1.2.4)
24773 * Handle 'xsi:type'.
24774 */
24775 if (localType == NULL)
24776 return (-1);
24777 *localType = NULL;
24778 if (iattr == NULL)
24779 return (0);
24780 else {
24781 const xmlChar *nsName = NULL, *local = NULL;
24782 /*
24783 * TODO: We should report a *warning* that the type was overriden
24784 * by the instance.
24785 */
24786 ACTIVATE_ATTRIBUTE(iattr);
24787 /*
24788 * (cvc-elt) (3.3.4) : (4.1)
24789 * (cvc-assess-elt) (1.2.1.2.2)
24790 */
24791 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24792 &nsName, &local);
24793 if (ret != 0) {
24794 if (ret < 0) {
24795 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24796 "calling xmlSchemaQNameExpand() to validate the "
24797 "attribute 'xsi:type'");
24798 goto internal_error;
24799 }
24800 goto exit;
24801 }
24802 /*
24803 * (cvc-elt) (3.3.4) : (4.2)
24804 * (cvc-assess-elt) (1.2.1.2.3)
24805 */
24806 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24807 if (*localType == NULL) {
24808 xmlChar *str = NULL;
24809
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024810 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024811 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024812 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024813 "The QName value '%s' of the xsi:type attribute does not "
24814 "resolve to a type definition",
24815 xmlSchemaFormatQName(&str, nsName, local), NULL);
24816 FREE_AND_NULL(str);
24817 ret = vctxt->err;
24818 goto exit;
24819 }
24820 if (elemDecl != NULL) {
24821 int set = 0;
24822
24823 /*
24824 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
Rob Richardsc6947bb2008-06-29 15:04:41 +000024825 * "The �local type definition� must be validly
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024826 * derived from the {type definition} given the union of
24827 * the {disallowed substitutions} and the {type definition}'s
24828 * {prohibited substitutions}, as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000024829 * Type Derivation OK (Complex) (�3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024830 * (if it is a complex type definition),
24831 * or given {disallowed substitutions} as defined in Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000024832 * Derivation OK (Simple) (�3.14.6) (if it is a simple type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024833 * definition)."
24834 *
24835 * {disallowed substitutions}: the "block" on the element decl.
24836 * {prohibited substitutions}: the "block" on the type def.
24837 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024838 /*
24839 * OPTIMIZE TODO: We could map types already evaluated
24840 * to be validly derived from other types to avoid checking
24841 * this over and over for the same types.
24842 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024843 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24844 (elemDecl->subtypes->flags &
24845 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24846 set |= SUBSET_EXTENSION;
24847
24848 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24849 (elemDecl->subtypes->flags &
24850 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24851 set |= SUBSET_RESTRICTION;
24852
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024853 /*
24854 * REMOVED and CHANGED since this produced a parser context
24855 * which adds to the string dict of the schema. So this would
24856 * change the schema and we don't want this. We don't need
24857 * the parser context anymore.
24858 *
24859 * if ((vctxt->pctxt == NULL) &&
24860 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24861 * return (-1);
24862 */
24863
24864 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024865 elemDecl->subtypes, set) != 0) {
24866 xmlChar *str = NULL;
24867
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024868 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024869 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24870 "The type definition '%s', specified by xsi:type, is "
24871 "blocked or not validly derived from the type definition "
24872 "of the element declaration",
24873 xmlSchemaFormatQName(&str,
24874 (*localType)->targetNamespace,
24875 (*localType)->name),
24876 NULL);
24877 FREE_AND_NULL(str);
24878 ret = vctxt->err;
24879 *localType = NULL;
24880 }
24881 }
24882 }
24883exit:
24884 ACTIVATE_ELEM;
24885 return (ret);
24886internal_error:
24887 ACTIVATE_ELEM;
24888 return (-1);
24889}
24890
24891static int
24892xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24893{
24894 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Daniel Veillard14b56432006-03-09 18:41:40 +000024895 xmlSchemaTypePtr actualType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024896
24897 /*
24898 * cvc-elt (3.3.4) : 1
24899 */
24900 if (elemDecl == NULL) {
24901 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24902 "No matching declaration available");
24903 return (vctxt->err);
24904 }
Daniel Veillard14b56432006-03-09 18:41:40 +000024905 actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024906 /*
24907 * cvc-elt (3.3.4) : 2
24908 */
24909 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24910 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24911 "The element declaration is abstract");
24912 return (vctxt->err);
24913 }
24914 if (actualType == NULL) {
24915 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24916 "The type definition is absent");
24917 return (XML_SCHEMAV_CVC_TYPE_1);
24918 }
24919 if (vctxt->nbAttrInfos != 0) {
24920 int ret;
24921 xmlSchemaAttrInfoPtr iattr;
24922 /*
24923 * cvc-elt (3.3.4) : 3
24924 * Handle 'xsi:nil'.
24925 */
24926 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24927 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24928 if (iattr) {
24929 ACTIVATE_ATTRIBUTE(iattr);
24930 /*
24931 * Validate the value.
24932 */
24933 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024934 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024935 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24936 iattr->value, &(iattr->val), 1, 0, 0);
24937 ACTIVATE_ELEM;
24938 if (ret < 0) {
24939 VERROR_INT("xmlSchemaValidateElemDecl",
24940 "calling xmlSchemaVCheckCVCSimpleType() to "
24941 "validate the attribute 'xsi:nil'");
24942 return (-1);
24943 }
24944 if (ret == 0) {
24945 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24946 /*
24947 * cvc-elt (3.3.4) : 3.1
24948 */
24949 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24950 "The element is not 'nillable'");
24951 /* Does not return an error on purpose. */
24952 } else {
24953 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24954 /*
24955 * cvc-elt (3.3.4) : 3.2.2
24956 */
24957 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24958 (elemDecl->value != NULL)) {
24959 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24960 "The element cannot be 'nilled' because "
24961 "there is a fixed value constraint defined "
24962 "for it");
24963 /* Does not return an error on purpose. */
24964 } else
24965 vctxt->inode->flags |=
24966 XML_SCHEMA_ELEM_INFO_NILLED;
24967 }
24968 }
24969 }
24970 }
24971 /*
24972 * cvc-elt (3.3.4) : 4
24973 * Handle 'xsi:type'.
24974 */
24975 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24976 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24977 if (iattr) {
24978 xmlSchemaTypePtr localType = NULL;
24979
24980 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24981 elemDecl);
24982 if (ret != 0) {
24983 if (ret == -1) {
24984 VERROR_INT("xmlSchemaValidateElemDecl",
24985 "calling xmlSchemaProcessXSIType() to "
24986 "process the attribute 'xsi:type'");
24987 return (-1);
24988 }
24989 /* Does not return an error on purpose. */
24990 }
24991 if (localType != NULL) {
24992 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24993 actualType = localType;
24994 }
24995 }
24996 }
24997 /*
24998 * IDC: Register identity-constraint XPath matchers.
24999 */
25000 if ((elemDecl->idcs != NULL) &&
25001 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
25002 return (-1);
25003 /*
25004 * No actual type definition.
25005 */
25006 if (actualType == NULL) {
25007 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
25008 "The type definition is absent");
25009 return (XML_SCHEMAV_CVC_TYPE_1);
25010 }
25011 /*
25012 * Remember the actual type definition.
25013 */
25014 vctxt->inode->typeDef = actualType;
25015
25016 return (0);
25017}
25018
25019static int
25020xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
25021{
25022 xmlSchemaAttrInfoPtr iattr;
25023 int ret = 0, i;
25024
25025 /*
25026 * SPEC cvc-type (3.1.1)
25027 * "The attributes of must be empty, excepting those whose namespace
25028 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
25029 * whose local name is one of type, nil, schemaLocation or
25030 * noNamespaceSchemaLocation."
25031 */
25032 if (vctxt->nbAttrInfos == 0)
25033 return (0);
25034 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25035 iattr = vctxt->attrInfos[i];
25036 if (! iattr->metaType) {
25037 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025038 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025039 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
25040 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
25041 }
25042 }
25043 ACTIVATE_ELEM
25044 return (ret);
25045}
25046
25047/*
25048* Cleanup currently used attribute infos.
25049*/
25050static void
25051xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
25052{
25053 int i;
25054 xmlSchemaAttrInfoPtr attr;
25055
25056 if (vctxt->nbAttrInfos == 0)
25057 return;
25058 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25059 attr = vctxt->attrInfos[i];
25060 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
25061 if (attr->localName != NULL)
25062 xmlFree((xmlChar *) attr->localName);
25063 if (attr->nsName != NULL)
25064 xmlFree((xmlChar *) attr->nsName);
25065 }
25066 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
25067 if (attr->value != NULL)
25068 xmlFree((xmlChar *) attr->value);
25069 }
25070 if (attr->val != NULL) {
25071 xmlSchemaFreeValue(attr->val);
25072 attr->val = NULL;
25073 }
25074 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
25075 }
25076 vctxt->nbAttrInfos = 0;
25077}
25078
25079/*
25080* 3.4.4 Complex Type Definition Validation Rules
25081* Element Locally Valid (Complex Type) (cvc-complex-type)
25082* 3.2.4 Attribute Declaration Validation Rules
25083* Validation Rule: Attribute Locally Valid (cvc-attribute)
25084* Attribute Locally Valid (Use) (cvc-au)
25085*
25086* Only "assessed" attribute information items will be visible to
25087* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
25088*/
25089static int
25090xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
25091{
Daniel Veillarddee23482008-04-11 12:58:43 +000025092 xmlSchemaTypePtr type = vctxt->inode->typeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025093 xmlSchemaItemListPtr attrUseList;
25094 xmlSchemaAttributeUsePtr attrUse = NULL;
25095 xmlSchemaAttributePtr attrDecl = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000025096 xmlSchemaAttrInfoPtr iattr, tmpiattr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025097 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025098 int xpathRes = 0, res, wildIDs = 0, fixed;
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025099 xmlNodePtr defAttrOwnerElem = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025100
25101 /*
25102 * SPEC (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025103 * (1) "The declaration must not be �absent� (see Missing
25104 * Sub-components (�5.3) for how this can fail to be
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025105 * the case)."
25106 * (2) "Its {type definition} must not be absent."
25107 *
25108 * NOTE (1) + (2): This is not handled here, since we currently do not
25109 * allow validation against schemas which have missing sub-components.
25110 *
25111 * SPEC (cvc-complex-type)
25112 * (3) "For each attribute information item in the element information
25113 * item's [attributes] excepting those whose [namespace name] is
25114 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
25115 * [local name] is one of type, nil, schemaLocation or
25116 * noNamespaceSchemaLocation, the appropriate case among the following
25117 * must be true:
25118 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025119 */
25120 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
25121 /*
25122 * @nbAttrs is the number of attributes present in the instance.
25123 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025124 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025125 if (attrUseList != NULL)
25126 nbUses = attrUseList->nbItems;
25127 else
25128 nbUses = 0;
25129 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025130 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025131 attrUse = attrUseList->items[i];
25132 attrDecl = WXS_ATTRUSE_DECL(attrUse);
25133 for (j = 0; j < nbAttrs; j++) {
25134 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025135 /*
25136 * SPEC (cvc-complex-type) (3)
25137 * Skip meta attributes.
25138 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025139 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025140 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025141 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025142 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025143 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025144 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025145 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025146 continue;
25147 found = 1;
25148 /*
25149 * SPEC (cvc-complex-type)
25150 * (3.1) "If there is among the {attribute uses} an attribute
25151 * use with an {attribute declaration} whose {name} matches
25152 * the attribute information item's [local name] and whose
25153 * {target namespace} is identical to the attribute information
Rob Richardsc6947bb2008-06-29 15:04:41 +000025154 * item's [namespace name] (where an �absent� {target namespace}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025155 * is taken to be identical to a [namespace name] with no value),
Rob Richardsc6947bb2008-06-29 15:04:41 +000025156 * then the attribute information must be �valid� with respect
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025157 * to that attribute use as per Attribute Locally Valid (Use)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025158 * (�3.5.4). In this case the {attribute declaration} of that
25159 * attribute use is the �context-determined declaration� for the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025160 * attribute information item with respect to Schema-Validity
Rob Richardsc6947bb2008-06-29 15:04:41 +000025161 * Assessment (Attribute) (�3.2.4) and
25162 * Assessment Outcome (Attribute) (�3.2.5).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025163 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025164 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
25165 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025166 /*
25167 * Context-determined declaration.
25168 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025169 iattr->decl = attrDecl;
25170 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025171 break;
25172 }
25173
25174 if (found)
25175 continue;
25176
25177 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
25178 /*
25179 * Handle non-existent, required attributes.
25180 *
25181 * SPEC (cvc-complex-type)
25182 * (4) "The {attribute declaration} of each attribute use in
25183 * the {attribute uses} whose {required} is true matches one
25184 * of the attribute information items in the element information
25185 * item's [attributes] as per clause 3.1 above."
25186 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025187 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25188 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025189 VERROR_INT(
25190 "xmlSchemaVAttributesComplex",
25191 "calling xmlSchemaGetFreshAttrInfo()");
25192 return (-1);
25193 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025194 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
25195 tmpiattr->use = attrUse;
Daniel Veillarddee23482008-04-11 12:58:43 +000025196 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025197 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
25198 ((attrUse->defValue != NULL) ||
25199 (attrDecl->defValue != NULL))) {
25200 /*
25201 * Handle non-existent, optional, default/fixed attributes.
25202 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025203 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25204 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025205 VERROR_INT(
25206 "xmlSchemaVAttributesComplex",
25207 "calling xmlSchemaGetFreshAttrInfo()");
25208 return (-1);
25209 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025210 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
25211 tmpiattr->use = attrUse;
25212 tmpiattr->decl = attrDecl;
25213 tmpiattr->typeDef = attrDecl->subtypes;
25214 tmpiattr->localName = attrDecl->name;
25215 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025216 }
25217 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025218
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025219 if (vctxt->nbAttrInfos == 0)
25220 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025221 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025222 /*
25223 * Validate against the wildcard.
25224 */
25225 if (type->attributeWildcard != NULL) {
25226 /*
25227 * SPEC (cvc-complex-type)
25228 * (3.2.1) "There must be an {attribute wildcard}."
25229 */
25230 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025231 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025232 /*
25233 * SPEC (cvc-complex-type) (3)
25234 * Skip meta attributes.
25235 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025236 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025237 continue;
25238 /*
25239 * SPEC (cvc-complex-type)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025240 * (3.2.2) "The attribute information item must be �valid� with
25241 * respect to it as defined in Item Valid (Wildcard) (�3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025242 *
25243 * SPEC Item Valid (Wildcard) (cvc-wildcard)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025244 * "... its [namespace name] must be �valid� with respect to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025245 * the wildcard constraint, as defined in Wildcard allows
Rob Richardsc6947bb2008-06-29 15:04:41 +000025246 * Namespace Name (�3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025247 */
25248 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025249 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025250 /*
25251 * Handle processContents.
25252 *
25253 * SPEC (cvc-wildcard):
25254 * processContents | context-determined declaration:
25255 * "strict" "mustFind"
25256 * "lax" "none"
25257 * "skip" "skip"
25258 */
25259 if (type->attributeWildcard->processContents ==
25260 XML_SCHEMAS_ANY_SKIP) {
25261 /*
25262 * context-determined declaration = "skip"
25263 *
25264 * SPEC PSVI Assessment Outcome (Attribute)
25265 * [validity] = "notKnown"
25266 * [validation attempted] = "none"
25267 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025268 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025269 continue;
25270 }
25271 /*
25272 * Find an attribute declaration.
25273 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025274 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
25275 iattr->localName, iattr->nsName);
25276 if (iattr->decl != NULL) {
25277 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025278 /*
25279 * SPEC (cvc-complex-type)
25280 * (5) "Let [Definition:] the wild IDs be the set of
25281 * all attribute information item to which clause 3.2
Rob Richardsc6947bb2008-06-29 15:04:41 +000025282 * applied and whose �validation� resulted in a
25283 * �context-determined declaration� of mustFind or no
25284 * �context-determined declaration� at all, and whose
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025285 * [local name] and [namespace name] resolve (as
Rob Richardsc6947bb2008-06-29 15:04:41 +000025286 * defined by QName resolution (Instance) (�3.15.4)) to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025287 * an attribute declaration whose {type definition} is
25288 * or is derived from ID. Then all of the following
25289 * must be true:"
25290 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025291 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025292 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025293 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025294 /*
25295 * SPEC (5.1) "There must be no more than one
Rob Richardsc6947bb2008-06-29 15:04:41 +000025296 * item in �wild IDs�."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025297 */
25298 if (wildIDs != 0) {
25299 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025300 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025301 TODO
25302 continue;
25303 }
25304 wildIDs++;
25305 /*
25306 * SPEC (cvc-complex-type)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025307 * (5.2) "If �wild IDs� is non-empty, there must not
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 * be any attribute uses among the {attribute uses}
25309 * whose {attribute declaration}'s {type definition}
25310 * is or is derived from ID."
25311 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025312 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025313 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025314 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025315 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025316 /* URGENT VAL TODO: implement */
25317 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025318 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025319 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025320 }
25321 }
25322 }
25323 } else if (type->attributeWildcard->processContents ==
25324 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025325 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025326 /*
25327 * SPEC PSVI Assessment Outcome (Attribute)
25328 * [validity] = "notKnown"
25329 * [validation attempted] = "none"
25330 */
25331 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025332 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025333 }
25334 }
25335 }
25336 }
25337
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025338 if (vctxt->nbAttrInfos == 0)
25339 return (0);
25340
25341 /*
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025342 * Get the owner element; needed for creation of default attributes.
25343 * This fixes bug #341337, reported by David Grohmann.
25344 */
25345 if (vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) {
25346 xmlSchemaNodeInfoPtr ielem = vctxt->elemInfos[vctxt->depth];
25347 if (ielem && ielem->node && ielem->node->doc)
25348 defAttrOwnerElem = ielem->node;
25349 }
25350 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025351 * Validate values, create default attributes, evaluate IDCs.
25352 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025353 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025354 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025355 /*
25356 * VAL TODO: Note that we won't try to resolve IDCs to
25357 * "lax" and "skip" validated attributes. Check what to
25358 * do in this case.
25359 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025360 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25361 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025362 continue;
25363 /*
25364 * VAL TODO: What to do if the type definition is missing?
25365 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025366 if (iattr->typeDef == NULL) {
25367 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025368 continue;
25369 }
25370
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025371 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025372 fixed = 0;
25373 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025374
25375 if (vctxt->xpathStates != NULL) {
25376 /*
25377 * Evaluate IDCs.
25378 */
25379 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25380 XML_ATTRIBUTE_NODE);
25381 if (xpathRes == -1) {
25382 VERROR_INT("xmlSchemaVAttributesComplex",
25383 "calling xmlSchemaXPathEvaluate()");
25384 goto internal_error;
25385 }
25386 }
25387
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025388 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025389 /*
25390 * Default/fixed attributes.
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025391 * We need the value only if we need to resolve IDCs or
25392 * will create default attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025393 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025394 if ((xpathRes) || (defAttrOwnerElem)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025395 if (iattr->use->defValue != NULL) {
25396 iattr->value = (xmlChar *) iattr->use->defValue;
25397 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025398 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025399 iattr->value = (xmlChar *) iattr->decl->defValue;
25400 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025401 }
25402 /*
25403 * IDCs will consume the precomputed default value,
25404 * so we need to clone it.
25405 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025406 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025407 VERROR_INT("xmlSchemaVAttributesComplex",
25408 "default/fixed value on an attribute use was "
25409 "not precomputed");
25410 goto internal_error;
25411 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025412 iattr->val = xmlSchemaCopyValue(iattr->val);
25413 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025414 VERROR_INT("xmlSchemaVAttributesComplex",
25415 "calling xmlSchemaCopyValue()");
25416 goto internal_error;
25417 }
25418 }
25419 /*
25420 * PSVI: Add the default attribute to the current element.
25421 * VAL TODO: Should we use the *normalized* value? This currently
25422 * uses the *initial* value.
25423 */
Daniel Veillarddee23482008-04-11 12:58:43 +000025424
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025425 if (defAttrOwnerElem) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025426 xmlChar *normValue;
25427 const xmlChar *value;
25428
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025429 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025430 /*
25431 * Normalize the value.
25432 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025433 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25434 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025435 if (normValue != NULL)
25436 value = BAD_CAST normValue;
25437
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025438 if (iattr->nsName == NULL) {
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025439 if (xmlNewProp(defAttrOwnerElem,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025440 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025441 VERROR_INT("xmlSchemaVAttributesComplex",
25442 "callling xmlNewProp()");
25443 if (normValue != NULL)
25444 xmlFree(normValue);
25445 goto internal_error;
25446 }
25447 } else {
25448 xmlNsPtr ns;
25449
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025450 ns = xmlSearchNsByHref(defAttrOwnerElem->doc,
25451 defAttrOwnerElem, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025452 if (ns == NULL) {
25453 xmlChar prefix[12];
25454 int counter = 0;
25455
25456 /*
25457 * Create a namespace declaration on the validation
25458 * root node if no namespace declaration is in scope.
25459 */
25460 do {
25461 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025462 ns = xmlSearchNs(defAttrOwnerElem->doc,
25463 defAttrOwnerElem, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025464 if (counter > 1000) {
25465 VERROR_INT(
25466 "xmlSchemaVAttributesComplex",
25467 "could not compute a ns prefix for a "
25468 "default/fixed attribute");
25469 if (normValue != NULL)
25470 xmlFree(normValue);
25471 goto internal_error;
25472 }
25473 } while (ns != NULL);
25474 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025475 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025476 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025477 /*
25478 * TODO:
25479 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25480 * If we have QNames: do we need to ensure there's a
25481 * prefix defined for the QName?
25482 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025483 xmlNewNsProp(defAttrOwnerElem, ns, iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025484 }
25485 if (normValue != NULL)
25486 xmlFree(normValue);
25487 }
25488 /*
25489 * Go directly to IDC evaluation.
25490 */
25491 goto eval_idcs;
25492 }
25493 /*
25494 * Validate the value.
25495 */
25496 if (vctxt->value != NULL) {
25497 /*
25498 * Free last computed value; just for safety reasons.
25499 */
25500 xmlSchemaFreeValue(vctxt->value);
25501 vctxt->value = NULL;
25502 }
25503 /*
25504 * Note that the attribute *use* can be unavailable, if
25505 * the attribute was a wild attribute.
25506 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025507 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25508 ((iattr->use != NULL) &&
25509 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025510 fixed = 1;
25511 else
25512 fixed = 0;
25513 /*
25514 * SPEC (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025515 * (3) "The item's �normalized value� must be locally �valid�
Daniel Veillarddee23482008-04-11 12:58:43 +000025516 * with respect to that {type definition} as per
Rob Richardsc6947bb2008-06-29 15:04:41 +000025517 * String Valid (�3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025518 *
25519 * VAL TODO: Do we already have the
25520 * "normalized attribute value" here?
25521 */
25522 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025523 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025524 /*
25525 * Request a computed value.
25526 */
25527 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025528 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025529 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025530 1, 1, 0);
25531 } else {
25532 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025533 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025534 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025535 1, 0, 0);
25536 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025537
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025538 if (res != 0) {
25539 if (res == -1) {
25540 VERROR_INT("xmlSchemaVAttributesComplex",
25541 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25542 goto internal_error;
25543 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025544 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025545 /*
25546 * SPEC PSVI Assessment Outcome (Attribute)
25547 * [validity] = "invalid"
25548 */
25549 goto eval_idcs;
25550 }
25551
Daniel Veillarddee23482008-04-11 12:58:43 +000025552 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025553 /*
25554 * SPEC Attribute Locally Valid (Use) (cvc-au)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025555 * "For an attribute information item to be�valid�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025556 * with respect to an attribute use its *normalized*
Rob Richardsc6947bb2008-06-29 15:04:41 +000025557 * value� must match the *canonical* lexical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025558 * representation of the attribute use's {value
25559 * constraint}value, if it is present and fixed."
25560 *
25561 * VAL TODO: The requirement for the *canonical* value
25562 * will be removed in XML Schema 1.1.
25563 */
25564 /*
25565 * SPEC Attribute Locally Valid (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025566 * (4) "The item's *actual* value� must match the *value* of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025567 * the {value constraint}, if it is present and fixed."
25568 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025569 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025570 /* VAL TODO: A value was not precomputed. */
25571 TODO
25572 goto eval_idcs;
25573 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025574 if ((iattr->use != NULL) &&
25575 (iattr->use->defValue != NULL)) {
25576 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025577 /* VAL TODO: A default value was not precomputed. */
25578 TODO
25579 goto eval_idcs;
25580 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025581 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025582 /*
25583 if (xmlSchemaCompareValuesWhtsp(attr->val,
25584 (xmlSchemaWhitespaceValueType) ws,
25585 attr->use->defVal,
25586 (xmlSchemaWhitespaceValueType) ws) != 0) {
25587 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025588 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25589 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025590 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025591 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025592 /* VAL TODO: A default value was not precomputed. */
25593 TODO
25594 goto eval_idcs;
25595 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025596 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025597 /*
25598 if (xmlSchemaCompareValuesWhtsp(attr->val,
25599 (xmlSchemaWhitespaceValueType) ws,
25600 attrDecl->defVal,
25601 (xmlSchemaWhitespaceValueType) ws) != 0) {
25602 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025603 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25604 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025605 }
25606 /*
25607 * [validity] = "valid"
25608 */
25609 }
25610eval_idcs:
25611 /*
25612 * Evaluate IDCs.
25613 */
25614 if (xpathRes) {
25615 if (xmlSchemaXPathProcessHistory(vctxt,
25616 vctxt->depth +1) == -1) {
25617 VERROR_INT("xmlSchemaVAttributesComplex",
25618 "calling xmlSchemaXPathEvaluate()");
25619 goto internal_error;
25620 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025621 } else if (vctxt->xpathStates != NULL)
25622 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025623 }
25624
25625 /*
25626 * Report errors.
25627 */
25628 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025629 iattr = vctxt->attrInfos[i];
25630 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25631 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25632 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25633 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025634 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025635 ACTIVATE_ATTRIBUTE(iattr);
25636 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025637 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25638 xmlChar *str = NULL;
25639 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025640 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025641 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25642 "The attribute '%s' is required but missing",
25643 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025644 iattr->decl->targetNamespace,
25645 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025646 NULL);
25647 FREE_AND_NULL(str)
25648 break;
25649 }
25650 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25651 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25652 "The type definition is absent");
25653 break;
25654 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025655 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025656 XML_SCHEMAV_CVC_AU, NULL, NULL,
25657 "The value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000025658 "value constraint '%s'",
25659 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025660 break;
25661 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25662 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25663 "No matching global attribute declaration available, but "
25664 "demanded by the strict wildcard");
25665 break;
25666 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025667 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025668 break;
25669 /*
25670 * MAYBE VAL TODO: One might report different error messages
25671 * for the following errors.
25672 */
25673 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025674 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025675 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025676 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025677 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025678 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025679 }
25680 break;
25681 default:
25682 break;
25683 }
25684 }
25685
25686 ACTIVATE_ELEM;
25687 return (0);
25688internal_error:
25689 ACTIVATE_ELEM;
25690 return (-1);
25691}
25692
25693static int
25694xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25695 int *skip)
25696{
25697 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25698 /*
25699 * The namespace of the element was already identified to be
25700 * matching the wildcard.
25701 */
25702 if ((skip == NULL) || (wild == NULL) ||
25703 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25704 VERROR_INT("xmlSchemaValidateElemWildcard",
25705 "bad arguments");
25706 return (-1);
25707 }
25708 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025709 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25710 /*
25711 * URGENT VAL TODO: Either we need to position the stream to the
25712 * next sibling, or walk the whole subtree.
25713 */
25714 *skip = 1;
25715 return (0);
25716 }
25717 {
25718 xmlSchemaElementPtr decl = NULL;
25719
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025720 decl = xmlSchemaGetElem(vctxt->schema,
Daniel Veillarddee23482008-04-11 12:58:43 +000025721 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025722 if (decl != NULL) {
25723 vctxt->inode->decl = decl;
25724 return (0);
25725 }
25726 }
25727 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25728 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025729 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025730 "No matching global element declaration available, but "
25731 "demanded by the strict wildcard");
25732 return (vctxt->err);
25733 }
25734 if (vctxt->nbAttrInfos != 0) {
25735 xmlSchemaAttrInfoPtr iattr;
25736 /*
25737 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25738 * (1.2.1.2.1) - (1.2.1.2.3 )
25739 *
25740 * Use the xsi:type attribute for the type definition.
25741 */
25742 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25743 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25744 if (iattr != NULL) {
25745 if (xmlSchemaProcessXSIType(vctxt, iattr,
25746 &(vctxt->inode->typeDef), NULL) == -1) {
25747 VERROR_INT("xmlSchemaValidateElemWildcard",
25748 "calling xmlSchemaProcessXSIType() to "
25749 "process the attribute 'xsi:nil'");
25750 return (-1);
25751 }
25752 /*
25753 * Don't return an error on purpose.
25754 */
25755 return (0);
25756 }
25757 }
25758 /*
25759 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25760 *
25761 * Fallback to "anyType".
25762 */
25763 vctxt->inode->typeDef =
25764 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25765 return (0);
25766}
25767
25768/*
25769* xmlSchemaCheckCOSValidDefault:
25770*
25771* This will be called if: not nilled, no content and a default/fixed
25772* value is provided.
25773*/
25774
25775static int
25776xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25777 const xmlChar *value,
25778 xmlSchemaValPtr *val)
Daniel Veillarddee23482008-04-11 12:58:43 +000025779{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025780 int ret = 0;
25781 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25782
25783 /*
25784 * cos-valid-default:
25785 * Schema Component Constraint: Element Default Valid (Immediate)
Daniel Veillarddee23482008-04-11 12:58:43 +000025786 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025787 * definition the appropriate case among the following must be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000025788 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025789 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025790 /*
25791 * Complex type.
25792 *
25793 * SPEC (2.1) "its {content type} must be a simple type definition
25794 * or mixed."
25795 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Rob Richardsc6947bb2008-06-29 15:04:41 +000025796 * type}'s particle must be �emptiable� as defined by
25797 * Particle Emptiable (�3.9.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025798 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025799 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25800 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25801 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025802 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25803 /* NOTE that this covers (2.2.2) as well. */
25804 VERROR(ret, NULL,
25805 "For a string to be a valid default, the type definition "
25806 "must be a simple type or a complex type with simple content "
25807 "or mixed content and a particle emptiable");
25808 return(ret);
25809 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025810 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025811 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000025812 * 1 If the type definition is a simple type definition, then the string
Rob Richardsc6947bb2008-06-29 15:04:41 +000025813 * must be �valid� with respect to that definition as defined by String
25814 * Valid (�3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025815 *
25816 * AND
25817 *
Daniel Veillarddee23482008-04-11 12:58:43 +000025818 * 2.2.1 If the {content type} is a simple type definition, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000025819 * string must be �valid� with respect to that simple type definition
25820 * as defined by String Valid (�3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000025821 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025822 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025823
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025824 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025825 NULL, inode->typeDef, value, val, 1, 1, 0);
25826
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025827 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025828
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025829 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025830 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25831 }
25832 if (ret < 0) {
25833 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25834 "calling xmlSchemaVCheckCVCSimpleType()");
Daniel Veillarddee23482008-04-11 12:58:43 +000025835 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025836 return (ret);
25837}
25838
25839static void
25840xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25841 const xmlChar * name ATTRIBUTE_UNUSED,
25842 xmlSchemaElementPtr item,
25843 xmlSchemaNodeInfoPtr inode)
25844{
25845 inode->decl = item;
25846#ifdef DEBUG_CONTENT
25847 {
25848 xmlChar *str = NULL;
25849
25850 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25851 xmlGenericError(xmlGenericErrorContext,
25852 "AUTOMATON callback for '%s' [declaration]\n",
25853 xmlSchemaFormatQName(&str,
25854 inode->localName, inode->nsName));
25855 } else {
25856 xmlGenericError(xmlGenericErrorContext,
25857 "AUTOMATON callback for '%s' [wildcard]\n",
25858 xmlSchemaFormatQName(&str,
25859 inode->localName, inode->nsName));
25860
25861 }
25862 FREE_AND_NULL(str)
25863 }
25864#endif
25865}
25866
25867static int
25868xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Daniel Veillarddee23482008-04-11 12:58:43 +000025869{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025870 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25871 if (vctxt->inode == NULL) {
25872 VERROR_INT("xmlSchemaValidatorPushElem",
25873 "calling xmlSchemaGetFreshElemInfo()");
25874 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000025875 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025876 vctxt->nbAttrInfos = 0;
25877 return (0);
25878}
25879
25880static int
25881xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25882 xmlSchemaNodeInfoPtr inode,
25883 xmlSchemaTypePtr type,
25884 const xmlChar *value)
25885{
25886 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25887 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025888 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025889 type, value, &(inode->val), 1, 1, 0));
25890 else
25891 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025892 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025893 type, value, NULL, 1, 0, 0));
25894}
25895
25896
25897
Daniel Veillarddee23482008-04-11 12:58:43 +000025898/*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025899* Process END of element.
25900*/
25901static int
25902xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25903{
25904 int ret = 0;
25905 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25906
25907 if (vctxt->nbAttrInfos != 0)
25908 xmlSchemaClearAttrInfos(vctxt);
25909 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25910 /*
25911 * This element was not expected;
25912 * we will not validate child elements of broken parents.
25913 * Skip validation of all content of the parent.
25914 */
25915 vctxt->skipDepth = vctxt->depth -1;
25916 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000025917 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025918 if ((inode->typeDef == NULL) ||
25919 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25920 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025921 * 1. the type definition might be missing if the element was
25922 * error prone
25923 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025924 */
25925 goto end_elem;
25926 }
25927 /*
25928 * Check the content model.
25929 */
25930 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25931 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25932
25933 /*
25934 * Workaround for "anyType".
25935 */
25936 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
Daniel Veillarddee23482008-04-11 12:58:43 +000025937 goto character_content;
25938
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025939 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25940 xmlChar *values[10];
25941 int terminal, nbval = 10, nbneg;
25942
25943 if (inode->regexCtxt == NULL) {
25944 /*
25945 * Create the regex context.
25946 */
25947 inode->regexCtxt =
25948 xmlRegNewExecCtxt(inode->typeDef->contModel,
25949 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25950 vctxt);
25951 if (inode->regexCtxt == NULL) {
25952 VERROR_INT("xmlSchemaValidatorPopElem",
25953 "failed to create a regex context");
25954 goto internal_error;
25955 }
25956#ifdef DEBUG_AUTOMATA
25957 xmlGenericError(xmlGenericErrorContext,
25958 "AUTOMATON create on '%s'\n", inode->localName);
Daniel Veillarddee23482008-04-11 12:58:43 +000025959#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025960 }
25961 /*
25962 * Get hold of the still expected content, since a further
25963 * call to xmlRegExecPushString() will loose this information.
Daniel Veillarddee23482008-04-11 12:58:43 +000025964 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025965 xmlRegExecNextValues(inode->regexCtxt,
25966 &nbval, &nbneg, &values[0], &terminal);
25967 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
Daniel Veillard65fcf272007-07-11 17:55:30 +000025968 if ((ret<0) || ((ret==0) && (!INODE_NILLED(inode)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025969 /*
25970 * Still missing something.
25971 */
25972 ret = 1;
25973 inode->flags |=
25974 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025975 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025976 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25977 "Missing child element(s)",
25978 nbval, nbneg, values);
25979#ifdef DEBUG_AUTOMATA
25980 xmlGenericError(xmlGenericErrorContext,
25981 "AUTOMATON missing ERROR on '%s'\n",
25982 inode->localName);
25983#endif
25984 } else {
25985 /*
25986 * Content model is satisfied.
25987 */
25988 ret = 0;
25989#ifdef DEBUG_AUTOMATA
25990 xmlGenericError(xmlGenericErrorContext,
25991 "AUTOMATON succeeded on '%s'\n",
25992 inode->localName);
25993#endif
25994 }
25995
25996 }
25997 }
25998 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25999 goto end_elem;
26000
26001character_content:
26002
26003 if (vctxt->value != NULL) {
26004 xmlSchemaFreeValue(vctxt->value);
26005 vctxt->value = NULL;
26006 }
26007 /*
26008 * Check character content.
26009 */
26010 if (inode->decl == NULL) {
26011 /*
26012 * Speedup if no declaration exists.
26013 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026014 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026015 ret = xmlSchemaVCheckINodeDataType(vctxt,
26016 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026017 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026018 ret = xmlSchemaVCheckINodeDataType(vctxt,
26019 inode, inode->typeDef->contentTypeDef,
26020 inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026021 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026022 if (ret < 0) {
26023 VERROR_INT("xmlSchemaValidatorPopElem",
26024 "calling xmlSchemaVCheckCVCSimpleType()");
26025 goto internal_error;
26026 }
26027 goto end_elem;
26028 }
26029 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026030 * cvc-elt (3.3.4) : 5
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026031 * The appropriate case among the following must be true:
26032 */
26033 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026034 * cvc-elt (3.3.4) : 5.1
26035 * If the declaration has a {value constraint},
26036 * the item has neither element nor character [children] and
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026037 * clause 3.2 has not applied, then all of the following must be true:
26038 */
26039 if ((inode->decl->value != NULL) &&
Daniel Veillarddee23482008-04-11 12:58:43 +000026040 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026041 (! INODE_NILLED(inode))) {
26042 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026043 * cvc-elt (3.3.4) : 5.1.1
Rob Richardsc6947bb2008-06-29 15:04:41 +000026044 * If the �actual type definition� is a �local type definition�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026045 * then the canonical lexical representation of the {value constraint}
Rob Richardsc6947bb2008-06-29 15:04:41 +000026046 * value must be a valid default for the �actual type definition� as
26047 * defined in Element Default Valid (Immediate) (�3.3.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026048 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026049 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026050 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026051 * NOTE: Although the *canonical* value is stated, it is not
26052 * relevant if canonical or not. Additionally XML Schema 1.1
26053 * will removed this requirement as well.
26054 */
26055 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
26056
26057 ret = xmlSchemaCheckCOSValidDefault(vctxt,
26058 inode->decl->value, &(inode->val));
26059 if (ret != 0) {
26060 if (ret < 0) {
26061 VERROR_INT("xmlSchemaValidatorPopElem",
26062 "calling xmlSchemaCheckCOSValidDefault()");
26063 goto internal_error;
26064 }
26065 goto end_elem;
26066 }
26067 /*
26068 * Stop here, to avoid redundant validation of the value
26069 * (see following).
26070 */
26071 goto default_psvi;
Daniel Veillarddee23482008-04-11 12:58:43 +000026072 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026073 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026074 * cvc-elt (3.3.4) : 5.1.2
26075 * The element information item with the canonical lexical
26076 * representation of the {value constraint} value used as its
Rob Richardsc6947bb2008-06-29 15:04:41 +000026077 * �normalized value� must be �valid� with respect to the
26078 * �actual type definition� as defined by Element Locally Valid (Type)
26079 * (�3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026080 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026081 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026082 ret = xmlSchemaVCheckINodeDataType(vctxt,
26083 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026084 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026085 ret = xmlSchemaVCheckINodeDataType(vctxt,
26086 inode, inode->typeDef->contentTypeDef,
Daniel Veillarddee23482008-04-11 12:58:43 +000026087 inode->decl->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026088 }
26089 if (ret != 0) {
26090 if (ret < 0) {
26091 VERROR_INT("xmlSchemaValidatorPopElem",
26092 "calling xmlSchemaVCheckCVCSimpleType()");
26093 goto internal_error;
26094 }
26095 goto end_elem;
26096 }
26097
26098default_psvi:
26099 /*
26100 * PSVI: Create a text node on the instance element.
26101 */
26102 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
26103 (inode->node != NULL)) {
26104 xmlNodePtr textChild;
26105 xmlChar *normValue;
26106 /*
26107 * VAL TODO: Normalize the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026108 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026109 normValue = xmlSchemaNormalizeValue(inode->typeDef,
26110 inode->decl->value);
26111 if (normValue != NULL) {
26112 textChild = xmlNewText(BAD_CAST normValue);
26113 xmlFree(normValue);
26114 } else
26115 textChild = xmlNewText(inode->decl->value);
26116 if (textChild == NULL) {
26117 VERROR_INT("xmlSchemaValidatorPopElem",
26118 "calling xmlNewText()");
26119 goto internal_error;
26120 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000026121 xmlAddChild(inode->node, textChild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026122 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026123
26124 } else if (! INODE_NILLED(inode)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026125 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026126 * 5.2.1 The element information item must be �valid� with respect
26127 * to the �actual type definition� as defined by Element Locally
26128 * Valid (Type) (�3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026129 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026130 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026131 /*
26132 * SPEC (cvc-type) (3.1)
26133 * "If the type definition is a simple type definition, ..."
26134 * (3.1.3) "If clause 3.2 of Element Locally Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000026135 * (Element) (�3.3.4) did not apply, then the �normalized value�
26136 * must be �valid� with respect to the type definition as defined
26137 * by String Valid (�3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026138 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026139 ret = xmlSchemaVCheckINodeDataType(vctxt,
26140 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026141 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026142 /*
26143 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
26144 * definition, then the element information item must be
Rob Richardsc6947bb2008-06-29 15:04:41 +000026145 * �valid� with respect to the type definition as per
26146 * Element Locally Valid (Complex Type) (�3.4.4);"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026147 *
26148 * SPEC (cvc-complex-type) (2.2)
Daniel Veillarddee23482008-04-11 12:58:43 +000026149 * "If the {content type} is a simple type definition, ...
Rob Richardsc6947bb2008-06-29 15:04:41 +000026150 * the �normalized value� of the element information item is
26151 * �valid� with respect to that simple type definition as
26152 * defined by String Valid (�3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026153 */
26154 ret = xmlSchemaVCheckINodeDataType(vctxt,
26155 inode, inode->typeDef->contentTypeDef, inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026156 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026157 if (ret != 0) {
26158 if (ret < 0) {
26159 VERROR_INT("xmlSchemaValidatorPopElem",
26160 "calling xmlSchemaVCheckCVCSimpleType()");
26161 goto internal_error;
26162 }
26163 goto end_elem;
26164 }
26165 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026166 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026167 * not applied, all of the following must be true:
26168 */
26169 if ((inode->decl->value != NULL) &&
26170 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
26171
26172 /*
26173 * TODO: We will need a computed value, when comparison is
26174 * done on computed values.
26175 */
26176 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026177 * 5.2.2.1 The element information item must have no element
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026178 * information item [children].
26179 */
26180 if (inode->flags &
26181 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
26182 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
26183 VERROR(ret, NULL,
26184 "The content must not containt element nodes since "
26185 "there is a fixed value constraint");
26186 goto end_elem;
26187 } else {
26188 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026189 * 5.2.2.2 The appropriate case among the following must
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026190 * be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000026191 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026192 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026193 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026194 * 5.2.2.2.1 If the {content type} of the �actual type
26195 * definition� is mixed, then the *initial value* of the
Daniel Veillarddee23482008-04-11 12:58:43 +000026196 * item must match the canonical lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026197 * of the {value constraint} value.
26198 *
Daniel Veillarddee23482008-04-11 12:58:43 +000026199 * ... the *initial value* of an element information
26200 * item is the string composed of, in order, the
26201 * [character code] of each character information item in
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026202 * the [children] of that element information item.
Daniel Veillarddee23482008-04-11 12:58:43 +000026203 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026204 if (! xmlStrEqual(inode->value, inode->decl->value)){
Daniel Veillarddee23482008-04-11 12:58:43 +000026205 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026206 * VAL TODO: Report invalid & expected values as well.
26207 * VAL TODO: Implement the canonical stuff.
26208 */
26209 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000026210 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026211 ret, NULL, NULL,
26212 "The initial value '%s' does not match the fixed "
26213 "value constraint '%s'",
26214 inode->value, inode->decl->value);
26215 goto end_elem;
26216 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026217 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026218 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026219 * 5.2.2.2.2 If the {content type} of the �actual type
26220 * definition� is a simple type definition, then the
Daniel Veillarddee23482008-04-11 12:58:43 +000026221 * *actual value* of the item must match the canonical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026222 * lexical representation of the {value constraint} value.
26223 */
26224 /*
26225 * VAL TODO: *actual value* is the normalized value, impl.
26226 * this.
26227 * VAL TODO: Report invalid & expected values as well.
26228 * VAL TODO: Implement a comparison with the computed values.
26229 */
26230 if (! xmlStrEqual(inode->value,
26231 inode->decl->value)) {
26232 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026233 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026234 ret, NULL, NULL,
26235 "The actual value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000026236 "value constraint '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026237 inode->value,
26238 inode->decl->value);
26239 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000026240 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026241 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026242 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026243 }
26244 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026245
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026246end_elem:
26247 if (vctxt->depth < 0) {
26248 /* TODO: raise error? */
26249 return (0);
26250 }
26251 if (vctxt->depth == vctxt->skipDepth)
26252 vctxt->skipDepth = -1;
26253 /*
26254 * Evaluate the history of XPath state objects.
Daniel Veillarddee23482008-04-11 12:58:43 +000026255 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026256 if (inode->appliedXPath &&
26257 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026258 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026259 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026260 * MAYBE TODO:
Rob Richardsc6947bb2008-06-29 15:04:41 +000026261 * SPEC (6) "The element information item must be �valid� with
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026262 * respect to each of the {identity-constraint definitions} as per
Rob Richardsc6947bb2008-06-29 15:04:41 +000026263 * Identity-constraint Satisfied (�3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026264 */
26265 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026266 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
26267 * need to be built in any case.
26268 * We will currently build IDC node-tables and bubble them only if
26269 * keyrefs do exist.
26270 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026271
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026272 /*
26273 * Add the current IDC target-nodes to the IDC node-tables.
26274 */
26275 if ((inode->idcMatchers != NULL) &&
26276 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26277 {
26278 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
26279 goto internal_error;
26280 }
26281 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026282 * Validate IDC keyrefs.
26283 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026284 if (vctxt->inode->hasKeyrefs)
26285 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
26286 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026287 /*
26288 * Merge/free the IDC table.
26289 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026290 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026291#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026292 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026293 inode->nsName,
26294 inode->localName,
26295 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026296#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026297 if ((vctxt->depth > 0) &&
26298 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26299 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026300 /*
26301 * Merge the IDC node table with the table of the parent node.
26302 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026303 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
26304 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000026305 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026306 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026307 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026308 * Clear the current ielem.
26309 * VAL TODO: Don't free the PSVI IDC tables if they are
26310 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026311 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000026312 xmlSchemaClearElemInfo(vctxt, inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026313 /*
26314 * Skip further processing if we are on the validation root.
26315 */
26316 if (vctxt->depth == 0) {
26317 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026318 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026319 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026320 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026321 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026322 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026323 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026324 if (vctxt->aidcs != NULL) {
26325 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
26326 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026327 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026328 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026329 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026330 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026331 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026332 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026333 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026334 }
26335 aidc = aidc->next;
26336 } while (aidc != NULL);
26337 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026338 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026339 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000026340 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026341 * VAL TODO: 7 If the element information item is the �validation root�, it must be
26342 * �valid� per Validation Root Valid (ID/IDREF) (�3.3.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026343 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026344 return (ret);
26345
26346internal_error:
26347 vctxt->err = -1;
26348 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026349}
26350
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026351/*
26352* 3.4.4 Complex Type Definition Validation Rules
26353* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26354*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026355static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026356xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026357{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026358 xmlSchemaNodeInfoPtr pielem;
26359 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026360 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026362 if (vctxt->depth <= 0) {
26363 VERROR_INT("xmlSchemaValidateChildElem",
26364 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026365 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026366 }
26367 pielem = vctxt->elemInfos[vctxt->depth -1];
26368 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26369 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026370 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026371 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026372 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026373 if (INODE_NILLED(pielem)) {
26374 /*
26375 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26376 */
26377 ACTIVATE_PARENT_ELEM;
26378 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26379 VERROR(ret, NULL,
26380 "Neither character nor element content is allowed, "
26381 "because the element was 'nilled'");
26382 ACTIVATE_ELEM;
26383 goto unexpected_elem;
26384 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026385
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026386 ptype = pielem->typeDef;
26387
26388 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26389 /*
26390 * Workaround for "anyType": we have currently no content model
26391 * assigned for "anyType", so handle it explicitely.
26392 * "anyType" has an unbounded, lax "any" wildcard.
26393 */
26394 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26395 vctxt->inode->localName,
26396 vctxt->inode->nsName);
26397
26398 if (vctxt->inode->decl == NULL) {
26399 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026400 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026401 * Process "xsi:type".
26402 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026403 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026404 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26405 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26406 if (iattr != NULL) {
26407 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26408 &(vctxt->inode->typeDef), NULL);
26409 if (ret != 0) {
26410 if (ret == -1) {
26411 VERROR_INT("xmlSchemaValidateChildElem",
26412 "calling xmlSchemaProcessXSIType() to "
26413 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026414 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026415 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026416 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026417 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026418 } else {
26419 /*
26420 * Fallback to "anyType".
26421 *
26422 * SPEC (cvc-assess-elt)
Rob Richardsc6947bb2008-06-29 15:04:41 +000026423 * "If the item cannot be �strictly assessed�, [...]
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026424 * an element information item's schema validity may be laxly
Rob Richardsc6947bb2008-06-29 15:04:41 +000026425 * assessed if its �context-determined declaration� is not
26426 * skip by �validating� with respect to the �ur-type
26427 * definition� as per Element Locally Valid (Type) (�3.3.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026428 */
26429 vctxt->inode->typeDef =
26430 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026431 }
26432 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026433 return (0);
26434 }
26435
26436 switch (ptype->contentType) {
26437 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026438 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026439 * SPEC (2.1) "If the {content type} is empty, then the
26440 * element information item has no character or element
26441 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026442 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026443 ACTIVATE_PARENT_ELEM
26444 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26445 VERROR(ret, NULL,
26446 "Element content is not allowed, "
26447 "because the content type is empty");
26448 ACTIVATE_ELEM
26449 goto unexpected_elem;
26450 break;
26451
26452 case XML_SCHEMA_CONTENT_MIXED:
26453 case XML_SCHEMA_CONTENT_ELEMENTS: {
26454 xmlRegExecCtxtPtr regexCtxt;
26455 xmlChar *values[10];
26456 int terminal, nbval = 10, nbneg;
26457
26458 /* VAL TODO: Optimized "anyType" validation.*/
26459
26460 if (ptype->contModel == NULL) {
26461 VERROR_INT("xmlSchemaValidateChildElem",
26462 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026463 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026464 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026465 /*
26466 * Safety belf for evaluation if the cont. model was already
26467 * examined to be invalid.
26468 */
26469 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26470 VERROR_INT("xmlSchemaValidateChildElem",
26471 "validating elem, but elem content is already invalid");
26472 return (-1);
26473 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026474
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026475 regexCtxt = pielem->regexCtxt;
26476 if (regexCtxt == NULL) {
26477 /*
26478 * Create the regex context.
26479 */
26480 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26481 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26482 vctxt);
26483 if (regexCtxt == NULL) {
26484 VERROR_INT("xmlSchemaValidateChildElem",
26485 "failed to create a regex context");
26486 return (-1);
26487 }
26488 pielem->regexCtxt = regexCtxt;
26489#ifdef DEBUG_AUTOMATA
26490 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26491 pielem->localName);
26492#endif
26493 }
26494
26495 /*
26496 * SPEC (2.4) "If the {content type} is element-only or mixed,
26497 * then the sequence of the element information item's
26498 * element information item [children], if any, taken in
Rob Richardsc6947bb2008-06-29 15:04:41 +000026499 * order, is �valid� with respect to the {content type}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026500 * particle, as defined in Element Sequence Locally Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000026501 * (Particle) (�3.9.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026502 */
26503 ret = xmlRegExecPushString2(regexCtxt,
26504 vctxt->inode->localName,
26505 vctxt->inode->nsName,
26506 vctxt->inode);
26507#ifdef DEBUG_AUTOMATA
26508 if (ret < 0)
26509 xmlGenericError(xmlGenericErrorContext,
26510 "AUTOMATON push ERROR for '%s' on '%s'\n",
26511 vctxt->inode->localName, pielem->localName);
26512 else
26513 xmlGenericError(xmlGenericErrorContext,
26514 "AUTOMATON push OK for '%s' on '%s'\n",
26515 vctxt->inode->localName, pielem->localName);
26516#endif
26517 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26518 VERROR_INT("xmlSchemaValidateChildElem",
26519 "calling xmlRegExecPushString2()");
26520 return (-1);
26521 }
26522 if (ret < 0) {
26523 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26524 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026525 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026526 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26527 "This element is not expected",
26528 nbval, nbneg, values);
26529 ret = vctxt->err;
26530 goto unexpected_elem;
26531 } else
26532 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026533 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026534 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026535 case XML_SCHEMA_CONTENT_SIMPLE:
26536 case XML_SCHEMA_CONTENT_BASIC:
26537 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026538 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026539 /*
26540 * SPEC (cvc-complex-type) (2.2)
26541 * "If the {content type} is a simple type definition, then
26542 * the element information item has no element information
26543 * item [children], ..."
26544 */
26545 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26546 VERROR(ret, NULL, "Element content is not allowed, "
26547 "because the content type is a simple type definition");
26548 } else {
26549 /*
26550 * SPEC (cvc-type) (3.1.2) "The element information item must
26551 * have no element information item [children]."
26552 */
26553 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26554 VERROR(ret, NULL, "Element content is not allowed, "
26555 "because the type definition is simple");
26556 }
26557 ACTIVATE_ELEM
26558 ret = vctxt->err;
26559 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026560 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026561
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026562 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026563 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026564 }
26565 return (ret);
26566unexpected_elem:
26567 /*
26568 * Pop this element and set the skipDepth to skip
26569 * all further content of the parent element.
26570 */
26571 vctxt->skipDepth = vctxt->depth;
26572 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26573 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26574 return (ret);
26575}
26576
26577#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26578#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26579#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26580
26581static int
26582xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26583 int nodeType, const xmlChar *value, int len,
26584 int mode, int *consumed)
26585{
26586 /*
26587 * Unfortunately we have to duplicate the text sometimes.
26588 * OPTIMIZE: Maybe we could skip it, if:
26589 * 1. content type is simple
26590 * 2. whitespace is "collapse"
26591 * 3. it consists of whitespace only
26592 *
26593 * Process character content.
26594 */
26595 if (consumed != NULL)
26596 *consumed = 0;
26597 if (INODE_NILLED(vctxt->inode)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026598 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026599 * SPEC cvc-elt (3.3.4 - 3.2.1)
26600 * "The element information item must have no character or
26601 * element information item [children]."
26602 */
26603 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26604 "Neither character nor element content is allowed "
26605 "because the element is 'nilled'");
26606 return (vctxt->err);
26607 }
26608 /*
26609 * SPEC (2.1) "If the {content type} is empty, then the
26610 * element information item has no character or element
26611 * information item [children]."
26612 */
26613 if (vctxt->inode->typeDef->contentType ==
Daniel Veillarddee23482008-04-11 12:58:43 +000026614 XML_SCHEMA_CONTENT_EMPTY) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026615 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26616 "Character content is not allowed, "
26617 "because the content type is empty");
26618 return (vctxt->err);
26619 }
26620
26621 if (vctxt->inode->typeDef->contentType ==
26622 XML_SCHEMA_CONTENT_ELEMENTS) {
26623 if ((nodeType != XML_TEXT_NODE) ||
26624 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026625 /*
26626 * SPEC cvc-complex-type (2.3)
26627 * "If the {content type} is element-only, then the
26628 * element information item has no character information
26629 * item [children] other than those whose [character
26630 * code] is defined as a white space in [XML 1.0 (Second
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026631 * Edition)]."
26632 */
26633 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26634 "Character content other than whitespace is not allowed "
26635 "because the content type is 'element-only'");
26636 return (vctxt->err);
26637 }
26638 return (0);
26639 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026640
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026641 if ((value == NULL) || (value[0] == 0))
26642 return (0);
26643 /*
26644 * Save the value.
26645 * NOTE that even if the content type is *mixed*, we need the
26646 * *initial value* for default/fixed value constraints.
26647 */
26648 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26649 ((vctxt->inode->decl == NULL) ||
26650 (vctxt->inode->decl->value == NULL)))
26651 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000026652
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026653 if (vctxt->inode->value == NULL) {
26654 /*
26655 * Set the value.
26656 */
26657 switch (mode) {
26658 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26659 /*
26660 * When working on a tree.
26661 */
26662 vctxt->inode->value = value;
26663 break;
26664 case XML_SCHEMA_PUSH_TEXT_CREATED:
26665 /*
26666 * When working with the reader.
26667 * The value will be freed by the element info.
26668 */
26669 vctxt->inode->value = value;
26670 if (consumed != NULL)
26671 *consumed = 1;
26672 vctxt->inode->flags |=
26673 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26674 break;
26675 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26676 /*
26677 * When working with SAX.
26678 * The value will be freed by the element info.
26679 */
26680 if (len != -1)
26681 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26682 else
26683 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26684 vctxt->inode->flags |=
26685 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26686 break;
26687 default:
26688 break;
26689 }
Kasimier T. Buchcik5bb0c082005-12-20 10:48:33 +000026690 } else {
26691 if (len < 0)
26692 len = xmlStrlen(value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026693 /*
26694 * Concat the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026695 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026696 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026697 vctxt->inode->value = BAD_CAST xmlStrncat(
26698 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026699 } else {
26700 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026701 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026702 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26703 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026704 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026705
26706 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026707}
26708
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026709static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026710xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026711{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026712 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026713
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026714 if ((vctxt->skipDepth != -1) &&
26715 (vctxt->depth >= vctxt->skipDepth)) {
26716 VERROR_INT("xmlSchemaValidateElem",
26717 "in skip-state");
26718 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026719 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026720 if (vctxt->xsiAssemble) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026721 /*
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026722 * We will stop validation if there was an error during
26723 * dynamic schema construction.
26724 * Note that we simply set @skipDepth to 0, this could
26725 * mean that a streaming document via SAX would be
26726 * still read to the end but it won't be validated any more.
26727 * TODO: If we are sure how to stop the validation at once
26728 * for all input scenarios, then this should be changed to
26729 * instantly stop the validation.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026730 */
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026731 ret = xmlSchemaAssembleByXSI(vctxt);
26732 if (ret != 0) {
26733 if (ret == -1)
26734 goto internal_error;
26735 vctxt->skipDepth = 0;
26736 return(ret);
26737 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026738 }
26739 if (vctxt->depth > 0) {
26740 /*
26741 * Validate this element against the content model
26742 * of the parent.
26743 */
26744 ret = xmlSchemaValidateChildElem(vctxt);
26745 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026746 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026747 VERROR_INT("xmlSchemaValidateElem",
26748 "calling xmlSchemaStreamValidateChildElement()");
26749 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026750 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026751 goto exit;
26752 }
26753 if (vctxt->depth == vctxt->skipDepth)
26754 goto exit;
26755 if ((vctxt->inode->decl == NULL) &&
26756 (vctxt->inode->typeDef == NULL)) {
26757 VERROR_INT("xmlSchemaValidateElem",
26758 "the child element was valid but neither the "
26759 "declaration nor the type was set");
26760 goto internal_error;
26761 }
26762 } else {
26763 /*
26764 * Get the declaration of the validation root.
26765 */
26766 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26767 vctxt->inode->localName,
26768 vctxt->inode->nsName);
26769 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026770 ret = XML_SCHEMAV_CVC_ELT_1;
26771 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026772 "No matching global declaration available "
26773 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026774 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026775 }
26776 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026777
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026778 if (vctxt->inode->decl == NULL)
26779 goto type_validation;
26780
26781 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26782 int skip;
26783 /*
26784 * Wildcards.
26785 */
26786 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26787 if (ret != 0) {
26788 if (ret < 0) {
26789 VERROR_INT("xmlSchemaValidateElem",
26790 "calling xmlSchemaValidateElemWildcard()");
26791 goto internal_error;
26792 }
26793 goto exit;
26794 }
26795 if (skip) {
26796 vctxt->skipDepth = vctxt->depth;
26797 goto exit;
26798 }
26799 /*
26800 * The declaration might be set by the wildcard validation,
26801 * when the processContents is "lax" or "strict".
26802 */
26803 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26804 /*
26805 * Clear the "decl" field to not confuse further processing.
26806 */
26807 vctxt->inode->decl = NULL;
26808 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026809 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026810 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026811 /*
26812 * Validate against the declaration.
26813 */
26814 ret = xmlSchemaValidateElemDecl(vctxt);
26815 if (ret != 0) {
26816 if (ret < 0) {
26817 VERROR_INT("xmlSchemaValidateElem",
26818 "calling xmlSchemaValidateElemDecl()");
26819 goto internal_error;
26820 }
26821 goto exit;
26822 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026823 /*
26824 * Validate against the type definition.
26825 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026826type_validation:
26827
26828 if (vctxt->inode->typeDef == NULL) {
26829 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26830 ret = XML_SCHEMAV_CVC_TYPE_1;
26831 VERROR(ret, NULL,
26832 "The type definition is absent");
26833 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000026834 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026835 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26836 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26837 ret = XML_SCHEMAV_CVC_TYPE_2;
26838 VERROR(ret, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000026839 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026840 goto exit;
26841 }
26842 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026843 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026844 * during validation against the declaration. This must be done
26845 * _before_ attribute validation.
26846 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026847 if (vctxt->xpathStates != NULL) {
26848 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026849 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026850 if (ret == -1) {
26851 VERROR_INT("xmlSchemaValidateElem",
26852 "calling xmlSchemaXPathEvaluate()");
26853 goto internal_error;
26854 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026855 }
26856 /*
26857 * Validate attributes.
26858 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026859 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026860 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026861 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026862
26863 ret = xmlSchemaVAttributesComplex(vctxt);
26864 }
26865 } else if (vctxt->nbAttrInfos != 0) {
26866
26867 ret = xmlSchemaVAttributesSimple(vctxt);
26868 }
26869 /*
26870 * Clear registered attributes.
26871 */
26872 if (vctxt->nbAttrInfos != 0)
26873 xmlSchemaClearAttrInfos(vctxt);
26874 if (ret == -1) {
26875 VERROR_INT("xmlSchemaValidateElem",
26876 "calling attributes validation");
26877 goto internal_error;
26878 }
26879 /*
26880 * Don't return an error if attributes are invalid on purpose.
26881 */
26882 ret = 0;
26883
26884exit:
26885 if (ret != 0)
26886 vctxt->skipDepth = vctxt->depth;
26887 return (ret);
26888internal_error:
26889 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026890}
26891
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026892#ifdef XML_SCHEMA_READER_ENABLED
26893static int
26894xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026895{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026896 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26897 int depth, nodeType, ret = 0, consumed;
26898 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026899
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026900 vctxt->depth = -1;
26901 ret = xmlTextReaderRead(vctxt->reader);
26902 /*
26903 * Move to the document element.
26904 */
26905 while (ret == 1) {
26906 nodeType = xmlTextReaderNodeType(vctxt->reader);
26907 if (nodeType == XML_ELEMENT_NODE)
26908 goto root_found;
26909 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026910 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026911 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026912
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026913root_found:
26914
26915 do {
26916 depth = xmlTextReaderDepth(vctxt->reader);
26917 nodeType = xmlTextReaderNodeType(vctxt->reader);
26918
26919 if (nodeType == XML_ELEMENT_NODE) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026920
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026921 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026922 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26923 VERROR_INT("xmlSchemaVReaderWalk",
26924 "calling xmlSchemaValidatorPushElem()");
26925 goto internal_error;
26926 }
26927 ielem = vctxt->inode;
26928 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26929 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26930 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26931 /*
26932 * Is the element empty?
26933 */
26934 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26935 if (ret == -1) {
26936 VERROR_INT("xmlSchemaVReaderWalk",
26937 "calling xmlTextReaderIsEmptyElement()");
26938 goto internal_error;
26939 }
26940 if (ret) {
26941 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26942 }
26943 /*
26944 * Register attributes.
26945 */
26946 vctxt->nbAttrInfos = 0;
26947 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26948 if (ret == -1) {
26949 VERROR_INT("xmlSchemaVReaderWalk",
26950 "calling xmlTextReaderMoveToFirstAttribute()");
26951 goto internal_error;
26952 }
26953 if (ret == 1) {
26954 do {
26955 /*
26956 * VAL TODO: How do we know that the reader works on a
26957 * node tree, to be able to pass a node here?
26958 */
26959 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26960 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26961 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26962 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26963
26964 VERROR_INT("xmlSchemaVReaderWalk",
26965 "calling xmlSchemaValidatorPushAttribute()");
26966 goto internal_error;
26967 }
26968 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26969 if (ret == -1) {
26970 VERROR_INT("xmlSchemaVReaderWalk",
26971 "calling xmlTextReaderMoveToFirstAttribute()");
26972 goto internal_error;
26973 }
26974 } while (ret == 1);
26975 /*
26976 * Back to element position.
26977 */
26978 ret = xmlTextReaderMoveToElement(vctxt->reader);
26979 if (ret == -1) {
26980 VERROR_INT("xmlSchemaVReaderWalk",
26981 "calling xmlTextReaderMoveToElement()");
26982 goto internal_error;
26983 }
26984 }
26985 /*
26986 * Validate the element.
26987 */
26988 ret= xmlSchemaValidateElem(vctxt);
26989 if (ret != 0) {
26990 if (ret == -1) {
26991 VERROR_INT("xmlSchemaVReaderWalk",
26992 "calling xmlSchemaValidateElem()");
26993 goto internal_error;
26994 }
26995 goto exit;
26996 }
26997 if (vctxt->depth == vctxt->skipDepth) {
26998 int curDepth;
26999 /*
27000 * Skip all content.
27001 */
27002 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
27003 ret = xmlTextReaderRead(vctxt->reader);
27004 curDepth = xmlTextReaderDepth(vctxt->reader);
27005 while ((ret == 1) && (curDepth != depth)) {
27006 ret = xmlTextReaderRead(vctxt->reader);
27007 curDepth = xmlTextReaderDepth(vctxt->reader);
27008 }
27009 if (ret < 0) {
27010 /*
27011 * VAL TODO: A reader error occured; what to do here?
27012 */
27013 ret = 1;
27014 goto exit;
27015 }
27016 }
27017 goto leave_elem;
27018 }
27019 /*
27020 * READER VAL TODO: Is an END_ELEM really never called
27021 * if the elem is empty?
27022 */
27023 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27024 goto leave_elem;
27025 } else if (nodeType == END_ELEM) {
27026 /*
27027 * Process END of element.
27028 */
27029leave_elem:
27030 ret = xmlSchemaValidatorPopElem(vctxt);
27031 if (ret != 0) {
27032 if (ret < 0) {
27033 VERROR_INT("xmlSchemaVReaderWalk",
27034 "calling xmlSchemaValidatorPopElem()");
27035 goto internal_error;
27036 }
27037 goto exit;
27038 }
27039 if (vctxt->depth >= 0)
27040 ielem = vctxt->inode;
27041 else
27042 ielem = NULL;
27043 } else if ((nodeType == XML_TEXT_NODE) ||
27044 (nodeType == XML_CDATA_SECTION_NODE) ||
27045 (nodeType == WHTSP) ||
27046 (nodeType == SIGN_WHTSP)) {
27047 /*
27048 * Process character content.
27049 */
27050 xmlChar *value;
27051
27052 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
27053 nodeType = XML_TEXT_NODE;
27054
27055 value = xmlTextReaderValue(vctxt->reader);
27056 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
27057 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
27058 if (! consumed)
27059 xmlFree(value);
27060 if (ret == -1) {
27061 VERROR_INT("xmlSchemaVReaderWalk",
27062 "calling xmlSchemaVPushText()");
27063 goto internal_error;
27064 }
27065 } else if ((nodeType == XML_ENTITY_NODE) ||
27066 (nodeType == XML_ENTITY_REF_NODE)) {
27067 /*
27068 * VAL TODO: What to do with entities?
27069 */
27070 TODO
27071 }
27072 /*
27073 * Read next node.
27074 */
27075 ret = xmlTextReaderRead(vctxt->reader);
27076 } while (ret == 1);
27077
27078exit:
27079 return (ret);
27080internal_error:
27081 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027082}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027083#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000027084
27085/************************************************************************
27086 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027087 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000027088 * *
27089 ************************************************************************/
27090
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027091/*
27092* Process text content.
27093*/
27094static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027095xmlSchemaSAXHandleText(void *ctx,
27096 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027097 int len)
27098{
27099 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27100
27101 if (vctxt->depth < 0)
27102 return;
27103 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27104 return;
27105 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27106 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27107 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
27108 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27109 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27110 "calling xmlSchemaVPushText()");
27111 vctxt->err = -1;
27112 xmlStopParser(vctxt->parserCtxt);
27113 }
27114}
27115
27116/*
27117* Process CDATA content.
27118*/
27119static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027120xmlSchemaSAXHandleCDataSection(void *ctx,
27121 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027122 int len)
Daniel Veillarddee23482008-04-11 12:58:43 +000027123{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027124 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27125
27126 if (vctxt->depth < 0)
27127 return;
27128 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27129 return;
27130 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27131 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27132 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
27133 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27134 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27135 "calling xmlSchemaVPushText()");
27136 vctxt->err = -1;
27137 xmlStopParser(vctxt->parserCtxt);
27138 }
27139}
27140
27141static void
27142xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
27143 const xmlChar * name ATTRIBUTE_UNUSED)
27144{
27145 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27146
27147 if (vctxt->depth < 0)
27148 return;
27149 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27150 return;
27151 /* SAX VAL TODO: What to do here? */
27152 TODO
27153}
27154
27155static void
27156xmlSchemaSAXHandleStartElementNs(void *ctx,
Daniel Veillarddee23482008-04-11 12:58:43 +000027157 const xmlChar * localname,
27158 const xmlChar * prefix ATTRIBUTE_UNUSED,
27159 const xmlChar * URI,
27160 int nb_namespaces,
27161 const xmlChar ** namespaces,
27162 int nb_attributes,
27163 int nb_defaulted ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027164 const xmlChar ** attributes)
Daniel Veillarddee23482008-04-11 12:58:43 +000027165{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027166 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27167 int ret;
27168 xmlSchemaNodeInfoPtr ielem;
27169 int i, j;
Daniel Veillarddee23482008-04-11 12:58:43 +000027170
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027171 /*
27172 * SAX VAL TODO: What to do with nb_defaulted?
27173 */
27174 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027175 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027176 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027177 vctxt->depth++;
27178 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027179 return;
27180 /*
27181 * Push the element.
27182 */
27183 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
27184 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27185 "calling xmlSchemaValidatorPushElem()");
27186 goto internal_error;
27187 }
27188 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027189 /*
27190 * TODO: Is this OK?
27191 */
27192 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027193 ielem->localName = localname;
27194 ielem->nsName = URI;
27195 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27196 /*
27197 * Register namespaces on the elem info.
Daniel Veillarddee23482008-04-11 12:58:43 +000027198 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027199 if (nb_namespaces != 0) {
27200 /*
27201 * Although the parser builds its own namespace list,
27202 * we have no access to it, so we'll use an own one.
27203 */
Daniel Veillarddee23482008-04-11 12:58:43 +000027204 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027205 /*
27206 * Store prefix and namespace name.
Daniel Veillarddee23482008-04-11 12:58:43 +000027207 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027208 if (ielem->nsBindings == NULL) {
27209 ielem->nsBindings =
27210 (const xmlChar **) xmlMalloc(10 *
27211 sizeof(const xmlChar *));
27212 if (ielem->nsBindings == NULL) {
27213 xmlSchemaVErrMemory(vctxt,
27214 "allocating namespace bindings for SAX validation",
27215 NULL);
27216 goto internal_error;
27217 }
27218 ielem->nbNsBindings = 0;
27219 ielem->sizeNsBindings = 5;
27220 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
27221 ielem->sizeNsBindings *= 2;
27222 ielem->nsBindings =
27223 (const xmlChar **) xmlRealloc(
27224 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027225 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027226 if (ielem->nsBindings == NULL) {
27227 xmlSchemaVErrMemory(vctxt,
27228 "re-allocating namespace bindings for SAX validation",
27229 NULL);
27230 goto internal_error;
27231 }
27232 }
27233
27234 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
27235 if (namespaces[j+1][0] == 0) {
27236 /*
27237 * Handle xmlns="".
27238 */
27239 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
27240 } else
27241 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
27242 namespaces[j+1];
Daniel Veillarddee23482008-04-11 12:58:43 +000027243 ielem->nbNsBindings++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027244 }
27245 }
27246 /*
27247 * Register attributes.
27248 * SAX VAL TODO: We are not adding namespace declaration
27249 * attributes yet.
27250 */
27251 if (nb_attributes != 0) {
27252 xmlChar *value;
27253
27254 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
27255 /*
27256 * Duplicate the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000027257 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027258 value = xmlStrndup(attributes[j+3],
27259 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027260 /*
27261 * TODO: Set the node line.
27262 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027263 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027264 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027265 value, 1);
27266 if (ret == -1) {
27267 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27268 "calling xmlSchemaValidatorPushAttribute()");
27269 goto internal_error;
27270 }
27271 }
27272 }
27273 /*
27274 * Validate the element.
27275 */
27276 ret = xmlSchemaValidateElem(vctxt);
27277 if (ret != 0) {
27278 if (ret == -1) {
27279 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27280 "calling xmlSchemaValidateElem()");
27281 goto internal_error;
27282 }
27283 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000027284 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027285
27286exit:
27287 return;
27288internal_error:
27289 vctxt->err = -1;
27290 xmlStopParser(vctxt->parserCtxt);
27291 return;
27292}
27293
27294static void
27295xmlSchemaSAXHandleEndElementNs(void *ctx,
27296 const xmlChar * localname ATTRIBUTE_UNUSED,
27297 const xmlChar * prefix ATTRIBUTE_UNUSED,
27298 const xmlChar * URI ATTRIBUTE_UNUSED)
27299{
27300 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27301 int res;
27302
27303 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027304 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027305 */
27306 if (vctxt->skipDepth != -1) {
27307 if (vctxt->depth > vctxt->skipDepth) {
27308 vctxt->depth--;
27309 return;
27310 } else
27311 vctxt->skipDepth = -1;
27312 }
27313 /*
27314 * SAX VAL TODO: Just a temporary check.
27315 */
27316 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
27317 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
27318 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27319 "elem pop mismatch");
27320 }
27321 res = xmlSchemaValidatorPopElem(vctxt);
27322 if (res != 0) {
27323 if (res < 0) {
27324 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27325 "calling xmlSchemaValidatorPopElem()");
27326 goto internal_error;
27327 }
27328 goto exit;
27329 }
27330exit:
27331 return;
27332internal_error:
27333 vctxt->err = -1;
27334 xmlStopParser(vctxt->parserCtxt);
27335 return;
27336}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027337
Daniel Veillard4255d502002-04-16 15:50:10 +000027338/************************************************************************
27339 * *
27340 * Validation interfaces *
27341 * *
27342 ************************************************************************/
27343
27344/**
27345 * xmlSchemaNewValidCtxt:
27346 * @schema: a precompiled XML Schemas
27347 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027348 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000027349 *
27350 * Returns the validation context or NULL in case of error
27351 */
27352xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027353xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
27354{
Daniel Veillard4255d502002-04-16 15:50:10 +000027355 xmlSchemaValidCtxtPtr ret;
27356
27357 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
27358 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027359 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000027360 return (NULL);
27361 }
27362 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027363 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027364 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027365 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027366 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027367 return (ret);
27368}
27369
27370/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027371 * xmlSchemaClearValidCtxt:
27372 * @ctxt: the schema validation context
27373 *
27374 * Free the resources associated to the schema validation context;
27375 * leaves some fields alive intended for reuse of the context.
27376 */
27377static void
27378xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27379{
27380 if (vctxt == NULL)
27381 return;
27382
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027383 /*
27384 * TODO: Should we clear the flags?
27385 * Might be problematic if one reuses the context
27386 * and assumes that the options remain the same.
27387 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027388 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027389 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027390 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027391#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027392 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027393#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000027394 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027395
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027396 if (vctxt->value != NULL) {
27397 xmlSchemaFreeValue(vctxt->value);
27398 vctxt->value = NULL;
27399 }
27400 /*
27401 * Augmented IDC information.
27402 */
27403 if (vctxt->aidcs != NULL) {
27404 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27405 do {
27406 next = cur->next;
27407 xmlFree(cur);
27408 cur = next;
27409 } while (cur != NULL);
27410 vctxt->aidcs = NULL;
27411 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027412 if (vctxt->idcMatcherCache != NULL) {
27413 xmlSchemaIDCMatcherPtr matcher = vctxt->idcMatcherCache, tmp;
27414
27415 while (matcher) {
27416 tmp = matcher;
27417 matcher = matcher->nextCached;
27418 xmlSchemaIDCFreeMatcherList(tmp);
27419 }
27420 vctxt->idcMatcherCache = NULL;
27421 }
27422
27423
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027424 if (vctxt->idcNodes != NULL) {
27425 int i;
27426 xmlSchemaPSVIIDCNodePtr item;
27427
27428 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027429 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027430 xmlFree(item->keys);
27431 xmlFree(item);
27432 }
27433 xmlFree(vctxt->idcNodes);
27434 vctxt->idcNodes = NULL;
Daniel Veillard1131e112006-08-07 11:02:54 +000027435 vctxt->nbIdcNodes = 0;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027436 vctxt->sizeIdcNodes = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027437 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027438 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027439 * Note that we won't delete the XPath state pool here.
27440 */
27441 if (vctxt->xpathStates != NULL) {
27442 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27443 vctxt->xpathStates = NULL;
27444 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027445 /*
27446 * Attribute info.
27447 */
27448 if (vctxt->nbAttrInfos != 0) {
27449 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027450 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027451 /*
27452 * Element info.
27453 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027454 if (vctxt->elemInfos != NULL) {
27455 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027456 xmlSchemaNodeInfoPtr ei;
27457
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027458 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027459 ei = vctxt->elemInfos[i];
27460 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027461 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027462 xmlSchemaClearElemInfo(vctxt, ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027463 }
Daniel Veillarddee23482008-04-11 12:58:43 +000027464 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027465 xmlSchemaItemListClear(vctxt->nodeQNames);
27466 /* Recreate the dict. */
27467 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027468 /*
27469 * TODO: Is is save to recreate it? Do we have a scenario
27470 * where the user provides the dict?
27471 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027472 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027473}
27474
27475/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027476 * xmlSchemaFreeValidCtxt:
27477 * @ctxt: the schema validation context
27478 *
27479 * Free the resources associated to the schema validation context
27480 */
27481void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027482xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27483{
Daniel Veillard4255d502002-04-16 15:50:10 +000027484 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027485 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027486 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027487 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027488 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027489 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027490 if (ctxt->idcNodes != NULL) {
27491 int i;
27492 xmlSchemaPSVIIDCNodePtr item;
27493
27494 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027495 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027496 xmlFree(item->keys);
27497 xmlFree(item);
27498 }
27499 xmlFree(ctxt->idcNodes);
27500 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027501 if (ctxt->idcKeys != NULL) {
27502 int i;
27503 for (i = 0; i < ctxt->nbIdcKeys; i++)
27504 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27505 xmlFree(ctxt->idcKeys);
27506 }
27507
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027508 if (ctxt->xpathStates != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027509 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027510 ctxt->xpathStates = NULL;
27511 }
27512 if (ctxt->xpathStatePool != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027513 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027514 ctxt->xpathStatePool = NULL;
27515 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027516
27517 /*
27518 * Augmented IDC information.
27519 */
27520 if (ctxt->aidcs != NULL) {
27521 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27522 do {
27523 next = cur->next;
27524 xmlFree(cur);
27525 cur = next;
27526 } while (cur != NULL);
27527 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027528 if (ctxt->attrInfos != NULL) {
27529 int i;
27530 xmlSchemaAttrInfoPtr attr;
27531
27532 /* Just a paranoid call to the cleanup. */
27533 if (ctxt->nbAttrInfos != 0)
27534 xmlSchemaClearAttrInfos(ctxt);
27535 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27536 attr = ctxt->attrInfos[i];
27537 xmlFree(attr);
27538 }
27539 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027540 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027541 if (ctxt->elemInfos != NULL) {
27542 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027543 xmlSchemaNodeInfoPtr ei;
27544
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027545 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027546 ei = ctxt->elemInfos[i];
27547 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027548 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027549 xmlSchemaClearElemInfo(ctxt, ei);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027550 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027551 }
27552 xmlFree(ctxt->elemInfos);
27553 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027554 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027555 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027556 if (ctxt->dict != NULL)
27557 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027558 xmlFree(ctxt);
27559}
27560
27561/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027562 * xmlSchemaIsValid:
27563 * @ctxt: the schema validation context
27564 *
27565 * Check if any error was detected during validation.
Daniel Veillarddee23482008-04-11 12:58:43 +000027566 *
Daniel Veillardf10ae122005-07-10 19:03:16 +000027567 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27568 * of internal error.
27569 */
27570int
27571xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27572{
27573 if (ctxt == NULL)
27574 return(-1);
27575 return(ctxt->err == 0);
27576}
27577
27578/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027579 * xmlSchemaSetValidErrors:
27580 * @ctxt: a schema validation context
27581 * @err: the error function
27582 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027583 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027584 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027585 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027586 */
27587void
27588xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027589 xmlSchemaValidityErrorFunc err,
27590 xmlSchemaValidityWarningFunc warn, void *ctx)
27591{
Daniel Veillard4255d502002-04-16 15:50:10 +000027592 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027593 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027594 ctxt->error = err;
27595 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027596 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027597 if (ctxt->pctxt != NULL)
27598 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027599}
27600
27601/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027602 * xmlSchemaSetValidStructuredErrors:
27603 * @ctxt: a schema validation context
27604 * @serror: the structured error function
27605 * @ctx: the functions context
27606 *
27607 * Set the structured error callback
27608 */
27609void
27610xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027611 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027612{
27613 if (ctxt == NULL)
27614 return;
27615 ctxt->serror = serror;
27616 ctxt->error = NULL;
27617 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027618 ctxt->errCtxt = ctx;
27619 if (ctxt->pctxt != NULL)
27620 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027621}
27622
27623/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027624 * xmlSchemaGetValidErrors:
Daniel Veillarddee23482008-04-11 12:58:43 +000027625 * @ctxt: a XML-Schema validation context
Daniel Veillard259f0df2004-08-18 09:13:18 +000027626 * @err: the error function result
27627 * @warn: the warning function result
27628 * @ctx: the functions context result
27629 *
27630 * Get the error and warning callback informations
27631 *
27632 * Returns -1 in case of error and 0 otherwise
27633 */
27634int
27635xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027636 xmlSchemaValidityErrorFunc * err,
27637 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027638{
27639 if (ctxt == NULL)
27640 return (-1);
27641 if (err != NULL)
27642 *err = ctxt->error;
27643 if (warn != NULL)
27644 *warn = ctxt->warning;
27645 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027646 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027647 return (0);
27648}
27649
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027650
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027651/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027652 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027653 * @ctxt: a schema validation context
27654 * @options: a combination of xmlSchemaValidOption
27655 *
27656 * Sets the options to be used during the validation.
27657 *
27658 * Returns 0 in case of success, -1 in case of an
27659 * API error.
27660 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027661int
27662xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27663 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027664
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027665{
27666 int i;
27667
27668 if (ctxt == NULL)
27669 return (-1);
27670 /*
27671 * WARNING: Change the start value if adding to the
27672 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027673 * TODO: Is there an other, more easy to maintain,
27674 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027675 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027676 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027677 if (options & 1<<i)
27678 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027679 }
27680 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027681 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027682}
27683
27684/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027685 * xmlSchemaValidCtxtGetOptions:
Daniel Veillarddee23482008-04-11 12:58:43 +000027686 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027687 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027688 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027689 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027690 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027691 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027692int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027693xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027694
27695{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027696 if (ctxt == NULL)
27697 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027698 else
27699 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027700}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027701
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027702static int
27703xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27704{
27705 xmlAttrPtr attr;
27706 int ret = 0;
27707 xmlSchemaNodeInfoPtr ielem = NULL;
27708 xmlNodePtr node, valRoot;
27709 const xmlChar *nsName;
27710
27711 /* DOC VAL TODO: Move this to the start function. */
27712 valRoot = xmlDocGetRootElement(vctxt->doc);
27713 if (valRoot == NULL) {
27714 /* VAL TODO: Error code? */
27715 VERROR(1, NULL, "The document has no document element");
27716 return (1);
27717 }
27718 vctxt->depth = -1;
27719 vctxt->validationRoot = valRoot;
27720 node = valRoot;
27721 while (node != NULL) {
27722 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27723 goto next_sibling;
27724 if (node->type == XML_ELEMENT_NODE) {
27725
27726 /*
27727 * Init the node-info.
27728 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027729 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027730 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27731 goto internal_error;
27732 ielem = vctxt->inode;
27733 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027734 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027735 ielem->localName = node->name;
27736 if (node->ns != NULL)
27737 ielem->nsName = node->ns->href;
27738 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27739 /*
27740 * Register attributes.
27741 * DOC VAL TODO: We do not register namespace declaration
27742 * attributes yet.
27743 */
27744 vctxt->nbAttrInfos = 0;
27745 if (node->properties != NULL) {
27746 attr = node->properties;
27747 do {
27748 if (attr->ns != NULL)
27749 nsName = attr->ns->href;
27750 else
27751 nsName = NULL;
27752 ret = xmlSchemaValidatorPushAttribute(vctxt,
27753 (xmlNodePtr) attr,
Daniel Veillarddee23482008-04-11 12:58:43 +000027754 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027755 * Note that we give it the line number of the
27756 * parent element.
27757 */
27758 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027759 attr->name, nsName, 0,
27760 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27761 if (ret == -1) {
27762 VERROR_INT("xmlSchemaDocWalk",
27763 "calling xmlSchemaValidatorPushAttribute()");
27764 goto internal_error;
27765 }
27766 attr = attr->next;
27767 } while (attr);
27768 }
27769 /*
27770 * Validate the element.
27771 */
27772 ret = xmlSchemaValidateElem(vctxt);
27773 if (ret != 0) {
27774 if (ret == -1) {
27775 VERROR_INT("xmlSchemaDocWalk",
27776 "calling xmlSchemaValidateElem()");
27777 goto internal_error;
27778 }
27779 /*
27780 * Don't stop validation; just skip the content
27781 * of this element.
27782 */
27783 goto leave_node;
27784 }
27785 if ((vctxt->skipDepth != -1) &&
27786 (vctxt->depth >= vctxt->skipDepth))
27787 goto leave_node;
27788 } else if ((node->type == XML_TEXT_NODE) ||
27789 (node->type == XML_CDATA_SECTION_NODE)) {
27790 /*
27791 * Process character content.
27792 */
Daniel Veillard14b56432006-03-09 18:41:40 +000027793 if ((ielem != NULL) && (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027794 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27795 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27796 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27797 if (ret < 0) {
27798 VERROR_INT("xmlSchemaVDocWalk",
27799 "calling xmlSchemaVPushText()");
27800 goto internal_error;
27801 }
27802 /*
27803 * DOC VAL TODO: Should we skip further validation of the
27804 * element content here?
27805 */
27806 } else if ((node->type == XML_ENTITY_NODE) ||
27807 (node->type == XML_ENTITY_REF_NODE)) {
27808 /*
27809 * DOC VAL TODO: What to do with entities?
Daniel Veillarddee23482008-04-11 12:58:43 +000027810 */
Kasimier T. Buchcik146a9822006-05-04 17:19:07 +000027811 VERROR_INT("xmlSchemaVDocWalk",
27812 "there is at least one entity reference in the node-tree "
27813 "currently being validated. Processing of entities with "
27814 "this XML Schema processor is not supported (yet). Please "
27815 "substitute entities before validation.");
27816 goto internal_error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027817 } else {
27818 goto leave_node;
27819 /*
27820 * DOC VAL TODO: XInclude nodes, etc.
27821 */
27822 }
27823 /*
27824 * Walk the doc.
27825 */
27826 if (node->children != NULL) {
27827 node = node->children;
27828 continue;
27829 }
27830leave_node:
27831 if (node->type == XML_ELEMENT_NODE) {
27832 /*
27833 * Leaving the scope of an element.
27834 */
27835 if (node != vctxt->inode->node) {
27836 VERROR_INT("xmlSchemaVDocWalk",
27837 "element position mismatch");
27838 goto internal_error;
27839 }
27840 ret = xmlSchemaValidatorPopElem(vctxt);
27841 if (ret != 0) {
27842 if (ret < 0) {
27843 VERROR_INT("xmlSchemaVDocWalk",
27844 "calling xmlSchemaValidatorPopElem()");
27845 goto internal_error;
27846 }
27847 }
27848 if (node == valRoot)
27849 goto exit;
27850 }
27851next_sibling:
27852 if (node->next != NULL)
27853 node = node->next;
27854 else {
27855 node = node->parent;
27856 goto leave_node;
27857 }
27858 }
27859
27860exit:
27861 return (ret);
27862internal_error:
27863 return (-1);
27864}
27865
27866static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027867xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027868 /*
27869 * Some initialization.
Daniel Veillarddee23482008-04-11 12:58:43 +000027870 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027871 vctxt->err = 0;
27872 vctxt->nberrors = 0;
27873 vctxt->depth = -1;
27874 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027875 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027876 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000027877#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027878 vctxt->createIDCNodeTables = 1;
27879#else
27880 vctxt->createIDCNodeTables = 0;
27881#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027882 /*
27883 * Create a schema + parser if necessary.
27884 */
27885 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027886 xmlSchemaParserCtxtPtr pctxt;
Daniel Veillarddee23482008-04-11 12:58:43 +000027887
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027888 vctxt->xsiAssemble = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000027889 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027890 * If not schema was given then we will create a schema
27891 * dynamically using XSI schema locations.
27892 *
27893 * Create the schema parser context.
27894 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027895 if ((vctxt->pctxt == NULL) &&
27896 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27897 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027898 pctxt = vctxt->pctxt;
27899 pctxt->xsiAssemble = 1;
27900 /*
27901 * Create the schema.
27902 */
27903 vctxt->schema = xmlSchemaNewSchema(pctxt);
27904 if (vctxt->schema == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000027905 return (-1);
27906 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027907 * Create the schema construction context.
27908 */
27909 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27910 if (pctxt->constructor == NULL)
27911 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027912 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027913 /*
27914 * Take ownership of the constructor to be able to free it.
27915 */
27916 pctxt->ownsConstructor = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000027917 }
27918 /*
27919 * Augment the IDC definitions for the main schema and all imported ones
Daniel Veillard3888f472007-08-23 09:29:03 +000027920 * NOTE: main schema if the first in the imported list
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027921 */
Daniel Veillard3888f472007-08-23 09:29:03 +000027922 xmlHashScan(vctxt->schema->schemasImports,(xmlHashScanner)xmlSchemaAugmentImportedIDC, vctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000027923
Daniel Veillardf10ae122005-07-10 19:03:16 +000027924 return(0);
27925}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027926
Daniel Veillardf10ae122005-07-10 19:03:16 +000027927static void
27928xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027929 if (vctxt->xsiAssemble) {
27930 if (vctxt->schema != NULL) {
27931 xmlSchemaFree(vctxt->schema);
27932 vctxt->schema = NULL;
27933 }
27934 }
27935 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027936}
27937
27938static int
27939xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27940{
27941 int ret = 0;
27942
27943 if (xmlSchemaPreRun(vctxt) < 0)
27944 return(-1);
27945
27946 if (vctxt->doc != NULL) {
27947 /*
27948 * Tree validation.
27949 */
27950 ret = xmlSchemaVDocWalk(vctxt);
27951#ifdef LIBXML_READER_ENABLED
27952 } else if (vctxt->reader != NULL) {
27953 /*
27954 * XML Reader validation.
27955 */
27956#ifdef XML_SCHEMA_READER_ENABLED
27957 ret = xmlSchemaVReaderWalk(vctxt);
27958#endif
27959#endif
27960 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27961 /*
27962 * SAX validation.
27963 */
27964 ret = xmlParseDocument(vctxt->parserCtxt);
27965 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027966 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027967 "no instance to validate");
27968 ret = -1;
27969 }
27970
27971 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027972 if (ret == 0)
27973 ret = vctxt->err;
27974 return (ret);
27975}
27976
27977/**
27978 * xmlSchemaValidateOneElement:
27979 * @ctxt: a schema validation context
27980 * @elem: an element node
27981 *
27982 * Validate a branch of a tree, starting with the given @elem.
27983 *
27984 * Returns 0 if the element and its subtree is valid, a positive error
27985 * code number otherwise and -1 in case of an internal or API error.
27986 */
27987int
27988xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27989{
27990 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27991 return (-1);
27992
27993 if (ctxt->schema == NULL)
27994 return (-1);
27995
27996 ctxt->doc = elem->doc;
27997 ctxt->node = elem;
27998 ctxt->validationRoot = elem;
27999 return(xmlSchemaVStart(ctxt));
28000}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000028001
Daniel Veillard259f0df2004-08-18 09:13:18 +000028002/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028003 * xmlSchemaValidateDoc:
28004 * @ctxt: a schema validation context
28005 * @doc: a parsed document tree
28006 *
28007 * Validate a document tree in memory.
28008 *
28009 * Returns 0 if the document is schemas valid, a positive error code
28010 * number otherwise and -1 in case of internal or API error.
28011 */
28012int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028013xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
28014{
Daniel Veillard4255d502002-04-16 15:50:10 +000028015 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028016 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000028017
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028018 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028019 ctxt->node = xmlDocGetRootElement(doc);
28020 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028021 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028022 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
28023 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028024 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028025 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028026 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028027 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028028 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000028029}
28030
Daniel Veillardcdc82732005-07-08 15:04:06 +000028031
28032/************************************************************************
28033 * *
28034 * Function and data for SAX streaming API *
28035 * *
28036 ************************************************************************/
28037typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
28038typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
28039
28040struct _xmlSchemaSplitSAXData {
28041 xmlSAXHandlerPtr user_sax;
28042 void *user_data;
28043 xmlSchemaValidCtxtPtr ctxt;
28044 xmlSAXHandlerPtr schemas_sax;
28045};
28046
Daniel Veillard971771e2005-07-09 17:32:57 +000028047#define XML_SAX_PLUG_MAGIC 0xdc43ba21
28048
28049struct _xmlSchemaSAXPlug {
28050 unsigned int magic;
28051
28052 /* the original callbacks informations */
28053 xmlSAXHandlerPtr *user_sax_ptr;
28054 xmlSAXHandlerPtr user_sax;
28055 void **user_data_ptr;
28056 void *user_data;
28057
28058 /* the block plugged back and validation informations */
28059 xmlSAXHandler schemas_sax;
28060 xmlSchemaValidCtxtPtr ctxt;
28061};
28062
Daniel Veillardcdc82732005-07-08 15:04:06 +000028063/* All those functions just bounces to the user provided SAX handlers */
28064static void
28065internalSubsetSplit(void *ctx, const xmlChar *name,
28066 const xmlChar *ExternalID, const xmlChar *SystemID)
28067{
Daniel Veillard971771e2005-07-09 17:32:57 +000028068 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028069 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28070 (ctxt->user_sax->internalSubset != NULL))
28071 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
28072 SystemID);
28073}
28074
28075static int
28076isStandaloneSplit(void *ctx)
28077{
Daniel Veillard971771e2005-07-09 17:32:57 +000028078 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028079 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28080 (ctxt->user_sax->isStandalone != NULL))
28081 return(ctxt->user_sax->isStandalone(ctxt->user_data));
28082 return(0);
28083}
28084
28085static int
28086hasInternalSubsetSplit(void *ctx)
28087{
Daniel Veillard971771e2005-07-09 17:32:57 +000028088 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028089 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28090 (ctxt->user_sax->hasInternalSubset != NULL))
28091 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
28092 return(0);
28093}
28094
28095static int
28096hasExternalSubsetSplit(void *ctx)
28097{
Daniel Veillard971771e2005-07-09 17:32:57 +000028098 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028099 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28100 (ctxt->user_sax->hasExternalSubset != NULL))
28101 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
28102 return(0);
28103}
28104
28105static void
28106externalSubsetSplit(void *ctx, const xmlChar *name,
28107 const xmlChar *ExternalID, const xmlChar *SystemID)
28108{
Daniel Veillard971771e2005-07-09 17:32:57 +000028109 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028110 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
Daniel Veillardb3e45362008-03-26 13:39:31 +000028111 (ctxt->user_sax->externalSubset != NULL))
28112 ctxt->user_sax->externalSubset(ctxt->user_data, name, ExternalID,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028113 SystemID);
28114}
28115
28116static xmlParserInputPtr
28117resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
28118{
Daniel Veillard971771e2005-07-09 17:32:57 +000028119 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028120 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28121 (ctxt->user_sax->resolveEntity != NULL))
28122 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
28123 systemId));
28124 return(NULL);
28125}
28126
28127static xmlEntityPtr
28128getEntitySplit(void *ctx, const xmlChar *name)
28129{
Daniel Veillard971771e2005-07-09 17:32:57 +000028130 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028131 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28132 (ctxt->user_sax->getEntity != NULL))
28133 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
28134 return(NULL);
28135}
28136
28137static xmlEntityPtr
28138getParameterEntitySplit(void *ctx, const xmlChar *name)
28139{
Daniel Veillard971771e2005-07-09 17:32:57 +000028140 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028141 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28142 (ctxt->user_sax->getParameterEntity != NULL))
28143 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
28144 return(NULL);
28145}
28146
28147
28148static void
28149entityDeclSplit(void *ctx, const xmlChar *name, int type,
28150 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
28151{
Daniel Veillard971771e2005-07-09 17:32:57 +000028152 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028153 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28154 (ctxt->user_sax->entityDecl != NULL))
28155 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
28156 systemId, content);
28157}
28158
28159static void
28160attributeDeclSplit(void *ctx, const xmlChar * elem,
28161 const xmlChar * name, int type, int def,
28162 const xmlChar * defaultValue, xmlEnumerationPtr tree)
28163{
Daniel Veillard971771e2005-07-09 17:32:57 +000028164 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028165 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28166 (ctxt->user_sax->attributeDecl != NULL)) {
28167 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
28168 def, defaultValue, tree);
28169 } else {
28170 xmlFreeEnumeration(tree);
28171 }
28172}
28173
28174static void
28175elementDeclSplit(void *ctx, const xmlChar *name, int type,
28176 xmlElementContentPtr content)
28177{
Daniel Veillard971771e2005-07-09 17:32:57 +000028178 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028179 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28180 (ctxt->user_sax->elementDecl != NULL))
28181 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
28182}
28183
28184static void
28185notationDeclSplit(void *ctx, const xmlChar *name,
28186 const xmlChar *publicId, const xmlChar *systemId)
28187{
Daniel Veillard971771e2005-07-09 17:32:57 +000028188 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028189 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28190 (ctxt->user_sax->notationDecl != NULL))
28191 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
28192 systemId);
28193}
28194
28195static void
28196unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
28197 const xmlChar *publicId, const xmlChar *systemId,
28198 const xmlChar *notationName)
28199{
Daniel Veillard971771e2005-07-09 17:32:57 +000028200 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028201 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28202 (ctxt->user_sax->unparsedEntityDecl != NULL))
28203 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
28204 systemId, notationName);
28205}
28206
28207static void
28208setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
28209{
Daniel Veillard971771e2005-07-09 17:32:57 +000028210 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028211 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28212 (ctxt->user_sax->setDocumentLocator != NULL))
28213 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
28214}
28215
28216static void
28217startDocumentSplit(void *ctx)
28218{
Daniel Veillard971771e2005-07-09 17:32:57 +000028219 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028220 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28221 (ctxt->user_sax->startDocument != NULL))
28222 ctxt->user_sax->startDocument(ctxt->user_data);
28223}
28224
28225static void
28226endDocumentSplit(void *ctx)
28227{
Daniel Veillard971771e2005-07-09 17:32:57 +000028228 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028229 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28230 (ctxt->user_sax->endDocument != NULL))
28231 ctxt->user_sax->endDocument(ctxt->user_data);
28232}
28233
28234static void
28235processingInstructionSplit(void *ctx, const xmlChar *target,
28236 const xmlChar *data)
28237{
Daniel Veillard971771e2005-07-09 17:32:57 +000028238 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028239 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28240 (ctxt->user_sax->processingInstruction != NULL))
28241 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
28242}
28243
28244static void
28245commentSplit(void *ctx, const xmlChar *value)
28246{
Daniel Veillard971771e2005-07-09 17:32:57 +000028247 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028248 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28249 (ctxt->user_sax->comment != NULL))
28250 ctxt->user_sax->comment(ctxt->user_data, value);
28251}
28252
28253/*
28254 * Varargs error callbacks to the user application, harder ...
28255 */
28256
Daniel Veillardffa3c742005-07-21 13:24:09 +000028257static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028258warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028259 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028260 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28261 (ctxt->user_sax->warning != NULL)) {
28262 TODO
28263 }
28264}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028265static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028266errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028267 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028268 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28269 (ctxt->user_sax->error != NULL)) {
28270 TODO
28271 }
28272}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028273static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028274fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028275 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028276 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28277 (ctxt->user_sax->fatalError != NULL)) {
28278 TODO
28279 }
28280}
28281
28282/*
28283 * Those are function where both the user handler and the schemas handler
28284 * need to be called.
28285 */
28286static void
28287charactersSplit(void *ctx, const xmlChar *ch, int len)
28288{
Daniel Veillard971771e2005-07-09 17:32:57 +000028289 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028290 if (ctxt == NULL)
28291 return;
28292 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
28293 ctxt->user_sax->characters(ctxt->user_data, ch, len);
28294 if (ctxt->ctxt != NULL)
28295 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28296}
28297
28298static void
28299ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
28300{
Daniel Veillard971771e2005-07-09 17:32:57 +000028301 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028302 if (ctxt == NULL)
28303 return;
28304 if ((ctxt->user_sax != NULL) &&
28305 (ctxt->user_sax->ignorableWhitespace != NULL))
28306 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
28307 if (ctxt->ctxt != NULL)
28308 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28309}
28310
28311static void
28312cdataBlockSplit(void *ctx, const xmlChar *value, int len)
28313{
Daniel Veillard971771e2005-07-09 17:32:57 +000028314 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028315 if (ctxt == NULL)
28316 return;
28317 if ((ctxt->user_sax != NULL) &&
Daniel Veillard7d0ca5e2008-02-05 08:43:52 +000028318 (ctxt->user_sax->cdataBlock != NULL))
28319 ctxt->user_sax->cdataBlock(ctxt->user_data, value, len);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028320 if (ctxt->ctxt != NULL)
28321 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
28322}
28323
28324static void
28325referenceSplit(void *ctx, const xmlChar *name)
28326{
Daniel Veillard971771e2005-07-09 17:32:57 +000028327 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillard14b56432006-03-09 18:41:40 +000028328 if (ctxt == NULL)
28329 return;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028330 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28331 (ctxt->user_sax->reference != NULL))
28332 ctxt->user_sax->reference(ctxt->user_data, name);
28333 if (ctxt->ctxt != NULL)
28334 xmlSchemaSAXHandleReference(ctxt->user_data, name);
28335}
28336
28337static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028338startElementNsSplit(void *ctx, const xmlChar * localname,
28339 const xmlChar * prefix, const xmlChar * URI,
28340 int nb_namespaces, const xmlChar ** namespaces,
28341 int nb_attributes, int nb_defaulted,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028342 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028343 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028344 if (ctxt == NULL)
28345 return;
28346 if ((ctxt->user_sax != NULL) &&
28347 (ctxt->user_sax->startElementNs != NULL))
28348 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
28349 URI, nb_namespaces, namespaces,
28350 nb_attributes, nb_defaulted,
28351 attributes);
28352 if (ctxt->ctxt != NULL)
28353 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
28354 URI, nb_namespaces, namespaces,
28355 nb_attributes, nb_defaulted,
28356 attributes);
28357}
28358
28359static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028360endElementNsSplit(void *ctx, const xmlChar * localname,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028361 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028362 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028363 if (ctxt == NULL)
28364 return;
28365 if ((ctxt->user_sax != NULL) &&
28366 (ctxt->user_sax->endElementNs != NULL))
28367 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
28368 if (ctxt->ctxt != NULL)
28369 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
28370}
28371
Daniel Veillard4255d502002-04-16 15:50:10 +000028372/**
Daniel Veillard971771e2005-07-09 17:32:57 +000028373 * xmlSchemaSAXPlug:
28374 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000028375 * @sax: a pointer to the original xmlSAXHandlerPtr
28376 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000028377 *
28378 * Plug a SAX based validation layer in a SAX parsing event flow.
28379 * The original @saxptr and @dataptr data are replaced by new pointers
28380 * but the calls to the original will be maintained.
28381 *
28382 * Returns a pointer to a data structure needed to unplug the validation layer
28383 * or NULL in case of errors.
28384 */
28385xmlSchemaSAXPlugPtr
28386xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28387 xmlSAXHandlerPtr *sax, void **user_data)
28388{
28389 xmlSchemaSAXPlugPtr ret;
28390 xmlSAXHandlerPtr old_sax;
28391
28392 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28393 return(NULL);
28394
28395 /*
28396 * We only allow to plug into SAX2 event streams
28397 */
28398 old_sax = *sax;
28399 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28400 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000028401 if ((old_sax != NULL) &&
Daniel Veillard971771e2005-07-09 17:32:57 +000028402 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28403 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28404 return(NULL);
28405
28406 /*
28407 * everything seems right allocate the local data needed for that layer
28408 */
28409 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28410 if (ret == NULL) {
28411 return(NULL);
28412 }
28413 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28414 ret->magic = XML_SAX_PLUG_MAGIC;
28415 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28416 ret->ctxt = ctxt;
28417 ret->user_sax_ptr = sax;
28418 ret->user_sax = old_sax;
Daniel Veillarddee23482008-04-11 12:58:43 +000028419 if (old_sax == NULL) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028420 /*
28421 * go direct, no need for the split block and functions.
28422 */
28423 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28424 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28425 /*
28426 * Note that we use the same text-function for both, to prevent
28427 * the parser from testing for ignorable whitespace.
28428 */
28429 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28430 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28431
28432 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28433 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28434
28435 ret->user_data = ctxt;
28436 *user_data = ctxt;
28437 } else {
28438 /*
28439 * for each callback unused by Schemas initialize it to the Split
Daniel Veillarddee23482008-04-11 12:58:43 +000028440 * routine only if non NULL in the user block, this can speed up
Daniel Veillard971771e2005-07-09 17:32:57 +000028441 * things at the SAX level.
28442 */
28443 if (old_sax->internalSubset != NULL)
28444 ret->schemas_sax.internalSubset = internalSubsetSplit;
28445 if (old_sax->isStandalone != NULL)
28446 ret->schemas_sax.isStandalone = isStandaloneSplit;
28447 if (old_sax->hasInternalSubset != NULL)
28448 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28449 if (old_sax->hasExternalSubset != NULL)
28450 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28451 if (old_sax->resolveEntity != NULL)
28452 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28453 if (old_sax->getEntity != NULL)
28454 ret->schemas_sax.getEntity = getEntitySplit;
28455 if (old_sax->entityDecl != NULL)
28456 ret->schemas_sax.entityDecl = entityDeclSplit;
28457 if (old_sax->notationDecl != NULL)
28458 ret->schemas_sax.notationDecl = notationDeclSplit;
28459 if (old_sax->attributeDecl != NULL)
28460 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28461 if (old_sax->elementDecl != NULL)
28462 ret->schemas_sax.elementDecl = elementDeclSplit;
28463 if (old_sax->unparsedEntityDecl != NULL)
28464 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28465 if (old_sax->setDocumentLocator != NULL)
28466 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28467 if (old_sax->startDocument != NULL)
28468 ret->schemas_sax.startDocument = startDocumentSplit;
28469 if (old_sax->endDocument != NULL)
28470 ret->schemas_sax.endDocument = endDocumentSplit;
28471 if (old_sax->processingInstruction != NULL)
28472 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28473 if (old_sax->comment != NULL)
28474 ret->schemas_sax.comment = commentSplit;
28475 if (old_sax->warning != NULL)
28476 ret->schemas_sax.warning = warningSplit;
28477 if (old_sax->error != NULL)
28478 ret->schemas_sax.error = errorSplit;
28479 if (old_sax->fatalError != NULL)
28480 ret->schemas_sax.fatalError = fatalErrorSplit;
28481 if (old_sax->getParameterEntity != NULL)
28482 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28483 if (old_sax->externalSubset != NULL)
28484 ret->schemas_sax.externalSubset = externalSubsetSplit;
28485
28486 /*
28487 * the 6 schemas callback have to go to the splitter functions
28488 * Note that we use the same text-function for ignorableWhitespace
28489 * if possible, to prevent the parser from testing for ignorable
28490 * whitespace.
28491 */
28492 ret->schemas_sax.characters = charactersSplit;
28493 if ((old_sax->ignorableWhitespace != NULL) &&
28494 (old_sax->ignorableWhitespace != old_sax->characters))
28495 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28496 else
28497 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28498 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28499 ret->schemas_sax.reference = referenceSplit;
28500 ret->schemas_sax.startElementNs = startElementNsSplit;
28501 ret->schemas_sax.endElementNs = endElementNsSplit;
28502
28503 ret->user_data_ptr = user_data;
28504 ret->user_data = *user_data;
28505 *user_data = ret;
28506 }
28507
28508 /*
28509 * plug the pointers back.
28510 */
28511 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028512 ctxt->sax = *sax;
28513 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28514 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028515 return(ret);
28516}
28517
28518/**
28519 * xmlSchemaSAXUnplug:
28520 * @plug: a data structure returned by xmlSchemaSAXPlug
28521 *
28522 * Unplug a SAX based validation layer in a SAX parsing event flow.
28523 * The original pointers used in the call are restored.
28524 *
28525 * Returns 0 in case of success and -1 in case of failure.
28526 */
28527int
28528xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28529{
28530 xmlSAXHandlerPtr *sax;
28531 void **user_data;
28532
28533 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28534 return(-1);
28535 plug->magic = 0;
28536
Daniel Veillardf10ae122005-07-10 19:03:16 +000028537 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028538 /* restore the data */
28539 sax = plug->user_sax_ptr;
28540 *sax = plug->user_sax;
28541 if (plug->user_sax != NULL) {
28542 user_data = plug->user_data_ptr;
28543 *user_data = plug->user_data;
28544 }
28545
28546 /* free and return */
28547 xmlFree(plug);
28548 return(0);
28549}
28550
28551/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028552 * xmlSchemaValidateStream:
28553 * @ctxt: a schema validation context
28554 * @input: the input to use for reading the data
28555 * @enc: an optional encoding information
28556 * @sax: a SAX handler for the resulting events
28557 * @user_data: the context to provide to the SAX handler.
28558 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028559 * Validate an input based on a flow of SAX event from the parser
28560 * and forward the events to the @sax handler with the provided @user_data
28561 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028562 *
28563 * Returns 0 if the document is schemas valid, a positive error code
28564 * number otherwise and -1 in case of internal or API error.
28565 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028566int
Daniel Veillard4255d502002-04-16 15:50:10 +000028567xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028568 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28569 xmlSAXHandlerPtr sax, void *user_data)
28570{
Daniel Veillard971771e2005-07-09 17:32:57 +000028571 xmlSchemaSAXPlugPtr plug = NULL;
28572 xmlSAXHandlerPtr old_sax = NULL;
28573 xmlParserCtxtPtr pctxt = NULL;
28574 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028575 int ret;
28576
Daniel Veillard4255d502002-04-16 15:50:10 +000028577 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028578 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028579
Daniel Veillardcdc82732005-07-08 15:04:06 +000028580 /*
28581 * prepare the parser
28582 */
28583 pctxt = xmlNewParserCtxt();
28584 if (pctxt == NULL)
28585 return (-1);
28586 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028587 pctxt->sax = sax;
28588 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028589#if 0
28590 if (options)
28591 xmlCtxtUseOptions(pctxt, options);
28592#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000028593 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028594
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028595 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028596 if (inputStream == NULL) {
28597 ret = -1;
28598 goto done;
28599 }
28600 inputPush(pctxt, inputStream);
28601 ctxt->parserCtxt = pctxt;
28602 ctxt->input = input;
28603
28604 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028605 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028606 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028607 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28608 if (plug == NULL) {
28609 ret = -1;
28610 goto done;
28611 }
28612 ctxt->input = input;
28613 ctxt->enc = enc;
28614 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028615 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28616 ret = xmlSchemaVStart(ctxt);
28617
28618 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28619 ret = ctxt->parserCtxt->errNo;
28620 if (ret == 0)
28621 ret = 1;
Daniel Veillard33fe2882008-04-10 08:09:25 +000028622 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028623
28624done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028625 ctxt->parserCtxt = NULL;
28626 ctxt->sax = NULL;
28627 ctxt->input = NULL;
28628 if (plug != NULL) {
28629 xmlSchemaSAXUnplug(plug);
28630 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028631 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028632 if (pctxt != NULL) {
28633 pctxt->sax = old_sax;
28634 xmlFreeParserCtxt(pctxt);
28635 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028636 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028637}
28638
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028639/**
28640 * xmlSchemaValidateFile:
28641 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028642 * @filename: the URI of the instance
28643 * @options: a future set of options, currently unused
28644 *
28645 * Do a schemas validation of the given resource, it will use the
28646 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028647 *
28648 * Returns 0 if the document is valid, a positive error code
28649 * number otherwise and -1 in case of an internal or API error.
28650 */
28651int
28652xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028653 const char * filename,
28654 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028655{
28656 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028657 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028658
28659 if ((ctxt == NULL) || (filename == NULL))
28660 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000028661
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028662 input = xmlParserInputBufferCreateFilename(filename,
28663 XML_CHAR_ENCODING_NONE);
28664 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028665 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028666 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
Daniel Veillarddee23482008-04-11 12:58:43 +000028667 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028668 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028669}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028670
Daniel Veillarddee23482008-04-11 12:58:43 +000028671/**
28672 * xmlSchemaValidCtxtGetParserCtxt:
28673 * @ctxt: a schema validation context
28674 *
28675 * allow access to the parser context of the schema validation context
28676 *
28677 * Returns the parser context of the schema validation context or NULL
28678 * in case of error.
28679 */
28680xmlParserCtxtPtr
28681xmlSchemaValidCtxtGetParserCtxt(xmlSchemaValidCtxtPtr ctxt)
28682{
28683 if (ctxt == NULL)
28684 return(NULL);
28685 return (ctxt->parserCtxt);
28686}
28687
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028688#define bottom_xmlschemas
28689#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028690#endif /* LIBXML_SCHEMAS_ENABLED */