blob: 43e66fbda7810eb3b56620b4a253c024f255db1e [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. Buchcik4efd90d2005-06-09 10:32:53 +000083#define DEBUG_ATTR_VALIDATION 0
Daniel Veillardc0826a72004-08-10 14:17:33 +000084
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000085/* #define DEBUG_IDC */
86
87/* #define DEBUG_IDC_NODE_TABLE */
88
89#ifdef DEBUG_IDC
90 #ifndef DEBUG_IDC_NODE_TABLE
91 #define DEBUG_IDC_NODE_TABLE
92 #endif
93#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. Buchcik27820272005-10-14 14:33:48 +0000101#define ENABLE_IDC_NODE_TABLES
102
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)
133#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
134#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
135#define WXS_PTC_CAST (xmlSchemaParticlePtr)
136#define WXS_TYPE_CAST (xmlSchemaTypePtr)
137#define WXS_ELEM_CAST (xmlSchemaElementPtr)
138#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
139#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
140#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
141#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
142#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
143#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
144#define WXS_IDC_CAST (xmlSchemaIDCPtr)
145#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
146#define WXS_LIST_CAST (xmlSchemaItemListPtr)
Daniel Veillardc0826a72004-08-10 14:17:33 +0000147
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000148/*
149* Macros to query common properties of components.
150*/
151#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
Daniel Veillard4255d502002-04-16 15:50:10 +0000152
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000153#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
154/*
155* Macros for element declarations.
156*/
157#define WXS_ELEM_TYPEDEF(e) (e)->subtypes
Daniel Veillardc0826a72004-08-10 14:17:33 +0000158
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000159#define WXS_SUBST_HEAD(item) (item)->refDecl
160/*
161* Macros for attribute declarations.
162*/
163#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
164/*
165* Macros for attribute uses.
166*/
167#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000168
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000169#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000170
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000171#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
172
173#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
174/*
175* Macros for attribute groups.
176*/
177#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
178#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
179/*
180* Macros for particles.
181*/
182#define WXS_PARTICLE(p) WXS_PTC_CAST (p)
183
184#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
185
186#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
187/*
188* Macros for model groups definitions.
189*/
190#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
191/*
192* Macros for model groups.
193*/
194#define WXS_IS_MODEL_GROUP(i) \
195 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
196 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
197 ((i)->type == XML_SCHEMA_TYPE_ALL))
198
199#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
200/*
201* Macros for schema buckets.
202*/
203#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
204 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
205
206#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
207 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
208
209#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
210
211#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
212/*
213* Macros for complex/simple types.
214*/
215#define WXS_IS_ANYTYPE(i) \
216 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
217 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
218
219#define WXS_IS_COMPLEX(i) \
220 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
221 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
222
223#define WXS_IS_SIMPLE(item) \
224 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
225 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000226 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000227
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000228#define WXS_IS_ANY_SIMPLE_TYPE(i) \
229 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
230 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000231
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000232#define WXS_IS_RESTRICTION(t) \
233 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000234
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000235#define WXS_IS_EXTENSION(t) \
236 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
237
238#define WXS_IS_TYPE_NOT_FIXED(i) \
239 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
240 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
241
242#define WXS_IS_TYPE_NOT_FIXED_1(item) \
243 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000244 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000245/*
246* Macros for exclusively for complex types.
247*/
248#define WXS_HAS_COMPLEX_CONTENT(item) \
249 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
250 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000251 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000253#define WXS_HAS_SIMPLE_CONTENT(item) \
254 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000255 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
256
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000257#define WXS_HAS_MIXED_CONTENT(item) \
258 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000259
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000260#define WXS_EMPTIABLE(t) \
261 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000262
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000263#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000264
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000265#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000266
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000267#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
268/*
269* Macros for exclusively for simple types.
270*/
271#define WXS_LIST_ITEMTYPE(t) (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000272
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000273#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000274
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000275#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000277#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
278/*
279* Misc parser context macros.
280*/
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000281#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
282
283#define WXS_HAS_BUCKETS(ctx) \
284( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
285(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
286
287#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
288
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000289#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000290
291#define WXS_SCHEMA(ctx) (ctx)->schema
292
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000293#define WXS_ADD_LOCAL(ctx, item) \
294 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000296#define WXS_ADD_GLOBAL(ctx, item) \
297 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000298
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000299#define WXS_ADD_PENDING(ctx, item) \
300 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
301/*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000302* xmlSchemaItemList macros.
303*/
304#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
305/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000306* Misc macros.
307*/
308#define IS_SCHEMA(node, type) \
309 ((node != NULL) && (node->ns != NULL) && \
310 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
311 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000312
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000313#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000314
315/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000316* Since we put the default/fixed values into the dict, we can
317* use pointer comparison for those values.
318* REMOVED: (xmlStrEqual((v1), (v2)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000319*/
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000320#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000321
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000322#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000323
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000324#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000326#define HFAILURE if (res == -1) goto exit_failure;
327
328#define HERROR if (res != 0) goto exit_error;
329
330#define HSTOP(ctx) if ((ctx)->stop) goto exit;
331/*
332* Some flags used for various schema constraints.
333*/
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000334#define SUBSET_RESTRICTION 1<<0
335#define SUBSET_EXTENSION 1<<1
336#define SUBSET_SUBSTITUTION 1<<2
337#define SUBSET_LIST 1<<3
338#define SUBSET_UNION 1<<4
339
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000340typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
341typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000342
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000343typedef struct _xmlSchemaItemList xmlSchemaItemList;
344typedef xmlSchemaItemList *xmlSchemaItemListPtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000345struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000346 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000347 int nbItems; /* used for dynamic addition of schemata */
348 int sizeItems; /* used for dynamic addition of schemata */
349};
350
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000351#define XML_SCHEMA_CTXT_PARSER 1
352#define XML_SCHEMA_CTXT_VALIDATOR 2
353
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000354typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
355typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
356struct _xmlSchemaAbstractCtxt {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000357 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000358};
359
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000360typedef struct _xmlSchemaBucket xmlSchemaBucket;
361typedef xmlSchemaBucket *xmlSchemaBucketPtr;
362
363#define XML_SCHEMA_SCHEMA_MAIN 0
364#define XML_SCHEMA_SCHEMA_IMPORT 1
365#define XML_SCHEMA_SCHEMA_INCLUDE 2
366#define XML_SCHEMA_SCHEMA_REDEFINE 3
367
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000368/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000369 * xmlSchemaSchemaRelation:
370 *
371 * Used to create a graph of schema relationships.
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000372 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000373typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
374typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
375struct _xmlSchemaSchemaRelation {
376 xmlSchemaSchemaRelationPtr next;
377 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
378 const xmlChar *importNamespace;
379 xmlSchemaBucketPtr bucket;
380};
381
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000382#define XML_SCHEMA_BUCKET_MARKED 1<<0
383#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
384
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000385struct _xmlSchemaBucket {
386 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000387 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000388 const xmlChar *schemaLocation;
389 const xmlChar *origTargetNamespace;
390 const xmlChar *targetNamespace;
391 xmlDocPtr doc;
392 xmlSchemaSchemaRelationPtr relations;
393 int located;
394 int parsed;
395 int imported;
396 int preserveDoc;
397 xmlSchemaItemListPtr globals; /* Global components. */
398 xmlSchemaItemListPtr locals; /* Local components. */
399};
400
401/**
402 * xmlSchemaImport:
403 * (extends xmlSchemaBucket)
404 *
405 * Reflects a schema. Holds some information
406 * about the schema and its toplevel components. Duplicate
407 * toplevel components are not checked at this level.
408 */
409typedef struct _xmlSchemaImport xmlSchemaImport;
410typedef xmlSchemaImport *xmlSchemaImportPtr;
411struct _xmlSchemaImport {
412 int type; /* Main OR import OR include. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000413 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000414 const xmlChar *schemaLocation; /* The URI of the schema document. */
415 /* For chameleon includes, @origTargetNamespace will be NULL */
416 const xmlChar *origTargetNamespace;
417 /*
418 * For chameleon includes, @targetNamespace will be the
419 * targetNamespace of the including schema.
420 */
421 const xmlChar *targetNamespace;
422 xmlDocPtr doc; /* The schema node-tree. */
423 /* @relations will hold any included/imported/redefined schemas. */
424 xmlSchemaSchemaRelationPtr relations;
425 int located;
426 int parsed;
427 int imported;
428 int preserveDoc;
429 xmlSchemaItemListPtr globals;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000430 xmlSchemaItemListPtr locals;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000431 /* The imported schema. */
432 xmlSchemaPtr schema;
433};
434
435/*
436* (extends xmlSchemaBucket)
437*/
438typedef struct _xmlSchemaInclude xmlSchemaInclude;
439typedef xmlSchemaInclude *xmlSchemaIncludePtr;
440struct _xmlSchemaInclude {
441 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000442 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000443 const xmlChar *schemaLocation;
444 const xmlChar *origTargetNamespace;
445 const xmlChar *targetNamespace;
446 xmlDocPtr doc;
447 xmlSchemaSchemaRelationPtr relations;
448 int located;
449 int parsed;
450 int imported;
451 int preserveDoc;
452 xmlSchemaItemListPtr globals; /* Global components. */
453 xmlSchemaItemListPtr locals; /* Local components. */
454
455 /* The owning main or import schema bucket. */
456 xmlSchemaImportPtr ownerImport;
457};
458
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000459/**
460 * xmlSchemaBasicItem:
461 *
462 * The abstract base type for schema components.
463 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000464typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
465typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
466struct _xmlSchemaBasicItem {
467 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000468};
469
470/**
471 * xmlSchemaAnnotItem:
472 *
473 * The abstract base type for annotated schema components.
474 * (Extends xmlSchemaBasicItem)
475 */
476typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
477typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
478struct _xmlSchemaAnnotItem {
479 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000480 xmlSchemaAnnotPtr annot;
481};
482
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000483/**
484 * xmlSchemaTreeItem:
485 *
486 * The abstract base type for tree-like structured schema components.
487 * (Extends xmlSchemaAnnotItem)
488 */
489typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
490typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
491struct _xmlSchemaTreeItem {
492 xmlSchemaTypeType type;
493 xmlSchemaAnnotPtr annot;
494 xmlSchemaTreeItemPtr next;
495 xmlSchemaTreeItemPtr children;
496};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000497
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000498
499#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
500/**
501 * xmlSchemaAttributeUsePtr:
502 *
503 * The abstract base type for tree-like structured schema components.
504 * (Extends xmlSchemaTreeItem)
505 */
506typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
507typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
508struct _xmlSchemaAttributeUse {
509 xmlSchemaTypeType type;
510 xmlSchemaAnnotPtr annot;
511 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
512 /*
513 * The attr. decl. OR a QName-ref. to an attr. decl. OR
514 * a QName-ref. to an attribute group definition.
515 */
516 xmlSchemaAttributePtr attrDecl;
517
518 int flags;
519 xmlNodePtr node;
520 int occurs; /* required, optional */
521 const xmlChar * defValue;
522 xmlSchemaValPtr defVal;
523};
524
525/**
526 * xmlSchemaAttributeUseProhibPtr:
527 *
528 * A helper component to reflect attribute prohibitions.
529 * (Extends xmlSchemaBasicItem)
530 */
531typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
532typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
533struct _xmlSchemaAttributeUseProhib {
534 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
535 xmlNodePtr node;
536 const xmlChar *name;
537 const xmlChar *targetNamespace;
538 int isRef;
539};
540
541/**
542 * xmlSchemaRedef:
543 */
544typedef struct _xmlSchemaRedef xmlSchemaRedef;
545typedef xmlSchemaRedef *xmlSchemaRedefPtr;
546struct _xmlSchemaRedef {
547 xmlSchemaRedefPtr next;
548 xmlSchemaBasicItemPtr item; /* The redefining component. */
549 xmlSchemaBasicItemPtr reference; /* The referencing component. */
550 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
551 const xmlChar *refName; /* The name of the to-be-redefined component. */
552 const xmlChar *refTargetNs; /* The target namespace of the
553 to-be-redefined comp. */
554 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
555};
556
557/**
558 * xmlSchemaConstructionCtxt:
559 */
560typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
561typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
562struct _xmlSchemaConstructionCtxt {
563 xmlSchemaPtr mainSchema; /* The main schema. */
564 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
565 xmlDictPtr dict;
566 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
567 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
568 xmlSchemaBucketPtr bucket; /* The current schema bucket */
569 xmlSchemaItemListPtr pending; /* All Components of all schemas that
570 need to be fixed. */
571 xmlHashTablePtr substGroups;
572 xmlSchemaRedefPtr redefs;
573 xmlSchemaRedefPtr lastRedef;
574};
575
576#define XML_SCHEMAS_PARSE_ERROR 1
577#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
578
579struct _xmlSchemaParserCtxt {
580 int type;
581 void *userData; /* user specific data block */
582 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
583 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
584 xmlSchemaValidError err;
585 int nberrors;
586 xmlStructuredErrorFunc serror;
587
588 xmlSchemaConstructionCtxtPtr constructor;
589 int ownsConstructor; /* TODO: Move this to parser *flags*. */
590
591 /* xmlSchemaPtr topschema; */
592 /* xmlHashTablePtr namespaces; */
593
594 xmlSchemaPtr schema; /* The main schema in use */
595 int counter;
596
597 const xmlChar *URL;
598 xmlDocPtr doc;
599 int preserve; /* Whether the doc should be freed */
600
601 const char *buffer;
602 int size;
603
604 /*
605 * Used to build complex element content models
606 */
607 xmlAutomataPtr am;
608 xmlAutomataStatePtr start;
609 xmlAutomataStatePtr end;
610 xmlAutomataStatePtr state;
611
612 xmlDictPtr dict; /* dictionnary for interned string names */
613 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
614 int options;
615 xmlSchemaValidCtxtPtr vctxt;
616 int isS4S;
617 int isRedefine;
618 int xsiAssemble;
619 int stop; /* If the parser should stop; i.e. a critical error. */
620 const xmlChar *targetNamespace;
621 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
622
623 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
624 int redefCounter; /* Used for redefinitions. */
625 xmlSchemaItemListPtr attrProhibs;
626};
627
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000628/**
629 * xmlSchemaQNameRef:
630 *
631 * A component reference item (not a schema component)
632 * (Extends xmlSchemaBasicItem)
633 */
634typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
635typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
636struct _xmlSchemaQNameRef {
637 xmlSchemaTypeType type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000638 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000639 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000640 const xmlChar *name;
641 const xmlChar *targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000642 xmlNodePtr node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000643};
644
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000645/**
646 * xmlSchemaParticle:
647 *
648 * A particle component.
649 * (Extends xmlSchemaTreeItem)
650 */
651typedef struct _xmlSchemaParticle xmlSchemaParticle;
652typedef xmlSchemaParticle *xmlSchemaParticlePtr;
653struct _xmlSchemaParticle {
654 xmlSchemaTypeType type;
655 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000656 xmlSchemaTreeItemPtr next; /* next particle */
657 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
658 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
659 etc.) */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000660 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000661 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000662 xmlNodePtr node;
663};
664
665/**
666 * xmlSchemaModelGroup:
667 *
668 * A model group component.
669 * (Extends xmlSchemaTreeItem)
670 */
671typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
672typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
673struct _xmlSchemaModelGroup {
674 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
675 xmlSchemaAnnotPtr annot;
676 xmlSchemaTreeItemPtr next; /* not used */
677 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
678 xmlNodePtr node;
679};
680
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000681#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000682#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000683/**
684 * xmlSchemaModelGroupDef:
685 *
686 * A model group definition component.
687 * (Extends xmlSchemaTreeItem)
688 */
689typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
690typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
691struct _xmlSchemaModelGroupDef {
692 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
693 xmlSchemaAnnotPtr annot;
694 xmlSchemaTreeItemPtr next; /* not used */
695 xmlSchemaTreeItemPtr children; /* the "model group" */
696 const xmlChar *name;
697 const xmlChar *targetNamespace;
698 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000699 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000700};
701
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000702typedef struct _xmlSchemaIDC xmlSchemaIDC;
703typedef xmlSchemaIDC *xmlSchemaIDCPtr;
704
705/**
706 * xmlSchemaIDCSelect:
707 *
708 * The identity-constraint "field" and "selector" item, holding the
709 * XPath expression.
710 */
711typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
712typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000713struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000714 xmlSchemaIDCSelectPtr next;
715 xmlSchemaIDCPtr idc;
716 int index; /* an index position if significant for IDC key-sequences */
717 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000718 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000719};
720
721/**
722 * xmlSchemaIDC:
723 *
724 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000725 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000726 */
727
728struct _xmlSchemaIDC {
729 xmlSchemaTypeType type;
730 xmlSchemaAnnotPtr annot;
731 xmlSchemaIDCPtr next;
732 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000733 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000734 const xmlChar *targetNamespace;
735 xmlSchemaIDCSelectPtr selector;
736 xmlSchemaIDCSelectPtr fields;
737 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000738 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000739};
740
741/**
742 * xmlSchemaIDCAug:
743 *
744 * The augmented IDC information used for validation.
745 */
746typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
747typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
748struct _xmlSchemaIDCAug {
749 xmlSchemaIDCAugPtr next; /* next in a list */
750 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000751 int keyrefDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000752 tables need to be bubbled upwards */
753};
754
755/**
756 * xmlSchemaPSVIIDCKeySequence:
757 *
758 * The key sequence of a node table item.
759 */
760typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
761typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
762struct _xmlSchemaPSVIIDCKey {
763 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000764 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000765};
766
767/**
768 * xmlSchemaPSVIIDCNode:
769 *
770 * The node table item of a node table.
771 */
772typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
773typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
774struct _xmlSchemaPSVIIDCNode {
775 xmlNodePtr node;
776 xmlSchemaPSVIIDCKeyPtr *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000777 int nodeLine;
778 int nodeQNameID;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000779
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000780};
781
782/**
783 * xmlSchemaPSVIIDCBinding:
784 *
785 * The identity-constraint binding item of the [identity-constraint table].
786 */
787typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
788typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
789struct _xmlSchemaPSVIIDCBinding {
790 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
791 xmlSchemaIDCPtr definition; /* the IDC definition */
792 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
793 int nbNodes; /* number of entries in the node table */
794 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000795 xmlSchemaItemListPtr dupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000796};
797
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000798
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000799#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
800#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
801
802#define XPATH_STATE_OBJ_MATCHES -2
803#define XPATH_STATE_OBJ_BLOCKED -3
804
805typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
806typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
807
808/**
809 * xmlSchemaIDCStateObj:
810 *
811 * The state object used to evaluate XPath expressions.
812 */
813typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
814typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
815struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000816 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000817 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000818 int depth; /* depth of creation */
819 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000820 int nbHistory;
821 int sizeHistory;
822 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
823 matcher */
824 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000825 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000826};
827
828#define IDC_MATCHER 0
829
830/**
831 * xmlSchemaIDCMatcher:
832 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000833 * Used to evaluate IDC selectors (and fields).
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000834 */
835struct _xmlSchemaIDCMatcher {
836 int type;
837 int depth; /* the tree depth at creation time */
838 xmlSchemaIDCMatcherPtr next; /* next in the list */
839 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000840 int idcType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000841 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
842 elements */
843 int sizeKeySeqs;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000844 int targetDepth;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000845 xmlSchemaItemListPtr targets; /* list of target-node
846 (xmlSchemaPSVIIDCNodePtr) entries */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000847};
848
849/*
850* Element info flags.
851*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000852#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
853#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
854#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
855#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
856
857#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
858#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
859#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
860
861#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
862#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
863#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
864#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000865
866/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000867 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000868 *
869 * Holds information of an element node.
870 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000871struct _xmlSchemaNodeInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000872 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000873 xmlNodePtr node;
874 int nodeLine;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000875 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000876 const xmlChar *nsName;
877 const xmlChar *value;
878 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000879 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000880
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000881 int flags; /* combination of node info flags */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000882
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000883 int valNeeded;
884 int normVal;
885
886 xmlSchemaElementPtr decl; /* the element/attribute declaration */
887 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000888 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
889 for the scope element*/
890 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
891 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000892 xmlRegExecCtxtPtr regexCtxt;
893
894 const xmlChar **nsBindings; /* Namespace bindings on this element */
895 int nbNsBindings;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000896 int sizeNsBindings;
897
898 int hasKeyrefs;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000899};
900
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000901#define XML_SCHEMAS_ATTR_UNKNOWN 1
902#define XML_SCHEMAS_ATTR_ASSESSED 2
903#define XML_SCHEMAS_ATTR_PROHIBITED 3
904#define XML_SCHEMAS_ATTR_ERR_MISSING 4
905#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
906#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
907#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
908#define XML_SCHEMAS_ATTR_DEFAULT 8
909#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
910#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
911#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
912#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
913#define XML_SCHEMAS_ATTR_WILD_SKIP 13
914#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
915#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
916#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
917#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000918/*
919* @metaType values of xmlSchemaAttrInfo.
920*/
921#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
922#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
923#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
924#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
925#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000926
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000927typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
928typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
929struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000930 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000931 xmlNodePtr node;
932 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000933 const xmlChar *localName;
934 const xmlChar *nsName;
935 const xmlChar *value;
936 xmlSchemaValPtr val; /* the pre-computed value if any */
937 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
938 int flags; /* combination of node info flags */
939
940 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000941 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000942 int state;
943 int metaType;
944 const xmlChar *vcValue; /* the value constraint value */
945 xmlSchemaNodeInfoPtr parent;
946};
947
948
949#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000950/**
951 * xmlSchemaValidCtxt:
952 *
953 * A Schemas validation context
954 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000955struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000956 int type;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000957 void *userData; /* user specific data block */
958 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000959 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000960 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000961
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000962 xmlSchemaPtr schema; /* The schema in use */
963 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000964 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000965 xmlCharEncoding enc;
966 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000967 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000968 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000969
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000970 int err;
971 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000972
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000973 xmlNodePtr node;
974 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000975 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000976
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000977 xmlRegExecCtxtPtr regexp;
978 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000979
Daniel Veillardc0826a72004-08-10 14:17:33 +0000980 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000981 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000982 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000983 xmlSchemaParserCtxtPtr pctxt;
984 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000985
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000986 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000987 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000988 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000989 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000990
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000991 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
992
993 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
994 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000995
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000996 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
997 int nbIdcNodes;
998 int sizeIdcNodes;
999
1000 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1001 int nbIdcKeys;
1002 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001003
1004 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001005
1006 xmlDictPtr dict;
1007
Daniel Veillard39e5c892005-07-03 22:48:50 +00001008#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001009 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001010#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001011
1012 xmlSchemaAttrInfoPtr *attrInfos;
1013 int nbAttrInfos;
1014 int sizeAttrInfos;
1015
1016 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001017 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001018 int hasKeyrefs;
1019 int createIDCNodeTables;
1020 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001021};
1022
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001023/**
1024 * xmlSchemaSubstGroup:
1025 *
1026 *
1027 */
1028typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1029typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1030struct _xmlSchemaSubstGroup {
1031 xmlSchemaElementPtr head;
1032 xmlSchemaItemListPtr members;
1033};
1034
Daniel Veillard4255d502002-04-16 15:50:10 +00001035/************************************************************************
1036 * *
1037 * Some predeclarations *
1038 * *
1039 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001040
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001041static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1042 xmlSchemaPtr schema,
1043 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001044static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1045 xmlSchemaPtr schema,
1046 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001047static int
1048xmlSchemaTypeFixup(xmlSchemaTypePtr type,
1049 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001050static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001051xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1052static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001053xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1054 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001055static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001056xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1057 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001058static void
1059xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001060static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001061xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001062static xmlSchemaTreeItemPtr
1063xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1064 xmlNodePtr node, xmlSchemaTypeType type,
1065 int withParticle);
1066static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001067xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001068static xmlSchemaTypeLinkPtr
1069xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001070static void
1071xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1072 const char *funcName,
1073 const char *message);
1074static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001075xmlSchemaCheckCOSSTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
1076 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001077 xmlSchemaTypePtr baseType,
1078 int subset);
1079static void
1080xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001081 xmlSchemaParserCtxtPtr ctxt);
1082static void
1083xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001084static xmlSchemaQNameRefPtr
1085xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1086 xmlSchemaPtr schema,
1087 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001088
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001089/************************************************************************
1090 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001091 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001092 * *
1093 ************************************************************************/
1094
1095/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001096 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001097 * @type: the type of the schema item
1098 *
1099 * Returns the component name of a schema item.
1100 */
1101static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001102xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001103{
1104 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001105 case XML_SCHEMA_TYPE_BASIC:
1106 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001107 case XML_SCHEMA_TYPE_SIMPLE:
1108 return(BAD_CAST "simple type definition");
1109 case XML_SCHEMA_TYPE_COMPLEX:
1110 return(BAD_CAST "complex type definition");
1111 case XML_SCHEMA_TYPE_ELEMENT:
1112 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001113 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1114 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001115 case XML_SCHEMA_TYPE_ATTRIBUTE:
1116 return(BAD_CAST "attribute declaration");
1117 case XML_SCHEMA_TYPE_GROUP:
1118 return(BAD_CAST "model group definition");
1119 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1120 return(BAD_CAST "attribute group definition");
1121 case XML_SCHEMA_TYPE_NOTATION:
1122 return(BAD_CAST "notation declaration");
1123 case XML_SCHEMA_TYPE_SEQUENCE:
1124 return(BAD_CAST "model group (sequence)");
1125 case XML_SCHEMA_TYPE_CHOICE:
1126 return(BAD_CAST "model group (choice)");
1127 case XML_SCHEMA_TYPE_ALL:
1128 return(BAD_CAST "model group (all)");
1129 case XML_SCHEMA_TYPE_PARTICLE:
1130 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001131 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001132 return(BAD_CAST "unique identity-constraint");
1133 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001134 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001135 return(BAD_CAST "key identity-constraint");
1136 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001137 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001138 return(BAD_CAST "keyref identity-constraint");
1139 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001140 case XML_SCHEMA_TYPE_ANY:
1141 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001142 case XML_SCHEMA_EXTRA_QNAMEREF:
1143 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001144 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1145 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001146 default:
1147 return(BAD_CAST "Not a schema component");
1148 }
1149}
1150
1151/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001152 * xmlSchemaGetComponentTypeStr:
1153 * @type: the type of the schema item
1154 *
1155 * Returns the component name of a schema item.
1156 */
1157static const xmlChar *
1158xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1159{
1160 switch (item->type) {
1161 case XML_SCHEMA_TYPE_BASIC:
1162 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1163 return(BAD_CAST "complex type definition");
1164 else
1165 return(BAD_CAST "simple type definition");
1166 default:
1167 return(xmlSchemaItemTypeToStr(item->type));
1168 }
1169}
1170
1171/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001172 * xmlSchemaGetComponentNode:
1173 * @item: a schema component
1174 *
1175 * Returns node associated with the schema component.
1176 * NOTE that such a node need not be available; plus, a component's
1177 * node need not to reflect the component directly, since there is no
1178 * one-to-one relationship between the XML Schema representation and
1179 * the component representation.
1180 */
1181static xmlNodePtr
1182xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1183{
1184 switch (item->type) {
1185 case XML_SCHEMA_TYPE_ELEMENT:
1186 return (((xmlSchemaElementPtr) item)->node);
1187 case XML_SCHEMA_TYPE_ATTRIBUTE:
1188 return (((xmlSchemaAttributePtr) item)->node);
1189 case XML_SCHEMA_TYPE_COMPLEX:
1190 case XML_SCHEMA_TYPE_SIMPLE:
1191 return (((xmlSchemaTypePtr) item)->node);
1192 case XML_SCHEMA_TYPE_ANY:
1193 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1194 return (((xmlSchemaWildcardPtr) item)->node);
1195 case XML_SCHEMA_TYPE_PARTICLE:
1196 return (((xmlSchemaParticlePtr) item)->node);
1197 case XML_SCHEMA_TYPE_SEQUENCE:
1198 case XML_SCHEMA_TYPE_CHOICE:
1199 case XML_SCHEMA_TYPE_ALL:
1200 return (((xmlSchemaModelGroupPtr) item)->node);
1201 case XML_SCHEMA_TYPE_GROUP:
1202 return (((xmlSchemaModelGroupDefPtr) item)->node);
1203 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1204 return (((xmlSchemaAttributeGroupPtr) item)->node);
1205 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1206 case XML_SCHEMA_TYPE_IDC_KEY:
1207 case XML_SCHEMA_TYPE_IDC_KEYREF:
1208 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001209 case XML_SCHEMA_EXTRA_QNAMEREF:
1210 return(((xmlSchemaQNameRefPtr) item)->node);
1211 /* TODO: What to do with NOTATIONs?
1212 case XML_SCHEMA_TYPE_NOTATION:
1213 return (((xmlSchemaNotationPtr) item)->node);
1214 */
1215 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1216 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001217 default:
1218 return (NULL);
1219 }
1220}
1221
1222#if 0
1223/**
1224 * xmlSchemaGetNextComponent:
1225 * @item: a schema component
1226 *
1227 * Returns the next sibling of the schema component.
1228 */
1229static xmlSchemaBasicItemPtr
1230xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1231{
1232 switch (item->type) {
1233 case XML_SCHEMA_TYPE_ELEMENT:
1234 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1235 case XML_SCHEMA_TYPE_ATTRIBUTE:
1236 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1237 case XML_SCHEMA_TYPE_COMPLEX:
1238 case XML_SCHEMA_TYPE_SIMPLE:
1239 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1240 case XML_SCHEMA_TYPE_ANY:
1241 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1242 return (NULL);
1243 case XML_SCHEMA_TYPE_PARTICLE:
1244 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1245 case XML_SCHEMA_TYPE_SEQUENCE:
1246 case XML_SCHEMA_TYPE_CHOICE:
1247 case XML_SCHEMA_TYPE_ALL:
1248 return (NULL);
1249 case XML_SCHEMA_TYPE_GROUP:
1250 return (NULL);
1251 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1252 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1253 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1254 case XML_SCHEMA_TYPE_IDC_KEY:
1255 case XML_SCHEMA_TYPE_IDC_KEYREF:
1256 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1257 default:
1258 return (NULL);
1259 }
1260}
1261#endif
1262
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001263
1264/**
1265 * xmlSchemaFormatQName:
1266 * @buf: the string buffer
1267 * @namespaceName: the namespace name
1268 * @localName: the local name
1269 *
1270 * Returns the given QName in the format "{namespaceName}localName" or
1271 * just "localName" if @namespaceName is NULL.
1272 *
1273 * Returns the localName if @namespaceName is NULL, a formatted
1274 * string otherwise.
1275 */
1276static const xmlChar*
1277xmlSchemaFormatQName(xmlChar **buf,
1278 const xmlChar *namespaceName,
1279 const xmlChar *localName)
1280{
1281 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001282 if (namespaceName != NULL) {
1283 *buf = xmlStrdup(BAD_CAST "{");
1284 *buf = xmlStrcat(*buf, namespaceName);
1285 *buf = xmlStrcat(*buf, BAD_CAST "}");
1286 }
1287 if (localName != NULL) {
1288 if (namespaceName == NULL)
1289 return(localName);
1290 *buf = xmlStrcat(*buf, localName);
1291 } else {
1292 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
1293 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001294 return ((const xmlChar *) *buf);
1295}
1296
1297static const xmlChar*
1298xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1299{
1300 if (ns != NULL)
1301 return (xmlSchemaFormatQName(buf, ns->href, localName));
1302 else
1303 return (xmlSchemaFormatQName(buf, NULL, localName));
1304}
1305
1306static const xmlChar *
1307xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1308{
1309 switch (item->type) {
1310 case XML_SCHEMA_TYPE_ELEMENT:
1311 return (((xmlSchemaElementPtr) item)->name);
1312 case XML_SCHEMA_TYPE_ATTRIBUTE:
1313 return (((xmlSchemaAttributePtr) item)->name);
1314 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1315 return (((xmlSchemaAttributeGroupPtr) item)->name);
1316 case XML_SCHEMA_TYPE_BASIC:
1317 case XML_SCHEMA_TYPE_SIMPLE:
1318 case XML_SCHEMA_TYPE_COMPLEX:
1319 return (((xmlSchemaTypePtr) item)->name);
1320 case XML_SCHEMA_TYPE_GROUP:
1321 return (((xmlSchemaModelGroupDefPtr) item)->name);
1322 case XML_SCHEMA_TYPE_IDC_KEY:
1323 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1324 case XML_SCHEMA_TYPE_IDC_KEYREF:
1325 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001326 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1327 if (WXS_ATTRUSE_DECL(item) != NULL) {
1328 return(xmlSchemaGetComponentName(
1329 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1330 } else
1331 return(NULL);
1332 case XML_SCHEMA_EXTRA_QNAMEREF:
1333 return (((xmlSchemaQNameRefPtr) item)->name);
1334 case XML_SCHEMA_TYPE_NOTATION:
1335 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001336 default:
1337 /*
1338 * Other components cannot have names.
1339 */
1340 break;
1341 }
1342 return (NULL);
1343}
1344
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001345#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1346#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1347/*
1348static const xmlChar *
1349xmlSchemaGetQNameRefName(void *ref)
1350{
1351 return(((xmlSchemaQNameRefPtr) ref)->name);
1352}
1353
1354static const xmlChar *
1355xmlSchemaGetQNameRefTargetNs(void *ref)
1356{
1357 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1358}
1359*/
1360
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001361static const xmlChar *
1362xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1363{
1364 switch (item->type) {
1365 case XML_SCHEMA_TYPE_ELEMENT:
1366 return (((xmlSchemaElementPtr) item)->targetNamespace);
1367 case XML_SCHEMA_TYPE_ATTRIBUTE:
1368 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1369 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1370 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1371 case XML_SCHEMA_TYPE_BASIC:
1372 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1373 case XML_SCHEMA_TYPE_SIMPLE:
1374 case XML_SCHEMA_TYPE_COMPLEX:
1375 return (((xmlSchemaTypePtr) item)->targetNamespace);
1376 case XML_SCHEMA_TYPE_GROUP:
1377 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1378 case XML_SCHEMA_TYPE_IDC_KEY:
1379 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1380 case XML_SCHEMA_TYPE_IDC_KEYREF:
1381 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001382 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1383 if (WXS_ATTRUSE_DECL(item) != NULL) {
1384 return(xmlSchemaGetComponentTargetNs(
1385 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1386 }
1387 /* TODO: Will returning NULL break something? */
1388 break;
1389 case XML_SCHEMA_EXTRA_QNAMEREF:
1390 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1391 case XML_SCHEMA_TYPE_NOTATION:
1392 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001393 default:
1394 /*
1395 * Other components cannot have names.
1396 */
1397 break;
1398 }
1399 return (NULL);
1400}
1401
1402static const xmlChar*
1403xmlSchemaGetComponentQName(xmlChar **buf,
1404 void *item)
1405{
1406 return (xmlSchemaFormatQName(buf,
1407 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1408 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1409}
1410
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001411static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001412xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001413{
1414 xmlChar *str = NULL;
1415
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001416 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001417 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001418 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1419 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001420 *buf = xmlStrcat(*buf, BAD_CAST "'");
1421 FREE_AND_NULL(str);
1422 return(*buf);
1423}
1424
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001425static const xmlChar*
1426xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1427{
1428 return(xmlSchemaGetComponentDesignation(buf, idc));
1429}
1430
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001431/**
1432 * xmlSchemaWildcardPCToString:
1433 * @pc: the type of processContents
1434 *
1435 * Returns a string representation of the type of
1436 * processContents.
1437 */
1438static const xmlChar *
1439xmlSchemaWildcardPCToString(int pc)
1440{
1441 switch (pc) {
1442 case XML_SCHEMAS_ANY_SKIP:
1443 return (BAD_CAST "skip");
1444 case XML_SCHEMAS_ANY_LAX:
1445 return (BAD_CAST "lax");
1446 case XML_SCHEMAS_ANY_STRICT:
1447 return (BAD_CAST "strict");
1448 default:
1449 return (BAD_CAST "invalid process contents");
1450 }
1451}
1452
1453/**
1454 * xmlSchemaGetCanonValueWhtspExt:
1455 * @val: the precomputed value
1456 * @retValue: the returned value
1457 * @ws: the whitespace type of the value
1458 *
1459 * Get a the cononical representation of the value.
1460 * The caller has to free the returned retValue.
1461 *
1462 * Returns 0 if the value could be built and -1 in case of
1463 * API errors or if the value type is not supported yet.
1464 */
1465static int
1466xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1467 xmlSchemaWhitespaceValueType ws,
1468 xmlChar **retValue)
1469{
1470 int list;
1471 xmlSchemaValType valType;
1472 const xmlChar *value, *value2 = NULL;
1473
1474
1475 if ((retValue == NULL) || (val == NULL))
1476 return (-1);
1477 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1478 *retValue = NULL;
1479 do {
1480 value = NULL;
1481 valType = xmlSchemaGetValType(val);
1482 switch (valType) {
1483 case XML_SCHEMAS_STRING:
1484 case XML_SCHEMAS_NORMSTRING:
1485 case XML_SCHEMAS_ANYSIMPLETYPE:
1486 value = xmlSchemaValueGetAsString(val);
1487 if (value != NULL) {
1488 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1489 value2 = xmlSchemaCollapseString(value);
1490 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1491 value2 = xmlSchemaWhiteSpaceReplace(value);
1492 if (value2 != NULL)
1493 value = value2;
1494 }
1495 break;
1496 default:
1497 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1498 if (value2 != NULL)
1499 xmlFree((xmlChar *) value2);
1500 goto internal_error;
1501 }
1502 value = value2;
1503 }
1504 if (*retValue == NULL)
1505 if (value == NULL) {
1506 if (! list)
1507 *retValue = xmlStrdup(BAD_CAST "");
1508 } else
1509 *retValue = xmlStrdup(value);
1510 else if (value != NULL) {
1511 /* List. */
1512 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1513 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1514 }
1515 FREE_AND_NULL(value2)
1516 val = xmlSchemaValueGetNext(val);
1517 } while (val != NULL);
1518
1519 return (0);
1520internal_error:
1521 if (*retValue != NULL)
1522 xmlFree((xmlChar *) (*retValue));
1523 if (value2 != NULL)
1524 xmlFree((xmlChar *) value2);
1525 return (-1);
1526}
1527
1528/**
1529 * xmlSchemaFormatItemForReport:
1530 * @buf: the string buffer
1531 * @itemDes: the designation of the item
1532 * @itemName: the name of the item
1533 * @item: the item as an object
1534 * @itemNode: the node of the item
1535 * @local: the local name
1536 * @parsing: if the function is used during the parse
1537 *
1538 * Returns a representation of the given item used
1539 * for error reports.
1540 *
1541 * The following order is used to build the resulting
1542 * designation if the arguments are not NULL:
1543 * 1a. If itemDes not NULL -> itemDes
1544 * 1b. If (itemDes not NULL) and (itemName not NULL)
1545 * -> itemDes + itemName
1546 * 2. If the preceding was NULL and (item not NULL) -> item
1547 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1548 *
1549 * If the itemNode is an attribute node, the name of the attribute
1550 * will be appended to the result.
1551 *
1552 * Returns the formatted string and sets @buf to the resulting value.
1553 */
1554static xmlChar*
1555xmlSchemaFormatItemForReport(xmlChar **buf,
1556 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001557 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001558 xmlNodePtr itemNode)
1559{
1560 xmlChar *str = NULL;
1561 int named = 1;
1562
1563 if (*buf != NULL) {
1564 xmlFree(*buf);
1565 *buf = NULL;
1566 }
1567
1568 if (itemDes != NULL) {
1569 *buf = xmlStrdup(itemDes);
1570 } else if (item != NULL) {
1571 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001572 case XML_SCHEMA_TYPE_BASIC: {
1573 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1574
1575 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001576 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001577 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001578 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001579 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001580 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1581 else
1582 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001583 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001584 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001585 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001586 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001587 case XML_SCHEMA_TYPE_SIMPLE: {
1588 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1589
1590 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001591 *buf = xmlStrdup(BAD_CAST"");
1592 } else {
1593 *buf = xmlStrdup(BAD_CAST "local ");
1594 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001595 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001596 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001597 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001598 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001599 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001600 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1601 else
1602 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001603 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001604 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001605 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001606 *buf = xmlStrcat(*buf, BAD_CAST "'");
1607 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001608 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001609 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001610 case XML_SCHEMA_TYPE_COMPLEX: {
1611 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1612
1613 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001614 *buf = xmlStrdup(BAD_CAST "");
1615 else
1616 *buf = xmlStrdup(BAD_CAST "local ");
1617 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001618 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001619 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001620 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001621 *buf = xmlStrcat(*buf, BAD_CAST "'");
1622 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001623 }
1624 break;
1625 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1626 xmlSchemaAttributeUsePtr ause;
1627
1628 ause = WXS_ATTR_USE_CAST item;
1629 *buf = xmlStrdup(BAD_CAST "attribute use ");
1630 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1631 *buf = xmlStrcat(*buf, BAD_CAST "'");
1632 *buf = xmlStrcat(*buf,
1633 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1634 FREE_AND_NULL(str)
1635 *buf = xmlStrcat(*buf, BAD_CAST "'");
1636 } else {
1637 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1638 }
1639 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001640 break;
1641 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1642 xmlSchemaAttributePtr attr;
1643
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001644 attr = (xmlSchemaAttributePtr) item;
1645 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1646 *buf = xmlStrcat(*buf, BAD_CAST " '");
1647 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1648 attr->targetNamespace, attr->name));
1649 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001650 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001651 }
1652 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001653 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1654 xmlSchemaGetComponentDesignation(buf, item);
1655 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001656 case XML_SCHEMA_TYPE_ELEMENT: {
1657 xmlSchemaElementPtr elem;
1658
1659 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001660 *buf = xmlStrdup(BAD_CAST "element decl.");
1661 *buf = xmlStrcat(*buf, BAD_CAST " '");
1662 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1663 elem->targetNamespace, elem->name));
1664 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001665 }
1666 break;
1667 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1668 case XML_SCHEMA_TYPE_IDC_KEY:
1669 case XML_SCHEMA_TYPE_IDC_KEYREF:
1670 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1671 *buf = xmlStrdup(BAD_CAST "unique '");
1672 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1673 *buf = xmlStrdup(BAD_CAST "key '");
1674 else
1675 *buf = xmlStrdup(BAD_CAST "keyRef '");
1676 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1677 *buf = xmlStrcat(*buf, BAD_CAST "'");
1678 break;
1679 case XML_SCHEMA_TYPE_ANY:
1680 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1681 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1682 ((xmlSchemaWildcardPtr) item)->processContents));
1683 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1684 break;
1685 case XML_SCHEMA_FACET_MININCLUSIVE:
1686 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1687 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1688 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1689 case XML_SCHEMA_FACET_TOTALDIGITS:
1690 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1691 case XML_SCHEMA_FACET_PATTERN:
1692 case XML_SCHEMA_FACET_ENUMERATION:
1693 case XML_SCHEMA_FACET_WHITESPACE:
1694 case XML_SCHEMA_FACET_LENGTH:
1695 case XML_SCHEMA_FACET_MAXLENGTH:
1696 case XML_SCHEMA_FACET_MINLENGTH:
1697 *buf = xmlStrdup(BAD_CAST "facet '");
1698 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1699 *buf = xmlStrcat(*buf, BAD_CAST "'");
1700 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001701 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001702 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001703 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001704 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001705 *buf = xmlStrcat(*buf, BAD_CAST "'");
1706 FREE_AND_NULL(str)
1707 }
1708 break;
1709 case XML_SCHEMA_TYPE_SEQUENCE:
1710 case XML_SCHEMA_TYPE_CHOICE:
1711 case XML_SCHEMA_TYPE_ALL:
1712 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001713 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1714 break;
1715 case XML_SCHEMA_TYPE_NOTATION: {
1716 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1717 *buf = xmlStrcat(*buf, BAD_CAST " '");
1718 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1719 *buf = xmlStrcat(*buf, BAD_CAST "'");
1720 FREE_AND_NULL(str);
1721 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001722 default:
1723 named = 0;
1724 }
1725 } else
1726 named = 0;
1727
1728 if ((named == 0) && (itemNode != NULL)) {
1729 xmlNodePtr elem;
1730
1731 if (itemNode->type == XML_ATTRIBUTE_NODE)
1732 elem = itemNode->parent;
1733 else
1734 elem = itemNode;
1735 *buf = xmlStrdup(BAD_CAST "Element '");
1736 if (elem->ns != NULL) {
1737 *buf = xmlStrcat(*buf,
1738 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1739 FREE_AND_NULL(str)
1740 } else
1741 *buf = xmlStrcat(*buf, elem->name);
1742 *buf = xmlStrcat(*buf, BAD_CAST "'");
1743
1744 }
1745 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1746 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1747 if (itemNode->ns != NULL) {
1748 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1749 itemNode->ns->href, itemNode->name));
1750 FREE_AND_NULL(str)
1751 } else
1752 *buf = xmlStrcat(*buf, itemNode->name);
1753 *buf = xmlStrcat(*buf, BAD_CAST "'");
1754 }
1755 FREE_AND_NULL(str)
1756
1757 return (*buf);
1758}
1759
1760/**
1761 * xmlSchemaFormatFacetEnumSet:
1762 * @buf: the string buffer
1763 * @type: the type holding the enumeration facets
1764 *
1765 * Builds a string consisting of all enumeration elements.
1766 *
1767 * Returns a string of all enumeration elements.
1768 */
1769static const xmlChar *
1770xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1771 xmlChar **buf, xmlSchemaTypePtr type)
1772{
1773 xmlSchemaFacetPtr facet;
1774 xmlSchemaWhitespaceValueType ws;
1775 xmlChar *value = NULL;
1776 int res;
1777
1778 if (*buf != NULL)
1779 xmlFree(*buf);
1780 *buf = NULL;
1781
1782 do {
1783 /*
1784 * Use the whitespace type of the base type.
1785 */
1786 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1787 for (facet = type->facets; facet != NULL; facet = facet->next) {
1788 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1789 continue;
1790 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1791 ws, &value);
1792 if (res == -1) {
1793 xmlSchemaInternalErr(actxt,
1794 "xmlSchemaFormatFacetEnumSet",
1795 "compute the canonical lexical representation");
1796 if (*buf != NULL)
1797 xmlFree(*buf);
1798 *buf = NULL;
1799 return (NULL);
1800 }
1801 if (*buf == NULL)
1802 *buf = xmlStrdup(BAD_CAST "'");
1803 else
1804 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1805 *buf = xmlStrcat(*buf, BAD_CAST value);
1806 *buf = xmlStrcat(*buf, BAD_CAST "'");
1807 if (value != NULL) {
1808 xmlFree((xmlChar *)value);
1809 value = NULL;
1810 }
1811 }
1812 type = type->baseType;
1813 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1814
1815 return ((const xmlChar *) *buf);
1816}
1817
1818/************************************************************************
1819 * *
1820 * Error functions *
1821 * *
1822 ************************************************************************/
1823
1824#if 0
1825static void
1826xmlSchemaErrMemory(const char *msg)
1827{
1828 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1829 msg);
1830}
1831#endif
1832
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001833static void
1834xmlSchemaPSimpleErr(const char *msg)
1835{
1836 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1837 msg);
1838}
1839
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001840/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001841 * xmlSchemaPErrMemory:
1842 * @node: a context node
1843 * @extra: extra informations
1844 *
1845 * Handle an out of memory condition
1846 */
1847static void
1848xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1849 const char *extra, xmlNodePtr node)
1850{
1851 if (ctxt != NULL)
1852 ctxt->nberrors++;
1853 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1854 extra);
1855}
1856
1857/**
1858 * xmlSchemaPErr:
1859 * @ctxt: the parsing context
1860 * @node: the context node
1861 * @error: the error code
1862 * @msg: the error message
1863 * @str1: extra data
1864 * @str2: extra data
1865 *
1866 * Handle a parser error
1867 */
1868static void
1869xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1870 const char *msg, const xmlChar * str1, const xmlChar * str2)
1871{
1872 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001873 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001874 void *data = NULL;
1875
1876 if (ctxt != NULL) {
1877 ctxt->nberrors++;
1878 channel = ctxt->error;
1879 data = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001880 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001881 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001882 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001883 error, XML_ERR_ERROR, NULL, 0,
1884 (const char *) str1, (const char *) str2, NULL, 0, 0,
1885 msg, str1, str2);
1886}
1887
1888/**
1889 * xmlSchemaPErr2:
1890 * @ctxt: the parsing context
1891 * @node: the context node
1892 * @node: the current child
1893 * @error: the error code
1894 * @msg: the error message
1895 * @str1: extra data
1896 * @str2: extra data
1897 *
1898 * Handle a parser error
1899 */
1900static void
1901xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1902 xmlNodePtr child, int error,
1903 const char *msg, const xmlChar * str1, const xmlChar * str2)
1904{
1905 if (child != NULL)
1906 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1907 else
1908 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1909}
1910
Daniel Veillard01fa6152004-06-29 17:04:39 +00001911
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001912/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001913 * xmlSchemaPErrExt:
1914 * @ctxt: the parsing context
1915 * @node: the context node
1916 * @error: the error code
1917 * @strData1: extra data
1918 * @strData2: extra data
1919 * @strData3: extra data
1920 * @msg: the message
1921 * @str1: extra parameter for the message display
1922 * @str2: extra parameter for the message display
1923 * @str3: extra parameter for the message display
1924 * @str4: extra parameter for the message display
1925 * @str5: extra parameter for the message display
1926 *
1927 * Handle a parser error
1928 */
1929static void
1930xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1931 const xmlChar * strData1, const xmlChar * strData2,
1932 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1933 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1934 const xmlChar * str5)
1935{
1936
1937 xmlGenericErrorFunc channel = NULL;
1938 xmlStructuredErrorFunc schannel = NULL;
1939 void *data = NULL;
1940
1941 if (ctxt != NULL) {
1942 ctxt->nberrors++;
1943 channel = ctxt->error;
1944 data = ctxt->userData;
1945 schannel = ctxt->serror;
1946 }
1947 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1948 error, XML_ERR_ERROR, NULL, 0,
1949 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001950 (const char *) strData3, 0, 0, msg, str1, str2,
1951 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001952}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001953
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001954/************************************************************************
1955 * *
1956 * Allround error functions *
1957 * *
1958 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001959
1960/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001961 * xmlSchemaVTypeErrMemory:
1962 * @node: a context node
1963 * @extra: extra informations
1964 *
1965 * Handle an out of memory condition
1966 */
1967static void
1968xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1969 const char *extra, xmlNodePtr node)
1970{
1971 if (ctxt != NULL) {
1972 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001973 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001974 }
1975 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1976 extra);
1977}
1978
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001979static void
1980xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1981 const char *msg, const xmlChar *str)
1982{
1983 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1984 msg, (const char *) str);
1985}
1986
1987#define WXS_ERROR_TYPE_ERROR 1
1988#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001989/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001990 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001991 * @ctxt: the validation context
1992 * @node: the context node
1993 * @error: the error code
1994 * @msg: the error message
1995 * @str1: extra data
1996 * @str2: extra data
1997 * @str3: extra data
1998 *
1999 * Handle a validation error
2000 */
2001static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002002xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002003 xmlErrorLevel errorLevel,
2004 int error, xmlNodePtr node, int line, const char *msg,
2005 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002006 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002007{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002008 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002009 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002010 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002011
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002012 if (ctxt != NULL) {
2013 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2014 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002015 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002016 if (errorLevel != XML_ERR_WARNING) {
2017 vctxt->nberrors++;
2018 vctxt->err = error;
2019 channel = vctxt->error;
2020 } else {
2021 channel = vctxt->warning;
2022 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002023 schannel = vctxt->serror;
2024 data = vctxt->userData;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002025
2026 /*
2027 * Error node. If we specify a line number, then
2028 * do not channel any node to the error function.
2029 */
2030 if (line == 0) {
2031 if ((node == NULL) &&
2032 (vctxt->depth >= 0) &&
2033 (vctxt->inode != NULL)) {
2034 node = vctxt->inode->node;
2035 }
2036 /*
2037 * Get filename and line if no node-tree.
2038 */
2039 if ((node == NULL) &&
2040 (vctxt->parserCtxt != NULL) &&
2041 (vctxt->parserCtxt->input != NULL)) {
2042 file = vctxt->parserCtxt->input->filename;
2043 line = vctxt->parserCtxt->input->line;
2044 }
2045 } else {
2046 /*
2047 * Override the given node's (if any) position
2048 * and channel only the given line number.
2049 */
2050 node = NULL;
2051 /*
2052 * Get filename.
2053 */
2054 if (vctxt->doc != NULL)
2055 file = (const char *) vctxt->doc->URL;
2056 else if ((vctxt->parserCtxt != NULL) &&
2057 (vctxt->parserCtxt->input != NULL))
2058 file = vctxt->parserCtxt->input->filename;
2059 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002060 __xmlRaiseError(schannel, channel, data, ctxt,
2061 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002062 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002063 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002064 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002065
2066 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2067 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002068 if (errorLevel != XML_ERR_WARNING) {
2069 pctxt->nberrors++;
2070 pctxt->err = error;
2071 channel = pctxt->error;
2072 } else {
2073 channel = pctxt->warning;
2074 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002075 schannel = pctxt->serror;
2076 data = pctxt->userData;
2077 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002078 node, XML_FROM_SCHEMASP, error,
2079 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002080 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002081 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002082 } else {
2083 TODO
2084 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002085 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002086}
2087
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002088/**
2089 * xmlSchemaErr3:
2090 * @ctxt: the validation context
2091 * @node: the context node
2092 * @error: the error code
2093 * @msg: the error message
2094 * @str1: extra data
2095 * @str2: extra data
2096 * @str3: extra data
2097 *
2098 * Handle a validation error
2099 */
2100static void
2101xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2102 int error, xmlNodePtr node, const char *msg,
2103 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2104{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002105 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2106 msg, str1, str2, str3, NULL);
2107}
2108
2109static void
2110xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2111 int error, xmlNodePtr node, const char *msg,
2112 const xmlChar *str1, const xmlChar *str2,
2113 const xmlChar *str3, const xmlChar *str4)
2114{
2115 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2116 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002117}
2118
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002119static void
2120xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2121 int error, xmlNodePtr node, const char *msg,
2122 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002123{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002124 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002125}
2126
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002127static xmlChar *
2128xmlSchemaFormatNodeForError(xmlChar ** msg,
2129 xmlSchemaAbstractCtxtPtr actxt,
2130 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002131{
2132 xmlChar *str = NULL;
2133
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002134 *msg = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002135 if (node != NULL) {
2136 /*
2137 * Work on tree nodes.
2138 */
2139 if (node->type == XML_ATTRIBUTE_NODE) {
2140 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002141
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002142 *msg = xmlStrdup(BAD_CAST "Element '");
2143 if (elem->ns != NULL)
2144 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2145 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002146 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002147 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2148 NULL, elem->name));
2149 FREE_AND_NULL(str);
2150 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2151 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2152 } else {
2153 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002154 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002155 if (node->ns != NULL)
2156 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2157 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002158 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002159 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2160 NULL, node->name));
2161 FREE_AND_NULL(str);
2162 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2163 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2164 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2165 /*
2166 * Work on node infos.
2167 */
2168 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2169 xmlSchemaNodeInfoPtr ielem =
2170 vctxt->elemInfos[vctxt->depth];
2171
2172 *msg = xmlStrdup(BAD_CAST "Element '");
2173 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2174 ielem->nsName, ielem->localName));
2175 FREE_AND_NULL(str);
2176 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2177 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2178 } else {
2179 *msg = xmlStrdup(BAD_CAST "Element '");
2180 }
2181 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2182 vctxt->inode->nsName, vctxt->inode->localName));
2183 FREE_AND_NULL(str);
2184 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002185 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2186 /*
2187 * Hmm, no node while parsing?
2188 * Return an empty string, in case NULL will break something.
2189 */
2190 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002191 } else {
2192 TODO
2193 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002194 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002195 /*
2196 * VAL TODO: The output of the given schema component is currently
2197 * disabled.
2198 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002199#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002200 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2201 *msg = xmlStrcat(*msg, BAD_CAST " [");
2202 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2203 NULL, type, NULL, 0));
2204 FREE_AND_NULL(str)
2205 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002206 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002207#endif
2208 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002209}
2210
Daniel Veillardc0826a72004-08-10 14:17:33 +00002211static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002212xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002213 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002214 const char *message,
2215 const xmlChar *str1,
2216 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002217{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002218 xmlChar *msg = NULL;
2219
2220 msg = xmlStrdup(BAD_CAST "Internal error: ");
2221 msg = xmlStrcat(msg, BAD_CAST funcName);
2222 msg = xmlStrcat(msg, BAD_CAST ", ");
2223 msg = xmlStrcat(msg, BAD_CAST message);
2224 msg = xmlStrcat(msg, BAD_CAST ".\n");
2225
2226 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2227 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002228 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002229
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002230 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002231 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002232 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002233
2234 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002235}
2236
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002237static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002238xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2239 const char *funcName,
2240 const char *message)
2241{
2242 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2243}
2244
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002245#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002246static void
2247xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2248 const char *funcName,
2249 const char *message,
2250 const xmlChar *str1,
2251 const xmlChar *str2)
2252{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002253 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002254 str1, str2);
2255}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002256#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002257
2258static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002259xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2260 xmlParserErrors error,
2261 xmlNodePtr node,
2262 xmlSchemaBasicItemPtr item,
2263 const char *message,
2264 const xmlChar *str1, const xmlChar *str2,
2265 const xmlChar *str3, const xmlChar *str4)
2266{
2267 xmlChar *msg = NULL;
2268
2269 if ((node == NULL) && (item != NULL) &&
2270 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2271 node = WXS_ITEM_NODE(item);
2272 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2273 msg = xmlStrcat(msg, BAD_CAST ": ");
2274 } else
2275 xmlSchemaFormatNodeForError(&msg, actxt, node);
2276 msg = xmlStrcat(msg, (const xmlChar *) message);
2277 msg = xmlStrcat(msg, BAD_CAST ".\n");
2278 xmlSchemaErr4(actxt, error, node,
2279 (const char *) msg, str1, str2, str3, str4);
2280 FREE_AND_NULL(msg)
2281}
2282
2283static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002284xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2285 xmlParserErrors error,
2286 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002287 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002288 const char *message,
2289 const xmlChar *str1,
2290 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002291{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002292 xmlSchemaCustomErr4(actxt, error, node, item,
2293 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002294}
2295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002296
2297
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002298static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002299xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2300 xmlParserErrors error,
2301 xmlNodePtr node,
2302 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2303 const char *message,
2304 const xmlChar *str1,
2305 const xmlChar *str2,
2306 const xmlChar *str3)
2307{
2308 xmlChar *msg = NULL;
2309
2310 xmlSchemaFormatNodeForError(&msg, actxt, node);
2311 msg = xmlStrcat(msg, (const xmlChar *) message);
2312 msg = xmlStrcat(msg, BAD_CAST ".\n");
2313
2314 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002315 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2316 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002317
2318 FREE_AND_NULL(msg)
2319}
2320
2321
2322
2323static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002324xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2325 xmlParserErrors error,
2326 xmlSchemaPSVIIDCNodePtr idcNode,
2327 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2328 const char *message,
2329 const xmlChar *str1,
2330 const xmlChar *str2)
2331{
2332 xmlChar *msg = NULL, *qname = NULL;
2333
2334 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2335 msg = xmlStrcat(msg, (const xmlChar *) message);
2336 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002337 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002338 error, NULL, idcNode->nodeLine, (const char *) msg,
2339 xmlSchemaFormatQName(&qname,
2340 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2341 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002342 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002343 FREE_AND_NULL(qname);
2344 FREE_AND_NULL(msg);
2345}
2346
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002347static int
2348xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2349 xmlNodePtr node)
2350{
2351 if (node != NULL)
2352 return (node->type);
2353 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2354 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2355 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2356 return (-1);
2357}
2358
2359static int
2360xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2361{
2362 switch (item->type) {
2363 case XML_SCHEMA_TYPE_COMPLEX:
2364 case XML_SCHEMA_TYPE_SIMPLE:
2365 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2366 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002367 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002368 case XML_SCHEMA_TYPE_GROUP:
2369 return (1);
2370 case XML_SCHEMA_TYPE_ELEMENT:
2371 if ( ((xmlSchemaElementPtr) item)->flags &
2372 XML_SCHEMAS_ELEM_GLOBAL)
2373 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002374 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002375 case XML_SCHEMA_TYPE_ATTRIBUTE:
2376 if ( ((xmlSchemaAttributePtr) item)->flags &
2377 XML_SCHEMAS_ATTR_GLOBAL)
2378 return(1);
2379 break;
2380 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002381 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002382 return(1);
2383 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002384 return (0);
2385}
2386
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002387static void
2388xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2389 xmlParserErrors error,
2390 xmlNodePtr node,
2391 const xmlChar *value,
2392 xmlSchemaTypePtr type,
2393 int displayValue)
2394{
2395 xmlChar *msg = NULL;
2396
2397 xmlSchemaFormatNodeForError(&msg, actxt, node);
2398
2399 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2400 XML_ATTRIBUTE_NODE))
2401 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2402 else
2403 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2404 "value of ");
2405
2406 if (! xmlSchemaIsGlobalItem(type))
2407 msg = xmlStrcat(msg, BAD_CAST "the local ");
2408 else
2409 msg = xmlStrcat(msg, BAD_CAST "the ");
2410
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002411 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002412 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002413 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002414 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002415 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002416 msg = xmlStrcat(msg, BAD_CAST "union type");
2417
2418 if (xmlSchemaIsGlobalItem(type)) {
2419 xmlChar *str = NULL;
2420 msg = xmlStrcat(msg, BAD_CAST " '");
2421 if (type->builtInType != 0) {
2422 msg = xmlStrcat(msg, BAD_CAST "xs:");
2423 msg = xmlStrcat(msg, type->name);
2424 } else
2425 msg = xmlStrcat(msg,
2426 xmlSchemaFormatQName(&str,
2427 type->targetNamespace, type->name));
2428 msg = xmlStrcat(msg, BAD_CAST "'");
2429 FREE_AND_NULL(str);
2430 }
2431 msg = xmlStrcat(msg, BAD_CAST ".\n");
2432 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2433 XML_ATTRIBUTE_NODE))
2434 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2435 else
2436 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2437 FREE_AND_NULL(msg)
2438}
2439
Daniel Veillardc0826a72004-08-10 14:17:33 +00002440static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002441xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2442 xmlSchemaNodeInfoPtr ni,
2443 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002444{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002445 if (node != NULL) {
2446 if (node->ns != NULL)
2447 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2448 else
2449 return (xmlSchemaFormatQName(str, NULL, node->name));
2450 } else if (ni != NULL)
2451 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2452 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002453}
2454
Daniel Veillardc0826a72004-08-10 14:17:33 +00002455static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002456xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2457 xmlParserErrors error,
2458 xmlSchemaAttrInfoPtr ni,
2459 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002460{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002461 xmlChar *msg = NULL, *str = NULL;
2462
2463 xmlSchemaFormatNodeForError(&msg, actxt, node);
2464 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2465 xmlSchemaErr(actxt, error, node, (const char *) msg,
2466 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2467 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002468 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002469 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002470}
2471
Daniel Veillardc0826a72004-08-10 14:17:33 +00002472static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002473xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2474 xmlParserErrors error,
2475 xmlNodePtr node,
2476 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002477 const char *message,
2478 int nbval,
2479 int nbneg,
2480 xmlChar **values)
2481{
2482 xmlChar *str = NULL, *msg = NULL;
2483 xmlChar *localName, *nsName;
2484 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00002485 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002486
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002487 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002488 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002489 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002490 /*
2491 * Note that is does not make sense to report that we have a
2492 * wildcard here, since the wildcard might be unfolded into
2493 * multiple transitions.
2494 */
2495 if (nbval + nbneg > 0) {
2496 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002497 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002498 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002499 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002500 nsName = NULL;
2501
2502 for (i = 0; i < nbval + nbneg; i++) {
2503 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002504 if (cur == NULL)
2505 continue;
2506 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2507 (cur[3] == ' ')) {
2508 is_not = 1;
2509 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002510 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002511 } else {
2512 is_not = 0;
2513 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002514 /*
2515 * Get the local name.
2516 */
2517 localName = NULL;
2518
2519 end = cur;
2520 if (*end == '*') {
2521 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002522 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002523 } else {
2524 while ((*end != 0) && (*end != '|'))
2525 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002526 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002527 }
2528 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002529 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002530 /*
2531 * Skip "*|*" if they come with negated expressions, since
2532 * they represent the same negated wildcard.
2533 */
2534 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2535 /*
2536 * Get the namespace name.
2537 */
2538 cur = end;
2539 if (*end == '*') {
2540 nsName = xmlStrdup(BAD_CAST "{*}");
2541 } else {
2542 while (*end != 0)
2543 end++;
2544
2545 if (i >= nbval)
2546 nsName = xmlStrdup(BAD_CAST "{##other:");
2547 else
2548 nsName = xmlStrdup(BAD_CAST "{");
2549
2550 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2551 nsName = xmlStrcat(nsName, BAD_CAST "}");
2552 }
2553 str = xmlStrcat(str, BAD_CAST nsName);
2554 FREE_AND_NULL(nsName)
2555 } else {
2556 FREE_AND_NULL(localName);
2557 continue;
2558 }
2559 }
2560 str = xmlStrcat(str, BAD_CAST localName);
2561 FREE_AND_NULL(localName);
2562
2563 if (i < nbval + nbneg -1)
2564 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002565 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002566 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002567 msg = xmlStrcat(msg, BAD_CAST str);
2568 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002569 } else
2570 msg = xmlStrcat(msg, BAD_CAST "\n");
2571 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002572 xmlFree(msg);
2573}
2574
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002575static void
2576xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2577 xmlParserErrors error,
2578 xmlNodePtr node,
2579 const xmlChar *value,
2580 unsigned long length,
2581 xmlSchemaTypePtr type,
2582 xmlSchemaFacetPtr facet,
2583 const char *message,
2584 const xmlChar *str1,
2585 const xmlChar *str2)
2586{
2587 xmlChar *str = NULL, *msg = NULL;
2588 xmlSchemaTypeType facetType;
2589 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2590
2591 xmlSchemaFormatNodeForError(&msg, actxt, node);
2592 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2593 facetType = XML_SCHEMA_FACET_ENUMERATION;
2594 /*
2595 * If enumerations are validated, one must not expect the
2596 * facet to be given.
2597 */
2598 } else
2599 facetType = facet->type;
2600 msg = xmlStrcat(msg, BAD_CAST "[");
2601 msg = xmlStrcat(msg, BAD_CAST "facet '");
2602 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2603 msg = xmlStrcat(msg, BAD_CAST "'] ");
2604 if (message == NULL) {
2605 /*
2606 * Use a default message.
2607 */
2608 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2609 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2610 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2611
2612 char len[25], actLen[25];
2613
2614 /* FIXME, TODO: What is the max expected string length of the
2615 * this value?
2616 */
2617 if (nodeType == XML_ATTRIBUTE_NODE)
2618 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2619 else
2620 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2621
2622 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2623 snprintf(actLen, 24, "%lu", length);
2624
2625 if (facetType == XML_SCHEMA_FACET_LENGTH)
2626 msg = xmlStrcat(msg,
2627 BAD_CAST "this differs from the allowed length of '%s'.\n");
2628 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2629 msg = xmlStrcat(msg,
2630 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2631 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2632 msg = xmlStrcat(msg,
2633 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2634
2635 if (nodeType == XML_ATTRIBUTE_NODE)
2636 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2637 value, (const xmlChar *) actLen, (const xmlChar *) len);
2638 else
2639 xmlSchemaErr(actxt, error, node, (const char *) msg,
2640 (const xmlChar *) actLen, (const xmlChar *) len);
2641
2642 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2643 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2644 "of the set {%s}.\n");
2645 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2646 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2647 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2648 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2649 "by the pattern '%s'.\n");
2650 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2651 facet->value);
2652 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2653 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2654 "minimum value allowed ('%s').\n");
2655 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2656 facet->value);
2657 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2658 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2659 "maximum value allowed ('%s').\n");
2660 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2661 facet->value);
2662 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2663 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2664 "'%s'.\n");
2665 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2666 facet->value);
2667 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2668 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be more than "
2669 "'%s'.\n");
2670 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2671 facet->value);
2672 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2673 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2674 "digits than are allowed ('%s').\n");
2675 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2676 facet->value);
2677 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2678 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2679 "digits than are allowed ('%s').\n");
2680 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2681 facet->value);
2682 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2683 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2684 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2685 } else {
2686 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2687 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2688 }
2689 } else {
2690 msg = xmlStrcat(msg, (const xmlChar *) message);
2691 msg = xmlStrcat(msg, BAD_CAST ".\n");
2692 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2693 }
2694 FREE_AND_NULL(str)
2695 xmlFree(msg);
2696}
2697
2698#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002699 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002700
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002701#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002702
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002703#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2704#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002705
2706#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2707
2708
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002709/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002710 * xmlSchemaPMissingAttrErr:
2711 * @ctxt: the schema validation context
2712 * @ownerDes: the designation of the owner
2713 * @ownerName: the name of the owner
2714 * @ownerItem: the owner as a schema object
2715 * @ownerElem: the owner as an element node
2716 * @node: the parent element node of the missing attribute node
2717 * @type: the corresponding type of the attribute node
2718 *
2719 * Reports an illegal attribute.
2720 */
2721static void
2722xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002723 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002724 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002725 xmlNodePtr ownerElem,
2726 const char *name,
2727 const char *message)
2728{
2729 xmlChar *des = NULL;
2730
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002731 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2732
Daniel Veillardc0826a72004-08-10 14:17:33 +00002733 if (message != NULL)
2734 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002735 else
2736 xmlSchemaPErr(ctxt, ownerElem, error,
2737 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002738 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002739 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002740}
2741
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002742
Daniel Veillardc0826a72004-08-10 14:17:33 +00002743/**
2744 * xmlSchemaPResCompAttrErr:
2745 * @ctxt: the schema validation context
2746 * @error: the error code
2747 * @ownerDes: the designation of the owner
2748 * @ownerItem: the owner as a schema object
2749 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002750 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002751 * @refName: the referenced local name
2752 * @refURI: the referenced namespace URI
2753 * @message: optional message
2754 *
2755 * Used to report QName attribute values that failed to resolve
2756 * to schema components.
2757 */
2758static void
2759xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002760 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002761 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002762 xmlNodePtr ownerElem,
2763 const char *name,
2764 const xmlChar *refName,
2765 const xmlChar *refURI,
2766 xmlSchemaTypeType refType,
2767 const char *refTypeStr)
2768{
2769 xmlChar *des = NULL, *strA = NULL;
2770
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002771 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002772 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002773 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002774 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002775 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002776 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2777 "%s.\n", BAD_CAST des, BAD_CAST name,
2778 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002779 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002780 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002781 FREE_AND_NULL(strA)
2782}
2783
William M. Brack2f2a6632004-08-20 23:09:47 +00002784/**
2785 * xmlSchemaPCustomAttrErr:
2786 * @ctxt: the schema parser context
2787 * @error: the error code
2788 * @ownerDes: the designation of the owner
2789 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002790 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002791 *
2792 * Reports an illegal attribute during the parse.
2793 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002794static void
2795xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002796 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002797 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002798 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002799 xmlAttrPtr attr,
2800 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002801{
2802 xmlChar *des = NULL;
2803
2804 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002805 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002806 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002807 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002808 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002809 } else
2810 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002811 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002812 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002813 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2814 if (ownerDes == NULL)
2815 FREE_AND_NULL(des);
2816}
2817
2818/**
2819 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002820 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002821 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002822 * @ownerDes: the designation of the attribute's owner
2823 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002824 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002825 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002826 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002827 */
2828static void
2829xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002830 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002831 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002832 xmlAttrPtr attr)
2833{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002834 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002835
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002836 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2837 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2838 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2839 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2840 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002841 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002842 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002843}
2844
William M. Brack2f2a6632004-08-20 23:09:47 +00002845/**
2846 * xmlSchemaPCustomErr:
2847 * @ctxt: the schema parser context
2848 * @error: the error code
2849 * @itemDes: the designation of the schema item
2850 * @item: the schema item
2851 * @itemElem: the node of the schema item
2852 * @message: the error message
2853 * @str1: an optional param for the error message
2854 * @str2: an optional param for the error message
2855 * @str3: an optional param for the error message
2856 *
2857 * Reports an error during parsing.
2858 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002859static void
2860xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002861 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002862 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002863 xmlNodePtr itemElem,
2864 const char *message,
2865 const xmlChar *str1,
2866 const xmlChar *str2,
2867 const xmlChar *str3)
2868{
2869 xmlChar *des = NULL, *msg = NULL;
2870
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002871 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002872 msg = xmlStrdup(BAD_CAST "%s: ");
2873 msg = xmlStrcat(msg, (const xmlChar *) message);
2874 msg = xmlStrcat(msg, BAD_CAST ".\n");
2875 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002876 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002877 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002878 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002879 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002880 FREE_AND_NULL(msg);
2881}
2882
William M. Brack2f2a6632004-08-20 23:09:47 +00002883/**
2884 * xmlSchemaPCustomErr:
2885 * @ctxt: the schema parser context
2886 * @error: the error code
2887 * @itemDes: the designation of the schema item
2888 * @item: the schema item
2889 * @itemElem: the node of the schema item
2890 * @message: the error message
2891 * @str1: the optional param for the error message
2892 *
2893 * Reports an error during parsing.
2894 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002895static void
2896xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002897 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002898 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002899 xmlNodePtr itemElem,
2900 const char *message,
2901 const xmlChar *str1)
2902{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002903 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002904 str1, NULL, NULL);
2905}
2906
William M. Brack2f2a6632004-08-20 23:09:47 +00002907/**
2908 * xmlSchemaPAttrUseErr:
2909 * @ctxt: the schema parser context
2910 * @error: the error code
2911 * @itemDes: the designation of the schema type
2912 * @item: the schema type
2913 * @itemElem: the node of the schema type
2914 * @attr: the invalid schema attribute
2915 * @message: the error message
2916 * @str1: the optional param for the error message
2917 *
2918 * Reports an attribute use error during parsing.
2919 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002920static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002921xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002922 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002923 xmlNodePtr node,
2924 xmlSchemaBasicItemPtr ownerItem,
2925 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002926 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002927 const xmlChar *str1, const xmlChar *str2,
2928 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002929{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002930 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002931
2932 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2933 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002934 msg = xmlStrcat(msg,
2935 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002936 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002937 FREE_AND_NULL(str);
2938 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002939 msg = xmlStrcat(msg, (const xmlChar *) message);
2940 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002941 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2942 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002943 xmlFree(msg);
2944}
2945
William M. Brack2f2a6632004-08-20 23:09:47 +00002946/**
2947 * xmlSchemaPIllegalFacetAtomicErr:
2948 * @ctxt: the schema parser context
2949 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002950 * @type: the schema type
2951 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002952 * @facet: the illegal facet
2953 *
2954 * Reports an illegal facet for atomic simple types.
2955 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002956static void
2957xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002958 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002959 xmlSchemaTypePtr type,
2960 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002961 xmlSchemaFacetPtr facet)
2962{
2963 xmlChar *des = NULL, *strT = NULL;
2964
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002965 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2966 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002967 "%s: The facet '%s' is not allowed on types derived from the "
2968 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002969 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002970 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002971 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002972 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002973 FREE_AND_NULL(strT);
2974}
2975
William M. Brack2f2a6632004-08-20 23:09:47 +00002976/**
2977 * xmlSchemaPIllegalFacetListUnionErr:
2978 * @ctxt: the schema parser context
2979 * @error: the error code
2980 * @itemDes: the designation of the schema item involved
2981 * @item: the schema item involved
2982 * @facet: the illegal facet
2983 *
2984 * Reports an illegal facet for <list> and <union>.
2985 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002986static void
2987xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002988 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002989 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002990 xmlSchemaFacetPtr facet)
2991{
2992 xmlChar *des = NULL, *strT = NULL;
2993
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002994 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
2995 type->node);
2996 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002997 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002998 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002999 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003000 FREE_AND_NULL(strT);
3001}
3002
3003/**
3004 * xmlSchemaPMutualExclAttrErr:
3005 * @ctxt: the schema validation context
3006 * @error: the error code
3007 * @elemDes: the designation of the parent element node
3008 * @attr: the bad attribute node
3009 * @type: the corresponding type of the attribute node
3010 *
3011 * Reports an illegal attribute.
3012 */
3013static void
3014xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3015 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003016 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003017 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003018 const char *name1,
3019 const char *name2)
3020{
3021 xmlChar *des = NULL;
3022
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003023 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003024 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003025 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003026 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003027 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003028}
3029
3030/**
3031 * xmlSchemaPSimpleTypeErr:
3032 * @ctxt: the schema validation context
3033 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003034 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003035 * @ownerDes: the designation of the owner
3036 * @ownerItem: the schema object if existent
3037 * @node: the validated node
3038 * @value: the validated value
3039 *
3040 * Reports a simple type validation error.
3041 * TODO: Should this report the value of an element as well?
3042 */
3043static void
3044xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3045 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003046 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003047 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003048 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003049 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003050 const xmlChar *value,
3051 const char *message,
3052 const xmlChar *str1,
3053 const xmlChar *str2)
3054{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003055 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003056
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003057 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003058 if (message == NULL) {
3059 /*
3060 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003061 */
3062 if (type != NULL) {
3063 if (node->type == XML_ATTRIBUTE_NODE)
3064 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3065 else
3066 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3067 "valid value of ");
3068 if (! xmlSchemaIsGlobalItem(type))
3069 msg = xmlStrcat(msg, BAD_CAST "the local ");
3070 else
3071 msg = xmlStrcat(msg, BAD_CAST "the ");
3072
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003073 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003074 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003075 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003076 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003077 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003078 msg = xmlStrcat(msg, BAD_CAST "union type");
3079
3080 if (xmlSchemaIsGlobalItem(type)) {
3081 xmlChar *str = NULL;
3082 msg = xmlStrcat(msg, BAD_CAST " '");
3083 if (type->builtInType != 0) {
3084 msg = xmlStrcat(msg, BAD_CAST "xs:");
3085 msg = xmlStrcat(msg, type->name);
3086 } else
3087 msg = xmlStrcat(msg,
3088 xmlSchemaFormatQName(&str,
3089 type->targetNamespace, type->name));
3090 msg = xmlStrcat(msg, BAD_CAST "'.");
3091 FREE_AND_NULL(str);
3092 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003093 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003094 if (node->type == XML_ATTRIBUTE_NODE)
3095 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3096 else
3097 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3098 "valid.");
3099 }
3100 if (expected) {
3101 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3102 msg = xmlStrcat(msg, BAD_CAST expected);
3103 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3104 } else
3105 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003106 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003107 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3108 else
3109 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3110 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003111 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003112 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003113 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003114 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003115 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003116 /* Cleanup. */
3117 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003118}
3119
William M. Brack2f2a6632004-08-20 23:09:47 +00003120/**
3121 * xmlSchemaPContentErr:
3122 * @ctxt: the schema parser context
3123 * @error: the error code
3124 * @onwerDes: the designation of the holder of the content
3125 * @ownerItem: the owner item of the holder of the content
3126 * @ownerElem: the node of the holder of the content
3127 * @child: the invalid child node
3128 * @message: the optional error message
3129 * @content: the optional string describing the correct content
3130 *
3131 * Reports an error concerning the content of a schema element.
3132 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003133static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003134xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003135 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003136 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003137 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003138 xmlNodePtr child,
3139 const char *message,
3140 const char *content)
3141{
3142 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003143
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003144 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003145 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003146 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3147 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003148 BAD_CAST des, BAD_CAST message);
3149 else {
3150 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003151 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3152 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153 BAD_CAST des, BAD_CAST content);
3154 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003155 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3156 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003157 BAD_CAST des, NULL);
3158 }
3159 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003160 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003161}
3162
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003163/************************************************************************
3164 * *
3165 * Streamable error functions *
3166 * *
3167 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003168
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003169
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003170
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003171
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003172/************************************************************************
3173 * *
3174 * Validation helper functions *
3175 * *
3176 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003177
Daniel Veillardc0826a72004-08-10 14:17:33 +00003178
Daniel Veillard4255d502002-04-16 15:50:10 +00003179/************************************************************************
3180 * *
3181 * Allocation functions *
3182 * *
3183 ************************************************************************/
3184
3185/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003186 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003187 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003188 *
3189 * Allocate a new Schema structure.
3190 *
3191 * Returns the newly allocated structure or NULL in case or error
3192 */
3193static xmlSchemaPtr
3194xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3195{
3196 xmlSchemaPtr ret;
3197
3198 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3199 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003200 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003201 return (NULL);
3202 }
3203 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003204 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003205 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003206
3207 return (ret);
3208}
3209
3210/**
3211 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003212 *
3213 * Allocate a new Facet structure.
3214 *
3215 * Returns the newly allocated structure or NULL in case or error
3216 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003217xmlSchemaFacetPtr
3218xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003219{
3220 xmlSchemaFacetPtr ret;
3221
3222 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3223 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003224 return (NULL);
3225 }
3226 memset(ret, 0, sizeof(xmlSchemaFacet));
3227
3228 return (ret);
3229}
3230
3231/**
3232 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003233 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003234 * @node: a node
3235 *
3236 * Allocate a new annotation structure.
3237 *
3238 * Returns the newly allocated structure or NULL in case or error
3239 */
3240static xmlSchemaAnnotPtr
3241xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3242{
3243 xmlSchemaAnnotPtr ret;
3244
3245 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3246 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003247 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003248 return (NULL);
3249 }
3250 memset(ret, 0, sizeof(xmlSchemaAnnot));
3251 ret->content = node;
3252 return (ret);
3253}
3254
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003255static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003256xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003257{
3258 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003259
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003260 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3261 if (ret == NULL) {
3262 xmlSchemaPErrMemory(NULL,
3263 "allocating an item list structure", NULL);
3264 return (NULL);
3265 }
3266 memset(ret, 0, sizeof(xmlSchemaItemList));
3267 return (ret);
3268}
3269
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003270static void
3271xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3272{
3273 if (list->items != NULL) {
3274 xmlFree(list->items);
3275 list->items = NULL;
3276 }
3277 list->nbItems = 0;
3278 list->sizeItems = 0;
3279}
3280
3281static int
3282xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3283{
3284 if (list->items == NULL) {
3285 list->items = (void **) xmlMalloc(
3286 20 * sizeof(void *));
3287 if (list->items == NULL) {
3288 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3289 return(-1);
3290 }
3291 list->sizeItems = 20;
3292 } else if (list->sizeItems <= list->nbItems) {
3293 list->sizeItems *= 2;
3294 list->items = (void **) xmlRealloc(list->items,
3295 list->sizeItems * sizeof(void *));
3296 if (list->items == NULL) {
3297 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3298 list->sizeItems = 0;
3299 return(-1);
3300 }
3301 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003302 list->items[list->nbItems++] = item;
3303 return(0);
3304}
3305
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003306static int
3307xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3308 int initialSize,
3309 void *item)
3310{
3311 if (list->items == NULL) {
3312 if (initialSize <= 0)
3313 initialSize = 1;
3314 list->items = (void **) xmlMalloc(
3315 initialSize * sizeof(void *));
3316 if (list->items == NULL) {
3317 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3318 return(-1);
3319 }
3320 list->sizeItems = initialSize;
3321 } else if (list->sizeItems <= list->nbItems) {
3322 list->sizeItems *= 2;
3323 list->items = (void **) xmlRealloc(list->items,
3324 list->sizeItems * sizeof(void *));
3325 if (list->items == NULL) {
3326 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3327 list->sizeItems = 0;
3328 return(-1);
3329 }
3330 }
3331 list->items[list->nbItems++] = item;
3332 return(0);
3333}
3334
3335static int
3336xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3337{
3338 if (list->items == NULL) {
3339 list->items = (void **) xmlMalloc(
3340 20 * sizeof(void *));
3341 if (list->items == NULL) {
3342 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3343 return(-1);
3344 }
3345 list->sizeItems = 20;
3346 } else if (list->sizeItems <= list->nbItems) {
3347 list->sizeItems *= 2;
3348 list->items = (void **) xmlRealloc(list->items,
3349 list->sizeItems * sizeof(void *));
3350 if (list->items == NULL) {
3351 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3352 list->sizeItems = 0;
3353 return(-1);
3354 }
3355 }
3356 /*
3357 * Just append if the index is greater/equal than the item count.
3358 */
3359 if (idx >= list->nbItems) {
3360 list->items[list->nbItems++] = item;
3361 } else {
3362 int i;
3363 for (i = list->nbItems; i > idx; i--)
3364 list->items[i] = list->items[i-1];
3365 list->items[idx] = item;
3366 list->nbItems++;
3367 }
3368 return(0);
3369}
3370
3371#if 0 /* enable if ever needed */
3372static int
3373xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3374 int initialSize,
3375 void *item,
3376 int idx)
3377{
3378 if (list->items == NULL) {
3379 if (initialSize <= 0)
3380 initialSize = 1;
3381 list->items = (void **) xmlMalloc(
3382 initialSize * sizeof(void *));
3383 if (list->items == NULL) {
3384 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3385 return(-1);
3386 }
3387 list->sizeItems = initialSize;
3388 } else if (list->sizeItems <= list->nbItems) {
3389 list->sizeItems *= 2;
3390 list->items = (void **) xmlRealloc(list->items,
3391 list->sizeItems * sizeof(void *));
3392 if (list->items == NULL) {
3393 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3394 list->sizeItems = 0;
3395 return(-1);
3396 }
3397 }
3398 /*
3399 * Just append if the index is greater/equal than the item count.
3400 */
3401 if (idx >= list->nbItems) {
3402 list->items[list->nbItems++] = item;
3403 } else {
3404 int i;
3405 for (i = list->nbItems; i > idx; i--)
3406 list->items[i] = list->items[i-1];
3407 list->items[idx] = item;
3408 list->nbItems++;
3409 }
3410 return(0);
3411}
3412#endif
3413
3414static int
3415xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3416{
3417 int i;
3418 if ((list->items == NULL) || (idx >= list->nbItems)) {
3419 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3420 "index error.\n");
3421 return(-1);
3422 }
3423
3424 if (list->nbItems == 1) {
3425 /* TODO: Really free the list? */
3426 xmlFree(list->items);
3427 list->items = NULL;
3428 list->nbItems = 0;
3429 list->sizeItems = 0;
3430 } else if (list->nbItems -1 == idx) {
3431 list->nbItems--;
3432 } else {
3433 for (i = idx; i < list->nbItems -1; i++)
3434 list->items[i] = list->items[i+1];
3435 list->nbItems--;
3436 }
3437 return(0);
3438}
3439
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003440/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003441 * xmlSchemaItemListFree:
3442 * @annot: a schema type structure
3443 *
3444 * Deallocate a annotation structure
3445 */
3446static void
3447xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3448{
3449 if (list == NULL)
3450 return;
3451 if (list->items != NULL)
3452 xmlFree(list->items);
3453 xmlFree(list);
3454}
3455
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003456static void
3457xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003458{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003459 if (bucket == NULL)
3460 return;
3461 if (bucket->globals != NULL) {
3462 xmlSchemaComponentListFree(bucket->globals);
3463 xmlSchemaItemListFree(bucket->globals);
3464 }
3465 if (bucket->locals != NULL) {
3466 xmlSchemaComponentListFree(bucket->locals);
3467 xmlSchemaItemListFree(bucket->locals);
3468 }
3469 if (bucket->relations != NULL) {
3470 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3471 do {
3472 prev = cur;
3473 cur = cur->next;
3474 xmlFree(prev);
3475 } while (cur != NULL);
3476 }
3477 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3478 xmlFreeDoc(bucket->doc);
3479 }
3480 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003481 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3482 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003483 }
3484 xmlFree(bucket);
3485}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003486
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003487static xmlSchemaBucketPtr
3488xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003489 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003490{
3491 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003492 int size;
3493 xmlSchemaPtr mainSchema;
3494
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003495 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003496 PERROR_INT("xmlSchemaBucketCreate",
3497 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003498 return(NULL);
3499 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003500 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003501 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003502 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003503 size = sizeof(xmlSchemaInclude);
3504 else
3505 size = sizeof(xmlSchemaImport);
3506 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3507 if (ret == NULL) {
3508 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3509 return(NULL);
3510 }
3511 memset(ret, 0, size);
3512 ret->targetNamespace = targetNamespace;
3513 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003514 ret->globals = xmlSchemaItemListCreate();
3515 if (ret->globals == NULL) {
3516 xmlFree(ret);
3517 return(NULL);
3518 }
3519 ret->locals = xmlSchemaItemListCreate();
3520 if (ret->locals == NULL) {
3521 xmlFree(ret);
3522 return(NULL);
3523 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003524 /*
3525 * The following will assure that only the first bucket is marked as
3526 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3527 * For each following import buckets an xmlSchema will be created.
3528 */
3529 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003530 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003531 PERROR_INT("xmlSchemaBucketCreate",
3532 "first bucket but it's an include or redefine");
3533 xmlSchemaBucketFree(ret);
3534 return(NULL);
3535 }
3536 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3537 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3538 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003539 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3540 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003541 } else {
3542 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3543 PERROR_INT("xmlSchemaBucketCreate",
3544 "main bucket but it's not the first one");
3545 xmlSchemaBucketFree(ret);
3546 return(NULL);
3547 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3548 /*
3549 * Create a schema for imports.
3550 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003551 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3552 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003553 xmlSchemaBucketFree(ret);
3554 return(NULL);
3555 }
3556 }
3557 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003558 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003559 int res;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003560 /* Imports go into the "schemasImports" slot of the main *schema*. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003561 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003562 mainSchema->schemasImports = xmlHashCreateDict(5,
3563 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003564 if (mainSchema->schemasImports == NULL) {
3565 xmlSchemaBucketFree(ret);
3566 return(NULL);
3567 }
3568 }
3569 if (targetNamespace == NULL)
3570 res = xmlHashAddEntry(mainSchema->schemasImports,
3571 XML_SCHEMAS_NO_NAMESPACE, ret);
3572 else
3573 res = xmlHashAddEntry(mainSchema->schemasImports,
3574 targetNamespace, ret);
3575 if (res != 0) {
3576 PERROR_INT("xmlSchemaBucketCreate",
3577 "failed to add the schema bucket to the hash");
3578 xmlSchemaBucketFree(ret);
3579 return(NULL);
3580 }
3581 } else {
3582 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003583 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3584 WXS_INCBUCKET(ret)->ownerImport =
3585 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003586 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003587 WXS_INCBUCKET(ret)->ownerImport =
3588 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003589
3590 /* Includes got into the "includes" slot of the *main* schema. */
3591 if (mainSchema->includes == NULL) {
3592 mainSchema->includes = xmlSchemaItemListCreate();
3593 if (mainSchema->includes == NULL) {
3594 xmlSchemaBucketFree(ret);
3595 return(NULL);
3596 }
3597 }
3598 xmlSchemaItemListAdd(mainSchema->includes, ret);
3599 }
3600 /*
3601 * Add to list of all buckets; this is used for lookup
3602 * during schema construction time only.
3603 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003604 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003605 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003606 return(ret);
3607}
3608
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003609static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003610xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003611{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003612 if (*list == NULL) {
3613 *list = xmlSchemaItemListCreate();
3614 if (*list == NULL)
3615 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003616 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003617 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003618 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003619}
3620
3621/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003622 * xmlSchemaFreeAnnot:
3623 * @annot: a schema type structure
3624 *
3625 * Deallocate a annotation structure
3626 */
3627static void
3628xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3629{
3630 if (annot == NULL)
3631 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003632 if (annot->next == NULL) {
3633 xmlFree(annot);
3634 } else {
3635 xmlSchemaAnnotPtr prev;
3636
3637 do {
3638 prev = annot;
3639 annot = annot->next;
3640 xmlFree(prev);
3641 } while (annot != NULL);
3642 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003643}
3644
3645/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003646 * xmlSchemaFreeNotation:
3647 * @schema: a schema notation structure
3648 *
3649 * Deallocate a Schema Notation structure.
3650 */
3651static void
3652xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3653{
3654 if (nota == NULL)
3655 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003656 xmlFree(nota);
3657}
3658
3659/**
3660 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003661 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003662 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003663 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003664 */
3665static void
3666xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3667{
3668 if (attr == NULL)
3669 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003670 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003671 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003672 if (attr->defVal != NULL)
3673 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003674 xmlFree(attr);
3675}
3676
3677/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003678 * xmlSchemaFreeAttributeUse:
3679 * @use: an attribute use
3680 *
3681 * Deallocates an attribute use structure.
3682 */
3683static void
3684xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3685{
3686 if (use == NULL)
3687 return;
3688 if (use->annot != NULL)
3689 xmlSchemaFreeAnnot(use->annot);
3690 if (use->defVal != NULL)
3691 xmlSchemaFreeValue(use->defVal);
3692 xmlFree(use);
3693}
3694
3695/**
3696 * xmlSchemaFreeAttributeUseProhib:
3697 * @prohib: an attribute use prohibition
3698 *
3699 * Deallocates an attribute use structure.
3700 */
3701static void
3702xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3703{
3704 if (prohib == NULL)
3705 return;
3706 xmlFree(prohib);
3707}
3708
3709/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003710 * xmlSchemaFreeWildcardNsSet:
3711 * set: a schema wildcard namespace
3712 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003713 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003714 */
3715static void
3716xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3717{
3718 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003719
Daniel Veillard3646d642004-06-02 19:19:14 +00003720 while (set != NULL) {
3721 next = set->next;
3722 xmlFree(set);
3723 set = next;
3724 }
3725}
3726
3727/**
3728 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003729 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003730 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003731 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003732 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003733void
Daniel Veillard3646d642004-06-02 19:19:14 +00003734xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3735{
3736 if (wildcard == NULL)
3737 return;
3738 if (wildcard->annot != NULL)
3739 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003740 if (wildcard->nsSet != NULL)
3741 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3742 if (wildcard->negNsSet != NULL)
3743 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003744 xmlFree(wildcard);
3745}
3746
3747/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003748 * xmlSchemaFreeAttributeGroup:
3749 * @schema: a schema attribute group structure
3750 *
3751 * Deallocate a Schema Attribute Group structure.
3752 */
3753static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003754xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003755{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003756 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003757 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003758 if (attrGr->annot != NULL)
3759 xmlSchemaFreeAnnot(attrGr->annot);
3760 if (attrGr->attrUses != NULL)
3761 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3762 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003763}
3764
3765/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003766 * xmlSchemaFreeQNameRef:
3767 * @item: a QName reference structure
3768 *
3769 * Deallocatea a QName reference structure.
3770 */
3771static void
3772xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3773{
3774 xmlFree(item);
3775}
3776
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003777/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003778 * xmlSchemaFreeTypeLinkList:
3779 * @alink: a type link
3780 *
3781 * Deallocate a list of types.
3782 */
3783static void
3784xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3785{
3786 xmlSchemaTypeLinkPtr next;
3787
3788 while (link != NULL) {
3789 next = link->next;
3790 xmlFree(link);
3791 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003792 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003793}
3794
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003795static void
3796xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3797{
3798 xmlSchemaIDCStateObjPtr next;
3799 while (sto != NULL) {
3800 next = sto->next;
3801 if (sto->history != NULL)
3802 xmlFree(sto->history);
3803 if (sto->xpathCtxt != NULL)
3804 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3805 xmlFree(sto);
3806 sto = next;
3807 }
3808}
3809
3810/**
3811 * xmlSchemaFreeIDC:
3812 * @idc: a identity-constraint definition
3813 *
3814 * Deallocates an identity-constraint definition.
3815 */
3816static void
3817xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3818{
3819 xmlSchemaIDCSelectPtr cur, prev;
3820
3821 if (idcDef == NULL)
3822 return;
3823 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003824 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003825 /* Selector */
3826 if (idcDef->selector != NULL) {
3827 if (idcDef->selector->xpathComp != NULL)
3828 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3829 xmlFree(idcDef->selector);
3830 }
3831 /* Fields */
3832 if (idcDef->fields != NULL) {
3833 cur = idcDef->fields;
3834 do {
3835 prev = cur;
3836 cur = cur->next;
3837 if (prev->xpathComp != NULL)
3838 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003839 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003840 } while (cur != NULL);
3841 }
3842 xmlFree(idcDef);
3843}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003844
Daniel Veillard01fa6152004-06-29 17:04:39 +00003845/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003846 * xmlSchemaFreeElement:
3847 * @schema: a schema element structure
3848 *
3849 * Deallocate a Schema Element structure.
3850 */
3851static void
3852xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3853{
3854 if (elem == NULL)
3855 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003856 if (elem->annot != NULL)
3857 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003858 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003859 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003860 if (elem->defVal != NULL)
3861 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003862 xmlFree(elem);
3863}
3864
3865/**
3866 * xmlSchemaFreeFacet:
3867 * @facet: a schema facet structure
3868 *
3869 * Deallocate a Schema Facet structure.
3870 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003871void
Daniel Veillard4255d502002-04-16 15:50:10 +00003872xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3873{
3874 if (facet == NULL)
3875 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003876 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003877 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003878 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003879 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003880 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003881 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003882 xmlFree(facet);
3883}
3884
3885/**
3886 * xmlSchemaFreeType:
3887 * @type: a schema type structure
3888 *
3889 * Deallocate a Schema Type structure.
3890 */
3891void
3892xmlSchemaFreeType(xmlSchemaTypePtr type)
3893{
3894 if (type == NULL)
3895 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003896 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003897 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003898 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003899 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003900
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003901 facet = type->facets;
3902 while (facet != NULL) {
3903 next = facet->next;
3904 xmlSchemaFreeFacet(facet);
3905 facet = next;
3906 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003907 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003908 if (type->attrUses != NULL)
3909 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003910 if (type->memberTypes != NULL)
3911 xmlSchemaFreeTypeLinkList(type->memberTypes);
3912 if (type->facetSet != NULL) {
3913 xmlSchemaFacetLinkPtr next, link;
3914
3915 link = type->facetSet;
3916 do {
3917 next = link->next;
3918 xmlFree(link);
3919 link = next;
3920 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003921 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003922 if (type->contModel != NULL)
3923 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003924 xmlFree(type);
3925}
3926
3927/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003928 * xmlSchemaFreeModelGroupDef:
3929 * @item: a schema model group definition
3930 *
3931 * Deallocates a schema model group definition.
3932 */
3933static void
3934xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3935{
3936 if (item->annot != NULL)
3937 xmlSchemaFreeAnnot(item->annot);
3938 xmlFree(item);
3939}
3940
3941/**
3942 * xmlSchemaFreeModelGroup:
3943 * @item: a schema model group
3944 *
3945 * Deallocates a schema model group structure.
3946 */
3947static void
3948xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3949{
3950 if (item->annot != NULL)
3951 xmlSchemaFreeAnnot(item->annot);
3952 xmlFree(item);
3953}
3954
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003955static void
3956xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3957{
3958 if ((list == NULL) || (list->nbItems == 0))
3959 return;
3960 {
3961 xmlSchemaTreeItemPtr item;
3962 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3963 int i;
3964
3965 for (i = 0; i < list->nbItems; i++) {
3966 item = items[i];
3967 if (item == NULL)
3968 continue;
3969 switch (item->type) {
3970 case XML_SCHEMA_TYPE_SIMPLE:
3971 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003972 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003973 break;
3974 case XML_SCHEMA_TYPE_ATTRIBUTE:
3975 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003976 break;
3977 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
3978 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
3979 break;
3980 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
3981 xmlSchemaFreeAttributeUseProhib(
3982 (xmlSchemaAttributeUseProhibPtr) item);
3983 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003984 case XML_SCHEMA_TYPE_ELEMENT:
3985 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
3986 break;
3987 case XML_SCHEMA_TYPE_PARTICLE:
3988 if (item->annot != NULL)
3989 xmlSchemaFreeAnnot(item->annot);
3990 xmlFree(item);
3991 break;
3992 case XML_SCHEMA_TYPE_SEQUENCE:
3993 case XML_SCHEMA_TYPE_CHOICE:
3994 case XML_SCHEMA_TYPE_ALL:
3995 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
3996 break;
3997 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
3998 xmlSchemaFreeAttributeGroup(
3999 (xmlSchemaAttributeGroupPtr) item);
4000 break;
4001 case XML_SCHEMA_TYPE_GROUP:
4002 xmlSchemaFreeModelGroupDef(
4003 (xmlSchemaModelGroupDefPtr) item);
4004 break;
4005 case XML_SCHEMA_TYPE_ANY:
4006 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4007 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4008 break;
4009 case XML_SCHEMA_TYPE_IDC_KEY:
4010 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4011 case XML_SCHEMA_TYPE_IDC_KEYREF:
4012 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4013 break;
4014 case XML_SCHEMA_TYPE_NOTATION:
4015 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4016 break;
4017 case XML_SCHEMA_EXTRA_QNAMEREF:
4018 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4019 break;
4020 default: {
4021 /* TODO: This should never be hit. */
4022 xmlSchemaPSimpleInternalErr(NULL,
4023 "Internal error: xmlSchemaComponentListFree, "
4024 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004025 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004026 }
4027 break;
4028 }
4029 }
4030 list->nbItems = 0;
4031 }
4032}
4033
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004034/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004035 * xmlSchemaFree:
4036 * @schema: a schema structure
4037 *
4038 * Deallocate a Schema structure.
4039 */
4040void
4041xmlSchemaFree(xmlSchemaPtr schema)
4042{
4043 if (schema == NULL)
4044 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004045 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004046 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004047 TODO
4048 /*
4049 * Note that those slots are not responsible for freeing
4050 * schema components anymore; this will now be done by
4051 * the schema buckets.
4052 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004053 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004054 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004055 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004056 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004057 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004058 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004059 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004060 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004061 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004062 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004063 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004064 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004065 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004066 xmlHashFree(schema->idcDef, NULL);
4067
Daniel Veillard1d913862003-11-21 00:28:39 +00004068 if (schema->schemasImports != NULL)
4069 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004070 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004071 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004072 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4073 int i;
4074 for (i = 0; i < list->nbItems; i++) {
4075 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4076 }
4077 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004078 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004079 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004080 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004081 /* Never free the doc here, since this will be done by the buckets. */
4082
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004083 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004084 xmlFree(schema);
4085}
4086
4087/************************************************************************
4088 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004089 * Debug functions *
4090 * *
4091 ************************************************************************/
4092
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004093#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004094
Daniel Veillard4255d502002-04-16 15:50:10 +00004095/**
4096 * xmlSchemaElementDump:
4097 * @elem: an element
4098 * @output: the file output
4099 *
4100 * Dump the element
4101 */
4102static void
4103xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004104 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004105 const xmlChar * namespace ATTRIBUTE_UNUSED,
4106 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004107{
4108 if (elem == NULL)
4109 return;
4110
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004111
4112 fprintf(output, "Element");
4113 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4114 fprintf(output, " (global)");
4115 fprintf(output, ": %s ", elem->name);
4116 if (namespace != NULL)
4117 fprintf(output, "ns %s", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004118 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004119#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004120 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004121 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004122 if (elem->maxOccurs >= UNBOUNDED)
4123 fprintf(output, "max: unbounded\n");
4124 else if (elem->maxOccurs != 1)
4125 fprintf(output, "max: %d\n", elem->maxOccurs);
4126 else
4127 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004128 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004129#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004130 /*
4131 * Misc other properties.
4132 */
4133 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4134 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4135 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004136 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004137 fprintf(output, " props: ");
4138 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4139 fprintf(output, "[fixed] ");
4140 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4141 fprintf(output, "[default] ");
4142 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4143 fprintf(output, "[abstract] ");
4144 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4145 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004146 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004147 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004148 /*
4149 * Default/fixed value.
4150 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004151 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004152 fprintf(output, " value: '%s'\n", elem->value);
4153 /*
4154 * Type.
4155 */
4156 if (elem->namedType != NULL) {
4157 fprintf(output, " type: %s ", elem->namedType);
4158 if (elem->namedTypeNs != NULL)
4159 fprintf(output, "ns %s\n", elem->namedTypeNs);
4160 else
4161 fprintf(output, "\n");
4162 }
4163 /*
4164 * Substitution group.
4165 */
4166 if (elem->substGroup != NULL) {
4167 fprintf(output, " substitutionGroup: %s ", elem->substGroup);
4168 if (elem->substGroupNs != NULL)
4169 fprintf(output, "ns %s\n", elem->substGroupNs);
4170 else
4171 fprintf(output, "\n");
4172 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004173}
4174
4175/**
4176 * xmlSchemaAnnotDump:
4177 * @output: the file output
4178 * @annot: a annotation
4179 *
4180 * Dump the annotation
4181 */
4182static void
4183xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4184{
4185 xmlChar *content;
4186
4187 if (annot == NULL)
4188 return;
4189
4190 content = xmlNodeGetContent(annot->content);
4191 if (content != NULL) {
4192 fprintf(output, " Annot: %s\n", content);
4193 xmlFree(content);
4194 } else
4195 fprintf(output, " Annot: empty\n");
4196}
4197
4198/**
4199 * xmlSchemaTypeDump:
4200 * @output: the file output
4201 * @type: a type structure
4202 *
4203 * Dump a SchemaType structure
4204 */
4205static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004206xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4207{
4208 xmlChar *str = NULL;
4209 xmlSchemaTreeItemPtr term;
4210 char shift[100];
4211 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004212
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004213 if (particle == NULL)
4214 return;
4215 for (i = 0;((i < depth) && (i < 25));i++)
4216 shift[2 * i] = shift[2 * i + 1] = ' ';
4217 shift[2 * i] = shift[2 * i + 1] = 0;
4218 fprintf(output, shift);
4219 if (particle->children == NULL) {
4220 fprintf(output, "MISSING particle term\n");
4221 return;
4222 }
4223 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004224 if (term == NULL) {
4225 fprintf(output, "(NULL)");
4226 } else {
4227 switch (term->type) {
4228 case XML_SCHEMA_TYPE_ELEMENT:
4229 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4230 ((xmlSchemaElementPtr)term)->targetNamespace,
4231 ((xmlSchemaElementPtr)term)->name));
4232 break;
4233 case XML_SCHEMA_TYPE_SEQUENCE:
4234 fprintf(output, "SEQUENCE");
4235 break;
4236 case XML_SCHEMA_TYPE_CHOICE:
4237 fprintf(output, "CHOICE");
4238 break;
4239 case XML_SCHEMA_TYPE_ALL:
4240 fprintf(output, "ALL");
4241 break;
4242 case XML_SCHEMA_TYPE_ANY:
4243 fprintf(output, "ANY");
4244 break;
4245 default:
4246 fprintf(output, "UNKNOWN\n");
4247 return;
4248 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004249 }
4250 if (particle->minOccurs != 1)
4251 fprintf(output, " min: %d", particle->minOccurs);
4252 if (particle->maxOccurs >= UNBOUNDED)
4253 fprintf(output, " max: unbounded");
4254 else if (particle->maxOccurs != 1)
4255 fprintf(output, " max: %d", particle->maxOccurs);
4256 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004257 if (term &&
4258 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4259 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4260 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4261 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004262 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4263 output, depth +1);
4264 }
4265 if (particle->next != NULL)
4266 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4267 output, depth);
4268}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004269
4270/**
4271 * xmlSchemaAttrUsesDump:
4272 * @uses: attribute uses list
4273 * @output: the file output
4274 *
4275 * Dumps a list of attribute use components.
4276 */
4277static void
4278xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4279{
4280 xmlSchemaAttributeUsePtr use;
4281 xmlSchemaAttributeUseProhibPtr prohib;
4282 xmlSchemaQNameRefPtr ref;
4283 const xmlChar *name, *tns;
4284 xmlChar *str = NULL;
4285 int i;
4286
4287 if ((uses == NULL) || (uses->nbItems == 0))
4288 return;
4289
4290 fprintf(output, "uses:\n");
4291 for (i = 0; i < uses->nbItems; i++) {
4292 use = uses->items[i];
4293 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4294 fprintf(output, " [prohibition] ");
4295 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4296 name = prohib->name;
4297 tns = prohib->targetNamespace;
4298 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4299 fprintf(output, " [reference] ");
4300 ref = (xmlSchemaQNameRefPtr) use;
4301 name = ref->name;
4302 tns = ref->targetNamespace;
4303 } else {
4304 fprintf(output, " [use] ");
4305 name = WXS_ATTRUSE_DECL_NAME(use);
4306 tns = WXS_ATTRUSE_DECL_TNS(use);
4307 }
4308 fprintf(output, "%s\n",
4309 (const char *) xmlSchemaFormatQName(&str, tns, name));
4310 FREE_AND_NULL(str);
4311 }
4312}
4313
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004314/**
4315 * xmlSchemaTypeDump:
4316 * @output: the file output
4317 * @type: a type structure
4318 *
4319 * Dump a SchemaType structure
4320 */
4321static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004322xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4323{
4324 if (type == NULL) {
4325 fprintf(output, "Type: NULL\n");
4326 return;
4327 }
4328 fprintf(output, "Type: ");
4329 if (type->name != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004330 fprintf(output, "%s ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004331 else
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004332 fprintf(output, "no name ");
4333 if (type->targetNamespace != NULL)
4334 fprintf(output, "ns %s ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004335 switch (type->type) {
4336 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004337 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004338 break;
4339 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004340 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004341 break;
4342 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004343 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004344 break;
4345 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004346 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004347 break;
4348 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004349 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004350 break;
4351 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004352 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004353 break;
4354 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004355 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004356 break;
4357 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004358 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004359 break;
4360 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004361 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004362 break;
4363 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004364 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004365 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004366 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004367 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004368 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004369 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004370 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004371 break;
4372 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004373 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004374 break;
4375 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004376 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004377 break;
4378 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004379 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004380 break;
4381 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004382 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004383 break;
4384 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004385 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004386 break;
4387 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004388 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004389 break;
4390 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004391 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004392 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004393 }
4394 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004395 if (type->base != NULL) {
4396 fprintf(output, " base type: %s", type->base);
4397 if (type->baseNs != NULL)
4398 fprintf(output, " ns %s\n", type->baseNs);
4399 else
4400 fprintf(output, "\n");
4401 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004402 if (type->attrUses != NULL)
4403 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 if (type->annot != NULL)
4405 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004406#ifdef DUMP_CONTENT_MODEL
4407 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4408 (type->subtypes != NULL)) {
4409 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4410 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004411 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004412#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004413}
4414
4415/**
4416 * xmlSchemaDump:
4417 * @output: the file output
4418 * @schema: a schema structure
4419 *
4420 * Dump a Schema structure.
4421 */
4422void
4423xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4424{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004425 if (output == NULL)
4426 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004427 if (schema == NULL) {
4428 fprintf(output, "Schemas: NULL\n");
4429 return;
4430 }
4431 fprintf(output, "Schemas: ");
4432 if (schema->name != NULL)
4433 fprintf(output, "%s, ", schema->name);
4434 else
4435 fprintf(output, "no name, ");
4436 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004437 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004438 else
4439 fprintf(output, "no target namespace");
4440 fprintf(output, "\n");
4441 if (schema->annot != NULL)
4442 xmlSchemaAnnotDump(output, schema->annot);
4443
4444 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4445 output);
4446 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004447 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004448}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004449
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004450#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004451/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004452 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004453 * @vctxt: the WXS validation context
4454 *
4455 * Displays the current IDC table for debug purposes.
4456 */
4457static void
4458xmlSchemaDebugDumpIDCTable(FILE * output,
4459 const xmlChar *namespaceName,
4460 const xmlChar *localName,
4461 xmlSchemaPSVIIDCBindingPtr bind)
4462{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004463 xmlChar *str = NULL;
4464 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004465 xmlSchemaPSVIIDCNodePtr tab;
4466 xmlSchemaPSVIIDCKeyPtr key;
4467 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004468
4469 fprintf(output, "IDC: TABLES on %s\n",
4470 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004471 FREE_AND_NULL(str)
4472
4473 if (bind == NULL)
4474 return;
4475 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004476 fprintf(output, "IDC: BINDING %s\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004477 xmlSchemaGetComponentQName(&str,
4478 bind->definition));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004479 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004480 for (i = 0; i < bind->nbNodes; i++) {
4481 tab = bind->nodeTable[i];
4482 fprintf(output, " ( ");
4483 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004484 key = tab->keys[j];
4485 if ((key != NULL) && (key->val != NULL)) {
4486 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004487 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004488 fprintf(output, "\"%s\" ", value);
4489 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004490 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004491 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004492 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004493 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004494 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004495 else
4496 fprintf(output, "(key missing), ");
4497 }
4498 fprintf(output, ")\n");
4499 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004500 if (bind->dupls && bind->dupls->nbItems) {
4501 fprintf(output, "IDC: dupls:\n");
4502 for (i = 0; i < bind->dupls->nbItems; i++) {
4503 tab = bind->dupls->items[i];
4504 fprintf(output, " ( ");
4505 for (j = 0; j < bind->definition->nbFields; j++) {
4506 key = tab->keys[j];
4507 if ((key != NULL) && (key->val != NULL)) {
4508 res = xmlSchemaGetCanonValue(key->val, &value);
4509 if (res >= 0)
4510 fprintf(output, "\"%s\" ", value);
4511 else
4512 fprintf(output, "CANON-VALUE-FAILED ");
4513 if (res == 0)
4514 FREE_AND_NULL(value)
4515 } else if (key != NULL)
4516 fprintf(output, "(no val), ");
4517 else
4518 fprintf(output, "(key missing), ");
4519 }
4520 fprintf(output, ")\n");
4521 }
4522 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004523 bind = bind->next;
4524 } while (bind != NULL);
4525}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004526#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004527#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004528
4529/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004530 * *
4531 * Utilities *
4532 * *
4533 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004534
Daniel Veillardc0826a72004-08-10 14:17:33 +00004535/**
4536 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004537 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004538 * @name: the name of the attribute
4539 *
4540 * Seeks an attribute with a name of @name in
4541 * no namespace.
4542 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004543 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004544 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004545static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004546xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004547{
4548 xmlAttrPtr prop;
4549
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004550 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004551 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004552 prop = node->properties;
4553 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004554 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004555 return(prop);
4556 prop = prop->next;
4557 }
4558 return (NULL);
4559}
4560
4561/**
4562 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004563 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004564 * @uri: the uri
4565 * @name: the name of the attribute
4566 *
4567 * Seeks an attribute with a local name of @name and
4568 * a namespace URI of @uri.
4569 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004570 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004571 */
4572static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004573xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004574{
4575 xmlAttrPtr prop;
4576
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004577 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004578 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004579 prop = node->properties;
4580 while (prop != NULL) {
4581 if ((prop->ns != NULL) &&
4582 xmlStrEqual(prop->name, BAD_CAST name) &&
4583 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004584 return(prop);
4585 prop = prop->next;
4586 }
4587 return (NULL);
4588}
4589
4590static const xmlChar *
4591xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4592{
4593 xmlChar *val;
4594 const xmlChar *ret;
4595
4596 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004597 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004598 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004599 ret = xmlDictLookup(ctxt->dict, val, -1);
4600 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004601 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004602}
4603
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004604static const xmlChar *
4605xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4606{
4607 return((const xmlChar*) xmlNodeGetContent(node));
4608}
4609
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004610/**
4611 * xmlSchemaGetProp:
4612 * @ctxt: the parser context
4613 * @node: the node
4614 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004615 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004616 * Read a attribute value and internalize the string
4617 *
4618 * Returns the string or NULL if not present.
4619 */
4620static const xmlChar *
4621xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4622 const char *name)
4623{
4624 xmlChar *val;
4625 const xmlChar *ret;
4626
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004627 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004628 if (val == NULL)
4629 return(NULL);
4630 ret = xmlDictLookup(ctxt->dict, val, -1);
4631 xmlFree(val);
4632 return(ret);
4633}
4634
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004635/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004636 * *
4637 * Parsing functions *
4638 * *
4639 ************************************************************************/
4640
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004641#define WXS_FIND_GLOBAL_ITEM(slot) \
4642 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4643 ret = xmlHashLookup(schema->slot, name); \
4644 if (ret != NULL) goto exit; \
4645 } \
4646 if (xmlHashSize(schema->schemasImports) > 1) { \
4647 xmlSchemaImportPtr import; \
4648 if (nsName == NULL) \
4649 import = xmlHashLookup(schema->schemasImports, \
4650 XML_SCHEMAS_NO_NAMESPACE); \
4651 else \
4652 import = xmlHashLookup(schema->schemasImports, nsName); \
4653 if (import == NULL) \
4654 goto exit; \
4655 ret = xmlHashLookup(import->schema->slot, name); \
4656 }
4657
Daniel Veillard4255d502002-04-16 15:50:10 +00004658/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004659 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004660 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004661 * @name: the element name
4662 * @ns: the element namespace
4663 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004664 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004665 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004666 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004667 */
4668static xmlSchemaElementPtr
4669xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004670 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004671{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004672 xmlSchemaElementPtr ret = NULL;
4673
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004674 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004675 return(NULL);
4676 if (schema != NULL) {
4677 WXS_FIND_GLOBAL_ITEM(elemDecl)
4678 }
4679exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004680#ifdef DEBUG
4681 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004682 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004683 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004684 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004685 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004686 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004687 }
4688#endif
4689 return (ret);
4690}
4691
4692/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004693 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004694 * @schema: the main schema
4695 * @name: the type's name
4696 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004697 *
4698 * Lookup a type in the schemas or the predefined types
4699 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004700 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004701 */
4702static xmlSchemaTypePtr
4703xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004704 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004705{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004706 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004707
4708 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004709 return (NULL);
4710 /* First try the built-in types. */
4711 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4712 ret = xmlSchemaGetPredefinedType(name, nsName);
4713 if (ret != NULL)
4714 goto exit;
4715 /*
4716 * Note that we try the parsed schemas as well here
4717 * since one might have parsed the S4S, which contain more
4718 * than the built-in types.
4719 * TODO: Can we optimize this?
4720 */
4721 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004722 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004723 WXS_FIND_GLOBAL_ITEM(typeDecl)
4724 }
4725exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004726
Daniel Veillard4255d502002-04-16 15:50:10 +00004727#ifdef DEBUG
4728 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004729 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004730 fprintf(stderr, "Unable to lookup type %s", name);
4731 else
4732 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004733 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004734 }
4735#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004736 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004737}
4738
Daniel Veillard3646d642004-06-02 19:19:14 +00004739/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004740 * xmlSchemaGetAttributeDecl:
4741 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004742 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004743 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004744 *
4745 * Lookup a an attribute in the schema or imported schemas
4746 *
4747 * Returns the attribute declaration or NULL if not found.
4748 */
4749static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004750xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004751 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004752{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004753 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004754
4755 if ((name == NULL) || (schema == NULL))
4756 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004757 if (schema != NULL) {
4758 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004759 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004760exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004761#ifdef DEBUG
4762 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004763 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004764 fprintf(stderr, "Unable to lookup attribute %s", name);
4765 else
4766 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004767 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004768 }
4769#endif
4770 return (ret);
4771}
4772
4773/**
4774 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004775 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004776 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004777 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004778 *
4779 * Lookup a an attribute group in the schema or imported schemas
4780 *
4781 * Returns the attribute group definition or NULL if not found.
4782 */
4783static xmlSchemaAttributeGroupPtr
4784xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004785 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004786{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004787 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004788
4789 if ((name == NULL) || (schema == NULL))
4790 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004791 if (schema != NULL) {
4792 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4793 }
4794exit:
4795 /* TODO:
4796 if ((ret != NULL) && (ret->redef != NULL)) {
4797 * Return the last redefinition. *
4798 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004799 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004800 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004801#ifdef DEBUG
4802 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004803 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004804 fprintf(stderr, "Unable to lookup attribute group %s", name);
4805 else
4806 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004807 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004808 }
4809#endif
4810 return (ret);
4811}
4812
4813/**
4814 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004815 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004816 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004817 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 *
4819 * Lookup a group in the schema or imported schemas
4820 *
4821 * Returns the group definition or NULL if not found.
4822 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004823static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004824xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004825 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004826{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004827 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004828
4829 if ((name == NULL) || (schema == NULL))
4830 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004831 if (schema != NULL) {
4832 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004833 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004834exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004835
Daniel Veillard3646d642004-06-02 19:19:14 +00004836#ifdef DEBUG
4837 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004838 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004839 fprintf(stderr, "Unable to lookup group %s", name);
4840 else
4841 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004842 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004843 }
4844#endif
4845 return (ret);
4846}
4847
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004848static xmlSchemaNotationPtr
4849xmlSchemaGetNotation(xmlSchemaPtr schema,
4850 const xmlChar *name,
4851 const xmlChar *nsName)
4852{
4853 xmlSchemaNotationPtr ret = NULL;
4854
4855 if ((name == NULL) || (schema == NULL))
4856 return (NULL);
4857 if (schema != NULL) {
4858 WXS_FIND_GLOBAL_ITEM(notaDecl)
4859 }
4860exit:
4861 return (ret);
4862}
4863
4864static xmlSchemaIDCPtr
4865xmlSchemaGetIDC(xmlSchemaPtr schema,
4866 const xmlChar *name,
4867 const xmlChar *nsName)
4868{
4869 xmlSchemaIDCPtr ret = NULL;
4870
4871 if ((name == NULL) || (schema == NULL))
4872 return (NULL);
4873 if (schema != NULL) {
4874 WXS_FIND_GLOBAL_ITEM(idcDef)
4875 }
4876exit:
4877 return (ret);
4878}
4879
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004880/**
4881 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004882 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004883 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004884 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004885 *
4886 * Lookup a group in the schema or imported schemas
4887 *
4888 * Returns the group definition or NULL if not found.
4889 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004890static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004891xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4892 xmlSchemaTypeType itemType,
4893 const xmlChar *name,
4894 const xmlChar *targetNs)
4895{
4896 switch (itemType) {
4897 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004898 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004899 name, targetNs));
4900 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004901 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004902 name, targetNs));
4903 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004904 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004905 return (NULL);
4906 }
4907}
4908
Daniel Veillard4255d502002-04-16 15:50:10 +00004909/************************************************************************
4910 * *
4911 * Parsing functions *
4912 * *
4913 ************************************************************************/
4914
4915#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004916 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004917
4918/**
4919 * xmlSchemaIsBlank:
4920 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004921 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004922 *
4923 * Check if a string is ignorable
4924 *
4925 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4926 */
4927static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004928xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004929{
Daniel Veillard4255d502002-04-16 15:50:10 +00004930 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004931 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004932 if (len < 0) {
4933 while (*str != 0) {
4934 if (!(IS_BLANK_CH(*str)))
4935 return (0);
4936 str++;
4937 }
4938 } else while ((*str != 0) && (len != 0)) {
4939 if (!(IS_BLANK_CH(*str)))
4940 return (0);
4941 str++;
4942 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004943 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004944
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004945 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004946}
4947
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004948#define WXS_COMP_NAME(c, t) ((t) (c))->name
4949#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4950/*
4951* xmlSchemaFindRedefCompInGraph:
4952* ATTENTION TODO: This uses pointer comp. for strings.
4953*/
4954static xmlSchemaBasicItemPtr
4955xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4956 xmlSchemaTypeType type,
4957 const xmlChar *name,
4958 const xmlChar *nsName)
4959{
4960 xmlSchemaBasicItemPtr ret;
4961 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004962
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004963 if ((bucket == NULL) || (name == NULL))
4964 return(NULL);
4965 if ((bucket->globals == NULL) ||
4966 (bucket->globals->nbItems == 0))
4967 goto subschemas;
4968 /*
4969 * Search in global components.
4970 */
4971 for (i = 0; i < bucket->globals->nbItems; i++) {
4972 ret = bucket->globals->items[i];
4973 if (ret->type == type) {
4974 switch (type) {
4975 case XML_SCHEMA_TYPE_COMPLEX:
4976 case XML_SCHEMA_TYPE_SIMPLE:
4977 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
4978 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
4979 nsName))
4980 {
4981 return(ret);
4982 }
4983 break;
4984 case XML_SCHEMA_TYPE_GROUP:
4985 if ((WXS_COMP_NAME(ret,
4986 xmlSchemaModelGroupDefPtr) == name) &&
4987 (WXS_COMP_TNS(ret,
4988 xmlSchemaModelGroupDefPtr) == nsName))
4989 {
4990 return(ret);
4991 }
4992 break;
4993 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4994 if ((WXS_COMP_NAME(ret,
4995 xmlSchemaAttributeGroupPtr) == name) &&
4996 (WXS_COMP_TNS(ret,
4997 xmlSchemaAttributeGroupPtr) == nsName))
4998 {
4999 return(ret);
5000 }
5001 default:
5002 /* Should not be hit. */
5003 return(NULL);
5004 }
5005 }
5006 }
5007subschemas:
5008 /*
5009 * Process imported/included schemas.
5010 */
5011 if (bucket->relations != NULL) {
5012 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5013
5014 /*
5015 * TODO: Marking the bucket will not avoid multiple searches
5016 * in the same schema, but avoids at least circularity.
5017 */
5018 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5019 do {
5020 if ((rel->bucket != NULL) &&
5021 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5022 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5023 type, name, nsName);
5024 if (ret != NULL)
5025 return(ret);
5026 }
5027 rel = rel->next;
5028 } while (rel != NULL);
5029 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5030 }
5031 return(NULL);
5032}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005033
5034/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005035 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005036 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005037 * @schema: the schema being built
5038 * @name: the item name
5039 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005040 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005041 * *WARNING* this interface is highly subject to change
5042 *
5043 * Returns the new struture or NULL in case of error
5044 */
5045static xmlSchemaNotationPtr
5046xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005047 const xmlChar *name, const xmlChar *nsName,
5048 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005049{
5050 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005051
5052 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5053 return (NULL);
5054
Daniel Veillard4255d502002-04-16 15:50:10 +00005055 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5056 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005057 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005058 return (NULL);
5059 }
5060 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005061 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005062 ret->name = name;
5063 ret->targetNamespace = nsName;
5064 /* TODO: do we need the node to be set?
5065 * ret->node = node;*/
5066 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005067 return (ret);
5068}
5069
Daniel Veillard4255d502002-04-16 15:50:10 +00005070/**
5071 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005072 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005073 * @schema: the schema being built
5074 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005075 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005076 *
5077 * Add an XML schema Attrribute declaration
5078 * *WARNING* this interface is highly subject to change
5079 *
5080 * Returns the new struture or NULL in case of error
5081 */
5082static xmlSchemaAttributePtr
5083xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005084 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005085 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005086{
5087 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005088
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005089 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005090 return (NULL);
5091
5092 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5093 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005094 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005095 return (NULL);
5096 }
5097 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005098 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5099 ret->node = node;
5100 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005101 ret->targetNamespace = nsName;
5102
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005103 if (topLevel)
5104 WXS_ADD_GLOBAL(ctxt, ret);
5105 else
5106 WXS_ADD_LOCAL(ctxt, ret);
5107 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005108 return (ret);
5109}
5110
5111/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005112 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005113 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005114 * @schema: the schema being built
5115 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005116 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005117 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005118 * Add an XML schema Attrribute declaration
5119 * *WARNING* this interface is highly subject to change
5120 *
5121 * Returns the new struture or NULL in case of error
5122 */
5123static xmlSchemaAttributeUsePtr
5124xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5125 xmlNodePtr node)
5126{
5127 xmlSchemaAttributeUsePtr ret = NULL;
5128
5129 if (pctxt == NULL)
5130 return (NULL);
5131
5132 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5133 if (ret == NULL) {
5134 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5135 return (NULL);
5136 }
5137 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5138 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5139 ret->node = node;
5140
5141 WXS_ADD_LOCAL(pctxt, ret);
5142 return (ret);
5143}
5144
5145/*
5146* xmlSchemaAddRedef:
5147*
5148* Adds a redefinition information. This is used at a later stage to:
5149* resolve references to the redefined components and to check constraints.
5150*/
5151static xmlSchemaRedefPtr
5152xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5153 xmlSchemaBucketPtr targetBucket,
5154 void *item,
5155 const xmlChar *refName,
5156 const xmlChar *refTargetNs)
5157{
5158 xmlSchemaRedefPtr ret;
5159
5160 ret = (xmlSchemaRedefPtr)
5161 xmlMalloc(sizeof(xmlSchemaRedef));
5162 if (ret == NULL) {
5163 xmlSchemaPErrMemory(pctxt,
5164 "allocating redefinition info", NULL);
5165 return (NULL);
5166 }
5167 memset(ret, 0, sizeof(xmlSchemaRedef));
5168 ret->item = item;
5169 ret->targetBucket = targetBucket;
5170 ret->refName = refName;
5171 ret->refTargetNs = refTargetNs;
5172 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5173 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5174 else
5175 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5176 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5177
5178 return (ret);
5179}
5180
5181/**
5182 * xmlSchemaAddAttributeGroupDefinition:
5183 * @ctxt: a schema parser context
5184 * @schema: the schema being built
5185 * @name: the item name
5186 * @nsName: the target namespace
5187 * @node: the corresponding node
5188 *
5189 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005190 *
5191 * Returns the new struture or NULL in case of error
5192 */
5193static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005194xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5195 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5196 const xmlChar *name,
5197 const xmlChar *nsName,
5198 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005199{
5200 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005201
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005202 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005203 return (NULL);
5204
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005205 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005206 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005207 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005208 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005209 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005210 }
5211 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005212 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005213 ret->name = name;
5214 ret->targetNamespace = nsName;
5215 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005216
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005217 /* TODO: Remove the flag. */
5218 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5219 if (pctxt->isRedefine) {
5220 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5221 ret, name, nsName);
5222 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005223 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005224 return(NULL);
5225 }
5226 pctxt->redefCounter = 0;
5227 }
5228 WXS_ADD_GLOBAL(pctxt, ret);
5229 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005230 return (ret);
5231}
5232
5233/**
5234 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005235 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005236 * @schema: the schema being built
5237 * @name: the type name
5238 * @namespace: the type namespace
5239 *
5240 * Add an XML schema Element declaration
5241 * *WARNING* this interface is highly subject to change
5242 *
5243 * Returns the new struture or NULL in case of error
5244 */
5245static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005246xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005247 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005248 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005249{
5250 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005251
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005252 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005253 return (NULL);
5254
Daniel Veillard4255d502002-04-16 15:50:10 +00005255 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5256 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005257 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005258 return (NULL);
5259 }
5260 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005261 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5262 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005263 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005264 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005265
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005266 if (topLevel)
5267 WXS_ADD_GLOBAL(ctxt, ret);
5268 else
5269 WXS_ADD_LOCAL(ctxt, ret);
5270 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005271 return (ret);
5272}
5273
5274/**
5275 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005276 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005277 * @schema: the schema being built
5278 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005279 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005280 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005281 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005282 * *WARNING* this interface is highly subject to change
5283 *
5284 * Returns the new struture or NULL in case of error
5285 */
5286static xmlSchemaTypePtr
5287xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005288 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005289 const xmlChar * name, const xmlChar * nsName,
5290 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005291{
5292 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005293
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005294 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005295 return (NULL);
5296
5297 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5298 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005299 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005300 return (NULL);
5301 }
5302 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005303 ret->type = type;
5304 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005305 ret->targetNamespace = nsName;
5306 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005307 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005308 if (ctxt->isRedefine) {
5309 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5310 ret, name, nsName);
5311 if (ctxt->redef == NULL) {
5312 xmlFree(ret);
5313 return(NULL);
5314 }
5315 ctxt->redefCounter = 0;
5316 }
5317 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005318 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005319 WXS_ADD_LOCAL(ctxt, ret);
5320 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005321 return (ret);
5322}
5323
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005324static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005325xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005326 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005327 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005328 const xmlChar *refNs)
5329{
5330 xmlSchemaQNameRefPtr ret;
5331
5332 ret = (xmlSchemaQNameRefPtr)
5333 xmlMalloc(sizeof(xmlSchemaQNameRef));
5334 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005335 xmlSchemaPErrMemory(pctxt,
5336 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005337 return (NULL);
5338 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005339 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005340 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5341 ret->name = refName;
5342 ret->targetNamespace = refNs;
5343 ret->item = NULL;
5344 ret->itemType = refType;
5345 /*
5346 * Store the reference item in the schema.
5347 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005348 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005349 return (ret);
5350}
5351
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005352static xmlSchemaAttributeUseProhibPtr
5353xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5354{
5355 xmlSchemaAttributeUseProhibPtr ret;
5356
5357 ret = (xmlSchemaAttributeUseProhibPtr)
5358 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5359 if (ret == NULL) {
5360 xmlSchemaPErrMemory(pctxt,
5361 "allocating attribute use prohibition", NULL);
5362 return (NULL);
5363 }
5364 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5365 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5366 WXS_ADD_LOCAL(pctxt, ret);
5367 return (ret);
5368}
5369
5370
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005371/**
5372 * xmlSchemaAddModelGroup:
5373 * @ctxt: a schema parser context
5374 * @schema: the schema being built
5375 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005376 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005377 *
5378 * Adds a schema model group
5379 * *WARNING* this interface is highly subject to change
5380 *
5381 * Returns the new struture or NULL in case of error
5382 */
5383static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005384xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5385 xmlSchemaPtr schema,
5386 xmlSchemaTypeType type,
5387 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005388{
5389 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005390
5391 if ((ctxt == NULL) || (schema == NULL))
5392 return (NULL);
5393
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005394 ret = (xmlSchemaModelGroupPtr)
5395 xmlMalloc(sizeof(xmlSchemaModelGroup));
5396 if (ret == NULL) {
5397 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5398 NULL);
5399 return (NULL);
5400 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005401 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005402 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005403 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005404 WXS_ADD_LOCAL(ctxt, ret);
5405 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5406 (type == XML_SCHEMA_TYPE_CHOICE))
5407 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005408 return (ret);
5409}
5410
5411
5412/**
5413 * xmlSchemaAddParticle:
5414 * @ctxt: a schema parser context
5415 * @schema: the schema being built
5416 * @node: the corresponding node in the schema doc
5417 * @min: the minOccurs
5418 * @max: the maxOccurs
5419 *
5420 * Adds an XML schema particle component.
5421 * *WARNING* this interface is highly subject to change
5422 *
5423 * Returns the new struture or NULL in case of error
5424 */
5425static xmlSchemaParticlePtr
5426xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5427 xmlNodePtr node, int min, int max)
5428{
5429 xmlSchemaParticlePtr ret = NULL;
5430 if ((ctxt == NULL) || (schema == NULL))
5431 return (NULL);
5432
5433#ifdef DEBUG
5434 fprintf(stderr, "Adding particle component\n");
5435#endif
5436 ret = (xmlSchemaParticlePtr)
5437 xmlMalloc(sizeof(xmlSchemaParticle));
5438 if (ret == NULL) {
5439 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5440 NULL);
5441 return (NULL);
5442 }
5443 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5444 ret->annot = NULL;
5445 ret->node = node;
5446 ret->minOccurs = min;
5447 ret->maxOccurs = max;
5448 ret->next = NULL;
5449 ret->children = NULL;
5450
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005451 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005452 /*
5453 * Note that addition to pending components will be done locally
5454 * to the specific parsing function, since the most particles
5455 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005456 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005457 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005458 return (ret);
5459}
5460
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005461/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005462 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005463 * @ctxt: a schema validation context
5464 * @schema: the schema being built
5465 * @name: the group name
5466 *
5467 * Add an XML schema Group definition
5468 *
5469 * Returns the new struture or NULL in case of error
5470 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005471static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005472xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5473 xmlSchemaPtr schema,
5474 const xmlChar *name,
5475 const xmlChar *nsName,
5476 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005477{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005478 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005479
5480 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5481 return (NULL);
5482
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005483 ret = (xmlSchemaModelGroupDefPtr)
5484 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005485 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005486 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005487 return (NULL);
5488 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005489 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005490 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005491 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005492 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005493 ret->targetNamespace = nsName;
5494
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005495 if (ctxt->isRedefine) {
5496 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5497 ret, name, nsName);
5498 if (ctxt->redef == NULL) {
5499 xmlFree(ret);
5500 return(NULL);
5501 }
5502 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005503 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005504 WXS_ADD_GLOBAL(ctxt, ret);
5505 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005506 return (ret);
5507}
5508
Daniel Veillard3646d642004-06-02 19:19:14 +00005509/**
5510 * xmlSchemaNewWildcardNs:
5511 * @ctxt: a schema validation context
5512 *
5513 * Creates a new wildcard namespace constraint.
5514 *
5515 * Returns the new struture or NULL in case of error
5516 */
5517static xmlSchemaWildcardNsPtr
5518xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5519{
5520 xmlSchemaWildcardNsPtr ret;
5521
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005522 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005523 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5524 if (ret == NULL) {
5525 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005526 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005527 }
5528 ret->value = NULL;
5529 ret->next = NULL;
5530 return (ret);
5531}
5532
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005533static xmlSchemaIDCPtr
5534xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5535 const xmlChar *name, const xmlChar *nsName,
5536 int category, xmlNodePtr node)
5537{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005538 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005539
5540 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5541 return (NULL);
5542
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005543 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5544 if (ret == NULL) {
5545 xmlSchemaPErrMemory(ctxt,
5546 "allocating an identity-constraint definition", NULL);
5547 return (NULL);
5548 }
5549 memset(ret, 0, sizeof(xmlSchemaIDC));
5550 /* The target namespace of the parent element declaration. */
5551 ret->targetNamespace = nsName;
5552 ret->name = name;
5553 ret->type = category;
5554 ret->node = node;
5555
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005556 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005557 /*
5558 * Only keyrefs need to be fixup up.
5559 */
5560 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005561 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005562 return (ret);
5563}
5564
Daniel Veillard3646d642004-06-02 19:19:14 +00005565/**
5566 * xmlSchemaAddWildcard:
5567 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005568 * @schema: a schema
5569 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005570 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005571 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005572 *
5573 * Returns the new struture or NULL in case of error
5574 */
5575static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005576xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5577 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005578{
5579 xmlSchemaWildcardPtr ret = NULL;
5580
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005581 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005582 return (NULL);
5583
5584 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5585 if (ret == NULL) {
5586 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5587 return (NULL);
5588 }
5589 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005590 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005591 ret->node = node;
5592 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005593 return (ret);
5594}
5595
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005596static void
5597xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5598{
5599 if (group == NULL)
5600 return;
5601 if (group->members != NULL)
5602 xmlSchemaItemListFree(group->members);
5603 xmlFree(group);
5604}
5605
5606static xmlSchemaSubstGroupPtr
5607xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5608 xmlSchemaElementPtr head)
5609{
5610 xmlSchemaSubstGroupPtr ret;
5611
5612 /* Init subst group hash. */
5613 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5614 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5615 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5616 return(NULL);
5617 }
5618 /* Create a new substitution group. */
5619 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5620 if (ret == NULL) {
5621 xmlSchemaPErrMemory(NULL,
5622 "allocating a substitution group container", NULL);
5623 return(NULL);
5624 }
5625 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5626 ret->head = head;
5627 /* Create list of members. */
5628 ret->members = xmlSchemaItemListCreate();
5629 if (ret->members == NULL) {
5630 xmlSchemaSubstGroupFree(ret);
5631 return(NULL);
5632 }
5633 /* Add subst group to hash. */
5634 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5635 head->name, head->targetNamespace, ret) != 0) {
5636 PERROR_INT("xmlSchemaSubstGroupAdd",
5637 "failed to add a new substitution container");
5638 xmlSchemaSubstGroupFree(ret);
5639 return(NULL);
5640 }
5641 return(ret);
5642}
5643
5644static xmlSchemaSubstGroupPtr
5645xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5646 xmlSchemaElementPtr head)
5647{
5648 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5649 return(NULL);
5650 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5651 head->name, head->targetNamespace));
5652
5653}
5654
5655/**
5656 * xmlSchemaAddElementSubstitutionMember:
5657 * @pctxt: a schema parser context
5658 * @head: the head of the substitution group
5659 * @member: the new member of the substitution group
5660 *
5661 * Allocate a new annotation structure.
5662 *
5663 * Returns the newly allocated structure or NULL in case or error
5664 */
5665static int
5666xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5667 xmlSchemaElementPtr head,
5668 xmlSchemaElementPtr member)
5669{
5670 xmlSchemaSubstGroupPtr substGroup = NULL;
5671
5672 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5673 return (-1);
5674
5675 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5676 if (substGroup == NULL)
5677 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5678 if (substGroup == NULL)
5679 return(-1);
5680 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5681 return(-1);
5682 return(0);
5683}
5684
Daniel Veillard4255d502002-04-16 15:50:10 +00005685/************************************************************************
5686 * *
5687 * Utilities for parsing *
5688 * *
5689 ************************************************************************/
5690
Daniel Veillard4255d502002-04-16 15:50:10 +00005691/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005692 * xmlSchemaPValAttrNodeQNameValue:
5693 * @ctxt: a schema parser context
5694 * @schema: the schema context
5695 * @ownerDes: the designation of the parent element
5696 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005697 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005698 * @local: the resulting local part if found, the attribute value otherwise
5699 * @uri: the resulting namespace URI if found
5700 *
5701 * Extracts the local name and the URI of a QName value and validates it.
5702 * This one is intended to be used on attribute values that
5703 * should resolve to schema components.
5704 *
5705 * Returns 0, in case the QName is valid, a positive error code
5706 * if not valid and -1 if an internal error occurs.
5707 */
5708static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005709xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005710 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005711 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005712 xmlAttrPtr attr,
5713 const xmlChar *value,
5714 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005715 const xmlChar **local)
5716{
5717 const xmlChar *pref;
5718 xmlNsPtr ns;
5719 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005720
Daniel Veillardc0826a72004-08-10 14:17:33 +00005721 *uri = NULL;
5722 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005723 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005724 if (ret > 0) {
5725 xmlSchemaPSimpleTypeErr(ctxt,
5726 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5727 ownerItem, (xmlNodePtr) attr,
5728 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5729 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005730 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005731 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005732 } else if (ret < 0)
5733 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005734
5735 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005736 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005737 if (ns)
5738 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5739 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005740 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5741 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005742 /*
5743 * This one takes care of included schemas with no
5744 * target namespace.
5745 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005746 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005747 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005748 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005749 return (0);
5750 }
5751 /*
5752 * At this point xmlSplitQName3 has to return a local name.
5753 */
5754 *local = xmlSplitQName3(value, &len);
5755 *local = xmlDictLookup(ctxt->dict, *local, -1);
5756 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005757 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5758 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005759 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005760 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005761 ownerItem, (xmlNodePtr) attr,
5762 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5763 "The value '%s' of simple type 'xs:QName' has no "
5764 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005765 return (ctxt->err);
5766 } else {
5767 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005768 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005769 return (0);
5770}
5771
5772/**
5773 * xmlSchemaPValAttrNodeQName:
5774 * @ctxt: a schema parser context
5775 * @schema: the schema context
5776 * @ownerDes: the designation of the owner element
5777 * @ownerItem: the owner as a schema object
5778 * @attr: the attribute node
5779 * @local: the resulting local part if found, the attribute value otherwise
5780 * @uri: the resulting namespace URI if found
5781 *
5782 * Extracts and validates the QName of an attribute value.
5783 * This one is intended to be used on attribute values that
5784 * should resolve to schema components.
5785 *
5786 * Returns 0, in case the QName is valid, a positive error code
5787 * if not valid and -1 if an internal error occurs.
5788 */
5789static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005790xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005791 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005792 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005793 xmlAttrPtr attr,
5794 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005795 const xmlChar **local)
5796{
5797 const xmlChar *value;
5798
5799 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005800 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005801 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005802}
5803
5804/**
5805 * xmlSchemaPValAttrQName:
5806 * @ctxt: a schema parser context
5807 * @schema: the schema context
5808 * @ownerDes: the designation of the parent element
5809 * @ownerItem: the owner as a schema object
5810 * @ownerElem: the parent node of the attribute
5811 * @name: the name of the attribute
5812 * @local: the resulting local part if found, the attribute value otherwise
5813 * @uri: the resulting namespace URI if found
5814 *
5815 * Extracts and validates the QName of an attribute value.
5816 *
5817 * Returns 0, in case the QName is valid, a positive error code
5818 * if not valid and -1 if an internal error occurs.
5819 */
5820static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005821xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5822 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005823 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005824 xmlNodePtr ownerElem,
5825 const char *name,
5826 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005827 const xmlChar **local)
5828{
5829 xmlAttrPtr attr;
5830
5831 attr = xmlSchemaGetPropNode(ownerElem, name);
5832 if (attr == NULL) {
5833 *local = NULL;
5834 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005835 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005836 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005837 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005838 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005839}
5840
5841/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005842 * xmlSchemaPValAttrID:
5843 * @ctxt: a schema parser context
5844 * @schema: the schema context
5845 * @ownerDes: the designation of the parent element
5846 * @ownerItem: the owner as a schema object
5847 * @ownerElem: the parent node of the attribute
5848 * @name: the name of the attribute
5849 *
5850 * Extracts and validates the ID of an attribute value.
5851 *
5852 * Returns 0, in case the ID is valid, a positive error code
5853 * if not valid and -1 if an internal error occurs.
5854 */
5855static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005856xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005857{
5858 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005859 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005860
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005861 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005862 return(0);
5863 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5864 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005865 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005866 /*
5867 * NOTE: the IDness might have already be declared in the DTD
5868 */
5869 if (attr->atype != XML_ATTRIBUTE_ID) {
5870 xmlIDPtr res;
5871 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005872
5873 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005874 * TODO: Use xmlSchemaStrip here; it's not exported at this
5875 * moment.
5876 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005877 strip = xmlSchemaCollapseString(value);
5878 if (strip != NULL) {
5879 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005880 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005881 }
5882 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005883 if (res == NULL) {
5884 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005885 xmlSchemaPSimpleTypeErr(ctxt,
5886 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005887 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005888 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5889 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005890 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005891 } else
5892 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005893 }
5894 } else if (ret > 0) {
5895 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005896 xmlSchemaPSimpleTypeErr(ctxt,
5897 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005898 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005899 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5900 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5901 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005902 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005903 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005904 if (value != NULL)
5905 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005906
5907 return (ret);
5908}
5909
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005910static int
5911xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5912 xmlNodePtr ownerElem,
5913 const xmlChar *name)
5914{
5915 xmlAttrPtr attr;
5916
5917 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5918 if (attr == NULL)
5919 return(0);
5920 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5921
5922}
5923
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005924/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005925 * xmlGetMaxOccurs:
5926 * @ctxt: a schema validation context
5927 * @node: a subtree containing XML Schema informations
5928 *
5929 * Get the maxOccurs property
5930 *
5931 * Returns the default if not found, or the value
5932 */
5933static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005934xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5935 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005936{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005937 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005938 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005939 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005940
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005941 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5942 if (attr == NULL)
5943 return (def);
5944 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005945
5946 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005947 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005948 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005949 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5950 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005951 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005952 val, NULL, NULL, NULL);
5953 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005954 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005955 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005956 }
5957
5958 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005959 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005960 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005961 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005962 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005963 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5964 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005965 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005966 val, NULL, NULL, NULL);
5967 return (def);
5968 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005969 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005970 ret = ret * 10 + (*cur - '0');
5971 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005972 }
William M. Brack76e95df2003-10-18 16:20:14 +00005973 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005974 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005975 /*
5976 * TODO: Restrict the maximal value to Integer.
5977 */
5978 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005979 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005980 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5981 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005982 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005983 val, NULL, NULL, NULL);
5984 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005985 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005986 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005987}
5988
5989/**
5990 * xmlGetMinOccurs:
5991 * @ctxt: a schema validation context
5992 * @node: a subtree containing XML Schema informations
5993 *
5994 * Get the minOccurs property
5995 *
5996 * Returns the default if not found, or the value
5997 */
5998static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005999xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006000 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006001{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006002 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006003 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006004 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006005
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006006 attr = xmlSchemaGetPropNode(node, "minOccurs");
6007 if (attr == NULL)
6008 return (def);
6009 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006010 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006011 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006012 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006013 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006014 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006015 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6016 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006017 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006018 val, NULL, NULL, NULL);
6019 return (def);
6020 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006021 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006022 ret = ret * 10 + (*cur - '0');
6023 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006024 }
William M. Brack76e95df2003-10-18 16:20:14 +00006025 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006026 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006027 /*
6028 * TODO: Restrict the maximal value to Integer.
6029 */
6030 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006031 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006032 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6033 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006034 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006035 val, NULL, NULL, NULL);
6036 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006037 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006038 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006039}
6040
6041/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006042 * xmlSchemaPGetBoolNodeValue:
6043 * @ctxt: a schema validation context
6044 * @ownerDes: owner designation
6045 * @ownerItem: the owner as a schema item
6046 * @node: the node holding the value
6047 *
6048 * Converts a boolean string value into 1 or 0.
6049 *
6050 * Returns 0 or 1.
6051 */
6052static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006053xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006054 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006055 xmlNodePtr node)
6056{
6057 xmlChar *value = NULL;
6058 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006059
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006060 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006061 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006062 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006063 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006064 * can have the following legal literals {true, false, 1, 0}.
6065 */
6066 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6067 res = 1;
6068 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6069 res = 0;
6070 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6071 res = 1;
6072 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006073 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006074 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006075 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006076 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006077 ownerItem, node,
6078 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6079 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006080 NULL, NULL, NULL);
6081 }
6082 if (value != NULL)
6083 xmlFree(value);
6084 return (res);
6085}
6086
6087/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006088 * xmlGetBooleanProp:
6089 * @ctxt: a schema validation context
6090 * @node: a subtree containing XML Schema informations
6091 * @name: the attribute name
6092 * @def: the default value
6093 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006094 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006095 *
6096 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006097 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006098 */
6099static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006100xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006101 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006102 const char *name, int def)
6103{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006104 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006105
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006106 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006107 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006108 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006109 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006110 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006111 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006112 * can have the following legal literals {true, false, 1, 0}.
6113 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006114 if (xmlStrEqual(val, BAD_CAST "true"))
6115 def = 1;
6116 else if (xmlStrEqual(val, BAD_CAST "false"))
6117 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006118 else if (xmlStrEqual(val, BAD_CAST "1"))
6119 def = 1;
6120 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006121 def = 0;
6122 else {
6123 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006124 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006125 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006126 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006127 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6128 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006129 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006130 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006131}
6132
6133/************************************************************************
6134 * *
6135 * Shema extraction from an Infoset *
6136 * *
6137 ************************************************************************/
6138static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6139 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006140 xmlNodePtr node,
6141 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006142static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6143 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006144 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006145 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006146 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006147static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6148 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006149 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006150 xmlNodePtr node,
6151 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006152static xmlSchemaBasicItemPtr
6153xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6154 xmlSchemaPtr schema,
6155 xmlNodePtr node,
6156 xmlSchemaItemListPtr uses,
6157 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006158static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6159 xmlSchemaPtr schema,
6160 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006161static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006162xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6163 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006164
6165/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006166 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006167 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006168 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006169 * @ownerDes: the designation of the parent element
6170 * @ownerItem: the schema object owner if existent
6171 * @attr: the schema attribute node being validated
6172 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006173 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006174 *
6175 * Validates a value against the given built-in type.
6176 * This one is intended to be used internally for validation
6177 * of schema attribute values during parsing of the schema.
6178 *
6179 * Returns 0 if the value is valid, a positive error code
6180 * number otherwise and -1 in case of an internal or API error.
6181 */
6182static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006183xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006184 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006185 xmlAttrPtr attr,
6186 const xmlChar *value,
6187 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006188{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006189
6190 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006191
6192 /*
6193 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6194 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006195 */
6196 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006197 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006198 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6199 PERROR_INT("xmlSchemaPValAttrNodeValue",
6200 "the given type is not a built-in type");
6201 return (-1);
6202 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006203 switch (type->builtInType) {
6204 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006205 case XML_SCHEMAS_QNAME:
6206 case XML_SCHEMAS_ANYURI:
6207 case XML_SCHEMAS_TOKEN:
6208 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006209 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6210 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006211 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006212 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006213 PERROR_INT("xmlSchemaPValAttrNodeValue",
6214 "validation using the given type is not supported");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006215 return (-1);
6216 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006217 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006218 /*
6219 * TODO: Should we use the S4S error codes instead?
6220 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006221 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006222 PERROR_INT("xmlSchemaPValAttrNodeValue",
6223 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006224 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006225 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006226 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006227 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6228 else
6229 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6230 xmlSchemaPSimpleTypeErr(pctxt,
6231 ret, ownerItem, (xmlNodePtr) attr,
6232 type, NULL, value, NULL, NULL, NULL);
6233 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006234 return (ret);
6235}
6236
6237/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006238 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006239 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006240 * @ctxt: a schema parser context
6241 * @ownerDes: the designation of the parent element
6242 * @ownerItem: the schema object owner if existent
6243 * @attr: the schema attribute node being validated
6244 * @type: the built-in type to be validated against
6245 * @value: the resulting value if any
6246 *
6247 * Extracts and validates a value against the given built-in type.
6248 * This one is intended to be used internally for validation
6249 * of schema attribute values during parsing of the schema.
6250 *
6251 * Returns 0 if the value is valid, a positive error code
6252 * number otherwise and -1 in case of an internal or API error.
6253 */
6254static int
6255xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006256 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006257 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006258 xmlSchemaTypePtr type,
6259 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006260{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006261 const xmlChar *val;
6262
6263 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006264 return (-1);
6265
Daniel Veillardc0826a72004-08-10 14:17:33 +00006266 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6267 if (value != NULL)
6268 *value = val;
6269
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006270 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006271 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006272}
6273
6274/**
6275 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006276 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006277 * @ctxt: a schema parser context
6278 * @node: the element node of the attribute
6279 * @ownerDes: the designation of the parent element
6280 * @ownerItem: the schema object owner if existent
6281 * @ownerElem: the owner element node
6282 * @name: the name of the schema attribute node
6283 * @type: the built-in type to be validated against
6284 * @value: the resulting value if any
6285 *
6286 * Extracts and validates a value against the given built-in type.
6287 * This one is intended to be used internally for validation
6288 * of schema attribute values during parsing of the schema.
6289 *
6290 * Returns 0 if the value is valid, a positive error code
6291 * number otherwise and -1 in case of an internal or API error.
6292 */
6293static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006294xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6295 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006296 xmlNodePtr ownerElem,
6297 const char *name,
6298 xmlSchemaTypePtr type,
6299 const xmlChar **value)
6300{
6301 xmlAttrPtr attr;
6302
6303 if ((ctxt == NULL) || (type == NULL)) {
6304 if (value != NULL)
6305 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006306 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006307 }
6308 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6309 if (value != NULL)
6310 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006311 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006312 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006313 "Internal error: xmlSchemaPValAttr, the given "
6314 "type '%s' is not a built-in type.\n",
6315 type->name, NULL);
6316 return (-1);
6317 }
6318 attr = xmlSchemaGetPropNode(ownerElem, name);
6319 if (attr == NULL) {
6320 if (value != NULL)
6321 *value = NULL;
6322 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006323 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006324 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006325 type, value));
6326}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006327
6328static int
6329xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006330 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006331 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006332 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006333 const xmlChar *namespaceName)
6334{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006335 /* TODO: Pointer comparison instead? */
6336 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006337 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006338 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006339 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006340 /*
6341 * Check if the referenced namespace was <import>ed.
6342 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006343 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006344 xmlSchemaSchemaRelationPtr rel;
6345
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006346 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006347 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006348 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006349 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006350 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006351 rel = rel->next;
6352 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006353 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006354 /*
6355 * No matching <import>ed namespace found.
6356 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006357 {
6358 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6359
6360 if (namespaceName == NULL)
6361 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6362 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6363 "References from this schema to components in no "
6364 "namespace are not allowed, since not indicated by an "
6365 "import statement", NULL, NULL);
6366 else
6367 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6368 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6369 "References from this schema to components in the "
6370 "namespace '%s' are not allowed, since not indicated by an "
6371 "import statement", namespaceName, NULL);
6372 }
6373 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006374}
6375
Daniel Veillardc0826a72004-08-10 14:17:33 +00006376/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006377 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006378 * @ctxt: a schema validation context
6379 * @schema: the schema being built
6380 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006381 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006382 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006383 * Parses attribute uses and attribute declarations and
6384 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006385 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006386static int
6387xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6388 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6389 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006390{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006391 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006392
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006393 while ((IS_SCHEMA((*child), "attribute")) ||
6394 (IS_SCHEMA((*child), "attributeGroup"))) {
6395 if (IS_SCHEMA((*child), "attribute")) {
6396 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6397 *list, parentType);
6398 } else {
6399 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6400 if ((item != NULL) && (hasRefs != NULL))
6401 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006402 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006403 if (item != NULL) {
6404 if (*list == NULL) {
6405 /* TODO: Customize grow factor. */
6406 *list = xmlSchemaItemListCreate();
6407 if (*list == NULL)
6408 return(-1);
6409 }
6410 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6411 return(-1);
6412 }
6413 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006414 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006415 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006416}
6417
6418/**
6419 * xmlSchemaParseAnnotation:
6420 * @ctxt: a schema validation context
6421 * @schema: the schema being built
6422 * @node: a subtree containing XML Schema informations
6423 *
6424 * parse a XML schema Attrribute declaration
6425 * *WARNING* this interface is highly subject to change
6426 *
William M. Bracke7091952004-05-11 15:09:58 +00006427 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006428 * 1 in case of success.
6429 */
6430static xmlSchemaAnnotPtr
6431xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006432 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006433{
6434 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006435 xmlNodePtr child = NULL;
6436 xmlAttrPtr attr;
6437 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006438
Daniel Veillardc0826a72004-08-10 14:17:33 +00006439 /*
6440 * INFO: S4S completed.
6441 */
6442 /*
6443 * id = ID
6444 * {any attributes with non-schema namespace . . .}>
6445 * Content: (appinfo | documentation)*
6446 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006447 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6448 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006449 if (needed)
6450 ret = xmlSchemaNewAnnot(ctxt, node);
6451 else
6452 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006453 attr = node->properties;
6454 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006455 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006456 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006457 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006458 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006459
6460 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006461 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006462 }
6463 attr = attr->next;
6464 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006465 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006466 /*
6467 * And now for the children...
6468 */
6469 child = node->children;
6470 while (child != NULL) {
6471 if (IS_SCHEMA(child, "appinfo")) {
6472 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006473 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006474 * source = anyURI
6475 * {any attributes with non-schema namespace . . .}>
6476 * Content: ({any})*
6477 */
6478 attr = child->properties;
6479 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006480 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006481 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006482 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006483 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006484
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006485 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006486 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006487 }
6488 attr = attr->next;
6489 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006490 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006491 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006492 child = child->next;
6493 } else if (IS_SCHEMA(child, "documentation")) {
6494 /* TODO: make available the content of "documentation". */
6495 /*
6496 * source = anyURI
6497 * {any attributes with non-schema namespace . . .}>
6498 * Content: ({any})*
6499 */
6500 attr = child->properties;
6501 while (attr != NULL) {
6502 if (attr->ns == NULL) {
6503 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006504 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006505 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006506 }
6507 } else {
6508 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6509 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6510 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006511
6512 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006513 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006514 }
6515 }
6516 attr = attr->next;
6517 }
6518 /*
6519 * Attribute "xml:lang".
6520 */
6521 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6522 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006523 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006524 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006525 child = child->next;
6526 } else {
6527 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006528 xmlSchemaPContentErr(ctxt,
6529 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006530 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006531 barked = 1;
6532 child = child->next;
6533 }
6534 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006535
Daniel Veillard4255d502002-04-16 15:50:10 +00006536 return (ret);
6537}
6538
6539/**
6540 * xmlSchemaParseFacet:
6541 * @ctxt: a schema validation context
6542 * @schema: the schema being built
6543 * @node: a subtree containing XML Schema informations
6544 *
6545 * parse a XML schema Facet declaration
6546 * *WARNING* this interface is highly subject to change
6547 *
6548 * Returns the new type structure or NULL in case of error
6549 */
6550static xmlSchemaFacetPtr
6551xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006552 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006553{
6554 xmlSchemaFacetPtr facet;
6555 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006556 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006557
6558 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6559 return (NULL);
6560
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006561 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006562 if (facet == NULL) {
6563 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6564 return (NULL);
6565 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006566 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006567 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006568 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006569 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6570 "Facet %s has no value\n", node->name, NULL);
6571 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006572 return (NULL);
6573 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006574 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006575 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006576 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006577 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006578 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006579 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006580 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006581 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006582 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006583 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006584 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006585 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006586 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006587 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006588 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006589 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006590 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006591 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006592 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006593 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006594 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006595 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6596 } else if (IS_SCHEMA(node, "minLength")) {
6597 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6598 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006599 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6600 "Unknown facet type %s\n", node->name, NULL);
6601 xmlSchemaFreeFacet(facet);
6602 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006604 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006605 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006606 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6607 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6608 const xmlChar *fixed;
6609
6610 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6611 if (fixed != NULL) {
6612 if (xmlStrEqual(fixed, BAD_CAST "true"))
6613 facet->fixed = 1;
6614 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006615 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006616 child = node->children;
6617
6618 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006619 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006620 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006621 }
6622 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006623 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6624 "Facet %s has unexpected child content\n",
6625 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 }
6627 return (facet);
6628}
6629
6630/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006631 * xmlSchemaParseWildcardNs:
6632 * @ctxt: a schema parser context
6633 * @wildc: the wildcard, already created
6634 * @node: a subtree containing XML Schema informations
6635 *
6636 * Parses the attribute "processContents" and "namespace"
6637 * of a xsd:anyAttribute and xsd:any.
6638 * *WARNING* this interface is highly subject to change
6639 *
6640 * Returns 0 if everything goes fine, a positive error code
6641 * if something is not valid and -1 if an internal error occurs.
6642 */
6643static int
6644xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006645 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006646 xmlSchemaWildcardPtr wildc,
6647 xmlNodePtr node)
6648{
6649 const xmlChar *pc, *ns, *dictnsItem;
6650 int ret = 0;
6651 xmlChar *nsItem;
6652 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6653 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006654
Daniel Veillardc0826a72004-08-10 14:17:33 +00006655 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6656 if ((pc == NULL)
6657 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6658 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6659 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6660 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6661 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6662 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6663 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006664 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006665 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006666 NULL, node,
6667 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006668 NULL, NULL, NULL);
6669 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006670 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006671 }
6672 /*
6673 * Build the namespace constraints.
6674 */
6675 attr = xmlSchemaGetPropNode(node, "namespace");
6676 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006677 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006678 wildc->any = 1;
6679 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6680 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006681 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006682 return (-1);
6683 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006684 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006685 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006686 const xmlChar *end, *cur;
6687
6688 cur = ns;
6689 do {
6690 while (IS_BLANK_CH(*cur))
6691 cur++;
6692 end = cur;
6693 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6694 end++;
6695 if (end == cur)
6696 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006697 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006698 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6699 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006700 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006701 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006702 NULL, (xmlNodePtr) attr,
6703 NULL,
6704 "((##any | ##other) | List of (xs:anyURI | "
6705 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006706 nsItem, NULL, NULL, NULL);
6707 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6708 } else {
6709 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006710 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006711 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6712 dictnsItem = NULL;
6713 } else {
6714 /*
6715 * Validate the item (anyURI).
6716 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006717 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006718 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6719 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6720 }
6721 /*
6722 * Avoid dublicate namespaces.
6723 */
6724 tmp = wildc->nsSet;
6725 while (tmp != NULL) {
6726 if (dictnsItem == tmp->value)
6727 break;
6728 tmp = tmp->next;
6729 }
6730 if (tmp == NULL) {
6731 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6732 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006733 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006734 return (-1);
6735 }
6736 tmp->value = dictnsItem;
6737 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006738 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006739 wildc->nsSet = tmp;
6740 else
6741 lastNs->next = tmp;
6742 lastNs = tmp;
6743 }
6744
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006745 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006746 xmlFree(nsItem);
6747 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006748 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006749 }
6750 return (ret);
6751}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006752
6753static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006754xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6755 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006756 xmlNodePtr node,
6757 int minOccurs,
6758 int maxOccurs) {
6759
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006760 if ((maxOccurs == 0) && ( minOccurs == 0))
6761 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006762 if (maxOccurs != UNBOUNDED) {
6763 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006764 * TODO: Maybe we should better not create the particle,
6765 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006766 * content model.
6767 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006768 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006769 * 3.9.6 Schema Component Constraint: Particle Correct
6770 *
6771 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006772 if (maxOccurs < 1) {
6773 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006774 * 2.2 {max occurs} must be greater than or equal to 1.
6775 */
6776 xmlSchemaPCustomAttrErr(ctxt,
6777 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006778 NULL, NULL,
6779 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006780 "The value must be greater than or equal to 1");
6781 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6782 } else if (minOccurs > maxOccurs) {
6783 /*
6784 * 2.1 {min occurs} must not be greater than {max occurs}.
6785 */
6786 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006787 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006788 NULL, NULL,
6789 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006790 "The value must not be greater than the value of 'maxOccurs'");
6791 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6792 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006793 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006794 return (0);
6795}
6796
Daniel Veillardc0826a72004-08-10 14:17:33 +00006797/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006798 * xmlSchemaParseAny:
6799 * @ctxt: a schema validation context
6800 * @schema: the schema being built
6801 * @node: a subtree containing XML Schema informations
6802 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006803 * Parsea a XML schema <any> element. A particle and wildcard
6804 * will be created (except if minOccurs==maxOccurs==0, in this case
6805 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006806 * *WARNING* this interface is highly subject to change
6807 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006808 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006809 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006810static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006811xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6812 xmlNodePtr node)
6813{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006814 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006815 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006816 xmlSchemaWildcardPtr wild;
6817 int min, max;
6818 xmlAttrPtr attr;
6819 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006820
6821 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6822 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006823 /*
6824 * Check for illegal attributes.
6825 */
6826 attr = node->properties;
6827 while (attr != NULL) {
6828 if (attr->ns == NULL) {
6829 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6830 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6831 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6832 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6833 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006834 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006835 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006836 }
6837 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006838 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006839 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006840 }
6841 attr = attr->next;
6842 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006843 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006844 /*
6845 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006846 */
6847 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6848 "(xs:nonNegativeInteger | unbounded)");
6849 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6850 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006851 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6852 /*
6853 * Create & parse the wildcard.
6854 */
6855 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6856 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006857 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006858 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006859 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006860 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006861 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006862 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006863 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006864 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006865 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006866 }
6867 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006868 xmlSchemaPContentErr(ctxt,
6869 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006870 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006871 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006872 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006873 /*
6874 * No component if minOccurs==maxOccurs==0.
6875 */
6876 if ((min == 0) && (max == 0)) {
6877 /* Don't free the wildcard, since it's already on the list. */
6878 return (NULL);
6879 }
6880 /*
6881 * Create the particle.
6882 */
6883 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6884 if (particle == NULL)
6885 return (NULL);
6886 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006887 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006888
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006889 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006890}
6891
6892/**
6893 * xmlSchemaParseNotation:
6894 * @ctxt: a schema validation context
6895 * @schema: the schema being built
6896 * @node: a subtree containing XML Schema informations
6897 *
6898 * parse a XML schema Notation declaration
6899 *
6900 * Returns the new structure or NULL in case of error
6901 */
6902static xmlSchemaNotationPtr
6903xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006904 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006905{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006906 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006907 xmlSchemaNotationPtr ret;
6908 xmlNodePtr child = NULL;
6909
6910 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6911 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006912 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006913 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006914 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6915 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006916 return (NULL);
6917 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006918 ret = xmlSchemaAddNotation(ctxt, schema, name,
6919 ctxt->targetNamespace, node);
6920 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006921 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006922 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006923
6924 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006925 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006926 child = child->next;
6927 }
6928
Daniel Veillard4255d502002-04-16 15:50:10 +00006929 child = node->children;
6930 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006931 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006932 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006933 }
6934 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006935 xmlSchemaPContentErr(ctxt,
6936 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006937 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006938 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006939 }
6940
6941 return (ret);
6942}
6943
6944/**
6945 * xmlSchemaParseAnyAttribute:
6946 * @ctxt: a schema validation context
6947 * @schema: the schema being built
6948 * @node: a subtree containing XML Schema informations
6949 *
6950 * parse a XML schema AnyAttrribute declaration
6951 * *WARNING* this interface is highly subject to change
6952 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006953 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006954 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006955static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006956xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6957 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006958{
Daniel Veillard3646d642004-06-02 19:19:14 +00006959 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006960 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006961 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006962
6963 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6964 return (NULL);
6965
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006966 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6967 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006968 if (ret == NULL) {
6969 return (NULL);
6970 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006971 /*
6972 * Check for illegal attributes.
6973 */
6974 attr = node->properties;
6975 while (attr != NULL) {
6976 if (attr->ns == NULL) {
6977 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6978 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6979 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006980 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006981 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006982 }
6983 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006984 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006985 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006986 }
6987 attr = attr->next;
6988 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006989 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006990 /*
6991 * Parse the namespace list.
6992 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006993 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006994 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006995 /*
6996 * And now for the children...
6997 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006998 child = node->children;
6999 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007000 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007001 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007002 }
7003 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007004 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007005 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007006 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007007 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007008 }
7009
7010 return (ret);
7011}
7012
7013
7014/**
7015 * xmlSchemaParseAttribute:
7016 * @ctxt: a schema validation context
7017 * @schema: the schema being built
7018 * @node: a subtree containing XML Schema informations
7019 *
7020 * parse a XML schema Attrribute declaration
7021 * *WARNING* this interface is highly subject to change
7022 *
William M. Bracke7091952004-05-11 15:09:58 +00007023 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007024 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007025static xmlSchemaBasicItemPtr
7026xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7027 xmlSchemaPtr schema,
7028 xmlNodePtr node,
7029 xmlSchemaItemListPtr uses,
7030 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007031{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007032 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7033 xmlSchemaAttributeUsePtr use = NULL;
7034 xmlNodePtr child = NULL;
7035 xmlAttrPtr attr;
7036 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7037 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7038 int nberrors, hasForm = 0, defValueType = 0;
7039
7040#define WXS_ATTR_DEF_VAL_DEFAULT 1
7041#define WXS_ATTR_DEF_VAL_FIXED 2
7042
7043 /*
7044 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7045 */
7046
7047 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7048 return (NULL);
7049 attr = xmlSchemaGetPropNode(node, "ref");
7050 if (attr != NULL) {
7051 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7052 NULL, attr, &tmpNs, &tmpName) != 0) {
7053 return (NULL);
7054 }
7055 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7056 return(NULL);
7057 isRef = 1;
7058 }
7059 nberrors = pctxt->nberrors;
7060 /*
7061 * Check for illegal attributes.
7062 */
7063 attr = node->properties;
7064 while (attr != NULL) {
7065 if (attr->ns == NULL) {
7066 if (isRef) {
7067 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7068 xmlSchemaPValAttrNodeID(pctxt, attr);
7069 goto attr_next;
7070 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7071 goto attr_next;
7072 }
7073 } else {
7074 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7075 goto attr_next;
7076 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7077 xmlSchemaPValAttrNodeID(pctxt, attr);
7078 goto attr_next;
7079 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7080 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7081 attr, &tmpNs, &tmpName);
7082 goto attr_next;
7083 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7084 /*
7085 * Evaluate the target namespace
7086 */
7087 hasForm = 1;
7088 attrValue = xmlSchemaGetNodeContent(pctxt,
7089 (xmlNodePtr) attr);
7090 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7091 ns = pctxt->targetNamespace;
7092 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7093 {
7094 xmlSchemaPSimpleTypeErr(pctxt,
7095 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7096 NULL, (xmlNodePtr) attr,
7097 NULL, "(qualified | unqualified)",
7098 attrValue, NULL, NULL, NULL);
7099 }
7100 goto attr_next;
7101 }
7102 }
7103 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7104
7105 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7106 /* TODO: Maybe we need to normalize the value beforehand. */
7107 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7108 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7109 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7110 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7111 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7112 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7113 else {
7114 xmlSchemaPSimpleTypeErr(pctxt,
7115 XML_SCHEMAP_INVALID_ATTR_USE,
7116 NULL, (xmlNodePtr) attr,
7117 NULL, "(optional | prohibited | required)",
7118 attrValue, NULL, NULL, NULL);
7119 }
7120 goto attr_next;
7121 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7122 /*
7123 * 3.2.3 : 1
7124 * default and fixed must not both be present.
7125 */
7126 if (defValue) {
7127 xmlSchemaPMutualExclAttrErr(pctxt,
7128 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7129 NULL, attr, "default", "fixed");
7130 } else {
7131 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7132 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7133 }
7134 goto attr_next;
7135 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7136 /*
7137 * 3.2.3 : 1
7138 * default and fixed must not both be present.
7139 */
7140 if (defValue) {
7141 xmlSchemaPMutualExclAttrErr(pctxt,
7142 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7143 NULL, attr, "default", "fixed");
7144 } else {
7145 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7146 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7147 }
7148 goto attr_next;
7149 }
7150 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7151 goto attr_next;
7152
7153 xmlSchemaPIllegalAttrErr(pctxt,
7154 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7155
7156attr_next:
7157 attr = attr->next;
7158 }
7159 /*
7160 * 3.2.3 : 2
7161 * If default and use are both present, use must have
7162 * the actual value optional.
7163 */
7164 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7165 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7166 xmlSchemaPSimpleTypeErr(pctxt,
7167 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7168 NULL, node, NULL,
7169 "(optional | prohibited | required)", NULL,
7170 "The value of the attribute 'use' must be 'optional' "
7171 "if the attribute 'default' is present",
7172 NULL, NULL);
7173 }
7174 /*
7175 * We want correct attributes.
7176 */
7177 if (nberrors != pctxt->nberrors)
7178 return(NULL);
7179 if (! isRef) {
7180 xmlSchemaAttributePtr attrDecl;
7181
7182 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7183 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7184 ns = pctxt->targetNamespace;
7185 /*
7186 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7187 * TODO: Move this to the component layer.
7188 */
7189 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7190 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7191 XML_SCHEMAP_NO_XSI,
7192 node, NULL,
7193 "The target namespace must not match '%s'",
7194 xmlSchemaInstanceNs, NULL);
7195 }
7196 attr = xmlSchemaGetPropNode(node, "name");
7197 if (attr == NULL) {
7198 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7199 NULL, node, "name", NULL);
7200 return (NULL);
7201 }
7202 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7203 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7204 return (NULL);
7205 }
7206 /*
7207 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7208 * TODO: Move this to the component layer.
7209 */
7210 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7211 xmlSchemaPSimpleTypeErr(pctxt,
7212 XML_SCHEMAP_NO_XMLNS,
7213 NULL, (xmlNodePtr) attr,
7214 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7215 "The value of the attribute must not match 'xmlns'",
7216 NULL, NULL);
7217 return (NULL);
7218 }
7219 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7220 goto check_children;
7221 /*
7222 * Create the attribute use component.
7223 */
7224 use = xmlSchemaAddAttributeUse(pctxt, node);
7225 if (use == NULL)
7226 return(NULL);
7227 use->occurs = occurs;
7228 /*
7229 * Create the attribute declaration.
7230 */
7231 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7232 if (attrDecl == NULL)
7233 return (NULL);
7234 if (tmpName != NULL) {
7235 attrDecl->typeName = tmpName;
7236 attrDecl->typeNs = tmpNs;
7237 }
7238 use->attrDecl = attrDecl;
7239 /*
7240 * Value constraint.
7241 */
7242 if (defValue != NULL) {
7243 attrDecl->defValue = defValue;
7244 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7245 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7246 }
7247 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7248 xmlSchemaQNameRefPtr ref;
7249
7250 /*
7251 * Create the attribute use component.
7252 */
7253 use = xmlSchemaAddAttributeUse(pctxt, node);
7254 if (use == NULL)
7255 return(NULL);
7256 /*
7257 * We need to resolve the reference at later stage.
7258 */
7259 WXS_ADD_PENDING(pctxt, use);
7260 use->occurs = occurs;
7261 /*
7262 * Create a QName reference to the attribute declaration.
7263 */
7264 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7265 tmpName, tmpNs);
7266 if (ref == NULL)
7267 return(NULL);
7268 /*
7269 * Assign the reference. This will be substituted for the
7270 * referenced attribute declaration when the QName is resolved.
7271 */
7272 use->attrDecl = WXS_ATTR_CAST ref;
7273 /*
7274 * Value constraint.
7275 */
7276 if (defValue != NULL)
7277 use->defValue = defValue;
7278 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7279 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7280 }
7281
7282check_children:
7283 /*
7284 * And now for the children...
7285 */
7286 child = node->children;
7287 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7288 xmlSchemaAttributeUseProhibPtr prohib;
7289
7290 if (IS_SCHEMA(child, "annotation")) {
7291 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7292 child = child->next;
7293 }
7294 if (child != NULL) {
7295 xmlSchemaPContentErr(pctxt,
7296 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7297 NULL, node, child, NULL,
7298 "(annotation?)");
7299 }
7300 /*
7301 * Check for pointlessness of attribute prohibitions.
7302 */
7303 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7304 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7305 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7306 node, NULL,
7307 "Skipping attribute use prohibition, since it is "
7308 "pointless inside an <attributeGroup>",
7309 NULL, NULL, NULL);
7310 return(NULL);
7311 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7312 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7313 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7314 node, NULL,
7315 "Skipping attribute use prohibition, since it is "
7316 "pointless when extending a type",
7317 NULL, NULL, NULL);
7318 return(NULL);
7319 }
7320 if (! isRef) {
7321 tmpName = name;
7322 tmpNs = ns;
7323 }
7324 /*
7325 * Check for duplicate attribute prohibitions.
7326 */
7327 if (uses) {
7328 int i;
7329
7330 for (i = 0; i < uses->nbItems; i++) {
7331 use = uses->items[i];
7332 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7333 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7334 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7335 {
7336 xmlChar *str = NULL;
7337
7338 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7339 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7340 node, NULL,
7341 "Skipping duplicate attribute use prohibition '%s'",
7342 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7343 NULL, NULL);
7344 FREE_AND_NULL(str)
7345 return(NULL);
7346 }
7347 }
7348 }
7349 /*
7350 * Create the attribute prohibition helper component.
7351 */
7352 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7353 if (prohib == NULL)
7354 return(NULL);
7355 prohib->node = node;
7356 prohib->name = tmpName;
7357 prohib->targetNamespace = tmpNs;
7358 if (isRef) {
7359 /*
7360 * We need at least to resolve to the attribute declaration.
7361 */
7362 WXS_ADD_PENDING(pctxt, prohib);
7363 }
7364 return(WXS_BASIC_CAST prohib);
7365 } else {
7366 if (IS_SCHEMA(child, "annotation")) {
7367 /*
7368 * TODO: Should this go into the attr decl?
7369 */
7370 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7371 child = child->next;
7372 }
7373 if (isRef) {
7374 if (child != NULL) {
7375 if (IS_SCHEMA(child, "simpleType"))
7376 /*
7377 * 3.2.3 : 3.2
7378 * If ref is present, then all of <simpleType>,
7379 * form and type must be absent.
7380 */
7381 xmlSchemaPContentErr(pctxt,
7382 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7383 NULL, node, child, NULL,
7384 "(annotation?)");
7385 else
7386 xmlSchemaPContentErr(pctxt,
7387 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7388 NULL, node, child, NULL,
7389 "(annotation?)");
7390 }
7391 } else {
7392 if (IS_SCHEMA(child, "simpleType")) {
7393 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7394 /*
7395 * 3.2.3 : 4
7396 * type and <simpleType> must not both be present.
7397 */
7398 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7399 NULL, node, child,
7400 "The attribute 'type' and the <simpleType> child "
7401 "are mutually exclusive", NULL);
7402 } else
7403 WXS_ATTRUSE_TYPEDEF(use) =
7404 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7405 child = child->next;
7406 }
7407 if (child != NULL)
7408 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7409 NULL, node, child, NULL,
7410 "(annotation?, simpleType?)");
7411 }
7412 }
7413 return (WXS_BASIC_CAST use);
7414}
7415
7416
7417static xmlSchemaAttributePtr
7418xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7419 xmlSchemaPtr schema,
7420 xmlNodePtr node)
7421{
7422 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007423 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007424 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007425 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007426
7427 /*
7428 * Note that the w3c spec assumes the schema to be validated with schema
7429 * for schemas beforehand.
7430 *
7431 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007432 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007433 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7434 return (NULL);
7435 /*
7436 * 3.2.3 : 3.1
7437 * One of ref or name must be present, but not both
7438 */
7439 attr = xmlSchemaGetPropNode(node, "name");
7440 if (attr == NULL) {
7441 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7442 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007443 return (NULL);
7444 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007445 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7446 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7447 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007448 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007449 /*
7450 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7451 * TODO: Move this to the component layer.
7452 */
7453 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7454 xmlSchemaPSimpleTypeErr(pctxt,
7455 XML_SCHEMAP_NO_XMLNS,
7456 NULL, (xmlNodePtr) attr,
7457 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7458 "The value of the attribute must not match 'xmlns'",
7459 NULL, NULL);
7460 return (NULL);
7461 }
7462 /*
7463 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7464 * TODO: Move this to the component layer.
7465 * Or better leave it here and add it to the component layer
7466 * if we have a schema construction API.
7467 */
7468 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7469 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7470 XML_SCHEMAP_NO_XSI, node, NULL,
7471 "The target namespace must not match '%s'",
7472 xmlSchemaInstanceNs, NULL);
7473 }
7474
7475 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7476 pctxt->targetNamespace, node, 1);
7477 if (ret == NULL)
7478 return (NULL);
7479 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7480
7481 /*
7482 * Check for illegal attributes.
7483 */
7484 attr = node->properties;
7485 while (attr != NULL) {
7486 if (attr->ns == NULL) {
7487 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7488 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7489 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7490 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7491 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7492 {
7493 xmlSchemaPIllegalAttrErr(pctxt,
7494 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7495 }
7496 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7497 xmlSchemaPIllegalAttrErr(pctxt,
7498 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7499 }
7500 attr = attr->next;
7501 }
7502 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7503 node, "type", &ret->typeNs, &ret->typeName);
7504
7505 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007506 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007507 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007508 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007509 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007510 if (ret->defValue != NULL)
7511 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007512 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007513 * Attribute "default".
7514 */
7515 attr = xmlSchemaGetPropNode(node, "default");
7516 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007517 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007518 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007519 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007520 */
7521 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007522 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7523 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007524 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007525 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007526 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007527 /*
7528 * And now for the children...
7529 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007530 child = node->children;
7531 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007532 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007533 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007534 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007535 if (IS_SCHEMA(child, "simpleType")) {
7536 if (ret->typeName != NULL) {
7537 /*
7538 * 3.2.3 : 4
7539 * type and <simpleType> must not both be present.
7540 */
7541 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7542 NULL, node, child,
7543 "The attribute 'type' and the <simpleType> child "
7544 "are mutually exclusive", NULL);
7545 } else
7546 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7547 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007548 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007549 if (child != NULL)
7550 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7551 NULL, node, child, NULL,
7552 "(annotation?, simpleType?)");
7553
Daniel Veillard4255d502002-04-16 15:50:10 +00007554 return (ret);
7555}
7556
7557/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007558 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007559 * @ctxt: a schema validation context
7560 * @schema: the schema being built
7561 * @node: a subtree containing XML Schema informations
7562 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007563 * Parse an attribute group definition reference.
7564 * Note that a reference to an attribute group does not
7565 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007566 * *WARNING* this interface is highly subject to change
7567 *
7568 * Returns the attribute group or NULL in case of error.
7569 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007570static xmlSchemaQNameRefPtr
7571xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7572 xmlSchemaPtr schema,
7573 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007574{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007575 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007576 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007577 xmlAttrPtr attr;
7578 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007579
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007580 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007581 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007582
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007583 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007584 if (attr == NULL) {
7585 xmlSchemaPMissingAttrErr(pctxt,
7586 XML_SCHEMAP_S4S_ATTR_MISSING,
7587 NULL, node, "ref", NULL);
7588 return (NULL);
7589 }
7590 xmlSchemaPValAttrNodeQName(pctxt, schema,
7591 NULL, attr, &refNs, &ref);
7592 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7593 return(NULL);
7594
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007595 /*
7596 * Check for illegal attributes.
7597 */
7598 attr = node->properties;
7599 while (attr != NULL) {
7600 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007601 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007602 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007603 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007604 xmlSchemaPIllegalAttrErr(pctxt,
7605 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007606 }
7607 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007608 xmlSchemaPIllegalAttrErr(pctxt,
7609 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007610 }
7611 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007612 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007613 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007614 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7615
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007616 /*
7617 * And now for the children...
7618 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007619 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007620 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007621 /*
7622 * TODO: We do not have a place to store the annotation, do we?
7623 */
7624 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007625 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007626 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007627 if (child != NULL) {
7628 xmlSchemaPContentErr(pctxt,
7629 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7630 NULL, node, child, NULL,
7631 "(annotation?)");
7632 }
7633
7634 /*
7635 * Handle attribute group redefinitions.
7636 */
7637 if (pctxt->isRedefine && pctxt->redef &&
7638 (pctxt->redef->item->type ==
7639 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7640 (ref == pctxt->redef->refName) &&
7641 (refNs == pctxt->redef->refTargetNs))
7642 {
7643 /*
7644 * SPEC src-redefine:
7645 * (7.1) "If it has an <attributeGroup> among its contents
7646 * the ·actual value· of whose ref [attribute] is the same
7647 * as the ·actual value· of its own name attribute plus
7648 * target namespace, then it must have exactly one such group."
7649 */
7650 if (pctxt->redefCounter != 0) {
7651 xmlChar *str = NULL;
7652
7653 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7654 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7655 "The redefining attribute group definition "
7656 "'%s' must not contain more than one "
7657 "reference to the redefined definition",
7658 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7659 FREE_AND_NULL(str);
7660 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007661 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007662 pctxt->redefCounter++;
7663 /*
7664 * URGENT TODO: How to ensure that the reference will not be
7665 * handled by the normal component resolution mechanism?
7666 */
7667 ret = xmlSchemaNewQNameRef(pctxt,
7668 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7669 if (ret == NULL)
7670 return(NULL);
7671 ret->node = node;
7672 pctxt->redef->reference = WXS_BASIC_CAST ret;
7673 } else {
7674 /*
7675 * Create a QName-reference helper component. We will substitute this
7676 * component for the attribute uses of the referenced attribute group
7677 * definition.
7678 */
7679 ret = xmlSchemaNewQNameRef(pctxt,
7680 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7681 if (ret == NULL)
7682 return(NULL);
7683 ret->node = node;
7684 /* Add to pending items, to be able to resolve the reference. */
7685 WXS_ADD_PENDING(pctxt, ret);
7686 }
7687 return (ret);
7688}
7689
7690/**
7691 * xmlSchemaParseAttributeGroupDefinition:
7692 * @pctxt: a schema validation context
7693 * @schema: the schema being built
7694 * @node: a subtree containing XML Schema informations
7695 *
7696 * parse a XML schema Attribute Group declaration
7697 * *WARNING* this interface is highly subject to change
7698 *
7699 * Returns the attribute group definition or NULL in case of error.
7700 */
7701static xmlSchemaAttributeGroupPtr
7702xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7703 xmlSchemaPtr schema,
7704 xmlNodePtr node)
7705{
7706 const xmlChar *name;
7707 xmlSchemaAttributeGroupPtr ret;
7708 xmlNodePtr child = NULL;
7709 xmlAttrPtr attr;
7710 int hasRefs = 0;
7711
7712 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7713 return (NULL);
7714
7715 attr = xmlSchemaGetPropNode(node, "name");
7716 if (attr == NULL) {
7717 xmlSchemaPMissingAttrErr(pctxt,
7718 XML_SCHEMAP_S4S_ATTR_MISSING,
7719 NULL, node, "name", NULL);
7720 return (NULL);
7721 }
7722 /*
7723 * The name is crucial, exit if invalid.
7724 */
7725 if (xmlSchemaPValAttrNode(pctxt,
7726 NULL, attr,
7727 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7728 return (NULL);
7729 }
7730 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7731 name, pctxt->targetNamespace, node);
7732 if (ret == NULL)
7733 return (NULL);
7734 /*
7735 * Check for illegal attributes.
7736 */
7737 attr = node->properties;
7738 while (attr != NULL) {
7739 if (attr->ns == NULL) {
7740 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7741 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7742 {
7743 xmlSchemaPIllegalAttrErr(pctxt,
7744 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7745 }
7746 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7747 xmlSchemaPIllegalAttrErr(pctxt,
7748 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7749 }
7750 attr = attr->next;
7751 }
7752 /* Attribute ID */
7753 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7754 /*
7755 * And now for the children...
7756 */
7757 child = node->children;
7758 if (IS_SCHEMA(child, "annotation")) {
7759 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7760 child = child->next;
7761 }
7762 /*
7763 * Parse contained attribute decls/refs.
7764 */
7765 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7766 (xmlSchemaItemListPtr *) &(ret->attrUses),
7767 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7768 return(NULL);
7769 if (hasRefs)
7770 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7771 /*
7772 * Parse the attribute wildcard.
7773 */
7774 if (IS_SCHEMA(child, "anyAttribute")) {
7775 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7776 schema, child);
7777 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007778 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007779 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007780 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007781 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007782 NULL, node, child, NULL,
7783 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007784 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007785 return (ret);
7786}
7787
7788/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007789 * xmlSchemaPValAttrFormDefault:
7790 * @value: the value
7791 * @flags: the flags to be modified
7792 * @flagQualified: the specific flag for "qualified"
7793 *
7794 * Returns 0 if the value is valid, 1 otherwise.
7795 */
7796static int
7797xmlSchemaPValAttrFormDefault(const xmlChar *value,
7798 int *flags,
7799 int flagQualified)
7800{
7801 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7802 if ((*flags & flagQualified) == 0)
7803 *flags |= flagQualified;
7804 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007805 return (1);
7806
William M. Brack2f2a6632004-08-20 23:09:47 +00007807 return (0);
7808}
7809
7810/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007811 * xmlSchemaPValAttrBlockFinal:
7812 * @value: the value
7813 * @flags: the flags to be modified
7814 * @flagAll: the specific flag for "#all"
7815 * @flagExtension: the specific flag for "extension"
7816 * @flagRestriction: the specific flag for "restriction"
7817 * @flagSubstitution: the specific flag for "substitution"
7818 * @flagList: the specific flag for "list"
7819 * @flagUnion: the specific flag for "union"
7820 *
7821 * Validates the value of the attribute "final" and "block". The value
7822 * is converted into the specified flag values and returned in @flags.
7823 *
7824 * Returns 0 if the value is valid, 1 otherwise.
7825 */
7826
7827static int
7828xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007829 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007830 int flagAll,
7831 int flagExtension,
7832 int flagRestriction,
7833 int flagSubstitution,
7834 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007835 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007836{
7837 int ret = 0;
7838
7839 /*
7840 * TODO: This does not check for dublicate entries.
7841 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007842 if ((flags == NULL) || (value == NULL))
7843 return (-1);
7844 if (value[0] == 0)
7845 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007846 if (xmlStrEqual(value, BAD_CAST "#all")) {
7847 if (flagAll != -1)
7848 *flags |= flagAll;
7849 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007850 if (flagExtension != -1)
7851 *flags |= flagExtension;
7852 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007853 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007854 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007855 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007856 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007857 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007858 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007859 *flags |= flagUnion;
7860 }
7861 } else {
7862 const xmlChar *end, *cur = value;
7863 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007864
Daniel Veillardc0826a72004-08-10 14:17:33 +00007865 do {
7866 while (IS_BLANK_CH(*cur))
7867 cur++;
7868 end = cur;
7869 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7870 end++;
7871 if (end == cur)
7872 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007873 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007874 if (xmlStrEqual(item, BAD_CAST "extension")) {
7875 if (flagExtension != -1) {
7876 if ((*flags & flagExtension) == 0)
7877 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007878 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007879 ret = 1;
7880 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7881 if (flagRestriction != -1) {
7882 if ((*flags & flagRestriction) == 0)
7883 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007884 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007885 ret = 1;
7886 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7887 if (flagSubstitution != -1) {
7888 if ((*flags & flagSubstitution) == 0)
7889 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007890 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007891 ret = 1;
7892 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7893 if (flagList != -1) {
7894 if ((*flags & flagList) == 0)
7895 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007896 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007897 ret = 1;
7898 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7899 if (flagUnion != -1) {
7900 if ((*flags & flagUnion) == 0)
7901 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007902 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007903 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007904 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007905 ret = 1;
7906 if (item != NULL)
7907 xmlFree(item);
7908 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007909 } while ((ret == 0) && (*cur != 0));
7910 }
7911
Daniel Veillardc0826a72004-08-10 14:17:33 +00007912 return (ret);
7913}
7914
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007915static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007916xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007917 xmlSchemaIDCPtr idc,
7918 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007919 xmlAttrPtr attr,
7920 int isField)
7921{
7922 xmlNodePtr node;
7923
7924 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007925 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007926 * Schema Component Constraint: Selector Value OK
7927 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007928 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007929 * in [XPath].
7930 */
7931 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007932 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007933 XML_SCHEMAP_INTERNAL,
7934 "Internal error: xmlSchemaCheckCSelectorXPath, "
7935 "the selector is not specified.\n", NULL, NULL);
7936 return (-1);
7937 }
7938 if (attr == NULL)
7939 node = idc->node;
7940 else
7941 node = (xmlNodePtr) attr;
7942 if (selector->xpath == NULL) {
7943 xmlSchemaPCustomErr(ctxt,
7944 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007945 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007946 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007947 "The XPath expression of the selector is not valid", NULL);
7948 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7949 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007950 const xmlChar **nsArray = NULL;
7951 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007952 /*
7953 * Compile the XPath expression.
7954 */
7955 /*
7956 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007957 * TODO: Call xmlPatterncompile with different options for selector/
7958 * field.
7959 */
7960 nsList = xmlGetNsList(attr->doc, attr->parent);
7961 /*
7962 * Build an array of prefixes and namespaces.
7963 */
7964 if (nsList != NULL) {
7965 int i, count = 0;
7966 xmlNsPtr ns;
7967
7968 for (i = 0; nsList[i] != NULL; i++)
7969 count++;
7970
7971 nsArray = (const xmlChar **) xmlMalloc(
7972 (count * 2 + 1) * sizeof(const xmlChar *));
7973 if (nsArray == NULL) {
7974 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
7975 NULL);
7976 return (-1);
7977 }
7978 for (i = 0; i < count; i++) {
7979 ns = nsList[i];
7980 nsArray[2 * i] = nsList[i]->href;
7981 nsArray[2 * i + 1] = nsList[i]->prefix;
7982 }
7983 nsArray[count * 2] = NULL;
7984 xmlFree(nsList);
7985 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007986 /*
7987 * TODO: Differentiate between "selector" and "field".
7988 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007989 if (isField)
7990 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00007991 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007992 else
7993 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00007994 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007995 if (nsArray != NULL)
7996 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007997
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00007998 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007999 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008000 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008001 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008002 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008003 "The XPath expression '%s' could not be "
8004 "compiled", selector->xpath);
8005 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008006 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008007 }
8008 return (0);
8009}
8010
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008011#define ADD_ANNOTATION(annot) \
8012 xmlSchemaAnnotPtr cur = item->annot; \
8013 if (item->annot == NULL) { \
8014 item->annot = annot; \
8015 return (annot); \
8016 } \
8017 cur = item->annot; \
8018 if (cur->next != NULL) { \
8019 cur = cur->next; \
8020 } \
8021 cur->next = annot;
8022
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008023/**
8024 * xmlSchemaAssignAnnotation:
8025 * @item: the schema component
8026 * @annot: the annotation
8027 *
8028 * Adds the annotation to the given schema component.
8029 *
8030 * Returns the given annotaion.
8031 */
8032static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008033xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8034 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008035{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008036 if ((annItem == NULL) || (annot == NULL))
8037 return (NULL);
8038 switch (annItem->type) {
8039 case XML_SCHEMA_TYPE_ELEMENT: {
8040 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8041 ADD_ANNOTATION(annot)
8042 }
8043 break;
8044 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8045 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8046 ADD_ANNOTATION(annot)
8047 }
8048 break;
8049 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8050 case XML_SCHEMA_TYPE_ANY: {
8051 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8052 ADD_ANNOTATION(annot)
8053 }
8054 break;
8055 case XML_SCHEMA_TYPE_PARTICLE:
8056 case XML_SCHEMA_TYPE_IDC_KEY:
8057 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008058 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008059 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8060 ADD_ANNOTATION(annot)
8061 }
8062 break;
8063 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8064 xmlSchemaAttributeGroupPtr item =
8065 (xmlSchemaAttributeGroupPtr) annItem;
8066 ADD_ANNOTATION(annot)
8067 }
8068 break;
8069 case XML_SCHEMA_TYPE_NOTATION: {
8070 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8071 ADD_ANNOTATION(annot)
8072 }
8073 break;
8074 case XML_SCHEMA_FACET_MININCLUSIVE:
8075 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8076 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8077 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8078 case XML_SCHEMA_FACET_TOTALDIGITS:
8079 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8080 case XML_SCHEMA_FACET_PATTERN:
8081 case XML_SCHEMA_FACET_ENUMERATION:
8082 case XML_SCHEMA_FACET_WHITESPACE:
8083 case XML_SCHEMA_FACET_LENGTH:
8084 case XML_SCHEMA_FACET_MAXLENGTH:
8085 case XML_SCHEMA_FACET_MINLENGTH: {
8086 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8087 ADD_ANNOTATION(annot)
8088 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008089 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008090 case XML_SCHEMA_TYPE_SIMPLE:
8091 case XML_SCHEMA_TYPE_COMPLEX: {
8092 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8093 ADD_ANNOTATION(annot)
8094 }
8095 break;
8096 case XML_SCHEMA_TYPE_GROUP: {
8097 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8098 ADD_ANNOTATION(annot)
8099 }
8100 break;
8101 case XML_SCHEMA_TYPE_SEQUENCE:
8102 case XML_SCHEMA_TYPE_CHOICE:
8103 case XML_SCHEMA_TYPE_ALL: {
8104 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8105 ADD_ANNOTATION(annot)
8106 }
8107 break;
8108 default:
8109 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008110 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008111 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008112 "Internal error: xmlSchemaAddAnnotation, "
8113 "The item is not a annotated schema component", NULL);
8114 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008115 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008116 return (annot);
8117}
8118
8119/**
8120 * xmlSchemaParseIDCSelectorAndField:
8121 * @ctxt: a schema validation context
8122 * @schema: the schema being built
8123 * @node: a subtree containing XML Schema informations
8124 *
8125 * Parses a XML Schema identity-contraint definition's
8126 * <selector> and <field> elements.
8127 *
8128 * Returns the parsed identity-constraint definition.
8129 */
8130static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008131xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008132 xmlSchemaPtr schema,
8133 xmlSchemaIDCPtr idc,
8134 xmlNodePtr node,
8135 int isField)
8136{
8137 xmlSchemaIDCSelectPtr item;
8138 xmlNodePtr child = NULL;
8139 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008140
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008141 /*
8142 * Check for illegal attributes.
8143 */
8144 attr = node->properties;
8145 while (attr != NULL) {
8146 if (attr->ns == NULL) {
8147 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8148 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008149 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008150 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008151 }
8152 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008153 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008154 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008155 }
8156 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008157 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008158 /*
8159 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008160 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008161 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8162 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008163 xmlSchemaPErrMemory(ctxt,
8164 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008165 NULL);
8166 return (NULL);
8167 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008168 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008169 /*
8170 * Attribute "xpath" (mandatory).
8171 */
8172 attr = xmlSchemaGetPropNode(node, "xpath");
8173 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008174 xmlSchemaPMissingAttrErr(ctxt,
8175 XML_SCHEMAP_S4S_ATTR_MISSING,
8176 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008177 "name", NULL);
8178 } else {
8179 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8180 /*
8181 * URGENT TODO: "field"s have an other syntax than "selector"s.
8182 */
8183
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008184 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8185 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008186 xmlSchemaPErr(ctxt,
8187 (xmlNodePtr) attr,
8188 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008189 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008190 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008191 NULL, NULL);
8192 }
8193
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008194 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008195 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008196 /*
8197 * And now for the children...
8198 */
8199 child = node->children;
8200 if (IS_SCHEMA(child, "annotation")) {
8201 /*
8202 * Add the annotation to the parent IDC.
8203 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008204 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008205 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008206 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008207 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008208 if (child != NULL) {
8209 xmlSchemaPContentErr(ctxt,
8210 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008211 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008212 NULL, "(annotation?)");
8213 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008214
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008215 return (item);
8216}
8217
8218/**
8219 * xmlSchemaParseIDC:
8220 * @ctxt: a schema validation context
8221 * @schema: the schema being built
8222 * @node: a subtree containing XML Schema informations
8223 *
8224 * Parses a XML Schema identity-contraint definition.
8225 *
8226 * Returns the parsed identity-constraint definition.
8227 */
8228static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008229xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008230 xmlSchemaPtr schema,
8231 xmlNodePtr node,
8232 xmlSchemaTypeType idcCategory,
8233 const xmlChar *targetNamespace)
8234{
8235 xmlSchemaIDCPtr item = NULL;
8236 xmlNodePtr child = NULL;
8237 xmlAttrPtr attr;
8238 const xmlChar *name = NULL;
8239 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008240
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008241 /*
8242 * Check for illegal attributes.
8243 */
8244 attr = node->properties;
8245 while (attr != NULL) {
8246 if (attr->ns == NULL) {
8247 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8248 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8249 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8250 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008251 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008252 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008253 }
8254 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008255 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008256 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008257 }
8258 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008259 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008260 /*
8261 * Attribute "name" (mandatory).
8262 */
8263 attr = xmlSchemaGetPropNode(node, "name");
8264 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008265 xmlSchemaPMissingAttrErr(ctxt,
8266 XML_SCHEMAP_S4S_ATTR_MISSING,
8267 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008268 "name", NULL);
8269 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008270 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008271 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008272 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8273 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008274 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008275 /* Create the component. */
8276 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8277 idcCategory, node);
8278 if (item == NULL)
8279 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008280
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008281 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008282 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8283 /*
8284 * Attribute "refer" (mandatory).
8285 */
8286 attr = xmlSchemaGetPropNode(node, "refer");
8287 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008288 xmlSchemaPMissingAttrErr(ctxt,
8289 XML_SCHEMAP_S4S_ATTR_MISSING,
8290 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008291 "refer", NULL);
8292 } else {
8293 /*
8294 * Create a reference item.
8295 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008296 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008297 NULL, NULL);
8298 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008299 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008300 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008301 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008302 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008303 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008304 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008305 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008306 }
8307 }
8308 /*
8309 * And now for the children...
8310 */
8311 child = node->children;
8312 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008313 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008314 child = child->next;
8315 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008316 if (child == NULL) {
8317 xmlSchemaPContentErr(ctxt,
8318 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008319 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008320 "A child element is missing",
8321 "(annotation?, (selector, field+))");
8322 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008323 /*
8324 * Child element <selector>.
8325 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008326 if (IS_SCHEMA(child, "selector")) {
8327 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008328 item, child, 0);
8329 child = child->next;
8330 /*
8331 * Child elements <field>.
8332 */
8333 if (IS_SCHEMA(child, "field")) {
8334 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008335 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008336 item, child, 1);
8337 if (field != NULL) {
8338 field->index = item->nbFields;
8339 item->nbFields++;
8340 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008341 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008342 else
8343 item->fields = field;
8344 lastField = field;
8345 }
8346 child = child->next;
8347 } while (IS_SCHEMA(child, "field"));
8348 } else {
8349 xmlSchemaPContentErr(ctxt,
8350 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008351 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008352 NULL, "(annotation?, (selector, field+))");
8353 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008354 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008355 if (child != NULL) {
8356 xmlSchemaPContentErr(ctxt,
8357 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008358 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008359 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008360 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008361
8362 return (item);
8363}
8364
Daniel Veillardc0826a72004-08-10 14:17:33 +00008365/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008366 * xmlSchemaParseElement:
8367 * @ctxt: a schema validation context
8368 * @schema: the schema being built
8369 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008370 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008371 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008372 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008373 * *WARNING* this interface is highly subject to change
8374 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008375 * Returns the element declaration or a particle; NULL in case
8376 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008377 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008378static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008379xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008380 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008381{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008382 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008383 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008384 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008385 xmlNodePtr child = NULL;
8386 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008387 int min, max, isRef = 0;
8388 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008389
8390 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8391 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008392
Daniel Veillard4255d502002-04-16 15:50:10 +00008393 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008394 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008395
8396 if (isElemRef != NULL)
8397 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008398 /*
8399 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008400 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008401 * robust.
8402 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008403 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008404 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008405 if ((topLevel) || (attr == NULL)) {
8406 if (nameAttr == NULL) {
8407 xmlSchemaPMissingAttrErr(ctxt,
8408 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008409 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008410 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008411 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008412 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008413 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008414
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008415 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008416 child = node->children;
8417 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008418 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008419 child = child->next;
8420 }
8421 /*
8422 * Skip particle part if a global declaration.
8423 */
8424 if (topLevel)
8425 goto declaration_part;
8426 /*
8427 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008428 */
8429 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8430 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8431 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008432 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8433 if (particle == NULL)
8434 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008435
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008436 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8437
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008438 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008439 const xmlChar *refNs = NULL, *ref = NULL;
8440 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008441 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008442 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008443 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008444 if (isElemRef != NULL)
8445 *isElemRef = 1;
8446
Daniel Veillardc0826a72004-08-10 14:17:33 +00008447 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008448 NULL, attr, &refNs, &ref);
8449 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008450 /*
8451 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008452 */
8453 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008454 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008455 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008456 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008457 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008458 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008459 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008460 attr = node->properties;
8461 while (attr != NULL) {
8462 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008463 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8464 xmlStrEqual(attr->name, BAD_CAST "name") ||
8465 xmlStrEqual(attr->name, BAD_CAST "id") ||
8466 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8467 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8468 {
8469 attr = attr->next;
8470 continue;
8471 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008472 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008473 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008474 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008475 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008476 "Only the attributes 'minOccurs', 'maxOccurs' and "
8477 "'id' are allowed in addition to 'ref'");
8478 break;
8479 }
8480 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8481 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008482 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008483 }
8484 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008485 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008486 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008487 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008488 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008489 if (child != NULL) {
8490 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008491 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008492 }
8493 if ((min == 0) && (max == 0))
8494 goto return_null;
8495 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008496 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008497 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008498 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008499 ref, refNs);
8500 if (refer == NULL)
8501 goto return_null;
8502 particle->children = (xmlSchemaTreeItemPtr) refer;
8503 particle->annot = annot;
8504 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008505 * Add the particle to pending components, since the reference
8506 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008507 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008508 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008509 return ((xmlSchemaBasicItemPtr) particle);
8510 }
8511 /*
8512 * The declaration part ===============================================
8513 */
8514declaration_part:
8515 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008516 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008517 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8518
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008519 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008520 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008521 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008522 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008523 * Evaluate the target namespace.
8524 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008525 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008526 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008527 } else {
8528 attr = xmlSchemaGetPropNode(node, "form");
8529 if (attr != NULL) {
8530 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8531 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008532 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008533 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008534 xmlSchemaPSimpleTypeErr(ctxt,
8535 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8536 NULL, (xmlNodePtr) attr,
8537 NULL, "(qualified | unqualified)",
8538 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008539 }
8540 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008541 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008542 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008543 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008544 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008545 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008546 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008547 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008548 * Check for illegal attributes.
8549 */
William M. Bracke7091952004-05-11 15:09:58 +00008550 attr = node->properties;
8551 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008552 if (attr->ns == NULL) {
8553 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8554 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008555 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008556 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008557 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008558 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008559 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8560 {
8561 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008562 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008563 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008564 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008565 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008566 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008567 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008568 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008569 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8570 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008571 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8572
8573 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008574 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008575 }
8576 }
8577 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008578
Daniel Veillardc0826a72004-08-10 14:17:33 +00008579 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008580 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008581 }
8582 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008583 }
William M. Bracke7091952004-05-11 15:09:58 +00008584 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008585 * Extract/validate attributes.
8586 */
8587 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008588 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008589 * Process top attributes of global element declarations here.
8590 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008591 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8592 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008593 xmlSchemaPValAttrQName(ctxt, schema,
8594 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008595 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008596 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008597 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008598 /*
8599 * Attribute "final".
8600 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008601 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008602 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008603 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8604 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8605 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8606 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008607 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008608 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8609 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008610 -1,
8611 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8612 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008613 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008614 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008615 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008616 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008617 attrValue, NULL, NULL, NULL);
8618 }
8619 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008620 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008621 /*
8622 * Attribute "block".
8623 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008624 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008625 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008626 /*
8627 * Apply default "block" values.
8628 */
8629 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8630 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8631 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8632 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8633 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8634 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008635 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008636 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8637 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008638 -1,
8639 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008640 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008641 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8642 xmlSchemaPSimpleTypeErr(ctxt,
8643 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008644 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008645 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008646 "restriction | substitution))", attrValue,
8647 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008648 }
8649 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008650 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008652
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008653 attr = xmlSchemaGetPropNode(node, "type");
8654 if (attr != NULL) {
8655 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008656 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008657 &(decl->namedTypeNs), &(decl->namedType));
8658 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008659 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008660 }
8661 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8662 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008663 if (attr != NULL) {
8664 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008665 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008666 /*
8667 * 3.3.3 : 1
8668 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008669 */
8670 xmlSchemaPMutualExclAttrErr(ctxt,
8671 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008672 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008673 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008674 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8675 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008676 }
William M. Bracke7091952004-05-11 15:09:58 +00008677 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008678 /*
8679 * And now for the children...
8680 */
William M. Bracke7091952004-05-11 15:09:58 +00008681 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008682 /*
8683 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008684 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008685 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008686 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008687 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008688 xmlSchemaPContentErr(ctxt,
8689 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008690 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008692 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008693 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008694 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008695 child = child->next;
8696 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008697 /*
8698 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008699 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008700 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008701 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008702 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008703 xmlSchemaPContentErr(ctxt,
8704 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008705 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008706 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008708 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008709 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008710 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008711 }
William M. Bracke7091952004-05-11 15:09:58 +00008712 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008713 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008714 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008715 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008716 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008717 } else if (IS_SCHEMA(child, "key")) {
8718 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008719 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008720 } else if (IS_SCHEMA(child, "keyref")) {
8721 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008722 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008723 }
8724 if (lastIDC != NULL)
8725 lastIDC->next = curIDC;
8726 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008727 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008728 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008729 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008730 }
8731 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008732 xmlSchemaPContentErr(ctxt,
8733 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008734 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008735 NULL, "(annotation?, ((simpleType | complexType)?, "
8736 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008737 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008738 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008739 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008740 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008741 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008742 * different layer.
8743 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008744 FREE_AND_NULL(des)
8745 if (topLevel)
8746 return ((xmlSchemaBasicItemPtr) decl);
8747 else {
8748 particle->children = (xmlSchemaTreeItemPtr) decl;
8749 return ((xmlSchemaBasicItemPtr) particle);
8750 }
8751
8752return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008753 FREE_AND_NULL(des);
8754 if (annot != NULL) {
8755 if (particle != NULL)
8756 particle->annot = NULL;
8757 if (decl != NULL)
8758 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008759 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008760 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008761 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008762}
8763
8764/**
8765 * xmlSchemaParseUnion:
8766 * @ctxt: a schema validation context
8767 * @schema: the schema being built
8768 * @node: a subtree containing XML Schema informations
8769 *
8770 * parse a XML schema Union definition
8771 * *WARNING* this interface is highly subject to change
8772 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008773 * Returns -1 in case of internal error, 0 in case of success and a positive
8774 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008775 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008776static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008777xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008778 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008779{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008780 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008781 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008782 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008783 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008784
8785 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008786 return (-1);
8787 /* Not a component, don't create it. */
8788 type = ctxt->ctxtType;
8789 /*
8790 * Mark the simple type as being of variety "union".
8791 */
8792 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008793 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008794 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8795 * then the ·simple ur-type definition·."
8796 */
8797 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008798 /*
8799 * Check for illegal attributes.
8800 */
8801 attr = node->properties;
8802 while (attr != NULL) {
8803 if (attr->ns == NULL) {
8804 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8805 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008806 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008807 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008808 }
8809 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008810 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008811 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008812 }
8813 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008814 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008815 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008816 /*
8817 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008818 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008819 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008820 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008821 if (attr != NULL) {
8822 const xmlChar *end;
8823 xmlChar *tmp;
8824 const xmlChar *localName, *nsName;
8825 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8826 xmlSchemaQNameRefPtr ref;
8827
8828 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008829 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008830 do {
8831 while (IS_BLANK_CH(*cur))
8832 cur++;
8833 end = cur;
8834 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8835 end++;
8836 if (end == cur)
8837 break;
8838 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008839 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008840 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008841 /*
8842 * Create the member type link.
8843 */
8844 link = (xmlSchemaTypeLinkPtr)
8845 xmlMalloc(sizeof(xmlSchemaTypeLink));
8846 if (link == NULL) {
8847 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8848 "allocating a type link", NULL);
8849 return (-1);
8850 }
8851 link->type = NULL;
8852 link->next = NULL;
8853 if (lastLink == NULL)
8854 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008855 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008856 lastLink->next = link;
8857 lastLink = link;
8858 /*
8859 * Create a reference item.
8860 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008861 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008862 localName, nsName);
8863 if (ref == NULL) {
8864 FREE_AND_NULL(tmp)
8865 return (-1);
8866 }
8867 /*
8868 * Assign the reference to the link, it will be resolved
8869 * later during fixup of the union simple type.
8870 */
8871 link->type = (xmlSchemaTypePtr) ref;
8872 }
8873 FREE_AND_NULL(tmp)
8874 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008875 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008876
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008877 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008878 /*
8879 * And now for the children...
8880 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008881 child = node->children;
8882 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008883 /*
8884 * Add the annotation to the simple type ancestor.
8885 */
8886 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008887 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008888 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008889 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008890 if (IS_SCHEMA(child, "simpleType")) {
8891 xmlSchemaTypePtr subtype, last = NULL;
8892
8893 /*
8894 * Anchor the member types in the "subtypes" field of the
8895 * simple type.
8896 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008897 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008898 subtype = (xmlSchemaTypePtr)
8899 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8900 if (subtype != NULL) {
8901 if (last == NULL) {
8902 type->subtypes = subtype;
8903 last = subtype;
8904 } else {
8905 last->next = subtype;
8906 last = subtype;
8907 }
8908 last->next = NULL;
8909 }
8910 child = child->next;
8911 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008912 }
8913 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008914 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008915 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008916 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008917 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008918 if ((attr == NULL) && (type->subtypes == NULL)) {
8919 /*
8920 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008921 * Either the memberTypes [attribute] of the <union> element must
8922 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008923 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008924 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008925 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008926 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008927 "Either the attribute 'memberTypes' or "
8928 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008929 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008930 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008931}
8932
8933/**
8934 * xmlSchemaParseList:
8935 * @ctxt: a schema validation context
8936 * @schema: the schema being built
8937 * @node: a subtree containing XML Schema informations
8938 *
8939 * parse a XML schema List definition
8940 * *WARNING* this interface is highly subject to change
8941 *
William M. Bracke7091952004-05-11 15:09:58 +00008942 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008943 * 1 in case of success.
8944 */
8945static xmlSchemaTypePtr
8946xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008947 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008948{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008949 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008950 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008951 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008952
8953 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8954 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008955 /* Not a component, don't create it. */
8956 type = ctxt->ctxtType;
8957 /*
8958 * Mark the type as being of variety "list".
8959 */
8960 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008961 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008962 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8963 * then the ·simple ur-type definition·."
8964 */
8965 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008966 /*
8967 * Check for illegal attributes.
8968 */
8969 attr = node->properties;
8970 while (attr != NULL) {
8971 if (attr->ns == NULL) {
8972 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8973 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008974 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008975 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008976 }
8977 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008978 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008979 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008980 }
8981 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008982 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008983
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008984 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008985
William M. Brack2f2a6632004-08-20 23:09:47 +00008986 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008987 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
8988 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008989 *
8990 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
8991 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00008992 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008993 xmlSchemaPValAttrQName(ctxt, schema, NULL,
8994 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00008995 /*
8996 * And now for the children...
8997 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008998 child = node->children;
8999 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009000 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009001 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009002 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009003 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009004 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009005 /*
9006 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009007 * Either the itemType [attribute] or the <simpleType> [child] of
9008 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009009 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009010 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009011 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009012 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009013 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009014 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009015 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009016 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009017 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009018 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009019 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009020 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009021 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009022 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009023 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009024 "Either the attribute 'itemType' or the <simpleType> child "
9025 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009026 }
9027 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009028 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009029 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009030 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009031 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009032 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009033 (type->subtypes == NULL) &&
9034 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009035 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009036 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009037 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009038 "Either the attribute 'itemType' or the <simpleType> child "
9039 "must be present", NULL);
9040 }
9041 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009042}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009043
Daniel Veillard4255d502002-04-16 15:50:10 +00009044/**
9045 * xmlSchemaParseSimpleType:
9046 * @ctxt: a schema validation context
9047 * @schema: the schema being built
9048 * @node: a subtree containing XML Schema informations
9049 *
9050 * parse a XML schema Simple Type definition
9051 * *WARNING* this interface is highly subject to change
9052 *
William M. Bracke7091952004-05-11 15:09:58 +00009053 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009054 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009055 */
9056static xmlSchemaTypePtr
9057xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009058 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009059{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009060 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009061 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009062 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009063 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009064 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009065
9066 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9067 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009068
Daniel Veillardc0826a72004-08-10 14:17:33 +00009069 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009070 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009071 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009072 xmlSchemaPMissingAttrErr(ctxt,
9073 XML_SCHEMAP_S4S_ATTR_MISSING,
9074 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009075 "name", NULL);
9076 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009077 } else {
9078 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009079 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009080 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9081 return (NULL);
9082 /*
9083 * Skip built-in types.
9084 */
9085 if (ctxt->isS4S) {
9086 xmlSchemaTypePtr biType;
9087
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009088 if (ctxt->isRedefine) {
9089 /*
9090 * REDEFINE: Disallow redefinition of built-in-types.
9091 * TODO: It seems that the spec does not say anything
9092 * about this case.
9093 */
9094 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009095 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009096 "Redefinition of built-in simple types is not "
9097 "supported", NULL);
9098 return(NULL);
9099 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009100 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9101 if (biType != NULL)
9102 return (biType);
9103 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009104 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009105 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009106 /*
9107 * TargetNamespace:
9108 * SPEC "The ·actual value· of the targetNamespace [attribute]
9109 * of the <schema> ancestor element information item if present,
9110 * otherwise ·absent·.
9111 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009112 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009113#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009114 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009115#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009116 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009117 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009118 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009119#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009120 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009121 type = xmlSchemaAddType(ctxt, schema,
9122 XML_SCHEMA_TYPE_SIMPLE,
9123 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009124 ctxt->targetNamespace, node, 0);
9125#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009126 type = xmlSchemaAddType(ctxt, schema,
9127 XML_SCHEMA_TYPE_SIMPLE,
9128 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009129#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009130 if (type == NULL)
9131 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009132 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009133 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009134 /*
9135 * Check for illegal attributes.
9136 */
9137 attr = node->properties;
9138 while (attr != NULL) {
9139 if (attr->ns == NULL) {
9140 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009141 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009142 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009143 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009144 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009145 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009146 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009147 }
9148 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009149 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009150 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009151 /*
9152 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009153 *
9154 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009155 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009156 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9157 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009158 if (type == NULL)
9159 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009160 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009161 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009162 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9163 /*
9164 * Check for illegal attributes.
9165 */
9166 attr = node->properties;
9167 while (attr != NULL) {
9168 if (attr->ns == NULL) {
9169 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9170 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009171 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009172 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009173 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009174 }
9175 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009176 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009177 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009178 }
9179 attr = attr->next;
9180 }
9181 /*
9182 * Attribute "final".
9183 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009184 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009185 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009186 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9187 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9188 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9189 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9190 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9191 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009192 } else {
9193 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009194 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9195 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009196 XML_SCHEMAS_TYPE_FINAL_LIST,
9197 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9198
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009199 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009200 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009201 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009202 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009203 attrValue, NULL, NULL, NULL);
9204 }
9205 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009206 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009207 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009208 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009209 /*
9210 * And now for the children...
9211 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009212 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009213
Daniel Veillardc0826a72004-08-10 14:17:33 +00009214 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009215
Daniel Veillard4255d502002-04-16 15:50:10 +00009216 child = node->children;
9217 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009218 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009219 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009220 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009221 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009222 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009223 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009224 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009225 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009226 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009227 XML_SCHEMA_TYPE_SIMPLE);
9228 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009229 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009230 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009231 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009232 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009233 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009234 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009235 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009236 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009237 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009238 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009239 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009240 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009241 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009242 /*
9243 * REDEFINE: SPEC src-redefine (5)
9244 * "Within the [children], each <simpleType> must have a
9245 * <restriction> among its [children] ... the ·actual value· of whose
9246 * base [attribute] must be the same as the ·actual value· of its own
9247 * name attribute plus target namespace;"
9248 */
9249 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9250 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009251 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009252 "<simpleType> must have a <restriction> child", NULL);
9253 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009254
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009255 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009256 return (type);
9257}
9258
Daniel Veillard4255d502002-04-16 15:50:10 +00009259/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009260 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009261 * @ctxt: the parser context
9262 * @schema: the schema being built
9263 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009264 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009265 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009266 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009267 * We will return a particle component with a qname-component or
9268 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009269 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009270static xmlSchemaTreeItemPtr
9271xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9272 xmlSchemaPtr schema,
9273 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009274{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009275 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009276 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009277 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009278 const xmlChar *ref = NULL, *refNs = NULL;
9279 int min, max;
9280
9281 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009282 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009283
9284 attr = xmlSchemaGetPropNode(node, "ref");
9285 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009286 xmlSchemaPMissingAttrErr(ctxt,
9287 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009288 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009289 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009290 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009291 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009292 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009293 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009294 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009295 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009296 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009297 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009298 /*
9299 * Check for illegal attributes.
9300 */
9301 attr = node->properties;
9302 while (attr != NULL) {
9303 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009304 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009305 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9306 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9307 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009308 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009309 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009310 }
9311 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009312 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009313 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009314 }
9315 attr = attr->next;
9316 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009317 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009318 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9319 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009320 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009321 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009322 * Create a qname-reference and set as the term; it will be substituted
9323 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009324 */
9325 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009326 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009327 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9328 /*
9329 * And now for the children...
9330 */
9331 child = node->children;
9332 /* TODO: Is annotation even allowed for a model group reference? */
9333 if (IS_SCHEMA(child, "annotation")) {
9334 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009335 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009336 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009337 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009338 child = child->next;
9339 }
9340 if (child != NULL) {
9341 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009342 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009343 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009344 "(annotation?)");
9345 }
9346 /*
9347 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9348 */
9349 if ((min == 0) && (max == 0))
9350 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009351
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009352 return ((xmlSchemaTreeItemPtr) item);
9353}
9354
9355/**
9356 * xmlSchemaParseModelGroupDefinition:
9357 * @ctxt: a schema validation context
9358 * @schema: the schema being built
9359 * @node: a subtree containing XML Schema informations
9360 *
9361 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009362 *
9363 * Note that the contraint src-redefine (6.2) can't be applied until
9364 * references have been resolved. So we will do this at the
9365 * component fixup level.
9366 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009367 * *WARNING* this interface is highly subject to change
9368 *
9369 * Returns -1 in case of error, 0 if the declaration is improper and
9370 * 1 in case of success.
9371 */
9372static xmlSchemaModelGroupDefPtr
9373xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9374 xmlSchemaPtr schema,
9375 xmlNodePtr node)
9376{
9377 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009378 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009379 xmlAttrPtr attr;
9380 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009381
9382 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009383 return (NULL);
9384
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009385 attr = xmlSchemaGetPropNode(node, "name");
9386 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009387 xmlSchemaPMissingAttrErr(ctxt,
9388 XML_SCHEMAP_S4S_ATTR_MISSING,
9389 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009390 "name", NULL);
9391 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009392 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009393 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9394 return (NULL);
9395 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009396 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9397 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009398 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009399 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 /*
9401 * Check for illegal attributes.
9402 */
9403 attr = node->properties;
9404 while (attr != NULL) {
9405 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009406 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009407 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009408 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009409 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009410 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009411 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009412 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009413 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009414 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009415 attr = attr->next;
9416 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009417 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009418 /*
9419 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009420 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009421 child = node->children;
9422 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009423 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009424 child = child->next;
9425 }
9426 if (IS_SCHEMA(child, "all")) {
9427 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9428 XML_SCHEMA_TYPE_ALL, 0);
9429 child = child->next;
9430 } else if (IS_SCHEMA(child, "choice")) {
9431 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9432 XML_SCHEMA_TYPE_CHOICE, 0);
9433 child = child->next;
9434 } else if (IS_SCHEMA(child, "sequence")) {
9435 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9436 XML_SCHEMA_TYPE_SEQUENCE, 0);
9437 child = child->next;
9438 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009439
9440
9441
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009442 if (child != NULL) {
9443 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009444 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009445 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009446 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009447 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009448 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009449}
9450
9451/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009452 * xmlSchemaCleanupDoc:
9453 * @ctxt: a schema validation context
9454 * @node: the root of the document.
9455 *
9456 * removes unwanted nodes in a schemas document tree
9457 */
9458static void
9459xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9460{
9461 xmlNodePtr delete, cur;
9462
9463 if ((ctxt == NULL) || (root == NULL)) return;
9464
9465 /*
9466 * Remove all the blank text nodes
9467 */
9468 delete = NULL;
9469 cur = root;
9470 while (cur != NULL) {
9471 if (delete != NULL) {
9472 xmlUnlinkNode(delete);
9473 xmlFreeNode(delete);
9474 delete = NULL;
9475 }
9476 if (cur->type == XML_TEXT_NODE) {
9477 if (IS_BLANK_NODE(cur)) {
9478 if (xmlNodeGetSpacePreserve(cur) != 1) {
9479 delete = cur;
9480 }
9481 }
9482 } else if ((cur->type != XML_ELEMENT_NODE) &&
9483 (cur->type != XML_CDATA_SECTION_NODE)) {
9484 delete = cur;
9485 goto skip_children;
9486 }
9487
9488 /*
9489 * Skip to next node
9490 */
9491 if (cur->children != NULL) {
9492 if ((cur->children->type != XML_ENTITY_DECL) &&
9493 (cur->children->type != XML_ENTITY_REF_NODE) &&
9494 (cur->children->type != XML_ENTITY_NODE)) {
9495 cur = cur->children;
9496 continue;
9497 }
9498 }
9499 skip_children:
9500 if (cur->next != NULL) {
9501 cur = cur->next;
9502 continue;
9503 }
9504
9505 do {
9506 cur = cur->parent;
9507 if (cur == NULL)
9508 break;
9509 if (cur == root) {
9510 cur = NULL;
9511 break;
9512 }
9513 if (cur->next != NULL) {
9514 cur = cur->next;
9515 break;
9516 }
9517 } while (cur != NULL);
9518 }
9519 if (delete != NULL) {
9520 xmlUnlinkNode(delete);
9521 xmlFreeNode(delete);
9522 delete = NULL;
9523 }
9524}
9525
William M. Brack2f2a6632004-08-20 23:09:47 +00009526
William M. Brack2f2a6632004-08-20 23:09:47 +00009527static void
9528xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9529{
9530 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9531 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9532
9533 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9534 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9535
9536 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9537 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9538 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9539 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9540 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9541 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9542 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9543 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9544
9545 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9546 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9547 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9548 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9549 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9550 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9551}
9552
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009553static int
9554xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009555 xmlSchemaPtr schema,
9556 xmlNodePtr node)
9557{
9558 xmlAttrPtr attr;
9559 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009560 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009561
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009562 /*
9563 * Those flags should be moved to the parser context flags,
9564 * since they are not visible at the component level. I.e.
9565 * they are used if processing schema *documents* only.
9566 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009567 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009568 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009569
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009570 /*
9571 * Since the version is of type xs:token, we won't bother to
9572 * check it.
9573 */
9574 /* REMOVED:
9575 attr = xmlSchemaGetPropNode(node, "version");
9576 if (attr != NULL) {
9577 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9578 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9579 HFAILURE;
9580 }
9581 */
9582 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9583 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009584 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009585 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9586 HFAILURE;
9587 if (res != 0) {
9588 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9589 goto exit;
9590 }
9591 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009592 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009593 if (attr != NULL) {
9594 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009595 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9596 XML_SCHEMAS_QUALIF_ELEM);
9597 HFAILURE;
9598 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009599 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009600 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009601 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009602 "(qualified | unqualified)", val, NULL, NULL, NULL);
9603 }
9604 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009605 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009606 if (attr != NULL) {
9607 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009608 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9609 XML_SCHEMAS_QUALIF_ATTR);
9610 HFAILURE;
9611 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009612 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009613 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009614 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009615 "(qualified | unqualified)", val, NULL, NULL, NULL);
9616 }
9617 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009618 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009619 if (attr != NULL) {
9620 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009621 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009622 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9623 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9624 -1,
9625 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009626 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9627 HFAILURE;
9628 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009629 xmlSchemaPSimpleTypeErr(ctxt,
9630 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009631 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009632 "(#all | List of (extension | restriction | list | union))",
9633 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009634 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009635 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009636 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009637 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009638 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9639 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009640 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9641 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009642 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9643 HFAILURE;
9644 if (res != 0) {
9645 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009646 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009647 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009648 "(#all | List of (extension | restriction | substitution))",
9649 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009650 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009651 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009652
9653exit:
9654 if (oldErrs != ctxt->nberrors)
9655 res = ctxt->err;
9656 return(res);
9657exit_failure:
9658 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009659}
9660
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009661/**
9662 * xmlSchemaParseSchemaTopLevel:
9663 * @ctxt: a schema validation context
9664 * @schema: the schemas
9665 * @nodes: the list of top level nodes
9666 *
9667 * Returns the internal XML Schema structure built from the resource or
9668 * NULL in case of error
9669 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009670static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009671xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9672 xmlSchemaPtr schema, xmlNodePtr nodes)
9673{
9674 xmlNodePtr child;
9675 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009676 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009677
9678 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009679 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009680
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009681 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009682 child = nodes;
9683 while ((IS_SCHEMA(child, "include")) ||
9684 (IS_SCHEMA(child, "import")) ||
9685 (IS_SCHEMA(child, "redefine")) ||
9686 (IS_SCHEMA(child, "annotation"))) {
9687 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009688 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009689 if (schema->annot == NULL)
9690 schema->annot = annot;
9691 else
9692 xmlSchemaFreeAnnot(annot);
9693 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009694 tmpOldErrs = ctxt->nberrors;
9695 res = xmlSchemaParseImport(ctxt, schema, child);
9696 HFAILURE;
9697 HSTOP(ctxt);
9698 if (tmpOldErrs != ctxt->nberrors)
9699 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009700 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009701 tmpOldErrs = ctxt->nberrors;
9702 res = xmlSchemaParseInclude(ctxt, schema, child);
9703 HFAILURE;
9704 HSTOP(ctxt);
9705 if (tmpOldErrs != ctxt->nberrors)
9706 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009707 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009708 tmpOldErrs = ctxt->nberrors;
9709 res = xmlSchemaParseRedefine(ctxt, schema, child);
9710 HFAILURE;
9711 HSTOP(ctxt);
9712 if (tmpOldErrs != ctxt->nberrors)
9713 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009714 }
9715 child = child->next;
9716 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009717 /*
9718 * URGENT TODO: Change the functions to return int results.
9719 * We need especially to catch internal errors.
9720 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009721 while (child != NULL) {
9722 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009723 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009724 child = child->next;
9725 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009726 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009727 child = child->next;
9728 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009729 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009730 child = child->next;
9731 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009732 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009733 child = child->next;
9734 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009735 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009736 child = child->next;
9737 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009738 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009739 child = child->next;
9740 } else if (IS_SCHEMA(child, "notation")) {
9741 xmlSchemaParseNotation(ctxt, schema, child);
9742 child = child->next;
9743 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009744 xmlSchemaPContentErr(ctxt,
9745 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009746 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009747 NULL, "((include | import | redefine | annotation)*, "
9748 "(((simpleType | complexType | group | attributeGroup) "
9749 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009750 child = child->next;
9751 }
9752 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009753 /*
9754 * TODO: We should add all annotations.
9755 */
9756 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009757 if (schema->annot == NULL)
9758 schema->annot = annot;
9759 else
9760 xmlSchemaFreeAnnot(annot);
9761 child = child->next;
9762 }
9763 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009764exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009765 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009766 if (oldErrs != ctxt->nberrors)
9767 res = ctxt->err;
9768 return(res);
9769exit_failure:
9770 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009771}
9772
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009773static xmlSchemaSchemaRelationPtr
9774xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009775{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009776 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009777
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009778 ret = (xmlSchemaSchemaRelationPtr)
9779 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009780 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009781 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009782 return(NULL);
9783 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009784 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009785 return(ret);
9786}
9787
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009788#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009789static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009790xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009791{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009792 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009793}
9794#endif
9795
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009796static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009797xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9798{
9799 xmlSchemaRedefPtr prev;
9800
9801 while (redef != NULL) {
9802 prev = redef;
9803 redef = redef->next;
9804 xmlFree(prev);
9805 }
9806}
9807
9808static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009809xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9810{
9811 /*
9812 * After the construction context has been freed, there will be
9813 * no schema graph available any more. Only the schema buckets
9814 * will stay alive, which are put into the "schemasImports" and
9815 * "includes" slots of the xmlSchema.
9816 */
9817 if (con->buckets != NULL)
9818 xmlSchemaItemListFree(con->buckets);
9819 if (con->pending != NULL)
9820 xmlSchemaItemListFree(con->pending);
9821 if (con->substGroups != NULL)
9822 xmlHashFree(con->substGroups,
9823 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009824 if (con->redefs != NULL)
9825 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009826 if (con->dict != NULL)
9827 xmlDictFree(con->dict);
9828 xmlFree(con);
9829}
9830
9831static xmlSchemaConstructionCtxtPtr
9832xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9833{
9834 xmlSchemaConstructionCtxtPtr ret;
9835
9836 ret = (xmlSchemaConstructionCtxtPtr)
9837 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9838 if (ret == NULL) {
9839 xmlSchemaPErrMemory(NULL,
9840 "allocating schema construction context", NULL);
9841 return (NULL);
9842 }
9843 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9844
9845 ret->buckets = xmlSchemaItemListCreate();
9846 if (ret->buckets == NULL) {
9847 xmlSchemaPErrMemory(NULL,
9848 "allocating list of schema buckets", NULL);
9849 xmlFree(ret);
9850 return (NULL);
9851 }
9852 ret->pending = xmlSchemaItemListCreate();
9853 if (ret->pending == NULL) {
9854 xmlSchemaPErrMemory(NULL,
9855 "allocating list of pending global components", NULL);
9856 xmlSchemaConstructionCtxtFree(ret);
9857 return (NULL);
9858 }
9859 ret->dict = dict;
9860 xmlDictReference(dict);
9861 return(ret);
9862}
9863
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009864static xmlSchemaParserCtxtPtr
9865xmlSchemaParserCtxtCreate(void)
9866{
9867 xmlSchemaParserCtxtPtr ret;
9868
9869 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9870 if (ret == NULL) {
9871 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9872 NULL);
9873 return (NULL);
9874 }
9875 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9876 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009877 ret->attrProhibs = xmlSchemaItemListCreate();
9878 if (ret->attrProhibs == NULL) {
9879 xmlFree(ret);
9880 return(NULL);
9881 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009882 return(ret);
9883}
9884
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009885/**
9886 * xmlSchemaNewParserCtxtUseDict:
9887 * @URL: the location of the schema
9888 * @dict: the dictionary to be used
9889 *
9890 * Create an XML Schemas parse context for that file/resource expected
9891 * to contain an XML Schemas file.
9892 *
9893 * Returns the parser context or NULL in case of error
9894 */
9895static xmlSchemaParserCtxtPtr
9896xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9897{
9898 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009899
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009900 ret = xmlSchemaParserCtxtCreate();
9901 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009902 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009903 ret->dict = dict;
9904 xmlDictReference(dict);
9905 if (URL != NULL)
9906 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009907 return (ret);
9908}
9909
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009910static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009911xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9912{
9913 if (vctxt->pctxt == NULL) {
9914 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009915 vctxt->pctxt =
9916 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009917 else
9918 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9919 if (vctxt->pctxt == NULL) {
9920 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9921 "failed to create a temp. parser context");
9922 return (-1);
9923 }
9924 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009925 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
9926 vctxt->warning, vctxt->userData);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009927 }
9928 return (0);
9929}
9930
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009931/**
9932 * xmlSchemaGetSchemaBucket:
9933 * @pctxt: the schema parser context
9934 * @schemaLocation: the URI of the schema document
9935 *
9936 * Returns a schema bucket if it was already parsed.
9937 *
9938 * Returns a schema bucket if it was already parsed from
9939 * @schemaLocation, NULL otherwise.
9940 */
9941static xmlSchemaBucketPtr
9942xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9943 const xmlChar *schemaLocation)
9944{
9945 xmlSchemaBucketPtr cur;
9946 xmlSchemaItemListPtr list;
9947
9948 list = pctxt->constructor->buckets;
9949 if (list->nbItems == 0)
9950 return(NULL);
9951 else {
9952 int i;
9953 for (i = 0; i < list->nbItems; i++) {
9954 cur = (xmlSchemaBucketPtr) list->items[i];
9955 /* Pointer comparison! */
9956 if (cur->schemaLocation == schemaLocation)
9957 return(cur);
9958 }
9959 }
9960 return(NULL);
9961}
9962
9963static xmlSchemaBucketPtr
9964xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9965 const xmlChar *schemaLocation,
9966 const xmlChar *targetNamespace)
9967{
9968 xmlSchemaBucketPtr cur;
9969 xmlSchemaItemListPtr list;
9970
9971 list = pctxt->constructor->buckets;
9972 if (list->nbItems == 0)
9973 return(NULL);
9974 else {
9975 int i;
9976 for (i = 0; i < list->nbItems; i++) {
9977 cur = (xmlSchemaBucketPtr) list->items[i];
9978 /* Pointer comparison! */
9979 if ((cur->origTargetNamespace == NULL) &&
9980 (cur->schemaLocation == schemaLocation) &&
9981 (cur->targetNamespace == targetNamespace))
9982 return(cur);
9983 }
9984 }
9985 return(NULL);
9986}
9987
9988
9989#define IS_BAD_SCHEMA_DOC(b) \
9990 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
9991
9992static xmlSchemaBucketPtr
9993xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
9994 const xmlChar *targetNamespace,
9995 int imported)
9996{
9997 xmlSchemaBucketPtr cur;
9998 xmlSchemaItemListPtr list;
9999
10000 list = pctxt->constructor->buckets;
10001 if (list->nbItems == 0)
10002 return(NULL);
10003 else {
10004 int i;
10005 for (i = 0; i < list->nbItems; i++) {
10006 cur = (xmlSchemaBucketPtr) list->items[i];
10007 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10008 (cur->origTargetNamespace == targetNamespace) &&
10009 ((imported && cur->imported) ||
10010 ((!imported) && (!cur->imported))))
10011 return(cur);
10012 }
10013 }
10014 return(NULL);
10015}
10016
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010017static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010018xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10019 xmlSchemaPtr schema,
10020 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010021{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010022 int oldFlags;
10023 xmlDocPtr oldDoc;
10024 xmlNodePtr node;
10025 int ret, oldErrs;
10026 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10027
10028 /*
10029 * Save old values; reset the *main* schema.
10030 * URGENT TODO: This is not good; move the per-document information
10031 * to the parser.
10032 */
10033 oldFlags = schema->flags;
10034 oldDoc = schema->doc;
10035 if (schema->flags != 0)
10036 xmlSchemaClearSchemaDefaults(schema);
10037 schema->doc = bucket->doc;
10038 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10039 pctxt->schema = schema;
10040 /*
10041 * Keep the current target namespace on the parser *not* on the
10042 * main schema.
10043 */
10044 pctxt->targetNamespace = bucket->targetNamespace;
10045 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010046
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010047 if ((bucket->targetNamespace != NULL) &&
10048 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010049 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010050 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010051 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010052 pctxt->isS4S = 1;
10053 }
10054 /* Mark it as parsed, even if parsing fails. */
10055 bucket->parsed++;
10056 /* Compile the schema doc. */
10057 node = xmlDocGetRootElement(bucket->doc);
10058 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10059 if (ret != 0)
10060 goto exit;
10061 /* An empty schema; just get out. */
10062 if (node->children == NULL)
10063 goto exit;
10064 oldErrs = pctxt->nberrors;
10065 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10066 if (ret != 0)
10067 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010068 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010069 * TODO: Not nice, but I'm not 100% sure we will get always an error
10070 * as a result of the obove functions; so better rely on pctxt->err
10071 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010072 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010073 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10074 ret = pctxt->err;
10075 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010076 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010077
10078exit:
10079 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10080 /* Restore schema values. */
10081 schema->doc = oldDoc;
10082 schema->flags = oldFlags;
10083 return(ret);
10084}
10085
10086static int
10087xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10088 xmlSchemaPtr schema,
10089 xmlSchemaBucketPtr bucket)
10090{
10091 xmlSchemaParserCtxtPtr newpctxt;
10092 int res = 0;
10093
10094 if (bucket == NULL)
10095 return(0);
10096 if (bucket->parsed) {
10097 PERROR_INT("xmlSchemaParseNewDoc",
10098 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010099 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010100 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010101 if (bucket->doc == NULL) {
10102 PERROR_INT("xmlSchemaParseNewDoc",
10103 "parsing a schema doc, but there's no doc");
10104 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010105 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010106 if (pctxt->constructor == NULL) {
10107 PERROR_INT("xmlSchemaParseNewDoc",
10108 "no constructor");
10109 return(-1);
10110 }
10111 /* Create and init the temporary parser context. */
10112 newpctxt = xmlSchemaNewParserCtxtUseDict(
10113 (const char *) bucket->schemaLocation, pctxt->dict);
10114 if (newpctxt == NULL)
10115 return(-1);
10116 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010117 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010118 * TODO: Can we avoid that the parser knows about the main schema?
10119 * It would be better if he knows about the current schema bucket
10120 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010121 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010122 newpctxt->schema = schema;
10123 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
10124 pctxt->userData);
10125 newpctxt->counter = pctxt->counter;
10126
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010127
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010128 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10129
10130 /* Channel back errors and cleanup the temporary parser context. */
10131 if (res != 0)
10132 pctxt->err = res;
10133 pctxt->nberrors += newpctxt->nberrors;
10134 pctxt->counter = newpctxt->counter;
10135 newpctxt->constructor = NULL;
10136 /* Free the parser context. */
10137 xmlSchemaFreeParserCtxt(newpctxt);
10138 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010139}
William M. Brack2f2a6632004-08-20 23:09:47 +000010140
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010141static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010142xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10143 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010144{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010145 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010146
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010147 if (cur == NULL) {
10148 bucket->relations = rel;
10149 return;
10150 }
10151 while (cur->next != NULL)
10152 cur = cur->next;
10153 cur->next = rel;
10154}
10155
10156
10157static const xmlChar *
10158xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10159 xmlNodePtr ctxtNode)
10160{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010161 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010162 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010163 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010164 if (location != NULL) {
10165 if (ctxtNode == NULL)
10166 return(location);
10167 else {
10168 xmlChar *base, *URI;
10169 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010170
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010171 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10172 if (base == NULL) {
10173 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10174 } else {
10175 URI = xmlBuildURI(location, base);
10176 xmlFree(base);
10177 }
10178 if (URI != NULL) {
10179 ret = xmlDictLookup(dict, URI, -1);
10180 xmlFree(URI);
10181 return(ret);
10182 }
10183 }
10184 }
10185 return(NULL);
10186}
10187
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010188
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010189
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010190/**
10191 * xmlSchemaAddSchemaDoc:
10192 * @pctxt: a schema validation context
10193 * @schema: the schema being built
10194 * @node: a subtree containing XML Schema informations
10195 *
10196 * Parse an included (and to-be-redefined) XML schema document.
10197 *
10198 * Returns 0 on success, a positive error code on errors and
10199 * -1 in case of an internal or API error.
10200 */
10201
10202static int
10203xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10204 int type, /* import or include or redefine */
10205 const xmlChar *schemaLocation,
10206 xmlDocPtr schemaDoc,
10207 const char *schemaBuffer,
10208 int schemaBufferLen,
10209 xmlNodePtr invokingNode,
10210 const xmlChar *sourceTargetNamespace,
10211 const xmlChar *importNamespace,
10212 xmlSchemaBucketPtr *bucket)
10213{
10214 const xmlChar *targetNamespace = NULL;
10215 xmlSchemaSchemaRelationPtr relation = NULL;
10216 xmlDocPtr doc = NULL;
10217 int res = 0, err = 0, located = 0, preserveDoc = 0;
10218 xmlSchemaBucketPtr bkt = NULL;
10219
10220 if (bucket != NULL)
10221 *bucket = NULL;
10222
10223 switch (type) {
10224 case XML_SCHEMA_SCHEMA_IMPORT:
10225 case XML_SCHEMA_SCHEMA_MAIN:
10226 err = XML_SCHEMAP_SRC_IMPORT;
10227 break;
10228 case XML_SCHEMA_SCHEMA_INCLUDE:
10229 err = XML_SCHEMAP_SRC_INCLUDE;
10230 break;
10231 case XML_SCHEMA_SCHEMA_REDEFINE:
10232 err = XML_SCHEMAP_SRC_REDEFINE;
10233 break;
10234 }
10235
10236
10237 /* Special handling for the main schema:
10238 * skip the location and relation logic and just parse the doc.
10239 * We need just a bucket to be returned in this case.
10240 */
10241 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10242 goto doc_load;
10243
10244 /* Note that we expect the location to be an absulute URI. */
10245 if (schemaLocation != NULL) {
10246 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10247 if ((bkt != NULL) &&
10248 (pctxt->constructor->bucket == bkt)) {
10249 /* Report self-imports/inclusions/redefinitions. */
10250
10251 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10252 invokingNode, NULL,
10253 "The schema must not import/include/redefine itself",
10254 NULL, NULL);
10255 goto exit;
10256 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010257 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010258 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010259 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010260 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010261 relation = xmlSchemaSchemaRelationCreate();
10262 if (relation == NULL)
10263 return(-1);
10264 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10265 relation);
10266 relation->type = type;
10267
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010268 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010269 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010270 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010271 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010272 relation->importNamespace = importNamespace;
10273 if (schemaLocation == NULL) {
10274 /*
10275 * No location; this is just an import of the namespace.
10276 * Note that we don't assign a bucket to the relation
10277 * in this case.
10278 */
10279 goto exit;
10280 }
10281 targetNamespace = importNamespace;
10282 }
10283
10284 /* Did we already fetch the doc? */
10285 if (bkt != NULL) {
10286 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010287 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010288 /* We included/redefined and then try to import a schema. */
10289 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10290 invokingNode, NULL,
10291 "The schema document '%s' cannot be imported, since "
10292 "it was already included or redefined",
10293 schemaLocation, NULL);
10294 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010295 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010296 /* We imported and then try to include/redefine a schema. */
10297 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10298 invokingNode, NULL,
10299 "The schema document '%s' cannot be included or "
10300 "redefined, since it was already imported",
10301 schemaLocation, NULL);
10302 goto exit;
10303 }
10304 }
10305
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010306 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010307 /*
10308 * Given that the schemaLocation [attribute] is only a hint, it is open
10309 * to applications to ignore all but the first <import> for a given
10310 * namespace, regardless of the ·actual value· of schemaLocation, but
10311 * such a strategy risks missing useful information when new
10312 * schemaLocations are offered.
10313 *
10314 * We will use the first <import> that comes with a location.
10315 * Further <import>s *with* a location, will result in an error.
10316 * TODO: Better would be to just report a warning here, but
10317 * we'll try it this way until someone complains.
10318 *
10319 * Schema Document Location Strategy:
10320 * 3 Based on the namespace name, identify an existing schema document,
10321 * either as a resource which is an XML document or a <schema> element
10322 * information item, in some local schema repository;
10323 * 5 Attempt to resolve the namespace name to locate such a resource.
10324 *
10325 * NOTE: (3) and (5) are not supported.
10326 */
10327 if (bkt != NULL) {
10328 relation->bucket = bkt;
10329 goto exit;
10330 }
10331 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10332 importNamespace, 1);
10333
10334 if (bkt != NULL) {
10335 relation->bucket = bkt;
10336 if (bkt->schemaLocation == NULL) {
10337 /* First given location of the schema; load the doc. */
10338 bkt->schemaLocation = schemaLocation;
10339 } else {
10340 if (!xmlStrEqual(schemaLocation,
10341 bkt->schemaLocation)) {
10342 /*
10343 * Additional location given; just skip it.
10344 * URGENT TODO: We should report a warning here.
10345 * res = XML_SCHEMAP_SRC_IMPORT;
10346 */
10347 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10348 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10349 invokingNode, NULL,
10350 "Skipping import of schema located at '%s' for the "
10351 "namespace '%s', since this namespace was already "
10352 "imported with the schema located at '%s'",
10353 schemaLocation, importNamespace, bkt->schemaLocation);
10354 }
10355 goto exit;
10356 }
10357 }
10358 /*
10359 * No bucket + first location: load the doc and create a
10360 * bucket.
10361 */
10362 } else {
10363 /* <include> and <redefine> */
10364 if (bkt != NULL) {
10365
10366 if ((bkt->origTargetNamespace == NULL) &&
10367 (bkt->targetNamespace != sourceTargetNamespace)) {
10368 xmlSchemaBucketPtr chamel;
10369
10370 /*
10371 * Chameleon include/redefine: skip loading only if it was
10372 * aleady build for the targetNamespace of the including
10373 * schema.
10374 */
10375 /*
10376 * URGENT TODO: If the schema is a chameleon-include then copy
10377 * the components into the including schema and modify the
10378 * targetNamespace of those components, do nothing otherwise.
10379 * NOTE: This is currently worked-around by compiling the
10380 * chameleon for every destinct including targetNamespace; thus
10381 * not performant at the moment.
10382 * TODO: Check when the namespace in wildcards for chameleons
10383 * needs to be converted: before we built wildcard intersections
10384 * or after.
10385 * Answer: after!
10386 */
10387 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10388 schemaLocation, sourceTargetNamespace);
10389 if (chamel != NULL) {
10390 /* A fitting chameleon was already parsed; NOP. */
10391 relation->bucket = chamel;
10392 goto exit;
10393 }
10394 /*
10395 * We need to parse the chameleon again for a different
10396 * targetNamespace.
10397 * CHAMELEON TODO: Optimize this by only parsing the
10398 * chameleon once, and then copying the components to
10399 * the new targetNamespace.
10400 */
10401 bkt = NULL;
10402 } else {
10403 relation->bucket = bkt;
10404 goto exit;
10405 }
10406 }
10407 }
10408 if ((bkt != NULL) && (bkt->doc != NULL)) {
10409 PERROR_INT("xmlSchemaAddSchemaDoc",
10410 "trying to load a schema doc, but a doc is already "
10411 "assigned to the schema bucket");
10412 goto exit_failure;
10413 }
10414
10415doc_load:
10416 /*
10417 * Load the document.
10418 */
10419 if (schemaDoc != NULL) {
10420 doc = schemaDoc;
10421 /* Don' free this one, since it was provided by the caller. */
10422 preserveDoc = 1;
10423 /* TODO: Does the context or the doc hold the location? */
10424 if (schemaDoc->URL != NULL)
10425 schemaLocation = xmlDictLookup(pctxt->dict,
10426 schemaDoc->URL, -1);
10427
10428 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10429 xmlParserCtxtPtr parserCtxt;
10430
10431 parserCtxt = xmlNewParserCtxt();
10432 if (parserCtxt == NULL) {
10433 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10434 "allocating a parser context", NULL);
10435 goto exit_failure;
10436 }
10437 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10438 /*
10439 * TODO: Do we have to burden the schema parser dict with all
10440 * the content of the schema doc?
10441 */
10442 xmlDictFree(parserCtxt->dict);
10443 parserCtxt->dict = pctxt->dict;
10444 xmlDictReference(parserCtxt->dict);
10445 }
10446 if (schemaLocation != NULL) {
10447 /* Parse from file. */
10448 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10449 NULL, SCHEMAS_PARSE_OPTIONS);
10450 } else if (schemaBuffer != NULL) {
10451 /* Parse from memory buffer. */
10452 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10453 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10454 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10455 if (doc != NULL)
10456 doc->URL = schemaLocation;
10457 }
10458 /*
10459 * For <import>:
10460 * 2.1 The referent is (a fragment of) a resource which is an
10461 * XML document (see clause 1.1), which in turn corresponds to
10462 * a <schema> element information item in a well-formed information
10463 * set, which in turn corresponds to a valid schema.
10464 * TODO: (2.1) fragments of XML documents are not supported.
10465 *
10466 * 2.2 The referent is a <schema> element information item in
10467 * a well-formed information set, which in turn corresponds
10468 * to a valid schema.
10469 * TODO: (2.2) is not supported.
10470 */
10471 if (doc == NULL) {
10472 xmlErrorPtr lerr;
10473 lerr = xmlGetLastError();
10474 /*
10475 * Check if this a parser error, or if the document could
10476 * just not be located.
10477 * TODO: Try to find specific error codes to react only on
10478 * localisation failures.
10479 */
10480 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10481 /*
10482 * We assume a parser error here.
10483 */
10484 located = 1;
10485 /* TODO: Error code ?? */
10486 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10487 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10488 invokingNode, NULL,
10489 "Failed to parse the XML resource '%s'",
10490 schemaLocation, NULL);
10491 }
10492 }
10493 xmlFreeParserCtxt(parserCtxt);
10494 if ((doc == NULL) && located)
10495 goto exit_error;
10496 } else {
10497 xmlSchemaPErr(pctxt, NULL,
10498 XML_SCHEMAP_NOTHING_TO_PARSE,
10499 "No information for parsing was provided with the "
10500 "given schema parser context.\n",
10501 NULL, NULL);
10502 goto exit_failure;
10503 }
10504 /*
10505 * Preprocess the document.
10506 */
10507 if (doc != NULL) {
10508 xmlNodePtr docElem = NULL;
10509
10510 located = 1;
10511 docElem = xmlDocGetRootElement(doc);
10512 if (docElem == NULL) {
10513 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10514 invokingNode, NULL,
10515 "The document '%s' has no document element",
10516 schemaLocation, NULL);
10517 xmlFreeDoc(doc);
10518 doc = NULL;
10519 goto exit_error;
10520 }
10521 /*
10522 * Remove all the blank text nodes.
10523 */
10524 xmlSchemaCleanupDoc(pctxt, docElem);
10525 /*
10526 * Check the schema's top level element.
10527 */
10528 if (!IS_SCHEMA(docElem, "schema")) {
10529 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10530 invokingNode, NULL,
10531 "The XML document '%s' is not a schema document",
10532 schemaLocation, NULL);
10533 xmlFreeDoc(doc);
10534 doc = NULL;
10535 goto exit_error;
10536 }
10537 /*
10538 * Note that we don't apply a type check for the
10539 * targetNamespace value here.
10540 */
10541 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10542 "targetNamespace");
10543 }
10544
10545/* after_doc_loading: */
10546 if ((bkt == NULL) && located) {
10547 /* Only create a bucket if the schema was located. */
10548 bkt = xmlSchemaBucketCreate(pctxt, type,
10549 targetNamespace);
10550 if (bkt == NULL)
10551 goto exit_failure;
10552 }
10553 if (bkt != NULL) {
10554 bkt->schemaLocation = schemaLocation;
10555 bkt->located = located;
10556 if (doc != NULL) {
10557 bkt->doc = doc;
10558 bkt->targetNamespace = targetNamespace;
10559 bkt->origTargetNamespace = targetNamespace;
10560 if (preserveDoc)
10561 bkt->preserveDoc = 1;
10562 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010563 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010564 bkt->imported++;
10565 /*
10566 * Add it to the graph of schemas.
10567 */
10568 if (relation != NULL)
10569 relation->bucket = bkt;
10570 }
10571
10572exit:
10573 /*
10574 * Return the bucket explicitely; this is needed for the
10575 * main schema.
10576 */
10577 if (bucket != NULL)
10578 *bucket = bkt;
10579 return (0);
10580
10581exit_error:
10582 if ((doc != NULL) && (! preserveDoc)) {
10583 xmlFreeDoc(doc);
10584 if (bkt != NULL)
10585 bkt->doc = NULL;
10586 }
10587 return(pctxt->err);
10588
10589exit_failure:
10590 if ((doc != NULL) && (! preserveDoc)) {
10591 xmlFreeDoc(doc);
10592 if (bkt != NULL)
10593 bkt->doc = NULL;
10594 }
10595 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010596}
10597
William M. Brack2f2a6632004-08-20 23:09:47 +000010598/**
10599 * xmlSchemaParseImport:
10600 * @ctxt: a schema validation context
10601 * @schema: the schema being built
10602 * @node: a subtree containing XML Schema informations
10603 *
10604 * parse a XML schema Import definition
10605 * *WARNING* this interface is highly subject to change
10606 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010607 * Returns 0 in case of success, a positive error code if
10608 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010609 */
10610static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010611xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010612 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010613{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010614 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010615 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10616 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010617 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010618 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010619 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010620
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010621 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010622 return (-1);
10623
10624 /*
10625 * Check for illegal attributes.
10626 */
10627 attr = node->properties;
10628 while (attr != NULL) {
10629 if (attr->ns == NULL) {
10630 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10631 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10632 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010633 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010634 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010635 }
10636 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010637 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010638 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010639 }
10640 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010641 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010642 /*
10643 * Extract and validate attributes.
10644 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010645 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010646 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010647 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010648 xmlSchemaPSimpleTypeErr(pctxt,
10649 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010650 NULL, node,
10651 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010652 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010653 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010654 }
10655
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010656 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010657 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010658 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010659 xmlSchemaPSimpleTypeErr(pctxt,
10660 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010661 NULL, node,
10662 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010663 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010664 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010665 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010666 /*
10667 * And now for the children...
10668 */
10669 child = node->children;
10670 if (IS_SCHEMA(child, "annotation")) {
10671 /*
10672 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010673 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010674 */
10675 child = child->next;
10676 }
10677 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010678 xmlSchemaPContentErr(pctxt,
10679 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010680 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010681 "(annotation?)");
10682 }
10683 /*
10684 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010685 *
10686 * Note that it is important to use the original @targetNamespace
10687 * (or none at all), to rule out imports of schemas _with_ a
10688 * @targetNamespace if the importing schema is a chameleon schema
10689 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010690 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010691 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010692 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010693 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010694 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10695 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010696 * targetNamespace [attribute].
10697 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010698 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010699 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010700 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010701 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010702 "The value of the attribute 'namespace' must not match "
10703 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010704 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010705 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010706 }
10707 } else {
10708 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010709 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010710 * <schema> must have a targetNamespace [attribute].
10711 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010712 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010713 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010714 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010715 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010716 "The attribute 'namespace' must be existent if "
10717 "the importing schema has no target namespace",
10718 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010719 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010720 }
10721 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010722 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010723 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010724 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010725 if (schemaLocation != NULL)
10726 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10727 schemaLocation, node);
10728 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010729 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010730 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010731
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010732 if (ret != 0)
10733 return(ret);
10734
10735 /*
10736 * For <import>: "It is *not* an error for the application
10737 * schema reference strategy to fail."
10738 * So just don't parse if no schema document was found.
10739 * Note that we will get no bucket if the schema could not be
10740 * located or if there was no schemaLocation.
10741 */
10742 if ((bucket == NULL) && (schemaLocation != NULL)) {
10743 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10744 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10745 node, NULL,
10746 "Failed to locate a schema at location '%s'. "
10747 "Skipping the import", schemaLocation, NULL, NULL);
10748 }
10749
10750 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10751 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10752 }
10753
10754 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010755}
10756
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010757static int
10758xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10759 xmlSchemaPtr schema,
10760 xmlNodePtr node,
10761 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010762 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010763{
10764 xmlAttrPtr attr;
10765
10766 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10767 (schemaLocation == NULL))
10768 return (-1);
10769
10770 *schemaLocation = NULL;
10771 /*
10772 * Check for illegal attributes.
10773 * Applies for both <include> and <redefine>.
10774 */
10775 attr = node->properties;
10776 while (attr != NULL) {
10777 if (attr->ns == NULL) {
10778 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10779 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10780 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010781 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010782 }
10783 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10784 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010785 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010786 }
10787 attr = attr->next;
10788 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010789 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010790 /*
10791 * Preliminary step, extract the URI-Reference and make an URI
10792 * from the base.
10793 */
10794 /*
10795 * Attribute "schemaLocation" is mandatory.
10796 */
10797 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10798 if (attr != NULL) {
10799 xmlChar *base = NULL;
10800 xmlChar *uri = NULL;
10801
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010802 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010803 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10804 (const xmlChar **) schemaLocation) != 0)
10805 goto exit_error;
10806 base = xmlNodeGetBase(node->doc, node);
10807 if (base == NULL) {
10808 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10809 } else {
10810 uri = xmlBuildURI(*schemaLocation, base);
10811 xmlFree(base);
10812 }
10813 if (uri == NULL) {
10814 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10815 "could not build an URI from the schemaLocation")
10816 goto exit_failure;
10817 }
10818 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10819 xmlFree(uri);
10820 } else {
10821 xmlSchemaPMissingAttrErr(pctxt,
10822 XML_SCHEMAP_S4S_ATTR_MISSING,
10823 NULL, node, "schemaLocation", NULL);
10824 goto exit_error;
10825 }
10826 /*
10827 * Report self-inclusion and self-redefinition.
10828 */
10829 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010830 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010831 xmlSchemaPCustomErr(pctxt,
10832 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010833 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010834 "The schema document '%s' cannot redefine itself.",
10835 *schemaLocation);
10836 } else {
10837 xmlSchemaPCustomErr(pctxt,
10838 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010839 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010840 "The schema document '%s' cannot include itself.",
10841 *schemaLocation);
10842 }
10843 goto exit_error;
10844 }
10845
10846 return(0);
10847exit_error:
10848 return(pctxt->err);
10849exit_failure:
10850 return(-1);
10851}
10852
10853static int
10854xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10855 xmlSchemaPtr schema,
10856 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010857 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010858{
10859 xmlNodePtr child = NULL;
10860 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010861 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010862 int isChameleon = 0, wasChameleon = 0;
10863 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010864
10865 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10866 return (-1);
10867
10868 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010869 * Parse attributes. Note that the returned schemaLocation will
10870 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010871 */
10872 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010873 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010874 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010875 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010876 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010877 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010878 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010879 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10880 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010881 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010882 return(res);
10883 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010884 * If we get no schema bucket back, then this means that the schema
10885 * document could not be located or was broken XML or was not
10886 * a schema document.
10887 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010888 if ((bucket == NULL) || (bucket->doc == NULL)) {
10889 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10890 /*
10891 * WARNING for <include>:
10892 * We will raise an error if the schema cannot be located
10893 * for inclusions, since the that was the feedback from the
10894 * schema people. I.e. the following spec piece will *not* be
10895 * satisfied:
10896 * SPEC src-include: "It is not an error for the ·actual value· of the
10897 * schemaLocation [attribute] to fail to resolve it all, in which
10898 * case no corresponding inclusion is performed.
10899 * So do we need a warning report here?"
10900 */
10901 res = XML_SCHEMAP_SRC_INCLUDE;
10902 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10903 node, NULL,
10904 "Failed to load the document '%s' for inclusion",
10905 schemaLocation, NULL);
10906 } else {
10907 /*
10908 * NOTE: This was changed to raise an error even if no redefinitions
10909 * are specified.
10910 *
10911 * SPEC src-redefine (1)
10912 * "If there are any element information items among the [children]
10913 * other than <annotation> then the ·actual value· of the
10914 * schemaLocation [attribute] must successfully resolve."
10915 * TODO: Ask the WG if a the location has always to resolve
10916 * here as well!
10917 */
10918 res = XML_SCHEMAP_SRC_REDEFINE;
10919 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10920 node, NULL,
10921 "Failed to load the document '%s' for redefinition",
10922 schemaLocation, NULL);
10923 }
10924 } else {
10925 /*
10926 * Check targetNamespace sanity before parsing the new schema.
10927 * TODO: Note that we won't check further content if the
10928 * targetNamespace was bad.
10929 */
10930 if (bucket->origTargetNamespace != NULL) {
10931 /*
10932 * SPEC src-include (2.1)
10933 * "SII has a targetNamespace [attribute], and its ·actual
10934 * value· is identical to the ·actual value· of the targetNamespace
10935 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10936 */
10937 if (pctxt->targetNamespace == NULL) {
10938 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10939 XML_SCHEMAP_SRC_INCLUDE,
10940 node, NULL,
10941 "The target namespace of the included/redefined schema "
10942 "'%s' has to be absent, since the including/redefining "
10943 "schema has no target namespace",
10944 schemaLocation, NULL);
10945 goto exit_error;
10946 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10947 pctxt->targetNamespace)) {
10948 /* TODO: Change error function. */
10949 xmlSchemaPCustomErrExt(pctxt,
10950 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010951 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010952 "The target namespace '%s' of the included/redefined "
10953 "schema '%s' differs from '%s' of the "
10954 "including/redefining schema",
10955 bucket->origTargetNamespace, schemaLocation,
10956 pctxt->targetNamespace);
10957 goto exit_error;
10958 }
10959 } else if (pctxt->targetNamespace != NULL) {
10960 /*
10961 * Chameleons: the original target namespace will
10962 * differ from the resulting namespace.
10963 */
10964 isChameleon = 1;
10965 if (bucket->parsed &&
10966 (bucket->targetNamespace != pctxt->targetNamespace)) {
10967 /*
10968 * This is a sanity check, I dunno yet if this can happen.
10969 */
10970 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10971 "trying to use an already parsed schema for a "
10972 "different targetNamespace");
10973 return(-1);
10974 }
10975 bucket->targetNamespace = pctxt->targetNamespace;
10976 }
10977 }
10978 /*
10979 * Parse the schema.
10980 */
10981 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
10982 if (isChameleon) {
10983 /* TODO: Get rid of this flag on the schema itself. */
10984 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
10985 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
10986 } else
10987 wasChameleon = 1;
10988 }
10989 xmlSchemaParseNewDoc(pctxt, schema, bucket);
10990 /* Restore chameleon flag. */
10991 if (isChameleon && (!wasChameleon))
10992 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
10993 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010994 /*
10995 * And now for the children...
10996 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010997 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010998 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010999 /*
11000 * Parse (simpleType | complexType | group | attributeGroup))*
11001 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011002 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011003 /*
11004 * How to proceed if the redefined schema was not located?
11005 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011006 pctxt->isRedefine = 1;
11007 while (IS_SCHEMA(child, "annotation") ||
11008 IS_SCHEMA(child, "simpleType") ||
11009 IS_SCHEMA(child, "complexType") ||
11010 IS_SCHEMA(child, "group") ||
11011 IS_SCHEMA(child, "attributeGroup")) {
11012 if (IS_SCHEMA(child, "annotation")) {
11013 /*
11014 * TODO: discard or not?
11015 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011016 } else if (IS_SCHEMA(child, "simpleType")) {
11017 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11018 } else if (IS_SCHEMA(child, "complexType")) {
11019 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011020 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011021 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011022 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011023 xmlSchemaParseModelGroupDefinition(pctxt,
11024 schema, child);
11025 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011026 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011027 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11028 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011029 }
11030 child = child->next;
11031 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011032 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011033 pctxt->isRedefine = 0;
11034 } else {
11035 if (IS_SCHEMA(child, "annotation")) {
11036 /*
11037 * TODO: discard or not?
11038 */
11039 child = child->next;
11040 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011041 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011042 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011043 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011044 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11045 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011046 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011047 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11048 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011049 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011050 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011051 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011052 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011053 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011054 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011055
11056exit_error:
11057 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011058}
11059
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011060static int
11061xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11062 xmlNodePtr node)
11063{
11064 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011065#ifndef ENABLE_REDEFINE
11066 TODO
11067 return(0);
11068#endif
11069 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11070 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011071 if (res != 0)
11072 return(res);
11073 return(0);
11074}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011075
11076static int
11077xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11078 xmlNodePtr node)
11079{
11080 int res;
11081
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011082 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11083 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011084 if (res != 0)
11085 return(res);
11086 return(0);
11087}
11088
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011089/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011090 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011091 * @ctxt: a schema validation context
11092 * @schema: the schema being built
11093 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011094 * @type: the "compositor" type
11095 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011096 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011097 * parse a XML schema Sequence definition.
11098 * Applies parts of:
11099 * Schema Representation Constraint:
11100 * Redefinition Constraints and Semantics (src-redefine)
11101 * (6.1), (6.1.1), (6.1.2)
11102 *
11103 * Schema Component Constraint:
11104 * All Group Limited (cos-all-limited) (2)
11105 * TODO: Actually this should go to component-level checks,
11106 * but is done here due to performance. Move it to an other layer
11107 * is schema construction via an API is implemented.
11108 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011109 * *WARNING* this interface is highly subject to change
11110 *
William M. Bracke7091952004-05-11 15:09:58 +000011111 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011112 * 1 in case of success.
11113 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011114static xmlSchemaTreeItemPtr
11115xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11116 xmlNodePtr node, xmlSchemaTypeType type,
11117 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011118{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011119 xmlSchemaModelGroupPtr item;
11120 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011121 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011122 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011123 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011124
11125 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011126 return (NULL);
11127 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011128 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011129 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011130 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011131 if (item == NULL)
11132 return (NULL);
11133
11134 if (withParticle) {
11135 if (type == XML_SCHEMA_TYPE_ALL) {
11136 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011137 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011138 } else {
11139 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011140 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11141 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11142 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011143 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011144 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11145 /*
11146 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011147 */
11148 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11149 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011150 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011151 particle->children = (xmlSchemaTreeItemPtr) item;
11152 /*
11153 * Check for illegal attributes.
11154 */
11155 attr = node->properties;
11156 while (attr != NULL) {
11157 if (attr->ns == NULL) {
11158 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11159 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11160 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011161 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011162 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011163 }
11164 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011165 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011166 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011167 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011168 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011169 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011170 } else {
11171 /*
11172 * Check for illegal attributes.
11173 */
11174 attr = node->properties;
11175 while (attr != NULL) {
11176 if (attr->ns == NULL) {
11177 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011178 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011179 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011180 }
11181 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011182 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011183 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011184 }
11185 attr = attr->next;
11186 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011187 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011188
William M. Brack2f2a6632004-08-20 23:09:47 +000011189 /*
11190 * Extract and validate attributes.
11191 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011192 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011193 /*
11194 * And now for the children...
11195 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011196 child = node->children;
11197 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011198 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011199 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011200 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011201 if (type == XML_SCHEMA_TYPE_ALL) {
11202 xmlSchemaParticlePtr part, last = NULL;
11203
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011204 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011205 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011206 schema, child, &isElemRef, 0);
11207 /*
11208 * SPEC cos-all-limited (2)
11209 * "The {max occurs} of all the particles in the {particles}
11210 * of the ('all') group must be 0 or 1.
11211 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011212 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011213 if (isElemRef)
11214 hasRefs++;
11215 if (part->minOccurs > 1) {
11216 xmlSchemaPCustomErr(ctxt,
11217 XML_SCHEMAP_COS_ALL_LIMITED,
11218 NULL, child,
11219 "Invalid value for minOccurs (must be 0 or 1)",
11220 NULL);
11221 /* Reset to 1. */
11222 part->minOccurs = 1;
11223 }
11224 if (part->maxOccurs > 1) {
11225 xmlSchemaPCustomErr(ctxt,
11226 XML_SCHEMAP_COS_ALL_LIMITED,
11227 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011228 "Invalid value for maxOccurs (must be 0 or 1)",
11229 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011230 /* Reset to 1. */
11231 part->maxOccurs = 1;
11232 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011233 if (last == NULL)
11234 item->children = (xmlSchemaTreeItemPtr) part;
11235 else
11236 last->next = (xmlSchemaTreeItemPtr) part;
11237 last = part;
11238 }
11239 child = child->next;
11240 }
11241 if (child != NULL) {
11242 xmlSchemaPContentErr(ctxt,
11243 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011244 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011245 "(annotation?, (annotation?, element*)");
11246 }
11247 } else {
11248 /* choice + sequence */
11249 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11250
11251 while ((IS_SCHEMA(child, "element")) ||
11252 (IS_SCHEMA(child, "group")) ||
11253 (IS_SCHEMA(child, "any")) ||
11254 (IS_SCHEMA(child, "choice")) ||
11255 (IS_SCHEMA(child, "sequence"))) {
11256
11257 if (IS_SCHEMA(child, "element")) {
11258 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011259 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11260 if (part && isElemRef)
11261 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011262 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011263 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011264 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011265 if (part != NULL)
11266 hasRefs++;
11267 /*
11268 * Handle redefinitions.
11269 */
11270 if (ctxt->isRedefine && ctxt->redef &&
11271 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11272 part && part->children)
11273 {
11274 if ((xmlSchemaGetQNameRefName(part->children) ==
11275 ctxt->redef->refName) &&
11276 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11277 ctxt->redef->refTargetNs))
11278 {
11279 /*
11280 * SPEC src-redefine:
11281 * (6.1) "If it has a <group> among its contents at
11282 * some level the ·actual value· of whose ref
11283 * [attribute] is the same as the ·actual value· of
11284 * its own name attribute plus target namespace, then
11285 * all of the following must be true:"
11286 * (6.1.1) "It must have exactly one such group."
11287 */
11288 if (ctxt->redefCounter != 0) {
11289 xmlChar *str = NULL;
11290
11291 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11292 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11293 "The redefining model group definition "
11294 "'%s' must not contain more than one "
11295 "reference to the redefined definition",
11296 xmlSchemaFormatQName(&str,
11297 ctxt->redef->refTargetNs,
11298 ctxt->redef->refName),
11299 NULL);
11300 FREE_AND_NULL(str)
11301 part = NULL;
11302 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11303 ((WXS_PARTICLE(part))->maxOccurs != 1))
11304 {
11305 xmlChar *str = NULL;
11306 /*
11307 * SPEC src-redefine:
11308 * (6.1.2) "The ·actual value· of both that
11309 * group's minOccurs and maxOccurs [attribute]
11310 * must be 1 (or ·absent·).
11311 */
11312 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11313 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11314 "The redefining model group definition "
11315 "'%s' must not contain a reference to the "
11316 "redefined definition with a "
11317 "maxOccurs/minOccurs other than 1",
11318 xmlSchemaFormatQName(&str,
11319 ctxt->redef->refTargetNs,
11320 ctxt->redef->refName),
11321 NULL);
11322 FREE_AND_NULL(str)
11323 part = NULL;
11324 }
11325 ctxt->redef->reference = WXS_BASIC_CAST part;
11326 ctxt->redefCounter++;
11327 }
11328 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011329 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011330 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011331 xmlSchemaParseAny(ctxt, schema, child);
11332 } else if (IS_SCHEMA(child, "choice")) {
11333 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11334 XML_SCHEMA_TYPE_CHOICE, 1);
11335 } else if (IS_SCHEMA(child, "sequence")) {
11336 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11337 XML_SCHEMA_TYPE_SEQUENCE, 1);
11338 }
11339 if (part != NULL) {
11340 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011341 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011342 else
11343 last->next = part;
11344 last = part;
11345 }
11346 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011347 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011348 if (child != NULL) {
11349 xmlSchemaPContentErr(ctxt,
11350 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011351 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011352 "(annotation?, (element | group | choice | sequence | any)*)");
11353 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011354 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011355 if ((max == 0) && (min == 0))
11356 return (NULL);
11357 if (hasRefs) {
11358 /*
11359 * We need to resolve references.
11360 */
11361 WXS_ADD_PENDING(ctxt, item);
11362 }
11363 if (withParticle)
11364 return ((xmlSchemaTreeItemPtr) particle);
11365 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011366 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011367}
11368
11369/**
11370 * xmlSchemaParseRestriction:
11371 * @ctxt: a schema validation context
11372 * @schema: the schema being built
11373 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011374 *
11375 * parse a XML schema Restriction definition
11376 * *WARNING* this interface is highly subject to change
11377 *
11378 * Returns the type definition or NULL in case of error
11379 */
11380static xmlSchemaTypePtr
11381xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011382 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011383{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011384 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011385 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011386 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011387
11388 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11389 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011390 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011391 type = ctxt->ctxtType;
11392 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011393
11394 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011395 * Check for illegal attributes.
11396 */
11397 attr = node->properties;
11398 while (attr != NULL) {
11399 if (attr->ns == NULL) {
11400 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11401 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011402 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011403 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011404 }
11405 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011406 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011407 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011408 }
11409 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011410 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011411 /*
11412 * Extract and validate attributes.
11413 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011414 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011415 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011416 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011417 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011418 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011419 * Extract the base type. The "base" attribute is mandatory if inside
11420 * a complex type or if redefining.
11421 *
11422 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011423 * among its [children]), the simple type definition which is
11424 * the {content type} of the type definition ·resolved· to by
11425 * the ·actual value· of the base [attribute]"
11426 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011427 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011428 &(type->baseNs), &(type->base)) == 0)
11429 {
11430 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11431 xmlSchemaPMissingAttrErr(ctxt,
11432 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011433 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011434 } else if ((ctxt->isRedefine) &&
11435 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11436 {
11437 if (type->base == NULL) {
11438 xmlSchemaPMissingAttrErr(ctxt,
11439 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011440 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011441 } else if ((! xmlStrEqual(type->base, type->name)) ||
11442 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11443 {
11444 xmlChar *str1 = NULL, *str2 = NULL;
11445 /*
11446 * REDEFINE: SPEC src-redefine (5)
11447 * "Within the [children], each <simpleType> must have a
11448 * <restriction> among its [children] ... the ·actual value· of
11449 * whose base [attribute] must be the same as the ·actual value·
11450 * of its own name attribute plus target namespace;"
11451 */
11452 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011453 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011454 "value '%s' of the 'base' attribute does not match the "
11455 "type's designation '%s'",
11456 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11457 xmlSchemaFormatQName(&str1, type->targetNamespace,
11458 type->name), NULL);
11459 FREE_AND_NULL(str1);
11460 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011461 /* Avoid confusion and erase the values. */
11462 type->base = NULL;
11463 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011464 }
11465 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011466 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011467 /*
11468 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011469 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011470 child = node->children;
11471 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011472 /*
11473 * Add the annotation to the simple type ancestor.
11474 */
11475 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011476 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011477 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011478 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011479 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11480 /*
11481 * Corresponds to <simpleType><restriction><simpleType>.
11482 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011483 if (IS_SCHEMA(child, "simpleType")) {
11484 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011485 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011486 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011487 * Either the base [attribute] or the simpleType [child] of the
11488 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011489 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011490 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011491 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011492 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011493 "The attribute 'base' and the <simpleType> child are "
11494 "mutually exclusive", NULL);
11495 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011496 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011497 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011498 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011499 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011500 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011501 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011502 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011503 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011504 "Either the attribute 'base' or a <simpleType> child "
11505 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011506 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011507 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11508 /*
11509 * Corresponds to <complexType><complexContent><restriction>...
11510 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011511 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011512 * Model groups <all>, <choice> and <sequence>.
11513 */
11514 if (IS_SCHEMA(child, "all")) {
11515 type->subtypes = (xmlSchemaTypePtr)
11516 xmlSchemaParseModelGroup(ctxt, schema, child,
11517 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011518 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011519 } else if (IS_SCHEMA(child, "choice")) {
11520 type->subtypes = (xmlSchemaTypePtr)
11521 xmlSchemaParseModelGroup(ctxt,
11522 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11523 child = child->next;
11524 } else if (IS_SCHEMA(child, "sequence")) {
11525 type->subtypes = (xmlSchemaTypePtr)
11526 xmlSchemaParseModelGroup(ctxt, schema, child,
11527 XML_SCHEMA_TYPE_SEQUENCE, 1);
11528 child = child->next;
11529 /*
11530 * Model group reference <group>.
11531 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011532 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011533 type->subtypes = (xmlSchemaTypePtr)
11534 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011535 /*
11536 * Note that the reference will be resolved in
11537 * xmlSchemaResolveTypeReferences();
11538 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011539 child = child->next;
11540 }
11541 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011542 /*
11543 * Corresponds to <complexType><simpleContent><restriction>...
11544 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011545 * "1.1 the simple type definition corresponding to the <simpleType>
11546 * among the [children] of <restriction> if there is one;"
11547 */
11548 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011549 /*
11550 * We will store the to-be-restricted simple type in
11551 * type->contentTypeDef *temporarily*.
11552 */
11553 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011554 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011555 if ( type->contentTypeDef == NULL)
11556 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011557 child = child->next;
11558 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011559 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011560
11561 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011562 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011563 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011564 /*
11565 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011566 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011567 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011568
Daniel Veillard01fa6152004-06-29 17:04:39 +000011569 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011570 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011571 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011572 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011573 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11574 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011575 * *Single Facet Value*
11576 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011577 while ((IS_SCHEMA(child, "minInclusive")) ||
11578 (IS_SCHEMA(child, "minExclusive")) ||
11579 (IS_SCHEMA(child, "maxInclusive")) ||
11580 (IS_SCHEMA(child, "maxExclusive")) ||
11581 (IS_SCHEMA(child, "totalDigits")) ||
11582 (IS_SCHEMA(child, "fractionDigits")) ||
11583 (IS_SCHEMA(child, "pattern")) ||
11584 (IS_SCHEMA(child, "enumeration")) ||
11585 (IS_SCHEMA(child, "whiteSpace")) ||
11586 (IS_SCHEMA(child, "length")) ||
11587 (IS_SCHEMA(child, "maxLength")) ||
11588 (IS_SCHEMA(child, "minLength"))) {
11589 facet = xmlSchemaParseFacet(ctxt, schema, child);
11590 if (facet != NULL) {
11591 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011592 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011593 else
11594 lastfacet->next = facet;
11595 lastfacet = facet;
11596 lastfacet->next = NULL;
11597 }
11598 child = child->next;
11599 }
11600 /*
11601 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011602 */
11603 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011604 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11605
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011606 facet = type->facets;
11607 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011608 facetLink = (xmlSchemaFacetLinkPtr)
11609 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011610 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011611 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011612 xmlFree(facetLink);
11613 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011614 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011615 facetLink->facet = facet;
11616 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011617 if (lastFacetLink == NULL)
11618 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011619 else
11620 lastFacetLink->next = facetLink;
11621 lastFacetLink = facetLink;
11622 facet = facet->next;
11623 } while (facet != NULL);
11624 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011625 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011626 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11627 /*
11628 * Attribute uses/declarations.
11629 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011630 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11631 (xmlSchemaItemListPtr *) &(type->attrUses),
11632 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11633 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011634 /*
11635 * Attribute wildcard.
11636 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011637 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011638 type->attributeWildcard =
11639 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011640 child = child->next;
11641 }
11642 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011643 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011644 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11645 xmlSchemaPContentErr(ctxt,
11646 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011647 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011648 "annotation?, (group | all | choice | sequence)?, "
11649 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011650 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011651 xmlSchemaPContentErr(ctxt,
11652 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011653 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011654 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11655 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11656 "length | minLength | maxLength | enumeration | whiteSpace | "
11657 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11658 } else {
11659 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011660 xmlSchemaPContentErr(ctxt,
11661 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011662 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011663 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11664 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11665 "length | minLength | maxLength | enumeration | whiteSpace | "
11666 "pattern)*))");
11667 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011668 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011669 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011670}
11671
11672/**
11673 * xmlSchemaParseExtension:
11674 * @ctxt: a schema validation context
11675 * @schema: the schema being built
11676 * @node: a subtree containing XML Schema informations
11677 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011678 * Parses an <extension>, which is found inside a
11679 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011680 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011681 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011682 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011683 */
11684static xmlSchemaTypePtr
11685xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011686 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011687{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011688 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011689 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011690 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011691
11692 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11693 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011694 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011695 type = ctxt->ctxtType;
11696 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011697
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011698 /*
11699 * Check for illegal attributes.
11700 */
11701 attr = node->properties;
11702 while (attr != NULL) {
11703 if (attr->ns == NULL) {
11704 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11705 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011706 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011707 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011708 }
11709 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011710 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011711 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011712 }
11713 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011714 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011715
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011716 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011717
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011718 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011719 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011720 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011721 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11722 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011723 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011724 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011725 XML_SCHEMAP_S4S_ATTR_MISSING,
11726 NULL, node, "base", NULL);
11727 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011728 /*
11729 * And now for the children...
11730 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011731 child = node->children;
11732 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011733 /*
11734 * Add the annotation to the type ancestor.
11735 */
11736 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011737 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011738 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011739 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011740 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11741 /*
11742 * Corresponds to <complexType><complexContent><extension>... and:
11743 *
11744 * Model groups <all>, <choice>, <sequence> and <group>.
11745 */
11746 if (IS_SCHEMA(child, "all")) {
11747 type->subtypes = (xmlSchemaTypePtr)
11748 xmlSchemaParseModelGroup(ctxt, schema,
11749 child, XML_SCHEMA_TYPE_ALL, 1);
11750 child = child->next;
11751 } else if (IS_SCHEMA(child, "choice")) {
11752 type->subtypes = (xmlSchemaTypePtr)
11753 xmlSchemaParseModelGroup(ctxt, schema,
11754 child, XML_SCHEMA_TYPE_CHOICE, 1);
11755 child = child->next;
11756 } else if (IS_SCHEMA(child, "sequence")) {
11757 type->subtypes = (xmlSchemaTypePtr)
11758 xmlSchemaParseModelGroup(ctxt, schema,
11759 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11760 child = child->next;
11761 } else if (IS_SCHEMA(child, "group")) {
11762 type->subtypes = (xmlSchemaTypePtr)
11763 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011764 /*
11765 * Note that the reference will be resolved in
11766 * xmlSchemaResolveTypeReferences();
11767 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011768 child = child->next;
11769 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011770 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011771 if (child != NULL) {
11772 /*
11773 * Attribute uses/declarations.
11774 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011775 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11776 (xmlSchemaItemListPtr *) &(type->attrUses),
11777 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11778 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011779 /*
11780 * Attribute wildcard.
11781 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011782 if (IS_SCHEMA(child, "anyAttribute")) {
11783 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011784 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11785 child = child->next;
11786 }
11787 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011788 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011789 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11790 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011791 xmlSchemaPContentErr(ctxt,
11792 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011793 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011794 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011795 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011796 } else {
11797 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011798 xmlSchemaPContentErr(ctxt,
11799 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011800 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011801 "(annotation?, ((attribute | attributeGroup)*, "
11802 "anyAttribute?))");
11803 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011804 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011805 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011806}
11807
11808/**
11809 * xmlSchemaParseSimpleContent:
11810 * @ctxt: a schema validation context
11811 * @schema: the schema being built
11812 * @node: a subtree containing XML Schema informations
11813 *
11814 * parse a XML schema SimpleContent definition
11815 * *WARNING* this interface is highly subject to change
11816 *
11817 * Returns the type definition or NULL in case of error
11818 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011819static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011820xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011821 xmlSchemaPtr schema, xmlNodePtr node,
11822 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011823{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011824 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011825 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011826 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011827
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011828 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11829 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011830 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011831 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011832 /* Not a component, don't create it. */
11833 type = ctxt->ctxtType;
11834 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11835 /*
11836 * Check for illegal attributes.
11837 */
11838 attr = node->properties;
11839 while (attr != NULL) {
11840 if (attr->ns == NULL) {
11841 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011842 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011843 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011844 }
11845 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011846 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011847 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011848 }
11849 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011850 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011851
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011852 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011853
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011854 /*
11855 * And now for the children...
11856 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011857 child = node->children;
11858 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011859 /*
11860 * Add the annotation to the complex type ancestor.
11861 */
11862 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011863 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011864 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011865 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011866 if (child == NULL) {
11867 xmlSchemaPContentErr(ctxt,
11868 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011869 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011870 "(annotation?, (restriction | extension))");
11871 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011872 if (child == NULL) {
11873 xmlSchemaPContentErr(ctxt,
11874 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011875 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011876 "(annotation?, (restriction | extension))");
11877 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011878 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011879 xmlSchemaParseRestriction(ctxt, schema, child,
11880 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011881 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011882 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011883 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011884 xmlSchemaParseExtension(ctxt, schema, child,
11885 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011886 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011887 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011888 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011889 if (child != NULL) {
11890 xmlSchemaPContentErr(ctxt,
11891 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011892 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011893 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011894 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011895 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011896}
11897
11898/**
11899 * xmlSchemaParseComplexContent:
11900 * @ctxt: a schema validation context
11901 * @schema: the schema being built
11902 * @node: a subtree containing XML Schema informations
11903 *
11904 * parse a XML schema ComplexContent definition
11905 * *WARNING* this interface is highly subject to change
11906 *
11907 * Returns the type definition or NULL in case of error
11908 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011909static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011910xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011911 xmlSchemaPtr schema, xmlNodePtr node,
11912 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011913{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011914 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011915 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011916 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011917
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011918 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11919 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011920 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011921 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011922 /* Not a component, don't create it. */
11923 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011924 /*
11925 * Check for illegal attributes.
11926 */
11927 attr = node->properties;
11928 while (attr != NULL) {
11929 if (attr->ns == NULL) {
11930 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011931 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011932 {
11933 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011934 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011935 }
11936 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11937 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011938 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011939 }
11940 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011941 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011942
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011943 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011944
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011945 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011946 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011947 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011948 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011949 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11950 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011951 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011952 child = node->children;
11953 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011954 /*
11955 * Add the annotation to the complex type ancestor.
11956 */
11957 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011958 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011959 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011960 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011961 if (child == NULL) {
11962 xmlSchemaPContentErr(ctxt,
11963 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011964 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011965 NULL, "(annotation?, (restriction | extension))");
11966 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011967 if (child == NULL) {
11968 xmlSchemaPContentErr(ctxt,
11969 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011970 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011971 NULL, "(annotation?, (restriction | extension))");
11972 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011973 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011974 xmlSchemaParseRestriction(ctxt, schema, child,
11975 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011976 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011977 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011978 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011979 xmlSchemaParseExtension(ctxt, schema, child,
11980 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011981 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011982 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011983 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011984 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011985 xmlSchemaPContentErr(ctxt,
11986 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011987 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011988 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011989 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011990 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011991}
11992
11993/**
11994 * xmlSchemaParseComplexType:
11995 * @ctxt: a schema validation context
11996 * @schema: the schema being built
11997 * @node: a subtree containing XML Schema informations
11998 *
11999 * parse a XML schema Complex Type definition
12000 * *WARNING* this interface is highly subject to change
12001 *
12002 * Returns the type definition or NULL in case of error
12003 */
12004static xmlSchemaTypePtr
12005xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012006 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012007{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012008 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012009 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012010 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012011 xmlAttrPtr attr;
12012 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012013#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012014 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012015#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012016 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012017
Daniel Veillard4255d502002-04-16 15:50:10 +000012018
12019 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12020 return (NULL);
12021
Daniel Veillard01fa6152004-06-29 17:04:39 +000012022 ctxtType = ctxt->ctxtType;
12023
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012024 if (topLevel) {
12025 attr = xmlSchemaGetPropNode(node, "name");
12026 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012027 xmlSchemaPMissingAttrErr(ctxt,
12028 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012029 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012030 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012031 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12032 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012033 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012034 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012035
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012036 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012037 /*
12038 * Parse as local complex type definition.
12039 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012040#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012041 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012042 type = xmlSchemaAddType(ctxt, schema,
12043 XML_SCHEMA_TYPE_COMPLEX,
12044 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012045 ctxt->targetNamespace, node, 0);
12046#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012047 type = xmlSchemaAddType(ctxt, schema,
12048 XML_SCHEMA_TYPE_COMPLEX,
12049 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012050#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012051 if (type == NULL)
12052 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012053 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012054 type->node = node;
12055 type->type = XML_SCHEMA_TYPE_COMPLEX;
12056 /*
12057 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012058 */
12059 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012060 /*
12061 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012062 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012063 type = xmlSchemaAddType(ctxt, schema,
12064 XML_SCHEMA_TYPE_COMPLEX,
12065 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012066 if (type == NULL)
12067 return (NULL);
12068 type->node = node;
12069 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012070 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012071 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012072 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012073 /*
12074 * Handle attributes.
12075 */
12076 attr = node->properties;
12077 while (attr != NULL) {
12078 if (attr->ns == NULL) {
12079 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12080 /*
12081 * Attribute "id".
12082 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012083 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012084 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12085 /*
12086 * Attribute "mixed".
12087 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012088 if (xmlSchemaPGetBoolNodeValue(ctxt,
12089 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012090 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12091 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012092 /*
12093 * Attributes of global complex type definitions.
12094 */
12095 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12096 /* Pass. */
12097 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12098 /*
12099 * Attribute "abstract".
12100 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012101 if (xmlSchemaPGetBoolNodeValue(ctxt,
12102 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012103 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12104 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12105 /*
12106 * Attribute "final".
12107 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012108 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012109 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012110 if (xmlSchemaPValAttrBlockFinal(attrValue,
12111 &(type->flags),
12112 -1,
12113 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12114 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12115 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012116 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012117 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012118 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012119 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012120 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012121 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012122 } else
12123 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012124 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12125 /*
12126 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012127 */
12128 attrValue = xmlSchemaGetNodeContent(ctxt,
12129 (xmlNodePtr) attr);
12130 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012131 -1,
12132 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012133 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012134 -1, -1, -1) != 0) {
12135 xmlSchemaPSimpleTypeErr(ctxt,
12136 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012137 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012138 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012139 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012140 } else
12141 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012142 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012143 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012144 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012145 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012146 } else {
12147 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012148 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012149 }
12150 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012151 xmlSchemaPIllegalAttrErr(ctxt,
12152 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012153 }
12154 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012155 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012156 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012157 /*
12158 * Apply default "block" values.
12159 */
12160 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12161 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12162 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12163 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12164 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012165 if (! final) {
12166 /*
12167 * Apply default "block" values.
12168 */
12169 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12170 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12171 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12172 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12173 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012174 /*
12175 * And now for the children...
12176 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012177 child = node->children;
12178 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012179 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012180 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012181 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012182 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012183 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012184 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012185 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012186 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012187 * Specifying mixed='true' when the <simpleContent>
12188 * alternative is chosen has no effect
12189 */
William M. Bracke7091952004-05-11 15:09:58 +000012190 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12191 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012192 xmlSchemaParseSimpleContent(ctxt, schema, child,
12193 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012194 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012195 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012196 /*
12197 * <complexType><complexContent>...
12198 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012199 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012200 xmlSchemaParseComplexContent(ctxt, schema, child,
12201 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012202 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012203 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012204 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012205 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12206 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012207 * SPEC
12208 * "...the third alternative (neither <simpleContent> nor
12209 * <complexContent>) is chosen. This case is understood as shorthand
12210 * for complex content restricting the ·ur-type definition·, and the
12211 * details of the mappings should be modified as necessary.
12212 */
12213 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12214 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012215 /*
12216 * Parse model groups.
12217 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012218 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012219 type->subtypes = (xmlSchemaTypePtr)
12220 xmlSchemaParseModelGroup(ctxt, schema, child,
12221 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012222 child = child->next;
12223 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012224 type->subtypes = (xmlSchemaTypePtr)
12225 xmlSchemaParseModelGroup(ctxt, schema, child,
12226 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012227 child = child->next;
12228 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012229 type->subtypes = (xmlSchemaTypePtr)
12230 xmlSchemaParseModelGroup(ctxt, schema, child,
12231 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012232 child = child->next;
12233 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012234 type->subtypes = (xmlSchemaTypePtr)
12235 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012236 /*
12237 * Note that the reference will be resolved in
12238 * xmlSchemaResolveTypeReferences();
12239 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012240 child = child->next;
12241 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012242 /*
12243 * Parse attribute decls/refs.
12244 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012245 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12246 (xmlSchemaItemListPtr *) &(type->attrUses),
12247 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12248 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012249 /*
12250 * Parse attribute wildcard.
12251 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012252 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012253 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12254 child = child->next;
12255 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012256 }
12257 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012258 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012259 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012260 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012261 NULL, "(annotation?, (simpleContent | complexContent | "
12262 "((group | all | choice | sequence)?, ((attribute | "
12263 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012264 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012265 /*
12266 * REDEFINE: SPEC src-redefine (5)
12267 */
12268 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12269 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012270 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012271 "<complexType> must have a <restriction> or <extension> "
12272 "grand-child", NULL);
12273 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012274 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012275 return (type);
12276}
12277
Daniel Veillard4255d502002-04-16 15:50:10 +000012278/************************************************************************
12279 * *
12280 * Validating using Schemas *
12281 * *
12282 ************************************************************************/
12283
12284/************************************************************************
12285 * *
12286 * Reading/Writing Schemas *
12287 * *
12288 ************************************************************************/
12289
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012290#if 0 /* Will be enabled if it is clear what options are needed. */
12291/**
12292 * xmlSchemaParserCtxtSetOptions:
12293 * @ctxt: a schema parser context
12294 * @options: a combination of xmlSchemaParserOption
12295 *
12296 * Sets the options to be used during the parse.
12297 *
12298 * Returns 0 in case of success, -1 in case of an
12299 * API error.
12300 */
12301static int
12302xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12303 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012304
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012305{
12306 int i;
12307
12308 if (ctxt == NULL)
12309 return (-1);
12310 /*
12311 * WARNING: Change the start value if adding to the
12312 * xmlSchemaParseOption.
12313 */
12314 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12315 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012316 return (-1);
12317 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012318 }
12319 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012320 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012321}
12322
12323/**
12324 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012325 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012326 *
12327 * Returns the option combination of the parser context.
12328 */
12329static int
12330xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012331
12332{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012333 if (ctxt == NULL)
12334 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012335 else
12336 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012337}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012338#endif
12339
Daniel Veillard4255d502002-04-16 15:50:10 +000012340/**
12341 * xmlSchemaNewParserCtxt:
12342 * @URL: the location of the schema
12343 *
12344 * Create an XML Schemas parse context for that file/resource expected
12345 * to contain an XML Schemas file.
12346 *
12347 * Returns the parser context or NULL in case of error
12348 */
12349xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012350xmlSchemaNewParserCtxt(const char *URL)
12351{
Daniel Veillard4255d502002-04-16 15:50:10 +000012352 xmlSchemaParserCtxtPtr ret;
12353
12354 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012355 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012356
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012357 ret = xmlSchemaParserCtxtCreate();
12358 if (ret == NULL)
12359 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012360 ret->dict = xmlDictCreate();
12361 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012362 return (ret);
12363}
12364
12365/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012366 * xmlSchemaNewMemParserCtxt:
12367 * @buffer: a pointer to a char array containing the schemas
12368 * @size: the size of the array
12369 *
12370 * Create an XML Schemas parse context for that memory buffer expected
12371 * to contain an XML Schemas file.
12372 *
12373 * Returns the parser context or NULL in case of error
12374 */
12375xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012376xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12377{
Daniel Veillard6045c902002-10-09 21:13:59 +000012378 xmlSchemaParserCtxtPtr ret;
12379
12380 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012381 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012382 ret = xmlSchemaParserCtxtCreate();
12383 if (ret == NULL)
12384 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012385 ret->buffer = buffer;
12386 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012387 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012388 return (ret);
12389}
12390
12391/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012392 * xmlSchemaNewDocParserCtxt:
12393 * @doc: a preparsed document tree
12394 *
12395 * Create an XML Schemas parse context for that document.
12396 * NB. The document may be modified during the parsing process.
12397 *
12398 * Returns the parser context or NULL in case of error
12399 */
12400xmlSchemaParserCtxtPtr
12401xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12402{
12403 xmlSchemaParserCtxtPtr ret;
12404
12405 if (doc == NULL)
12406 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012407 ret = xmlSchemaParserCtxtCreate();
12408 if (ret == NULL)
12409 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012410 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012411 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012412 /* The application has responsibility for the document */
12413 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012414
12415 return (ret);
12416}
12417
12418/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012419 * xmlSchemaFreeParserCtxt:
12420 * @ctxt: the schema parser context
12421 *
12422 * Free the resources associated to the schema parser context
12423 */
12424void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012425xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12426{
Daniel Veillard4255d502002-04-16 15:50:10 +000012427 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012428 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012429 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012430 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012431 if (ctxt->vctxt != NULL) {
12432 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12433 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012434 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12435 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12436 ctxt->constructor = NULL;
12437 ctxt->ownsConstructor = 0;
12438 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012439 if (ctxt->attrProhibs != NULL)
12440 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012441 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012442 xmlFree(ctxt);
12443}
12444
12445/************************************************************************
12446 * *
12447 * Building the content models *
12448 * *
12449 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012450
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012451static void
12452xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012453 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012454{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012455 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012456 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012457 xmlSchemaSubstGroupPtr substGroup;
12458 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012459
12460 elemDecl = (xmlSchemaElementPtr) particle->children;
12461 /*
12462 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012463 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012464 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012465 if (end == NULL)
12466 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012467 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012468 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012469 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012470 XML_SCHEMAP_INTERNAL,
12471 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12472 "declaration is marked having a subst. group but none "
12473 "available.\n", elemDecl->name, NULL);
12474 return;
12475 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012476 if (counter >= 0) {
12477 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012478 * NOTE that we put the declaration in, even if it's abstract.
12479 * However, an error will be raised during *validation* if an element
12480 * information item shall be validated against an abstract element
12481 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012482 */
12483 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12484 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12485 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12486 /*
12487 * Add subst. group members.
12488 */
12489 for (i = 0; i < substGroup->members->nbItems; i++) {
12490 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12491 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12492 member->name, member->targetNamespace, member);
12493 }
12494 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012495 /*
12496 * NOTE that we put the declaration in, even if it's abstract,
12497 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012498 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012499 xmlAutomataNewTransition2(pctxt->am,
12500 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012501 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12502 /*
12503 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012504 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012505 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012506 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012507 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12508 member->name, member->targetNamespace,
12509 1, 1, member);
12510 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012511 }
12512 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012513 xmlAutomataStatePtr hop;
12514 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12515 UNBOUNDED : particle->maxOccurs - 1;
12516 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12517
12518 counter =
12519 xmlAutomataNewCounter(pctxt->am, minOccurs,
12520 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012521 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012522
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012523 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012524 xmlAutomataNewTransition2(pctxt->am,
12525 start, NULL,
12526 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012527 hop);
12528 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012529 * Add subst. group members.
12530 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012531 for (i = 0; i < substGroup->members->nbItems; i++) {
12532 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12533 xmlAutomataNewEpsilon(pctxt->am,
12534 xmlAutomataNewTransition2(pctxt->am,
12535 start, NULL,
12536 member->name, member->targetNamespace, member),
12537 hop);
12538 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012539 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12540 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12541 }
12542 if (particle->minOccurs == 0)
12543 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012544 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012545}
12546
12547static void
12548xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12549 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012550{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012551 if (((xmlSchemaElementPtr) particle->children)->flags &
12552 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012553 /*
12554 * Substitution groups.
12555 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012556 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012557 } else {
12558 xmlSchemaElementPtr elemDecl;
12559 xmlAutomataStatePtr start;
12560
12561 elemDecl = (xmlSchemaElementPtr) particle->children;
12562
12563 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012564 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012565 if (particle->maxOccurs == 1) {
12566 start = ctxt->state;
12567 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012568 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12569 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12570 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012571 /* Special case. */
12572 start = ctxt->state;
12573 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12574 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012575 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012576 } else {
12577 int counter;
12578 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12579 UNBOUNDED : particle->maxOccurs - 1;
12580 int minOccurs = particle->minOccurs < 1 ?
12581 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012582
12583 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012584 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12585 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12586 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12587 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12588 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12589 NULL, counter);
12590 }
12591 if (particle->minOccurs == 0)
12592 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12593 }
12594}
12595
Daniel Veillard4255d502002-04-16 15:50:10 +000012596/**
12597 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012598 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012599 * @particle: the particle component
12600 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012601 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012602 * Create the automaton for the {content type} of a complex type.
12603 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012604 */
12605static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012606xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012607 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012608{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012609 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012610 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012611 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012612 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012613 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012614 /*
12615 * Just return in this case. A missing "term" of the particle
12616 * might arise due to an invalid "term" component.
12617 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012618 return;
12619 }
12620
12621 switch (particle->children->type) {
12622 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012623 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012624 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012625 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012626
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012627 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012628
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012629 start = pctxt->state;
12630 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012631
12632 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012633 if (wild->any == 1) {
12634 /*
12635 * We need to add both transitions:
12636 *
12637 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012638 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012639 pctxt->state =
12640 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012641 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012642 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012643 /*
12644 * 2. the {"*"} for elements in no namespace.
12645 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012646 pctxt->state =
12647 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012648 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012649 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012650
12651 } else if (wild->nsSet != NULL) {
12652 ns = wild->nsSet;
12653 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012654 pctxt->state = start;
12655 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12656 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12657 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012658 ns = ns->next;
12659 } while (ns != NULL);
12660
12661 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012662 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12663 start, end, BAD_CAST "*", wild->negNsSet->value,
12664 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012665 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012666 } else {
12667 int counter;
12668 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012669 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012670 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012671 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012672 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012673
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012674 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12675 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012676 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012677 pctxt->state =
12678 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012679 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012680 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12681 pctxt->state =
12682 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012683 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012684 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012685 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012686 ns = wild->nsSet;
12687 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012688 pctxt->state =
12689 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012690 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012691 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012692 ns = ns->next;
12693 } while (ns != NULL);
12694
12695 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012696 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012697 start, hop, BAD_CAST "*", wild->negNsSet->value,
12698 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012699 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012700 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12701 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012702 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012703 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012704 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012705 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012707 break;
12708 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012709 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012710 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012711 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012712 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012713 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012714
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012715 /*
12716 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012717 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012718 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012719 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12720 sub = particle->children->children;
12721 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012722 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012723 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012724 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012725 }
12726 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012727 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012728
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012729 if (particle->maxOccurs >= UNBOUNDED) {
12730 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012731 xmlAutomataStatePtr tmp;
12732 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012733
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012734 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012735 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012736 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012737
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012738 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012739 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012740
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012741 sub = particle->children->children;
12742 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012743 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012744 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012745 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012746 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012747 tmp = pctxt->state;
12748 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012749 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012750 pctxt->state =
12751 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012752 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012753
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012754 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012755 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012756 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012757 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012758
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012759 sub = particle->children->children;
12760 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012761 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012762 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012763 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012764 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012765 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012766 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012767 /*
12768 * epsilon needed to block previous trans from
12769 * being allowed to enter back from another
12770 * construct
12771 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012772 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12773 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012774 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012775 xmlAutomataNewEpsilon(pctxt->am,
12776 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012777 }
12778 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012779 } else if ((particle->maxOccurs > 1)
12780 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012781 xmlAutomataStatePtr tmp;
12782 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012783
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012784 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012785 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012786 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012787
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012788 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012789 particle->minOccurs - 1,
12790 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012791
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012792 sub = particle->children->children;
12793 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012794 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012795 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012796 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012797 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012798 tmp = pctxt->state;
12799 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012800 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012801 pctxt->state =
12802 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012803 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012804 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012805 xmlAutomataNewEpsilon(pctxt->am,
12806 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012807 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012808 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012809 sub = particle->children->children;
12810 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012811 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012812 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012813 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012814 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012815 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012816 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12817 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012818 }
12819 }
12820 }
12821 break;
12822 }
12823 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012824 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012825 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012826
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012827 start = pctxt->state;
12828 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012829
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012830 /*
12831 * iterate over the subtypes and remerge the end with an
12832 * epsilon transition
12833 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012834 if (particle->maxOccurs == 1) {
12835 sub = particle->children->children;
12836 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012837 pctxt->state = start;
12838 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012839 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012840 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012841 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012842 }
12843 } else {
12844 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012845 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012846 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12847 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012848 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012849 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012850
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012851 /*
12852 * use a counter to keep track of the number of transtions
12853 * which went through the choice.
12854 */
12855 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012856 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12857 hop = xmlAutomataNewState(pctxt->am);
12858 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012859
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012860 sub = particle->children->children;
12861 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012862 pctxt->state = base;
12863 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012864 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012865 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012866 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012867 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012868 xmlAutomataNewEpsilon(pctxt->am, start, base);
12869 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12870 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012871 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012872 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012873 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012874 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012875 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012876 break;
12877 }
12878 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012879 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012880 xmlSchemaParticlePtr sub;
12881 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012882 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012883
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012884 sub = (xmlSchemaParticlePtr) particle->children->children;
12885 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012886 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012887 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012888 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012889 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012890
12891 elemDecl = (xmlSchemaElementPtr) sub->children;
12892 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012893 PERROR_INT("xmlSchemaBuildAContentModel",
12894 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012895 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012896 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012897 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012898 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012899 * {particles} of the group must be 0 or 1; this is
12900 * already ensured during the parse of the content of
12901 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012902 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012903 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12904 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012905
Daniel Veillarda980bef2005-07-18 21:34:03 +000012906 /*
12907 * This is an abstract group, we need to share
12908 * the same counter for all the element transitions
12909 * derived from the group
12910 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012911 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012912 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012913 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12914 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012915 } else {
12916 if ((sub->minOccurs == 1) &&
12917 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012918 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12919 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012920 elemDecl->name,
12921 elemDecl->targetNamespace,
12922 1, 1, elemDecl);
12923 } else if ((sub->minOccurs == 0) &&
12924 (sub->maxOccurs == 1)) {
12925
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012926 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12927 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012928 elemDecl->name,
12929 elemDecl->targetNamespace,
12930 0,
12931 1,
12932 elemDecl);
12933 }
12934 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012935 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012936 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012937 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012938 pctxt->state =
12939 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012940 break;
12941 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012942 case XML_SCHEMA_TYPE_GROUP:
12943 /*
12944 * If we hit a model group definition, then this means that
12945 * it was empty, thus was not substituted for the containing
12946 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012947 * TODO: But the group should be substituted and not occur at
12948 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012949 */
12950 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012951 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012952 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12953 "xmlSchemaBuildAContentModel",
12954 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012955 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012956 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012957 }
12958}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012959
Daniel Veillard4255d502002-04-16 15:50:10 +000012960/**
12961 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012962 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012963 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012964 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012965 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012966 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012967 */
12968static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012969xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012970 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012971{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012972 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
12973 (type->contModel != NULL) ||
12974 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
12975 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012976 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012977
12978#ifdef DEBUG_CONTENT
12979 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012980 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012981#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012982 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000012983 ctxt->am = xmlNewAutomata();
12984 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012985 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012986 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012987 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012988 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000012989 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012990 /*
12991 * Build the automaton.
12992 */
12993 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000012994 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012995 type->contModel = xmlAutomataCompile(ctxt->am);
12996 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012997 xmlSchemaPCustomErr(ctxt,
12998 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012999 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013000 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013001 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013002 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013003 XML_SCHEMAP_NOT_DETERMINISTIC,
13004 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013005 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013006 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013007 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013008#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013009 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013010 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013011 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013012#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013013 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013014 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013015 xmlFreeAutomata(ctxt->am);
13016 ctxt->am = NULL;
13017}
13018
13019/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013020 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013021 * @elem: the schema element context
13022 * @ctxt: the schema parser context
13023 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013024 * Resolves the references of an element declaration
13025 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013026 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013027 */
13028static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013029xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13030 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013031{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013032 if ((ctxt == NULL) || (elemDecl == NULL) ||
13033 ((elemDecl != NULL) &&
13034 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013035 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013036 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013037
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013038 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013039 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013040
13041 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013042 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013043 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013044 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013045 elemDecl->namedTypeNs);
13046 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013047 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013048 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013049 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013050 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013051 XML_SCHEMA_TYPE_BASIC, "type definition");
13052 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013053 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013054 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013055 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013056 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013057
Daniel Veillardc0826a72004-08-10 14:17:33 +000013058 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013059 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013060 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013061 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013062 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13063 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013064 if (substHead == NULL) {
13065 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013066 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013067 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013068 "substitutionGroup", elemDecl->substGroup,
13069 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013070 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013071 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013072 /*
13073 * Set the "substitution group affiliation".
13074 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013075 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013076 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013077 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013078 * The type definitions is set to:
13079 * SPEC "...the {type definition} of the element
13080 * declaration ·resolved· to by the ·actual value·
13081 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013082 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013083 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013084 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013085 }
13086 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013087 /*
13088 * SPEC "The definition of anyType serves as the default type definition
13089 * for element declarations whose XML representation does not specify one."
13090 */
13091 if ((elemDecl->subtypes == NULL) &&
13092 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013093 (elemDecl->substGroup == NULL))
13094 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013095}
13096
13097/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013098 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013099 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013100 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013101 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013102 * Checks and builds the "member type definitions" property of the union
13103 * simple type. This handles part (1), part (2) is done in
13104 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13105 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013106 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013107 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013108static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013109xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13110 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013111{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013112
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013113 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013114 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013115
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013116 /*
13117 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13118 * define the explicit members as the type definitions ·resolved·
13119 * to by the items in the ·actual value· of the memberTypes [attribute],
13120 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013121 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013122 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013123 /*
13124 * Resolve references.
13125 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013126 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013127 lastLink = NULL;
13128 while (link != NULL) {
13129 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013130
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013131 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13132 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13133
13134 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013135 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013136 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013137 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013138 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13139 /*
13140 * Remove the member type link.
13141 */
13142 if (lastLink == NULL)
13143 type->memberTypes = link->next;
13144 else
13145 lastLink->next = link->next;
13146 newLink = link;
13147 link = link->next;
13148 xmlFree(newLink);
13149 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013150 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013151 lastLink = link;
13152 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013153 }
13154 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013155 /*
13156 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013157 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013158 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013159 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013160 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13161 if (link == NULL) {
13162 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13163 return (-1);
13164 }
13165 link->type = memberType;
13166 link->next = NULL;
13167 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013168 type->memberTypes = link;
13169 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013170 lastLink->next = link;
13171 lastLink = link;
13172 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013173 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013174 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013175}
13176
Daniel Veillard4255d502002-04-16 15:50:10 +000013177/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013178 * xmlSchemaIsDerivedFromBuiltInType:
13179 * @ctxt: the schema parser context
13180 * @type: the type definition
13181 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013182 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013183 *
13184 * Returns 1 if the type has the given value type, or
13185 * is derived from such a type.
13186 */
William M. Brack803812b2004-06-03 02:11:24 +000013187static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013188xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013189{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013190 if (type == NULL)
13191 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013192 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013193 return (0);
13194 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13195 if (type->builtInType == valType)
13196 return(1);
13197 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13198 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13199 return (0);
13200 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13201 } else
13202 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013203
13204 return (0);
13205}
13206
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013207#if 0
13208/**
13209 * xmlSchemaIsDerivedFromBuiltInType:
13210 * @ctxt: the schema parser context
13211 * @type: the type definition
13212 * @valType: the value type
13213 *
13214 *
13215 * Returns 1 if the type has the given value type, or
13216 * is derived from such a type.
13217 */
13218static int
13219xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13220{
13221 if (type == NULL)
13222 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013223 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013224 return (0);
13225 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13226 if (type->builtInType == valType)
13227 return(1);
13228 return (0);
13229 } else
13230 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13231
13232 return (0);
13233}
13234#endif
13235
13236static xmlSchemaTypePtr
13237xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13238{
13239 if (type == NULL)
13240 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013241 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013242 return (NULL);
13243 if (type->type == XML_SCHEMA_TYPE_BASIC)
13244 return(type);
13245 else
13246 return(xmlSchemaQueryBuiltInType(type->subtypes));
13247
13248 return (NULL);
13249}
13250
Daniel Veillard3646d642004-06-02 19:19:14 +000013251/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013252 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013253 * @type: the simpleType definition
13254 *
13255 * Returns the primitive type of the given type or
13256 * NULL in case of error.
13257 */
13258static xmlSchemaTypePtr
13259xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13260{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013261
Daniel Veillard01fa6152004-06-29 17:04:39 +000013262 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013263 /*
13264 * Note that anySimpleType is actually not a primitive type
13265 * but we need that here.
13266 */
13267 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13268 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013269 return (type);
13270 type = type->baseType;
13271 }
13272
13273 return (NULL);
13274}
13275
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013276#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013277/**
13278 * xmlSchemaGetBuiltInTypeAncestor:
13279 * @type: the simpleType definition
13280 *
13281 * Returns the primitive type of the given type or
13282 * NULL in case of error.
13283 */
13284static xmlSchemaTypePtr
13285xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13286{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013287 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013288 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013289 while (type != NULL) {
13290 if (type->type == XML_SCHEMA_TYPE_BASIC)
13291 return (type);
13292 type = type->baseType;
13293 }
13294
13295 return (NULL);
13296}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013297#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013298
Daniel Veillard01fa6152004-06-29 17:04:39 +000013299/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013300 * xmlSchemaCloneWildcardNsConstraints:
13301 * @ctxt: the schema parser context
13302 * @dest: the destination wildcard
13303 * @source: the source wildcard
13304 *
13305 * Clones the namespace constraints of source
13306 * and assignes them to dest.
13307 * Returns -1 on internal error, 0 otherwise.
13308 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013309static int
13310xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013311 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013312 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013313{
13314 xmlSchemaWildcardNsPtr cur, tmp, last;
13315
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013316 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013317 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013318 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013319 cur = source->nsSet;
13320 last = NULL;
13321 while (cur != NULL) {
13322 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13323 if (tmp == NULL)
13324 return(-1);
13325 tmp->value = cur->value;
13326 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013327 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013328 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013329 last->next = tmp;
13330 last = tmp;
13331 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013332 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013333 if (dest->negNsSet != NULL)
13334 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013335 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013336 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13337 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013338 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013339 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013340 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013341 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013342 return(0);
13343}
13344
Daniel Veillard50355f02004-06-08 17:52:16 +000013345/**
13346 * xmlSchemaUnionWildcards:
13347 * @ctxt: the schema parser context
13348 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013349 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013350 *
13351 * Unions the namespace constraints of the given wildcards.
13352 * @completeWild will hold the resulting union.
13353 * Returns a positive error code on failure, -1 in case of an
13354 * internal error, 0 otherwise.
13355 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013356static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013357xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013358 xmlSchemaWildcardPtr completeWild,
13359 xmlSchemaWildcardPtr curWild)
13360{
13361 xmlSchemaWildcardNsPtr cur, curB, tmp;
13362
13363 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013364 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013365 * value.
13366 */
13367 if ((completeWild->any == curWild->any) &&
13368 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13369 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013370
Daniel Veillard3646d642004-06-02 19:19:14 +000013371 if ((completeWild->negNsSet == NULL) ||
13372 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013373
Daniel Veillard3646d642004-06-02 19:19:14 +000013374 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013375 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013376
13377 /*
13378 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013379 */
13380 cur = completeWild->nsSet;
13381 while (cur != NULL) {
13382 found = 0;
13383 curB = curWild->nsSet;
13384 while (curB != NULL) {
13385 if (cur->value == curB->value) {
13386 found = 1;
13387 break;
13388 }
13389 curB = curB->next;
13390 }
13391 if (!found)
13392 break;
13393 cur = cur->next;
13394 }
13395 if (found)
13396 return(0);
13397 } else
13398 return(0);
13399 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013400 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013401 /*
13402 * 2 If either O1 or O2 is any, then any must be the value
13403 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013404 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013405 if (completeWild->any == 0) {
13406 completeWild->any = 1;
13407 if (completeWild->nsSet != NULL) {
13408 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13409 completeWild->nsSet = NULL;
13410 }
13411 if (completeWild->negNsSet != NULL) {
13412 xmlFree(completeWild->negNsSet);
13413 completeWild->negNsSet = NULL;
13414 }
13415 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013416 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013417 }
13418 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013419 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013420 * then the union of those sets must be the value.
13421 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013422 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013423 int found;
13424 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013425
Daniel Veillard3646d642004-06-02 19:19:14 +000013426 cur = curWild->nsSet;
13427 start = completeWild->nsSet;
13428 while (cur != NULL) {
13429 found = 0;
13430 curB = start;
13431 while (curB != NULL) {
13432 if (cur->value == curB->value) {
13433 found = 1;
13434 break;
13435 }
13436 curB = curB->next;
13437 }
13438 if (!found) {
13439 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013440 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013441 return (-1);
13442 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013443 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013444 completeWild->nsSet = tmp;
13445 }
13446 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013447 }
13448
Daniel Veillard3646d642004-06-02 19:19:14 +000013449 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013450 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013451 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013452 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013453 * or ·absent·), then a pair of not and ·absent· must be the value.
13454 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013455 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013456 (curWild->negNsSet != NULL) &&
13457 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13458 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013459
13460 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013461 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013462 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013463 * 5.
13464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013465 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013466 (completeWild->negNsSet->value != NULL) &&
13467 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013468 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013469 (curWild->negNsSet->value != NULL) &&
13470 (completeWild->nsSet != NULL))) {
13471
13472 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013473
Daniel Veillard3646d642004-06-02 19:19:14 +000013474 if (completeWild->nsSet != NULL) {
13475 cur = completeWild->nsSet;
13476 curB = curWild->negNsSet;
13477 } else {
13478 cur = curWild->nsSet;
13479 curB = completeWild->negNsSet;
13480 }
13481 nsFound = 0;
13482 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013484 absentFound = 1;
13485 else if (cur->value == curB->value)
13486 nsFound = 1;
13487 if (nsFound && absentFound)
13488 break;
13489 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013490 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013491
13492 if (nsFound && absentFound) {
13493 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013494 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013495 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013496 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013497 completeWild->any = 1;
13498 if (completeWild->nsSet != NULL) {
13499 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13500 completeWild->nsSet = NULL;
13501 }
13502 if (completeWild->negNsSet != NULL) {
13503 xmlFree(completeWild->negNsSet);
13504 completeWild->negNsSet = NULL;
13505 }
13506 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013507 /*
13508 * 5.2 If the set S includes the negated namespace name
13509 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013510 * be the value.
13511 */
13512 if (completeWild->nsSet != NULL) {
13513 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13514 completeWild->nsSet = NULL;
13515 }
13516 if (completeWild->negNsSet == NULL) {
13517 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13518 if (completeWild->negNsSet == NULL)
13519 return (-1);
13520 }
13521 completeWild->negNsSet->value = NULL;
13522 } else if ((!nsFound) && absentFound) {
13523 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013524 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013525 * namespace name, then the union is not expressible.
13526 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013527 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013528 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013529 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013530 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013531 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013532 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013533 /*
13534 * 5.4 If the set S does not include either the negated namespace
13535 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013536 * and a namespace name must be the value.
13537 */
13538 if (completeWild->negNsSet == NULL) {
13539 if (completeWild->nsSet != NULL) {
13540 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13541 completeWild->nsSet = NULL;
13542 }
13543 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13544 if (completeWild->negNsSet == NULL)
13545 return (-1);
13546 completeWild->negNsSet->value = curWild->negNsSet->value;
13547 }
13548 }
13549 return (0);
13550 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013551 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013552 * 6.
13553 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013554 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013555 (completeWild->negNsSet->value == NULL) &&
13556 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013557 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013558 (curWild->negNsSet->value == NULL) &&
13559 (completeWild->nsSet != NULL))) {
13560
13561 if (completeWild->nsSet != NULL) {
13562 cur = completeWild->nsSet;
13563 } else {
13564 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013565 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013566 while (cur != NULL) {
13567 if (cur->value == NULL) {
13568 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013569 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013570 * value.
13571 */
13572 completeWild->any = 1;
13573 if (completeWild->nsSet != NULL) {
13574 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13575 completeWild->nsSet = NULL;
13576 }
13577 if (completeWild->negNsSet != NULL) {
13578 xmlFree(completeWild->negNsSet);
13579 completeWild->negNsSet = NULL;
13580 }
13581 return (0);
13582 }
13583 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013584 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013585 if (completeWild->negNsSet == NULL) {
13586 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013587 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013588 * and ·absent· must be the value.
13589 */
13590 if (completeWild->nsSet != NULL) {
13591 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13592 completeWild->nsSet = NULL;
13593 }
13594 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13595 if (completeWild->negNsSet == NULL)
13596 return (-1);
13597 completeWild->negNsSet->value = NULL;
13598 }
13599 return (0);
13600 }
13601 return (0);
13602
13603}
13604
Daniel Veillard50355f02004-06-08 17:52:16 +000013605/**
13606 * xmlSchemaIntersectWildcards:
13607 * @ctxt: the schema parser context
13608 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013609 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013610 *
13611 * Intersects the namespace constraints of the given wildcards.
13612 * @completeWild will hold the resulting intersection.
13613 * Returns a positive error code on failure, -1 in case of an
13614 * internal error, 0 otherwise.
13615 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013616static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013617xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013618 xmlSchemaWildcardPtr completeWild,
13619 xmlSchemaWildcardPtr curWild)
13620{
William M. Brack803812b2004-06-03 02:11:24 +000013621 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013622
13623 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013624 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013625 * value.
13626 */
13627 if ((completeWild->any == curWild->any) &&
13628 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13629 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013630
Daniel Veillard3646d642004-06-02 19:19:14 +000013631 if ((completeWild->negNsSet == NULL) ||
13632 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013633
Daniel Veillard3646d642004-06-02 19:19:14 +000013634 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013635 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013636
13637 /*
13638 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013639 */
13640 cur = completeWild->nsSet;
13641 while (cur != NULL) {
13642 found = 0;
13643 curB = curWild->nsSet;
13644 while (curB != NULL) {
13645 if (cur->value == curB->value) {
13646 found = 1;
13647 break;
13648 }
13649 curB = curB->next;
13650 }
13651 if (!found)
13652 break;
13653 cur = cur->next;
13654 }
13655 if (found)
13656 return(0);
13657 } else
13658 return(0);
13659 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013660 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013661 /*
13662 * 2 If either O1 or O2 is any, then the other must be the value.
13663 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013664 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013665 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013666 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013667 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013668 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013669 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013670 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13671 * name or ·absent·) and the other is a set of (namespace names or
13672 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013673 * the set, minus ·absent· if it was in the set, must be the value.
13674 */
13675 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13676 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13677 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013678
Daniel Veillard3646d642004-06-02 19:19:14 +000013679 if (completeWild->nsSet == NULL) {
13680 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013681 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013682 return(-1);
13683 } else
13684 neg = curWild->negNsSet->value;
13685 /*
13686 * Remove absent and negated.
13687 */
13688 prev = NULL;
13689 cur = completeWild->nsSet;
13690 while (cur != NULL) {
13691 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013692 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013693 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013694 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013695 prev->next = cur->next;
13696 xmlFree(cur);
13697 break;
13698 }
13699 prev = cur;
13700 cur = cur->next;
13701 }
13702 if (neg != NULL) {
13703 prev = NULL;
13704 cur = completeWild->nsSet;
13705 while (cur != NULL) {
13706 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013707 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013708 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013709 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013710 prev->next = cur->next;
13711 xmlFree(cur);
13712 break;
13713 }
13714 prev = cur;
13715 cur = cur->next;
13716 }
13717 }
13718
13719 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013720 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013721 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013722 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013723 * then the intersection of those sets must be the value.
13724 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013725 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013726 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013727
Daniel Veillard3646d642004-06-02 19:19:14 +000013728 cur = completeWild->nsSet;
13729 prev = NULL;
13730 while (cur != NULL) {
13731 found = 0;
13732 curB = curWild->nsSet;
13733 while (curB != NULL) {
13734 if (cur->value == curB->value) {
13735 found = 1;
13736 break;
13737 }
13738 curB = curB->next;
13739 }
13740 if (!found) {
13741 if (prev == NULL)
13742 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013743 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013744 prev->next = cur->next;
13745 tmp = cur->next;
13746 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013747 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013748 continue;
13749 }
13750 prev = cur;
13751 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013752 }
13753
Daniel Veillard3646d642004-06-02 19:19:14 +000013754 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013755 }
13756 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013757 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013758 */
13759 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013760 (curWild->negNsSet != NULL) &&
13761 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013762 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013763 (curWild->negNsSet->value != NULL)) {
13764
13765 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013766 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013767 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013768 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013769 }
13770 /*
13771 * 6 If the one is a negation of a namespace name and the other
13772 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013773 * of a namespace name must be the value.
13774 */
13775 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13776 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013777 (completeWild->negNsSet->value == NULL)) {
13778 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013779 }
13780 return(0);
13781}
13782
Daniel Veillard50355f02004-06-08 17:52:16 +000013783/**
13784 * xmlSchemaIsWildcardNsConstraintSubset:
13785 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013786 * @sub: the first wildcard
13787 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013788 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013789 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13790 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013791 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013792 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013793 */
13794static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013795xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13796 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013797{
Daniel Veillard50355f02004-06-08 17:52:16 +000013798 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013799 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013800 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013801 if (super->any)
13802 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013803 /*
13804 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13805 * 2.2 super must be a pair of not and the same value.
13806 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013807 if ((sub->negNsSet != NULL) &&
13808 (super->negNsSet != NULL) &&
13809 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013810 return (0);
13811 /*
13812 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013813 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013814 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013815 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013816 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013817 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013818 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013819 xmlSchemaWildcardNsPtr cur, curB;
13820 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013821
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013822 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013823 while (cur != NULL) {
13824 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013825 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013826 while (curB != NULL) {
13827 if (cur->value == curB->value) {
13828 found = 1;
13829 break;
13830 }
13831 curB = curB->next;
13832 }
13833 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013834 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013835 cur = cur->next;
13836 }
13837 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013838 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013839 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013840 xmlSchemaWildcardNsPtr cur;
13841 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013842 * 3.2.2 super must be a pair of not and a namespace name or
13843 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013844 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013845 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013846 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013847 if (cur->value == super->negNsSet->value)
13848 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013849 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013850 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013851 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013852 }
13853 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013854 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013855}
13856
Daniel Veillard50355f02004-06-08 17:52:16 +000013857static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013858xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013859 int *fixed,
13860 const xmlChar **value,
13861 xmlSchemaValPtr *val)
13862{
13863 *fixed = 0;
13864 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013865 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013866 *val = NULL;
13867
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013868 if (attruse->defValue != NULL) {
13869 *value = attruse->defValue;
13870 *val = attruse->defVal;
13871 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013872 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013873 return(1);
13874 } else if ((WXS_ATTRUSE_DECL(attruse) != NULL) &&
13875 (WXS_ATTRUSE_DECL(attruse)->defValue != NULL)) {
13876 *value = (WXS_ATTRUSE_DECL(attruse))->defValue;
13877 *val = (WXS_ATTRUSE_DECL(attruse))->defVal;
13878 if ((WXS_ATTRUSE_DECL(attruse))->flags & XML_SCHEMAS_ATTR_FIXED)
13879 *fixed = 1;
13880 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013881 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013882 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013883}
Daniel Veillard3646d642004-06-02 19:19:14 +000013884/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013885 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013886 * @wild: the wildcard
13887 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013888 *
13889 * Validation Rule: Wildcard allows Namespace Name
13890 * (cvc-wildcard-namespace)
13891 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013892 * Returns 0 if the given namespace matches the wildcard,
13893 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013894 */
13895static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013896xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13897 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013898{
13899 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013900 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013901
13902 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013903 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013904 else if (wild->nsSet != NULL) {
13905 xmlSchemaWildcardNsPtr cur;
13906
13907 cur = wild->nsSet;
13908 while (cur != NULL) {
13909 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013910 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013911 cur = cur->next;
13912 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013913 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013914 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013915 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013916
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013917 return(1);
13918}
13919
13920#define XML_SCHEMA_ACTION_DERIVE 0
13921#define XML_SCHEMA_ACTION_REDEFINE 1
13922
13923#define WXS_ACTION_STR(a) \
13924((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13925
13926/*
13927* Schema Component Constraint:
13928* Derivation Valid (Restriction, Complex)
13929* derivation-ok-restriction (2) - (4)
13930*
13931* ATTENTION:
13932* In XML Schema 1.1 this will be:
13933* Validation Rule:
13934* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13935*
13936*/
13937static int
13938xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13939 int action,
13940 xmlSchemaBasicItemPtr item,
13941 xmlSchemaBasicItemPtr baseItem,
13942 xmlSchemaItemListPtr uses,
13943 xmlSchemaItemListPtr baseUses,
13944 xmlSchemaWildcardPtr wild,
13945 xmlSchemaWildcardPtr baseWild)
13946{
13947 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013948 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013949 const xmlChar *bEffValue;
13950 int effFixed;
13951
13952 if (uses != NULL) {
13953 for (i = 0; i < uses->nbItems; i++) {
13954 cur = uses->items[i];
13955 found = 0;
13956 if (baseUses == NULL)
13957 goto not_found;
13958 for (j = 0; j < baseUses->nbItems; j++) {
13959 bcur = baseUses->items[j];
13960 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13961 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13962 (WXS_ATTRUSE_DECL_TNS(cur) ==
13963 WXS_ATTRUSE_DECL_TNS(bcur)))
13964 {
13965 /*
13966 * (2.1) "If there is an attribute use in the {attribute
13967 * uses} of the {base type definition} (call this B) whose
13968 * {attribute declaration} has the same {name} and {target
13969 * namespace}, then all of the following must be true:"
13970 */
13971 found = 1;
13972
13973 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
13974 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
13975 {
13976 xmlChar *str = NULL;
13977 /*
13978 * (2.1.1) "one of the following must be true:"
13979 * (2.1.1.1) "B's {required} is false."
13980 * (2.1.1.2) "R's {required} is true."
13981 */
13982 xmlSchemaPAttrUseErr4(pctxt,
13983 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
13984 WXS_ITEM_NODE(item), item, cur,
13985 "The 'optional' attribute use is inconsistent "
13986 "with the corresponding 'required' attribute use of "
13987 "the %s %s",
13988 WXS_ACTION_STR(action),
13989 xmlSchemaGetComponentDesignation(&str, baseItem),
13990 NULL, NULL);
13991 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013992 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013993 } else if (xmlSchemaCheckCOSSTDerivedOK(pctxt,
13994 WXS_ATTRUSE_TYPEDEF(cur),
13995 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
13996 {
13997 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
13998
13999 /*
14000 * SPEC (2.1.2) "R's {attribute declaration}'s
14001 * {type definition} must be validly derived from
14002 * B's {type definition} given the empty set as
14003 * defined in Type Derivation OK (Simple) (§3.14.6)."
14004 */
14005 xmlSchemaPAttrUseErr4(pctxt,
14006 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14007 WXS_ITEM_NODE(item), item, cur,
14008 "The attribute declaration's %s "
14009 "is not validly derived from "
14010 "the corresponding %s of the "
14011 "attribute declaration in the %s %s",
14012 xmlSchemaGetComponentDesignation(&strA,
14013 WXS_ATTRUSE_TYPEDEF(cur)),
14014 xmlSchemaGetComponentDesignation(&strB,
14015 WXS_ATTRUSE_TYPEDEF(bcur)),
14016 WXS_ACTION_STR(action),
14017 xmlSchemaGetComponentDesignation(&strC, baseItem));
14018 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14019 FREE_AND_NULL(strA);
14020 FREE_AND_NULL(strB);
14021 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014022 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014023 } else {
14024 /*
14025 * 2.1.3 [Definition:] Let the effective value
14026 * constraint of an attribute use be its {value
14027 * constraint}, if present, otherwise its {attribute
14028 * declaration}'s {value constraint} .
14029 */
14030 xmlSchemaGetEffectiveValueConstraint(bcur,
14031 &effFixed, &bEffValue, NULL);
14032 /*
14033 * 2.1.3 ... one of the following must be true
14034 *
14035 * 2.1.3.1 B's ·effective value constraint· is
14036 * ·absent· or default.
14037 */
14038 if ((bEffValue != NULL) &&
14039 (effFixed == 1)) {
14040 const xmlChar *rEffValue = NULL;
14041
14042 xmlSchemaGetEffectiveValueConstraint(bcur,
14043 &effFixed, &rEffValue, NULL);
14044 /*
14045 * 2.1.3.2 R's ·effective value constraint· is
14046 * fixed with the same string as B's.
14047 * MAYBE TODO: Compare the computed values.
14048 * Hmm, it says "same string" so
14049 * string-equality might really be sufficient.
14050 */
14051 if ((effFixed == 0) ||
14052 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14053 {
14054 xmlChar *str = NULL;
14055
14056 xmlSchemaPAttrUseErr4(pctxt,
14057 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14058 WXS_ITEM_NODE(item), item, cur,
14059 "The effective value constraint of the "
14060 "attribute use is inconsistent with "
14061 "its correspondent in the %s %s",
14062 WXS_ACTION_STR(action),
14063 xmlSchemaGetComponentDesignation(&str,
14064 baseItem),
14065 NULL, NULL);
14066 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014067 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014068 }
14069 }
14070 }
14071 break;
14072 }
14073 }
14074not_found:
14075 if (!found) {
14076 /*
14077 * (2.2) "otherwise the {base type definition} must have an
14078 * {attribute wildcard} and the {target namespace} of the
14079 * R's {attribute declaration} must be ·valid· with respect
14080 * to that wildcard, as defined in Wildcard allows Namespace
14081 * Name (§3.10.4)."
14082 */
14083 if ((baseWild == NULL) ||
14084 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14085 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14086 {
14087 xmlChar *str = NULL;
14088
14089 xmlSchemaPAttrUseErr4(pctxt,
14090 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14091 WXS_ITEM_NODE(item), item, cur,
14092 "Neither a matching attribute use, "
14093 "nor a matching wildcard exists in the %s %s",
14094 WXS_ACTION_STR(action),
14095 xmlSchemaGetComponentDesignation(&str, baseItem),
14096 NULL, NULL);
14097 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014098 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014099 }
14100 }
14101 }
14102 }
14103 /*
14104 * SPEC derivation-ok-restriction (3):
14105 * (3) "For each attribute use in the {attribute uses} of the {base type
14106 * definition} whose {required} is true, there must be an attribute
14107 * use with an {attribute declaration} with the same {name} and
14108 * {target namespace} as its {attribute declaration} in the {attribute
14109 * uses} of the complex type definition itself whose {required} is true.
14110 */
14111 if (baseUses != NULL) {
14112 for (j = 0; j < baseUses->nbItems; j++) {
14113 bcur = baseUses->items[j];
14114 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14115 continue;
14116 found = 0;
14117 if (uses != NULL) {
14118 for (i = 0; i < uses->nbItems; i++) {
14119 cur = uses->items[i];
14120 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14121 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14122 (WXS_ATTRUSE_DECL_TNS(cur) ==
14123 WXS_ATTRUSE_DECL_TNS(bcur))) {
14124 found = 1;
14125 break;
14126 }
14127 }
14128 }
14129 if (!found) {
14130 xmlChar *strA = NULL, *strB = NULL;
14131
14132 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14133 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14134 NULL, item,
14135 "A matching attribute use for the "
14136 "'required' %s of the %s %s is missing",
14137 xmlSchemaGetComponentDesignation(&strA, bcur),
14138 WXS_ACTION_STR(action),
14139 xmlSchemaGetComponentDesignation(&strB, baseItem),
14140 NULL);
14141 FREE_AND_NULL(strA);
14142 FREE_AND_NULL(strB);
14143 }
14144 }
14145 }
14146 /*
14147 * derivation-ok-restriction (4)
14148 */
14149 if (wild != NULL) {
14150 /*
14151 * (4) "If there is an {attribute wildcard}, all of the
14152 * following must be true:"
14153 */
14154 if (baseWild == NULL) {
14155 xmlChar *str = NULL;
14156
14157 /*
14158 * (4.1) "The {base type definition} must also have one."
14159 */
14160 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14161 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14162 NULL, item,
14163 "The %s has an attribute wildcard, "
14164 "but the %s %s '%s' does not have one",
14165 WXS_ITEM_TYPE_NAME(item),
14166 WXS_ACTION_STR(action),
14167 WXS_ITEM_TYPE_NAME(baseItem),
14168 xmlSchemaGetComponentQName(&str, baseItem));
14169 FREE_AND_NULL(str);
14170 return(pctxt->err);
14171 } else if ((baseWild->any == 0) &&
14172 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14173 {
14174 xmlChar *str = NULL;
14175 /*
14176 * (4.2) "The complex type definition's {attribute wildcard}'s
14177 * {namespace constraint} must be a subset of the {base type
14178 * definition}'s {attribute wildcard}'s {namespace constraint},
14179 * as defined by Wildcard Subset (§3.10.6)."
14180 */
14181 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14182 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14183 NULL, item,
14184 "The attribute wildcard is not a valid "
14185 "subset of the wildcard in the %s %s '%s'",
14186 WXS_ACTION_STR(action),
14187 WXS_ITEM_TYPE_NAME(baseItem),
14188 xmlSchemaGetComponentQName(&str, baseItem),
14189 NULL);
14190 FREE_AND_NULL(str);
14191 return(pctxt->err);
14192 }
14193 /* 4.3 Unless the {base type definition} is the ·ur-type
14194 * definition·, the complex type definition's {attribute
14195 * wildcard}'s {process contents} must be identical to or
14196 * stronger than the {base type definition}'s {attribute
14197 * wildcard}'s {process contents}, where strict is stronger
14198 * than lax is stronger than skip.
14199 */
14200 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14201 (wild->processContents < baseWild->processContents)) {
14202 xmlChar *str = NULL;
14203 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14204 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14205 NULL, baseItem,
14206 "The {process contents} of the attribute wildcard is "
14207 "weaker than the one in the %s %s '%s'",
14208 WXS_ACTION_STR(action),
14209 WXS_ITEM_TYPE_NAME(baseItem),
14210 xmlSchemaGetComponentQName(&str, baseItem),
14211 NULL);
14212 FREE_AND_NULL(str)
14213 return(pctxt->err);
14214 }
14215 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014216 return(0);
14217}
14218
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014219
14220static int
14221xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14222 xmlSchemaBasicItemPtr item,
14223 xmlSchemaWildcardPtr *completeWild,
14224 xmlSchemaItemListPtr list,
14225 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014226/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014227 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014228 * @ctxt: the schema parser context
14229 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014230 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014231 *
14232 * Builds the wildcard and the attribute uses on the given complex type.
14233 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014234 *
14235 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14236 * strings, so recheck this if we start to hardcode some schemata, since
14237 * they might not be in the same dict.
14238 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014239 */
14240static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014241xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014242 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014243{
14244 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014245 xmlSchemaAttributeUsePtr use;
14246 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014247
Daniel Veillard01fa6152004-06-29 17:04:39 +000014248 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014249 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014250 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014251 return (-1);
14252 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014253 baseType = type->baseType;
14254 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14255 if (xmlSchemaTypeFixup(baseType, pctxt) == -1)
14256 return(-1);
14257
14258 uses = type->attrUses;
14259 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014260 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014261 * Expand attribute group references. And build the 'complete'
14262 * wildcard, i.e. intersect multiple wildcards.
14263 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014264 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014265 if (uses != NULL) {
14266 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014267 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014268 * This one will transfer all attr. prohibitions
14269 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014270 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014271 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14272 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14273 pctxt->attrProhibs) == -1)
14274 {
14275 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14276 "failed to expand attributes");
14277 }
14278 if (pctxt->attrProhibs->nbItems != 0)
14279 prohibs = pctxt->attrProhibs;
14280 } else {
14281 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14282 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14283 NULL) == -1)
14284 {
14285 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14286 "failed to expand attributes");
14287 }
14288 }
14289 }
14290 /*
14291 * Inherit the attribute uses of the base type.
14292 */
14293 if (baseUses != NULL) {
14294 int i, j;
14295 xmlSchemaAttributeUseProhibPtr pro;
14296
14297 if (WXS_IS_RESTRICTION(type)) {
14298 int usesCount;
14299 xmlSchemaAttributeUsePtr tmp;
14300
14301 if (uses != NULL)
14302 usesCount = uses->nbItems;
14303 else
14304 usesCount = 0;
14305
14306 /* Restriction. */
14307 for (i = 0; i < baseUses->nbItems; i++) {
14308 use = baseUses->items[i];
14309 if (prohibs) {
14310 /*
14311 * Filter out prohibited uses.
14312 */
14313 for (j = 0; j < prohibs->nbItems; j++) {
14314 pro = prohibs->items[j];
14315 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14316 (WXS_ATTRUSE_DECL_TNS(use) ==
14317 pro->targetNamespace))
14318 {
14319 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014320 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014321 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014322 }
14323 if (usesCount) {
14324 /*
14325 * Filter out existing uses.
14326 */
14327 for (j = 0; j < usesCount; j++) {
14328 tmp = uses->items[j];
14329 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14330 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14331 (WXS_ATTRUSE_DECL_TNS(use) ==
14332 WXS_ATTRUSE_DECL_TNS(tmp)))
14333 {
14334 goto inherit_next;
14335 }
14336 }
14337 }
14338 if (uses == NULL) {
14339 type->attrUses = xmlSchemaItemListCreate();
14340 if (type->attrUses == NULL)
14341 goto exit_failure;
14342 uses = type->attrUses;
14343 }
14344 xmlSchemaItemListAddSize(uses, 2, use);
14345inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014346 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014347 } else {
14348 /* Extension. */
14349 for (i = 0; i < baseUses->nbItems; i++) {
14350 use = baseUses->items[i];
14351 if (uses == NULL) {
14352 type->attrUses = xmlSchemaItemListCreate();
14353 if (type->attrUses == NULL)
14354 goto exit_failure;
14355 uses = type->attrUses;
14356 }
14357 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14358 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014359 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014360 }
14361 /*
14362 * Shrink attr. uses.
14363 */
14364 if (uses) {
14365 if (uses->nbItems == 0) {
14366 xmlSchemaItemListFree(uses);
14367 type->attrUses = NULL;
14368 }
14369 /*
14370 * TODO: We could shrink the size of the array
14371 * to fit the actual number of items.
14372 */
14373 }
14374 /*
14375 * Compute the complete wildcard.
14376 */
14377 if (WXS_IS_EXTENSION(type)) {
14378 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014379 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014380 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14381 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014382 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014383 if (type->attributeWildcard != NULL) {
14384 /*
14385 * Union the complete wildcard with the base wildcard.
14386 * SPEC {attribute wildcard}
14387 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14388 * and {annotation} are those of the ·complete wildcard·,
14389 * and whose {namespace constraint} is the intensional union
14390 * of the {namespace constraint} of the ·complete wildcard·
14391 * and of the ·base wildcard·, as defined in Attribute
14392 * Wildcard Union (§3.10.6)."
14393 */
14394 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14395 baseType->attributeWildcard) == -1)
14396 goto exit_failure;
14397 } else {
14398 /*
14399 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14400 * then the ·base wildcard·."
14401 */
14402 type->attributeWildcard = baseType->attributeWildcard;
14403 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014404 } else {
14405 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014406 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14407 * ·complete wildcard"
14408 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014409 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014410 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014411 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014412 /*
14413 * SPEC {attribute wildcard}
14414 * (3.1) "If the <restriction> alternative is chosen, then the
14415 * ·complete wildcard·;"
14416 * NOOP
14417 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014418 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014419
Daniel Veillard3646d642004-06-02 19:19:14 +000014420 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014421
14422exit_failure:
14423 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014424}
14425
14426/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014427 * xmlSchemaTypeFinalContains:
14428 * @schema: the schema
14429 * @type: the type definition
14430 * @final: the final
14431 *
14432 * Evaluates if a type definition contains the given "final".
14433 * This does take "finalDefault" into account as well.
14434 *
14435 * Returns 1 if the type does containt the given "final",
14436 * 0 otherwise.
14437 */
14438static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014439xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014440{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014441 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014442 return (0);
14443 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014444 return (1);
14445 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014446 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014447}
14448
14449/**
14450 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14451 * @type: the Union Simple Type
14452 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014453 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014454 * returns NULL otherwise.
14455 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014456static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014457xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14458{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014459 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014460 if (type->memberTypes != NULL)
14461 return (type->memberTypes);
14462 else
14463 type = type->baseType;
14464 }
14465 return (NULL);
14466}
14467
14468/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014469 * xmlSchemaGetParticleTotalRangeMin:
14470 * @particle: the particle
14471 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014472 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014473 * (all and sequence) + (choice)
14474 *
14475 * Returns the minimun Effective Total Range.
14476 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014477static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014478xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014479{
14480 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014481 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014482 return (0);
14483 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014484 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014485 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014486 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014487
14488 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014489 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014490 while (part != NULL) {
14491 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14492 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014493 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014494 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014495 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014496 if (cur == 0)
14497 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014498 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014499 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014500 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014501 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014502 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014503 } else {
14504 /* <all> and <sequence> */
14505 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014506 xmlSchemaParticlePtr part =
14507 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014508
14509 if (part == NULL)
14510 return (0);
14511 do {
14512 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14513 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014514 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014515 else
14516 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014517 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014518 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014519 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014520 }
14521}
14522
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014523/**
14524 * xmlSchemaGetParticleTotalRangeMax:
14525 * @particle: the particle
14526 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014527 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014528 * (all and sequence) + (choice)
14529 *
14530 * Returns the maximum Effective Total Range.
14531 */
14532static int
14533xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14534{
14535 if ((particle->children == NULL) ||
14536 (particle->children->children == NULL))
14537 return (0);
14538 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14539 int max = -1, cur;
14540 xmlSchemaParticlePtr part =
14541 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014542
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014543 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14544 if (part->children == NULL)
14545 continue;
14546 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14547 (part->children->type == XML_SCHEMA_TYPE_ANY))
14548 cur = part->maxOccurs;
14549 else
14550 cur = xmlSchemaGetParticleTotalRangeMax(part);
14551 if (cur == UNBOUNDED)
14552 return (UNBOUNDED);
14553 if ((max < cur) || (max == -1))
14554 max = cur;
14555 }
14556 /* TODO: Handle overflows? */
14557 return (particle->maxOccurs * max);
14558 } else {
14559 /* <all> and <sequence> */
14560 int sum = 0, cur;
14561 xmlSchemaParticlePtr part =
14562 (xmlSchemaParticlePtr) particle->children->children;
14563
14564 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14565 if (part->children == NULL)
14566 continue;
14567 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14568 (part->children->type == XML_SCHEMA_TYPE_ANY))
14569 cur = part->maxOccurs;
14570 else
14571 cur = xmlSchemaGetParticleTotalRangeMax(part);
14572 if (cur == UNBOUNDED)
14573 return (UNBOUNDED);
14574 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14575 return (UNBOUNDED);
14576 sum += cur;
14577 }
14578 /* TODO: Handle overflows? */
14579 return (particle->maxOccurs * sum);
14580 }
14581}
14582
14583/**
14584 * xmlSchemaIsParticleEmptiable:
14585 * @particle: the particle
14586 *
14587 * Schema Component Constraint: Particle Emptiable
14588 * Checks whether the given particle is emptiable.
14589 *
14590 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014591 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014592static int
14593xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14594{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014595 /*
14596 * SPEC (1) "Its {min occurs} is 0."
14597 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014598 if ((particle == NULL) || (particle->minOccurs == 0) ||
14599 (particle->children == NULL))
14600 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014601 /*
14602 * SPEC (2) "Its {term} is a group and the minimum part of the
14603 * effective total range of that group, [...] is 0."
14604 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014605 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014606 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014607 return (1);
14608 }
14609 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014610}
14611
14612/**
14613 * xmlSchemaCheckCOSSTDerivedOK:
14614 * @type: the derived simple type definition
14615 * @baseType: the base type definition
14616 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014617 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014618 * Type Derivation OK (Simple) (cos-st-derived-OK)
14619 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014620 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014621 * derived from @baseType.
14622 *
14623 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014624 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014625static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014626xmlSchemaCheckCOSSTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
14627 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014628 xmlSchemaTypePtr baseType,
14629 int subset)
14630{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014631 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014632 * 1 They are the same type definition.
14633 * TODO: The identy check might have to be more complex than this.
14634 */
14635 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014636 return (0);
14637 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014638 * 2.1 restriction is not in the subset, or in the {final}
14639 * of its own {base type definition};
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014640 * TODO: Revise this, it looks strange.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014641 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014642 if (WXS_IS_TYPE_NOT_FIXED(type))
14643 if (xmlSchemaTypeFixup(type, ctxt) == -1)
14644 return(-1);
14645 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14646 if (xmlSchemaTypeFixup(baseType, ctxt) == -1)
14647 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014648 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014649 (xmlSchemaTypeFinalContains(type->baseType,
14650 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14651 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014652 }
14653 /* 2.2 */
14654 if (type->baseType == baseType) {
14655 /*
14656 * 2.2.1 D's ·base type definition· is B.
14657 */
14658 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014659 }
14660 /*
14661 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14662 * and is validly derived from B given the subset, as defined by this
14663 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014664 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014665 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
14666 (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014667 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014668 return (0);
14669 }
14670 /*
14671 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014672 * definition·.
14673 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014674 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14675 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014676 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014677 }
14678 /*
14679 * 2.2.4 B's {variety} is union and D is validly derived from a type
14680 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014681 * defined by this constraint.
14682 *
14683 * NOTE: This seems not to involve built-in types, since there is no
14684 * built-in Union Simple Type.
14685 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014686 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014687 xmlSchemaTypeLinkPtr cur;
14688
14689 cur = baseType->memberTypes;
14690 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014691 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
14692 if (xmlSchemaTypeFixup(cur->type, ctxt) == -1)
14693 return(-1);
14694 if (xmlSchemaCheckCOSSTDerivedOK(ctxt,
14695 type, cur->type, subset) == 0)
14696 {
14697 /*
14698 * It just has to be validly derived from at least one
14699 * member-type.
14700 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014701 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014702 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014703 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014704 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014705 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014706 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14707}
14708
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014709/**
14710 * xmlSchemaCheckTypeDefCircularInternal:
14711 * @pctxt: the schema parser context
14712 * @ctxtType: the type definition
14713 * @ancestor: an ancestor of @ctxtType
14714 *
14715 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014716 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014717 *
14718 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14719 * circular, 0 otherwise.
14720 */
14721static int
14722xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14723 xmlSchemaTypePtr ctxtType,
14724 xmlSchemaTypePtr ancestor)
14725{
14726 int ret;
14727
14728 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14729 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014730
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014731 if (ctxtType == ancestor) {
14732 xmlSchemaPCustomErr(pctxt,
14733 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014734 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014735 "The definition is circular", NULL);
14736 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14737 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014738 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14739 /*
14740 * Avoid inifinite recursion on circular types not yet checked.
14741 */
14742 return (0);
14743 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014744 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14745 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14746 ancestor->baseType);
14747 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14748 return (ret);
14749}
14750
14751/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014752 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014753 * @item: the complex/simple type definition
14754 * @ctxt: the parser context
14755 * @name: the name
14756 *
14757 * Checks for circular type definitions.
14758 */
14759static void
14760xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014761 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014762{
14763 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014764 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14765 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014766 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014767 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14768 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014769}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014770
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014771/*
14772* Simple Type Definition Representation OK (src-simple-type) 4
14773*
14774* "4 Circular union type definition is disallowed. That is, if the
14775* <union> alternative is chosen, there must not be any entries in the
14776* memberTypes [attribute] at any depth which resolve to the component
14777* corresponding to the <simpleType>."
14778*
14779* Note that this should work on the *representation* of a component,
14780* thus assumes any union types in the member types not being yet
14781* substituted. At this stage we need the variety of the types
14782* to be already computed.
14783*/
14784static int
14785xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14786 xmlSchemaTypePtr ctxType,
14787 xmlSchemaTypeLinkPtr members)
14788{
14789 xmlSchemaTypeLinkPtr member;
14790 xmlSchemaTypePtr memberType;
14791
14792 member = members;
14793 while (member != NULL) {
14794 memberType = member->type;
14795 while ((memberType != NULL) &&
14796 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14797 if (memberType == ctxType) {
14798 xmlSchemaPCustomErr(pctxt,
14799 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014800 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014801 "The union type definition is circular", NULL);
14802 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14803 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014804 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014805 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14806 {
14807 int res;
14808 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14809 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14810 ctxType,
14811 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14812 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14813 if (res != 0)
14814 return(res);
14815 }
14816 memberType = memberType->baseType;
14817 }
14818 member = member->next;
14819 }
14820 return(0);
14821}
14822
14823static int
14824xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14825 xmlSchemaTypePtr type)
14826{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014827 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014828 return(0);
14829 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14830 type->memberTypes));
14831}
14832
Daniel Veillard01fa6152004-06-29 17:04:39 +000014833/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014834 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014835 * @item: the complex/simple type definition
14836 * @ctxt: the parser context
14837 * @name: the name
14838 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014839 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014840 */
14841static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014842xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014843 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014844{
14845 if (typeDef == NULL)
14846 return;
14847
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014848 /*
14849 * Resolve the base type.
14850 */
14851 if (typeDef->baseType == NULL) {
14852 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14853 typeDef->base, typeDef->baseNs);
14854 if (typeDef->baseType == NULL) {
14855 xmlSchemaPResCompAttrErr(ctxt,
14856 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014857 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014858 "base", typeDef->base, typeDef->baseNs,
14859 XML_SCHEMA_TYPE_SIMPLE, NULL);
14860 return;
14861 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014862 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014863 if (WXS_IS_SIMPLE(typeDef)) {
14864 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014865 /*
14866 * Resolve the memberTypes.
14867 */
14868 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14869 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014870 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014871 /*
14872 * Resolve the itemType.
14873 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014874 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14875
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014876 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014877 typeDef->base, typeDef->baseNs);
14878
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014879 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014880 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14881 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014882 typeDef->subtypes = NULL;
14883 xmlSchemaPResCompAttrErr(ctxt,
14884 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014885 WXS_BASIC_CAST typeDef, typeDef->node,
14886 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014887 XML_SCHEMA_TYPE_SIMPLE, NULL);
14888 }
14889 }
14890 return;
14891 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014892 }
14893 /*
14894 * The ball of letters below means, that if we have a particle
14895 * which has a QName-helper component as its {term}, we want
14896 * to resolve it...
14897 */
14898 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14899 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14900 XML_SCHEMA_TYPE_PARTICLE) &&
14901 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14902 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14903 XML_SCHEMA_EXTRA_QNAMEREF))
14904 {
14905 xmlSchemaQNameRefPtr ref =
14906 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14907 xmlSchemaModelGroupDefPtr groupDef;
14908
14909 /*
14910 * URGENT TODO: Test this.
14911 */
14912 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14913 /*
14914 * Resolve the MG definition reference.
14915 */
14916 groupDef =
14917 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14918 ref->itemType, ref->name, ref->targetNamespace);
14919 if (groupDef == NULL) {
14920 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14921 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14922 "ref", ref->name, ref->targetNamespace, ref->itemType,
14923 NULL);
14924 /* Remove the particle. */
14925 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14926 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14927 /* Remove the particle. */
14928 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14929 else {
14930 /*
14931 * Assign the MG definition's {model group} to the
14932 * particle's {term}.
14933 */
14934 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14935
14936 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14937 /*
14938 * SPEC cos-all-limited (1.2)
14939 * "1.2 the {term} property of a particle with
14940 * {max occurs}=1 which is part of a pair which constitutes
14941 * the {content type} of a complex type definition."
14942 */
14943 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14944 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14945 /* TODO: error code */
14946 XML_SCHEMAP_COS_ALL_LIMITED,
14947 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14948 "The particle's {max occurs} must be 1, since the "
14949 "reference resolves to an 'all' model group",
14950 NULL, NULL);
14951 }
14952 }
14953 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014954 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014955}
14956
14957
14958
14959/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014960 * xmlSchemaCheckSTPropsCorrect:
14961 * @ctxt: the schema parser context
14962 * @type: the simple type definition
14963 *
14964 * Checks st-props-correct.
14965 *
14966 * Returns 0 if the properties are correct,
14967 * if not, a positive error code and -1 on internal
14968 * errors.
14969 */
14970static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014971xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014972 xmlSchemaTypePtr type)
14973{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014974 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000014975 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000014976
Daniel Veillardc0826a72004-08-10 14:17:33 +000014977 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014978 /*
14979 * Schema Component Constraint: Simple Type Definition Properties Correct
14980 *
14981 * NOTE: This is somehow redundant, since we actually built a simple type
14982 * to have all the needed information; this acts as an self test.
14983 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014984 /* Base type: If the datatype has been ·derived· by ·restriction·
14985 * then the Simple Type Definition component from which it is ·derived·,
14986 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000014987 */
14988 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014989 /*
14990 * TODO: Think about: "modulo the impact of Missing
14991 * Sub-components (§5.3)."
14992 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000014993 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014994 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014995 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000014996 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014997 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014998
Daniel Veillard01fa6152004-06-29 17:04:39 +000014999 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015000 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015001 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015002 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015003 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015004 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015005 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015006 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015007 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15008 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015009 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015010 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015011 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015012 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015013 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015014 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015015 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015016 "the simple ur-type definition as base type, not '%s'",
15017 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015018 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015019 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15020 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015021 /*
15022 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015023 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015024 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15025 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015026 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015027 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015028 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015029 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015030 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15031 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015032 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015033
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015034 /*
15035 * 3 The {final} of the {base type definition} must not contain restriction.
15036 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015037 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015038 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15039 xmlSchemaPCustomErr(ctxt,
15040 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015041 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015042 "The 'final' of its base type '%s' must not contain "
15043 "'restriction'",
15044 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015045 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015046 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015047 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015048
15049 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015050 * 2 All simple type definitions must be derived ultimately from the ·simple
15051 * ur-type definition (so· circular definitions are disallowed). That is, it
15052 * must be possible to reach a built-in primitive datatype or the ·simple
15053 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015054 *
15055 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015056 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015057 return (0);
15058}
15059
15060/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015061 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015062 * @ctxt: the schema parser context
15063 * @type: the simple type definition
15064 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015065 * Schema Component Constraint:
15066 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15067
15068 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015069 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015070 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015071 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015072 * a positive error code otherwise.
15073 */
15074static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015075xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015076 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015077{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015078 xmlChar *str = NULL;
15079
Daniel Veillard01fa6152004-06-29 17:04:39 +000015080 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015081 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15082 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015083 return (-1);
15084 }
15085
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015086 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015087 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015088 /*
15089 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015090 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015091 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015092 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015093 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015094 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015095 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015096 "The base type '%s' is not an atomic simple type",
15097 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015098 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015099 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15100 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015101 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015102 * restriction.
15103 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015104 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015105 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015106 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015107 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015108 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015109 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015110 "The final of its base type '%s' must not contain 'restriction'",
15111 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015112 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015113 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15114 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015115
15116 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015117 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015118 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015119 * Primitive datatypes.
15120 */
15121 if (type->facets != NULL) {
15122 xmlSchemaFacetPtr facet;
15123 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015124
Daniel Veillard01fa6152004-06-29 17:04:39 +000015125 primitive = xmlSchemaGetPrimitiveType(type);
15126 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015127 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15128 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015129 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015130 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015131 facet = type->facets;
15132 do {
15133 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015134 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015135 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015136 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015137 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015138 }
15139 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015140 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015141 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015142 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015143 }
15144 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015145 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15146 * of the {base type definition} (call this BF),then the DF's {value}
15147 * must be a valid restriction of BF's {value} as defined in
15148 * [XML Schemas: Datatypes]."
15149 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015150 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015151 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015152 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015153 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015154 xmlSchemaTypePtr itemType = NULL;
15155
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015156 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015157 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015158 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15159 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015160 return (-1);
15161 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015162 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015163 xmlSchemaTypeFixup(itemType, pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015164 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015165 * 2.1 The {item type definition} must have a {variety} of atomic or
15166 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015167 * must be atomic).
15168 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015169 if ((! WXS_IS_ATOMIC(itemType)) &&
15170 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015171 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015172 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015173 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015174 "The item type '%s' does not have a variety of atomic or union",
15175 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015176 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015177 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015178 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015179 xmlSchemaTypeLinkPtr member;
15180
15181 member = itemType->memberTypes;
15182 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015183 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015184 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015185 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015186 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015187 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015188 "member type '%s' of this item type is not atomic",
15189 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015190 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015191 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15192 }
15193 member = member->next;
15194 }
15195 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015196
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015197 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015198 xmlSchemaFacetPtr facet;
15199 /*
15200 * This is the case if we have: <simpleType><list ..
15201 */
15202 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015203 * 2.3.1
15204 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015205 * contain list.
15206 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015207 if (xmlSchemaTypeFinalContains(itemType,
15208 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15209 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015210 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015211 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015212 "The final of its item type '%s' must not contain 'list'",
15213 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015214 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015215 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15216 }
15217 /*
15218 * 2.3.1.2 The {facets} must only contain the whiteSpace
15219 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015220 * OPTIMIZE TODO: the S4S already disallows any facet
15221 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015222 */
15223 if (type->facets != NULL) {
15224 facet = type->facets;
15225 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015226 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015227 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015228 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015229 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015230 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15231 }
15232 facet = facet->next;
15233 } while (facet != NULL);
15234 }
15235 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015236 * MAYBE TODO: (Hmm, not really) Datatypes states:
15237 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15238 * whose ·lexical space· allows space (such as string or anyURI)or
15239 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015240 * ·lexical space· allows space.
15241 */
15242 } else {
15243 /*
15244 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015245 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015246 */
15247 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015248 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015249 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15250 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015251 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015252 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015253 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015254 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015255 "The base type '%s' must be a list type",
15256 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015257 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015258 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15259 }
15260 /*
15261 * 2.3.2.2 The {final} of the {base type definition} must not
15262 * contain restriction.
15263 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015264 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015265 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015266 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015267 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015268 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015269 "The 'final' of the base type '%s' must not contain 'restriction'",
15270 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015271 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015272 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15273 }
15274 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015275 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015276 * from the {base type definition}'s {item type definition} given
15277 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15278 */
15279 {
15280 xmlSchemaTypePtr baseItemType;
15281
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015282 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015283 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015284 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15285 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015286 return (-1);
15287 }
15288 if ((itemType != baseItemType) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015289 (xmlSchemaCheckCOSSTDerivedOK(pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015290 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015291 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015292 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015293 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015294 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015295 "The item type '%s' is not validly derived from "
15296 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015297 xmlSchemaGetComponentQName(&str, itemType),
15298 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15299 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015300
15301 FREE_AND_NULL(str)
15302 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015303 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015304 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15305 }
15306 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015307
Daniel Veillard01fa6152004-06-29 17:04:39 +000015308 if (type->facets != NULL) {
15309 xmlSchemaFacetPtr facet;
15310 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015311 /*
15312 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015313 * and enumeration facet components are allowed among the {facets}.
15314 */
15315 facet = type->facets;
15316 do {
15317 switch (facet->type) {
15318 case XML_SCHEMA_FACET_LENGTH:
15319 case XML_SCHEMA_FACET_MINLENGTH:
15320 case XML_SCHEMA_FACET_MAXLENGTH:
15321 case XML_SCHEMA_FACET_WHITESPACE:
15322 /*
15323 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015324 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015325 */
15326 case XML_SCHEMA_FACET_PATTERN:
15327 case XML_SCHEMA_FACET_ENUMERATION:
15328 break;
15329 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015330 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015331 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015332 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015333 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015334 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015335 * invalid facets.
15336 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015337 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015338 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015339 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015340 facet = facet->next;
15341 } while (facet != NULL);
15342 if (ok == 0)
15343 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15344 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015345 * SPEC (2.3.2.5) (same as 1.3.2)
15346 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015347 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015348 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015349 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015350 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015351 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015352 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015353 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015354 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015355 * atomic or list.
15356 */
15357 xmlSchemaTypeLinkPtr member;
15358
15359 member = type->memberTypes;
15360 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015361 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015362 xmlSchemaTypeFixup(member->type, pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015363
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015364 if ((! WXS_IS_ATOMIC(member->type)) &&
15365 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015366 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015367 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015368 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015369 "The member type '%s' is neither an atomic, nor a list type",
15370 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015371 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015372 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15373 }
15374 member = member->next;
15375 }
15376 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015377 * 3.3.1 If the {base type definition} is the ·simple ur-type
15378 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015379 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015380 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015381 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015382 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015383 * {final} which does not contain union.
15384 */
15385 member = type->memberTypes;
15386 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015387 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015388 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015389 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015390 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015391 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015392 "The 'final' of member type '%s' contains 'union'",
15393 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015394 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015395 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15396 }
15397 member = member->next;
15398 }
15399 /*
15400 * 3.3.1.2 The {facets} must be empty.
15401 */
15402 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015403 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015404 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015405 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015406 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015407 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15408 }
15409 } else {
15410 /*
15411 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015412 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015413 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015414 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015415 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015416 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015417 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015418 "The base type '%s' is not a union type",
15419 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015420 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015421 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15422 }
15423 /*
15424 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15425 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015426 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015427 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015428 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015429 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015430 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015431 "The 'final' of its base type '%s' must not contain 'restriction'",
15432 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015433 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015434 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15435 }
15436 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015437 * 3.3.2.3 The {member type definitions}, in order, must be validly
15438 * derived from the corresponding type definitions in the {base
15439 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015440 * as defined in Type Derivation OK (Simple) (§3.14.6).
15441 */
15442 {
15443 xmlSchemaTypeLinkPtr baseMember;
15444
15445 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015446 * OPTIMIZE: if the type is restricting, it has no local defined
15447 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015448 * thus a check for equality can be skipped.
15449 */
15450 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015451 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015452 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015453 * types of it's base type. This check seems not necessary with
15454 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015455 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015456 */
15457 if (type->memberTypes != NULL) {
15458 member = type->memberTypes;
15459 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015460 if ((member == NULL) && (baseMember != NULL)) {
15461 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15462 "different number of member types in base");
15463 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015464 while (member != NULL) {
15465 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015466 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15467 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015468 }
15469 if ((member->type != baseMember->type) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015470 (xmlSchemaCheckCOSSTDerivedOK(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015471 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015472 xmlChar *strBMT = NULL, *strBT = NULL;
15473
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015474 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015475 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015476 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015477 "The member type %s is not validly "
15478 "derived from its corresponding member "
15479 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015480 xmlSchemaGetComponentQName(&str, member->type),
15481 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15482 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015483 FREE_AND_NULL(str)
15484 FREE_AND_NULL(strBMT)
15485 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015486 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015487 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015488 member = member->next;
15489 baseMember = baseMember->next;
15490 }
15491 }
15492 }
15493 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015494 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015495 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015496 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015497 if (type->facets != NULL) {
15498 xmlSchemaFacetPtr facet;
15499 int ok = 1;
15500
15501 facet = type->facets;
15502 do {
15503 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15504 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015505 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015506 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015507 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015508 ok = 0;
15509 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015510 facet = facet->next;
15511 } while (facet != NULL);
15512 if (ok == 0)
15513 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015514
Daniel Veillard01fa6152004-06-29 17:04:39 +000015515 }
15516 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015517 * SPEC (3.3.2.5) (same as 1.3.2)
15518 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015519 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015520 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015521 */
15522 }
15523 }
15524
15525 return (0);
15526}
15527
15528/**
15529 * xmlSchemaCheckSRCSimpleType:
15530 * @ctxt: the schema parser context
15531 * @type: the simple type definition
15532 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015533 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015534 *
15535 * Returns 0 if the constraints are satisfied,
15536 * if not a positive error code and -1 on internal
15537 * errors.
15538 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015539#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015540static int
15541xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15542 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015543{
15544 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015545 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015546 * must satisfy the conditions set out in Constraints on Simple Type
15547 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015548 */
15549 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015550 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015551 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015552 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015553 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015554 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015555 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015556 /*
15557 *
15558 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015559 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015560 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015561 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015562 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015563 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015564 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015565 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015566 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015567 /*
15568 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015569 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015570 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015571 return (0);
15572}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015573#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015574
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015575static int
15576xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15577{
15578 if (ctxt->vctxt == NULL) {
15579 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15580 if (ctxt->vctxt == NULL) {
15581 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015582 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015583 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015584 "failed to create a temp. validation context.\n",
15585 NULL, NULL);
15586 return (-1);
15587 }
15588 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015589 xmlSchemaSetValidErrors(ctxt->vctxt,
15590 ctxt->error, ctxt->warning, ctxt->userData);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015591 }
15592 return (0);
15593}
15594
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015595static int
15596xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15597 xmlNodePtr node,
15598 xmlSchemaTypePtr type,
15599 const xmlChar *value,
15600 xmlSchemaValPtr *retVal,
15601 int fireErrors,
15602 int normalize,
15603 int isNormalized);
15604
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015605/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015606 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015607 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015608 * @type: the simple type definition
15609 * @value: the default value
15610 * @node: an optional node (the holder of the value)
15611 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015612 * Schema Component Constraint: Element Default Valid (Immediate)
15613 * (cos-valid-default)
15614 * This will be used by the parser only. For the validator there's
15615 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015616 *
15617 * Returns 0 if the constraints are satisfied,
15618 * if not, a positive error code and -1 on internal
15619 * errors.
15620 */
15621static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015622xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15623 xmlNodePtr node,
15624 xmlSchemaTypePtr type,
15625 const xmlChar *value,
15626 xmlSchemaValPtr *val)
15627{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015628 int ret = 0;
15629
15630 /*
15631 * cos-valid-default:
15632 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015633 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015634 * definition the appropriate case among the following must be true:
15635 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015636 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015637 /*
15638 * Complex type.
15639 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015640 * SPEC (2.1) "its {content type} must be a simple type definition
15641 * or mixed."
15642 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015643 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015644 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015645 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015646 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15647 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015648 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015649 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015650 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015651 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015652 "For a string to be a valid default, the type definition "
15653 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015654 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015655 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15656 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015657 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015658 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015659 * 1 If the type definition is a simple type definition, then the string
15660 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015661 * Valid (§3.14.4).
15662 *
15663 * AND
15664 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015665 * 2.2.1 If the {content type} is a simple type definition, then the
15666 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015667 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015668 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015669 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015670 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015671 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015672 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015673 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015674 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015675 else
15676 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015677
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015678 if (ret < 0) {
15679 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15680 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015681 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015682
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015683 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015684}
15685
15686/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015687 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015688 * @ctxt: the schema parser context
15689 * @type: the complex type definition
15690 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015691 *.(4.6) Constraints on Complex Type Definition Schema Components
15692 * Schema Component Constraint:
15693 * Complex Type Definition Properties Correct (ct-props-correct)
15694 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015695 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015696 * Returns 0 if the constraints are satisfied, a positive
15697 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015698 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015699static int
15700xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15701 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015702{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015703 /*
15704 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15705 *
15706 * SPEC (1) "The values of the properties of a complex type definition must
15707 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015708 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015709 * Sub-components (§5.3)."
15710 */
15711 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015712 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015713 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015714 /*
15715 * SPEC (2) "If the {base type definition} is a simple type definition,
15716 * the {derivation method} must be extension."
15717 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015718 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015719 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015720 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015721 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015722 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015723 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015724 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015725 /*
15726 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15727 * definition·. That is, it must be possible to reach the ·ur-type
15728 * definition by repeatedly following the {base type definition}."
15729 *
15730 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015731 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015732 /*
15733 * NOTE that (4) and (5) need the following:
15734 * - attribute uses need to be already inherited (apply attr. prohibitions)
15735 * - attribute group references need to be expanded already
15736 * - simple types need to be typefixed already
15737 */
15738 if (type->attrUses &&
15739 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15740 {
15741 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15742 xmlSchemaAttributeUsePtr use, tmp;
15743 int i, j, hasId = 0;
15744
15745 for (i = uses->nbItems -1; i >= 0; i--) {
15746 use = uses->items[i];
15747
15748 /*
15749 * SPEC ct-props-correct
15750 * (4) "Two distinct attribute declarations in the
15751 * {attribute uses} must not have identical {name}s and
15752 * {target namespace}s."
15753 */
15754 if (i > 0) {
15755 for (j = i -1; j >= 0; j--) {
15756 tmp = uses->items[j];
15757 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15758 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15759 (WXS_ATTRUSE_DECL_TNS(use) ==
15760 WXS_ATTRUSE_DECL_TNS(tmp)))
15761 {
15762 xmlChar *str = NULL;
15763
15764 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15765 XML_SCHEMAP_AG_PROPS_CORRECT,
15766 NULL, WXS_BASIC_CAST type,
15767 "Duplicate %s",
15768 xmlSchemaGetComponentDesignation(&str, use),
15769 NULL);
15770 FREE_AND_NULL(str);
15771 /*
15772 * Remove the duplicate.
15773 */
15774 if (xmlSchemaItemListRemove(uses, i) == -1)
15775 goto exit_failure;
15776 goto next_use;
15777 }
15778 }
15779 }
15780 /*
15781 * SPEC ct-props-correct
15782 * (5) "Two distinct attribute declarations in the
15783 * {attribute uses} must not have {type definition}s which
15784 * are or are derived from ID."
15785 */
15786 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15787 if (xmlSchemaIsDerivedFromBuiltInType(
15788 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15789 {
15790 if (hasId) {
15791 xmlChar *str = NULL;
15792
15793 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15794 XML_SCHEMAP_AG_PROPS_CORRECT,
15795 NULL, WXS_BASIC_CAST type,
15796 "There must not exist more than one attribute "
15797 "declaration of type 'xs:ID' "
15798 "(or derived from 'xs:ID'). The %s violates this "
15799 "constraint",
15800 xmlSchemaGetComponentDesignation(&str, use),
15801 NULL);
15802 FREE_AND_NULL(str);
15803 if (xmlSchemaItemListRemove(uses, i) == -1)
15804 goto exit_failure;
15805 }
15806
15807 hasId = 1;
15808 }
15809 }
15810next_use: {}
15811 }
15812 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015813 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015814exit_failure:
15815 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015816}
15817
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015818static int
15819xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15820 xmlSchemaTypePtr typeB)
15821{
15822 /*
15823 * TODO: This should implement component-identity
15824 * in the future.
15825 */
15826 if ((typeA == NULL) || (typeB == NULL))
15827 return (0);
15828 return (typeA == typeB);
15829}
15830
15831/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015832 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015833 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015834 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015835 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015836 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015837 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015838 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015839 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15840 *
15841 * STATUS: completed
15842 *
15843 * Returns 0 if the constraints are satisfied, or 1
15844 * if not.
15845 */
15846static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015847xmlSchemaCheckCOSCTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15848 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015849 xmlSchemaTypePtr baseType,
15850 int set)
15851{
15852 int equal = xmlSchemaAreEqualTypes(type, baseType);
15853 /* TODO: Error codes. */
15854 /*
15855 * SPEC "For a complex type definition (call it D, for derived)
15856 * to be validly derived from a type definition (call this
15857 * B, for base) given a subset of {extension, restriction}
15858 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015859 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015860 if (! equal) {
15861 /*
15862 * SPEC (1) "If B and D are not the same type definition, then the
15863 * {derivation method} of D must not be in the subset."
15864 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015865 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15866 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015867 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015868 } else {
15869 /*
15870 * SPEC (2.1) "B and D must be the same type definition."
15871 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015872 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015873 }
15874 /*
15875 * SPEC (2.2) "B must be D's {base type definition}."
15876 */
15877 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015878 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015879 /*
15880 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15881 * definition·."
15882 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015883 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015884 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015885
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015886 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015887 /*
15888 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15889 * must be validly derived from B given the subset as defined by this
15890 * constraint."
15891 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015892 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015893 baseType, set));
15894 } else {
15895 /*
15896 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15897 * must be validly derived from B given the subset as defined in Type
15898 * Derivation OK (Simple) (§3.14.6).
15899 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015900 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
15901 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015902 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015903}
15904
15905/**
15906 * xmlSchemaCheckCOSDerivedOK:
15907 * @type: the derived simple type definition
15908 * @baseType: the base type definition
15909 *
15910 * Calls:
15911 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015912 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015913 * Checks wheter @type can be validly derived from @baseType.
15914 *
15915 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015916 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015917static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015918xmlSchemaCheckCOSDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15919 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015920 xmlSchemaTypePtr baseType,
15921 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015922{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015923 if (WXS_IS_SIMPLE(type))
15924 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015925 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015926 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015927}
15928
William M. Brack2f2a6632004-08-20 23:09:47 +000015929/**
15930 * xmlSchemaCheckCOSCTExtends:
15931 * @ctxt: the schema parser context
15932 * @type: the complex type definition
15933 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015934 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015935 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015936 * Derivation Valid (Extension) (cos-ct-extends)
15937 *
15938 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015939 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015940 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015941 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015942 *
15943 * Returns 0 if the constraints are satisfied, a positive
15944 * error code if not and -1 if an internal error occured.
15945 */
15946static int
15947xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15948 xmlSchemaTypePtr type)
15949{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015950 xmlSchemaTypePtr base = type->baseType;
15951 /*
15952 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15953 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015954 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015955 /*
15956 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015957 * then all of the following must be true:"
15958 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015959 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015960 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015961 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015962 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000015963 */
15964 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
15965 xmlSchemaPCustomErr(ctxt,
15966 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015967 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000015968 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015969 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000015970 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
15971 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015972
15973 /*
15974 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
15975 * since they are automatically satisfied through the
15976 * inheriting mechanism.
15977 * Note that even if redefining components, the inheriting mechanism
15978 * is used.
15979 */
15980#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000015981 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015982 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015983 * uses}
15984 * of the complex type definition itself, that is, for every attribute
15985 * use in the {attribute uses} of the {base type definition}, there
15986 * must be an attribute use in the {attribute uses} of the complex
15987 * type definition itself whose {attribute declaration} has the same
15988 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015989 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000015990 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015991 if (base->attrUses != NULL) {
15992 int i, j, found;
15993 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000015994
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015995 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
15996 buse = (WXS_LIST_CAST base->attrUses)->items[i];
15997 found = 0;
15998 if (type->attrUses != NULL) {
15999 use = (WXS_LIST_CAST type->attrUses)->items[j];
16000 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16001 {
16002 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16003 WXS_ATTRUSE_DECL_NAME(buse)) &&
16004 (WXS_ATTRUSE_DECL_TNS(use) ==
16005 WXS_ATTRUSE_DECL_TNS(buse)) &&
16006 (WXS_ATTRUSE_TYPEDEF(use) ==
16007 WXS_ATTRUSE_TYPEDEF(buse))
16008 {
16009 found = 1;
16010 break;
16011 }
16012 }
16013 }
16014 if (! found) {
16015 xmlChar *str = NULL;
16016
16017 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16018 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16019 NULL, WXS_BASIC_CAST type,
16020 /*
16021 * TODO: The report does not indicate that also the
16022 * type needs to be the same.
16023 */
16024 "This type is missing a matching correspondent "
16025 "for its {base type}'s %s in its {attribute uses}",
16026 xmlSchemaGetComponentDesignation(&str,
16027 buse->children),
16028 NULL);
16029 FREE_AND_NULL(str)
16030 }
16031 }
16032 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016033 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016034 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16035 * definition must also have one, and the base type definition's
16036 * {attribute wildcard}'s {namespace constraint} must be a subset
16037 * of the complex type definition's {attribute wildcard}'s {namespace
16038 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016039 */
16040
16041 /*
16042 * MAYBE TODO: Enable if ever needed. But this will be needed only
16043 * if created the type via a schema construction API.
16044 */
16045 if (base->attributeWildcard != NULL) {
16046 if (type->attributeWilcard == NULL) {
16047 xmlChar *str = NULL;
16048
16049 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16050 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16051 NULL, type,
16052 "The base %s has an attribute wildcard, "
16053 "but this type is missing an attribute wildcard",
16054 xmlSchemaGetComponentDesignation(&str, base));
16055 FREE_AND_NULL(str)
16056
16057 } else if (xmlSchemaCheckCOSNSSubset(
16058 base->attributeWildcard, type->attributeWildcard))
16059 {
16060 xmlChar *str = NULL;
16061
16062 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16063 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16064 NULL, type,
16065 "The attribute wildcard is not a valid "
16066 "superset of the one in the base %s",
16067 xmlSchemaGetComponentDesignation(&str, base));
16068 FREE_AND_NULL(str)
16069 }
16070 }
16071#endif
16072 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016073 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016074 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016075 if ((type->contentTypeDef != NULL) &&
16076 (type->contentTypeDef == base->contentTypeDef)) {
16077 /*
16078 * SPEC (1.4.1) "The {content type} of the {base type definition}
16079 * and the {content type} of the complex type definition itself
16080 * must be the same simple type definition"
16081 * PASS
16082 */
16083 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16084 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16085 /*
16086 * SPEC (1.4.2) "The {content type} of both the {base type
16087 * definition} and the complex type definition itself must
16088 * be empty."
16089 * PASS
16090 */
16091 } else {
16092 /*
16093 * SPEC (1.4.3) "All of the following must be true:"
16094 */
16095 if (type->subtypes == NULL) {
16096 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016097 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016098 * definition itself must specify a particle.
16099 */
16100 xmlSchemaPCustomErr(ctxt,
16101 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016102 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016103 "The content type must specify a particle", NULL);
16104 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16105 }
16106 /*
16107 * SPEC (1.4.3.2) "One of the following must be true:"
16108 */
16109 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16110 /*
16111 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16112 * definition} must be empty.
16113 * PASS
16114 */
16115 } else {
16116 /*
16117 * SPEC (1.4.3.2.2) "All of the following must be true:"
16118 */
16119 if ((type->contentType != base->contentType) ||
16120 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16121 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16122 /*
16123 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16124 * or both must be element-only."
16125 */
16126 xmlSchemaPCustomErr(ctxt,
16127 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016128 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016129 "The content type of both, the type and its base "
16130 "type, must either 'mixed' or 'element-only'", NULL);
16131 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016132 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016133 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016134 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016135 * complex type definition must be a ·valid extension·
16136 * of the {base type definition}'s particle, as defined
16137 * in Particle Valid (Extension) (§3.9.6)."
16138 *
16139 * NOTE that we won't check "Particle Valid (Extension)",
16140 * since it is ensured by the derivation process in
16141 * xmlSchemaTypeFixup(). We need to implement this when heading
16142 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016143 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016144 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016145 }
16146 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016147 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016148 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016149 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016150 } else {
16151 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016152 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016153 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016154 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016155 if (type->contentTypeDef != base) {
16156 /*
16157 * SPEC (2.1) "The {content type} must be the same simple type
16158 * definition."
16159 */
16160 xmlSchemaPCustomErr(ctxt,
16161 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016162 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016163 "The content type must be the simple base type", NULL);
16164 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16165 }
16166 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16167 /*
16168 * SPEC (2.2) "The {final} of the {base type definition} must not
16169 * contain extension"
16170 * NOTE that this is the same as (1.1).
16171 */
16172 xmlSchemaPCustomErr(ctxt,
16173 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016174 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016175 "The 'final' of the base type definition "
16176 "contains 'extension'", NULL);
16177 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016178 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016179 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016180 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016181}
16182
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016183/**
16184 * xmlSchemaCheckDerivationOKRestriction:
16185 * @ctxt: the schema parser context
16186 * @type: the complex type definition
16187 *
16188 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016189 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016190 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16191 *
16192 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016193 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016194 * (5.4.2) ???
16195 *
16196 * ATTENTION:
16197 * In XML Schema 1.1 this will be:
16198 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016199 *
16200 * Returns 0 if the constraints are satisfied, a positive
16201 * error code if not and -1 if an internal error occured.
16202 */
16203static int
16204xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16205 xmlSchemaTypePtr type)
16206{
16207 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016208
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016209 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016210 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016211 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016212 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016213 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016214 if (! WXS_IS_COMPLEX(base)) {
16215 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16216 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16217 type->node, WXS_BASIC_CAST type,
16218 "The base type must be a complex type", NULL, NULL);
16219 return(ctxt->err);
16220 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016221 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16222 /*
16223 * SPEC (1) "The {base type definition} must be a complex type
16224 * definition whose {final} does not contain restriction."
16225 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016226 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16227 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16228 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016229 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016230 "contains 'restriction'", NULL, NULL);
16231 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016232 }
16233 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016234 * SPEC (2), (3) and (4)
16235 * Those are handled in a separate function, since the
16236 * same constraints are needed for redefinition of
16237 * attribute groups as well.
16238 */
16239 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16240 XML_SCHEMA_ACTION_DERIVE,
16241 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16242 type->attrUses, base->attrUses,
16243 type->attributeWildcard,
16244 base->attributeWildcard) == -1)
16245 {
16246 return(-1);
16247 }
16248 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016249 * SPEC (5) "One of the following must be true:"
16250 */
16251 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16252 /*
16253 * SPEC (5.1) "The {base type definition} must be the
16254 * ·ur-type definition·."
16255 * PASS
16256 */
16257 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16258 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16259 /*
16260 * SPEC (5.2.1) "The {content type} of the complex type definition
16261 * must be a simple type definition"
16262 *
16263 * SPEC (5.2.2) "One of the following must be true:"
16264 */
16265 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016266 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16267 {
16268 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016269 /*
16270 * SPEC (5.2.2.1) "The {content type} of the {base type
16271 * definition} must be a simple type definition from which
16272 * the {content type} is validly derived given the empty
16273 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016274 *
16275 * ATTENTION TODO: This seems not needed if the type implicitely
16276 * derived from the base type.
16277 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016278 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016279 err = xmlSchemaCheckCOSSTDerivedOK(ctxt, type->contentTypeDef,
16280 base->contentTypeDef, 0);
16281 if (err != 0) {
16282 xmlChar *strA = NULL, *strB = NULL;
16283
16284 if (err == -1)
16285 return(-1);
16286 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16287 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16288 NULL, WXS_BASIC_CAST type,
16289 "The {content type} %s is not validly derived from the "
16290 "base type's {content type} %s",
16291 xmlSchemaGetComponentDesignation(&strA,
16292 type->contentTypeDef),
16293 xmlSchemaGetComponentDesignation(&strB,
16294 base->contentTypeDef));
16295 FREE_AND_NULL(strA);
16296 FREE_AND_NULL(strB);
16297 return(ctxt->err);
16298 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016299 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16300 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016301 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016302 /*
16303 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16304 * and have a particle which is ·emptiable· as defined in
16305 * Particle Emptiable (§3.9.6)."
16306 * PASS
16307 */
16308 } else {
16309 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016310 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16311 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016312 "The content type of the base type must be either "
16313 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016314 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016315 }
16316 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16317 /*
16318 * SPEC (5.3.1) "The {content type} of the complex type itself must
16319 * be empty"
16320 */
16321 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16322 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016323 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016324 * definition} must also be empty."
16325 * PASS
16326 */
16327 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16328 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16329 xmlSchemaIsParticleEmptiable(
16330 (xmlSchemaParticlePtr) base->subtypes)) {
16331 /*
16332 * SPEC (5.3.2.2) "The {content type} of the {base type
16333 * definition} must be elementOnly or mixed and have a particle
16334 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16335 * PASS
16336 */
16337 } else {
16338 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016339 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16340 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016341 "The content type of the base type must be either "
16342 "empty or 'mixed' (or 'elements-only') and an emptiable "
16343 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016344 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016345 }
16346 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016347 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016348 /*
16349 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16350 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016351 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016352 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016353 /*
16354 * SPEC (5.4.1.2) "The {content type} of the complex type
16355 * definition itself and of the {base type definition} must be
16356 * mixed"
16357 */
16358 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016359 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16360 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016361 "If the content type is 'mixed', then the content type of the "
16362 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016363 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016364 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016365 /*
16366 * SPEC (5.4.2) "The particle of the complex type definition itself
16367 * must be a ·valid restriction· of the particle of the {content
16368 * type} of the {base type definition} as defined in Particle Valid
16369 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016370 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016371 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016372 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016373 } else {
16374 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016375 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16376 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016377 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016378 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016379 }
16380 return (0);
16381}
16382
16383/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016384 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016385 * @ctxt: the schema parser context
16386 * @type: the complex type definition
16387 *
16388 * (3.4.6) Constraints on Complex Type Definition Schema Components
16389 *
16390 * Returns 0 if the constraints are satisfied, a positive
16391 * error code if not and -1 if an internal error occured.
16392 */
16393static int
16394xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16395 xmlSchemaTypePtr type)
16396{
16397 int ret;
16398 /*
16399 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016400 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016401 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16402 if (ret != 0)
16403 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016404 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016405 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16406 else
16407 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16408 return (ret);
16409}
16410
16411/**
16412 * xmlSchemaCheckSRCCT:
16413 * @ctxt: the schema parser context
16414 * @type: the complex type definition
16415 *
16416 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016417 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016418 * Complex Type Definition Representation OK (src-ct)
16419 *
16420 * Returns 0 if the constraints are satisfied, a positive
16421 * error code if not and -1 if an internal error occured.
16422 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016423static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016424xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016425 xmlSchemaTypePtr type)
16426{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016427 xmlSchemaTypePtr base;
16428 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016429
16430 /*
16431 * TODO: Adjust the error codes here, as I used
16432 * XML_SCHEMAP_SRC_CT_1 only yet.
16433 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016434 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016435 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016436 /*
16437 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016438 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016439 * must be a complex type definition;
16440 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016441 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016442 xmlChar *str = NULL;
16443 xmlSchemaPCustomErr(ctxt,
16444 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016445 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016446 "If using <complexContent>, the base type is expected to be "
16447 "a complex type. The base type '%s' is a simple type",
16448 xmlSchemaFormatQName(&str, base->targetNamespace,
16449 base->name));
16450 FREE_AND_NULL(str)
16451 return (XML_SCHEMAP_SRC_CT_1);
16452 }
16453 } else {
16454 /*
16455 * SPEC
16456 * 2 If the <simpleContent> alternative is chosen, all of the
16457 * following must be true:
16458 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16459 * base [attribute] must be one of the following:
16460 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016461 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016462 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016463 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016464 /*
16465 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016466 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016467 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016468 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016469 xmlSchemaPCustomErr(ctxt,
16470 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016471 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016472 "If using <simpleContent> and <restriction>, the base "
16473 "type must be a complex type. The base type '%s' is "
16474 "a simple type",
16475 xmlSchemaFormatQName(&str, base->targetNamespace,
16476 base->name));
16477 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016478 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016479 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016480 } else {
16481 /* Base type is a complex type. */
16482 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16483 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16484 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016485 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016486 * simple type definition;
16487 * PASS
16488 */
16489 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016490 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016491 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016492 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016493 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016494 type->name);
16495 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016496 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016497 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016498 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016499
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016500 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016501 * 2.1.2 only if the <restriction> alternative is also
16502 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016503 * is mixed and a particle emptiable.
16504 */
16505 if (! xmlSchemaIsParticleEmptiable(
16506 (xmlSchemaParticlePtr) base->subtypes)) {
16507 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016508 } else
16509 /*
16510 * Attention: at this point the <simpleType> child is in
16511 * ->contentTypeDef (put there during parsing).
16512 */
16513 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016514 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016515 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016516 * 2.2 If clause 2.1.2 above is satisfied, then there
16517 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016518 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016519 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016520 /* TODO: Change error code to ..._SRC_CT_2_2. */
16521 xmlSchemaPCustomErr(ctxt,
16522 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016523 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016524 "A <simpleType> is expected among the children "
16525 "of <restriction>, if <simpleContent> is used and "
16526 "the base type '%s' is a complex type",
16527 xmlSchemaFormatQName(&str, base->targetNamespace,
16528 base->name));
16529 FREE_AND_NULL(str)
16530 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016531 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016532 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016533 ret = XML_SCHEMAP_SRC_CT_1;
16534 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016535 }
16536 if (ret > 0) {
16537 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016538 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016539 xmlSchemaPCustomErr(ctxt,
16540 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016541 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016542 "If <simpleContent> and <restriction> is used, the "
16543 "base type must be a simple type or a complex type with "
16544 "mixed content and particle emptiable. The base type "
16545 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016546 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016547 base->name));
16548 } else {
16549 xmlSchemaPCustomErr(ctxt,
16550 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016551 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016552 "If <simpleContent> and <extension> is used, the "
16553 "base type must be a simple type. The base type '%s' "
16554 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016555 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016556 base->name));
16557 }
16558 FREE_AND_NULL(str)
16559 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016560 }
16561 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016562 * SPEC (3) "The corresponding complex type definition component must
16563 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016564 * Definition Schema Components (§3.4.6);"
16565 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016566 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016567 /*
16568 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016569 * above for {attribute wildcard} is satisfied, the intensional
16570 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016571 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016572 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016573 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016574 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016575}
William M. Brack2f2a6632004-08-20 23:09:47 +000016576
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016577#ifdef ENABLE_PARTICLE_RESTRICTION
16578/**
16579 * xmlSchemaCheckParticleRangeOK:
16580 * @ctxt: the schema parser context
16581 * @type: the complex type definition
16582 *
16583 * (3.9.6) Constraints on Particle Schema Components
16584 * Schema Component Constraint:
16585 * Occurrence Range OK (range-ok)
16586 *
16587 * STATUS: complete
16588 *
16589 * Returns 0 if the constraints are satisfied, a positive
16590 * error code if not and -1 if an internal error occured.
16591 */
16592static int
16593xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16594 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016595{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016596 if (rmin < bmin)
16597 return (1);
16598 if ((bmax != UNBOUNDED) &&
16599 (rmax > bmax))
16600 return (1);
16601 return (0);
16602}
16603
16604/**
16605 * xmlSchemaCheckRCaseNameAndTypeOK:
16606 * @ctxt: the schema parser context
16607 * @r: the restricting element declaration particle
16608 * @b: the base element declaration particle
16609 *
16610 * (3.9.6) Constraints on Particle Schema Components
16611 * Schema Component Constraint:
16612 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16613 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016614 *
16615 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016616 * MISSING (3.2.3)
16617 * CLARIFY: (3.2.2)
16618 *
16619 * Returns 0 if the constraints are satisfied, a positive
16620 * error code if not and -1 if an internal error occured.
16621 */
16622static int
16623xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16624 xmlSchemaParticlePtr r,
16625 xmlSchemaParticlePtr b)
16626{
16627 xmlSchemaElementPtr elemR, elemB;
16628
16629 /* TODO: Error codes (rcase-NameAndTypeOK). */
16630 elemR = (xmlSchemaElementPtr) r->children;
16631 elemB = (xmlSchemaElementPtr) b->children;
16632 /*
16633 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16634 * the same."
16635 */
16636 if ((elemR != elemB) &&
16637 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16638 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16639 return (1);
16640 /*
16641 * SPEC (2) "R's occurrence range is a valid restriction of B's
16642 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16643 */
16644 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16645 b->minOccurs, b->maxOccurs) != 0)
16646 return (1);
16647 /*
16648 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16649 * {scope} are global."
16650 */
16651 if (elemR == elemB)
16652 return (0);
16653 /*
16654 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16655 */
16656 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16657 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16658 return (1);
16659 /*
16660 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16661 * or is not fixed, or R's declaration's {value constraint} is fixed
16662 * with the same value."
16663 */
16664 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16665 ((elemR->value == NULL) ||
16666 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16667 /* TODO: Equality of the initial value or normalized or canonical? */
16668 (! xmlStrEqual(elemR->value, elemB->value))))
16669 return (1);
16670 /*
16671 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16672 * definitions} is a subset of B's declaration's {identity-constraint
16673 * definitions}, if any."
16674 */
16675 if (elemB->idcs != NULL) {
16676 /* TODO */
16677 }
16678 /*
16679 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16680 * superset of B's declaration's {disallowed substitutions}."
16681 */
16682 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16683 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16684 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16685 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16686 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16687 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16688 return (1);
16689 /*
16690 * SPEC (3.2.5) "R's {type definition} is validly derived given
16691 * {extension, list, union} from B's {type definition}"
16692 *
16693 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16694 * set, if the corresponding constraints handle "restriction" and
16695 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016696 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016697 */
16698 {
16699 int set = 0;
16700
16701 set |= SUBSET_EXTENSION;
16702 set |= SUBSET_LIST;
16703 set |= SUBSET_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016704 if (xmlSchemaCheckCOSDerivedOK(elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016705 elemB->subtypes, set) != 0)
16706 return (1);
16707 }
16708 return (0);
16709}
16710
16711/**
16712 * xmlSchemaCheckRCaseNSCompat:
16713 * @ctxt: the schema parser context
16714 * @r: the restricting element declaration particle
16715 * @b: the base wildcard particle
16716 *
16717 * (3.9.6) Constraints on Particle Schema Components
16718 * Schema Component Constraint:
16719 * Particle Derivation OK (Elt:Any -- NSCompat)
16720 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016721 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016722 * STATUS: complete
16723 *
16724 * Returns 0 if the constraints are satisfied, a positive
16725 * error code if not and -1 if an internal error occured.
16726 */
16727static int
16728xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16729 xmlSchemaParticlePtr r,
16730 xmlSchemaParticlePtr b)
16731{
16732 /* TODO:Error codes (rcase-NSCompat). */
16733 /*
16734 * SPEC "For an element declaration particle to be a ·valid restriction·
16735 * of a wildcard particle all of the following must be true:"
16736 *
16737 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16738 * with respect to the wildcard's {namespace constraint} as defined by
16739 * Wildcard allows Namespace Name (§3.10.4)."
16740 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016741 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016742 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16743 return (1);
16744 /*
16745 * SPEC (2) "R's occurrence range is a valid restriction of B's
16746 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16747 */
16748 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16749 b->minOccurs, b->maxOccurs) != 0)
16750 return (1);
16751
16752 return (0);
16753}
16754
16755/**
16756 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16757 * @ctxt: the schema parser context
16758 * @r: the restricting element declaration particle
16759 * @b: the base model group particle
16760 *
16761 * (3.9.6) Constraints on Particle Schema Components
16762 * Schema Component Constraint:
16763 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16764 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016765 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016766 * STATUS: TODO
16767 *
16768 * Returns 0 if the constraints are satisfied, a positive
16769 * error code if not and -1 if an internal error occured.
16770 */
16771static int
16772xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16773 xmlSchemaParticlePtr r,
16774 xmlSchemaParticlePtr b)
16775{
16776 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16777 TODO
16778 return (0);
16779}
16780
16781/**
16782 * xmlSchemaCheckRCaseNSSubset:
16783 * @ctxt: the schema parser context
16784 * @r: the restricting wildcard particle
16785 * @b: the base wildcard particle
16786 *
16787 * (3.9.6) Constraints on Particle Schema Components
16788 * Schema Component Constraint:
16789 * Particle Derivation OK (Any:Any -- NSSubset)
16790 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016791 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016792 * STATUS: complete
16793 *
16794 * Returns 0 if the constraints are satisfied, a positive
16795 * error code if not and -1 if an internal error occured.
16796 */
16797static int
16798xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16799 xmlSchemaParticlePtr r,
16800 xmlSchemaParticlePtr b,
16801 int isAnyTypeBase)
16802{
16803 /* TODO: Error codes (rcase-NSSubset). */
16804 /*
16805 * SPEC (1) "R's occurrence range is a valid restriction of B's
16806 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16807 */
16808 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16809 b->minOccurs, b->maxOccurs))
16810 return (1);
16811 /*
16812 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16813 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16814 */
16815 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16816 (xmlSchemaWildcardPtr) b->children))
16817 return (1);
16818 /*
16819 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16820 * definition·, R's {process contents} must be identical to or stronger
16821 * than B's {process contents}, where strict is stronger than lax is
16822 * stronger than skip."
16823 */
16824 if (! isAnyTypeBase) {
16825 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16826 ((xmlSchemaWildcardPtr) b->children)->processContents)
16827 return (1);
16828 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016829
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016830 return (0);
16831}
16832
16833/**
16834 * xmlSchemaCheckCOSParticleRestrict:
16835 * @ctxt: the schema parser context
16836 * @type: the complex type definition
16837 *
16838 * (3.9.6) Constraints on Particle Schema Components
16839 * Schema Component Constraint:
16840 * Particle Valid (Restriction) (cos-particle-restrict)
16841 *
16842 * STATUS: TODO
16843 *
16844 * Returns 0 if the constraints are satisfied, a positive
16845 * error code if not and -1 if an internal error occured.
16846 */
16847static int
16848xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16849 xmlSchemaParticlePtr r,
16850 xmlSchemaParticlePtr b)
16851{
16852 int ret = 0;
16853
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016854 /*part = WXS_TYPE_PARTICLE(type);
16855 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016856 */
16857
16858 TODO
16859
16860 /*
16861 * SPEC (1) "They are the same particle."
16862 */
16863 if (r == b)
16864 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016865
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016866
16867 return (0);
16868}
16869
16870/**
16871 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16872 * @ctxt: the schema parser context
16873 * @r: the model group particle
16874 * @b: the base wildcard particle
16875 *
16876 * (3.9.6) Constraints on Particle Schema Components
16877 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016878 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016879 * NSRecurseCheckCardinality)
16880 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016881 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016882 * STATUS: TODO: subst-groups
16883 *
16884 * Returns 0 if the constraints are satisfied, a positive
16885 * error code if not and -1 if an internal error occured.
16886 */
16887static int
16888xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16889 xmlSchemaParticlePtr r,
16890 xmlSchemaParticlePtr b)
16891{
16892 xmlSchemaParticlePtr part;
16893 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16894 if ((r->children == NULL) || (r->children->children == NULL))
16895 return (-1);
16896 /*
16897 * SPEC "For a group particle to be a ·valid restriction· of a
16898 * wildcard particle..."
16899 *
16900 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016901 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016902 * Particle Valid (Restriction) (§3.9.6)."
16903 */
16904 part = (xmlSchemaParticlePtr) r->children->children;
16905 do {
16906 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16907 return (1);
16908 part = (xmlSchemaParticlePtr) part->next;
16909 } while (part != NULL);
16910 /*
16911 * SPEC (2) "The effective total range of the group [...] is a
16912 * valid restriction of B's occurrence range as defined by
16913 * Occurrence Range OK (§3.9.6)."
16914 */
16915 if (xmlSchemaCheckParticleRangeOK(
16916 xmlSchemaGetParticleTotalRangeMin(r),
16917 xmlSchemaGetParticleTotalRangeMax(r),
16918 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016919 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016920 return (0);
16921}
16922
16923/**
16924 * xmlSchemaCheckRCaseRecurse:
16925 * @ctxt: the schema parser context
16926 * @r: the <all> or <sequence> model group particle
16927 * @b: the base <all> or <sequence> model group particle
16928 *
16929 * (3.9.6) Constraints on Particle Schema Components
16930 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016931 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016932 Recurse)
16933 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016934 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016935 * STATUS: ?
16936 * TODO: subst-groups
16937 *
16938 * Returns 0 if the constraints are satisfied, a positive
16939 * error code if not and -1 if an internal error occured.
16940 */
16941static int
16942xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16943 xmlSchemaParticlePtr r,
16944 xmlSchemaParticlePtr b)
16945{
16946 /* xmlSchemaParticlePtr part; */
16947 /* TODO: Error codes (rcase-Recurse). */
16948 if ((r->children == NULL) || (b->children == NULL) ||
16949 (r->children->type != b->children->type))
16950 return (-1);
16951 /*
16952 * SPEC "For an all or sequence group particle to be a ·valid
16953 * restriction· of another group particle with the same {compositor}..."
16954 *
16955 * SPEC (1) "R's occurrence range is a valid restriction of B's
16956 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16957 */
16958 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16959 b->minOccurs, b->maxOccurs))
16960 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016961
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016962
16963 return (0);
16964}
16965
16966#endif
16967
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016968#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
16969 xmlSchemaPCustomErrExt(pctxt, \
16970 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016971 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016972 "It is an error for both '%s' and '%s' to be specified on the "\
16973 "same type definition", \
16974 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
16975 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
16976
16977#define FACET_RESTR_ERR(fac1, msg) \
16978 xmlSchemaPCustomErr(pctxt, \
16979 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016980 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016981 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016982
16983#define FACET_RESTR_FIXED_ERR(fac) \
16984 xmlSchemaPCustomErr(pctxt, \
16985 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016986 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016987 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016988 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016989
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016990static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016991xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
16992 xmlSchemaFacetPtr facet1,
16993 xmlSchemaFacetPtr facet2,
16994 int lessGreater,
16995 int orEqual,
16996 int ofBase)
16997{
16998 xmlChar *msg = NULL;
16999
17000 msg = xmlStrdup(BAD_CAST "'");
17001 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17002 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17003 if (lessGreater == 0)
17004 msg = xmlStrcat(msg, BAD_CAST " equal to");
17005 if (lessGreater == 1)
17006 msg = xmlStrcat(msg, BAD_CAST " greater than");
17007 else
17008 msg = xmlStrcat(msg, BAD_CAST " less than");
17009
17010 if (orEqual)
17011 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17012 msg = xmlStrcat(msg, BAD_CAST " '");
17013 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17014 if (ofBase)
17015 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17016 else
17017 msg = xmlStrcat(msg, BAD_CAST "'");
17018
17019 xmlSchemaPCustomErr(pctxt,
17020 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017021 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017022 (const char *) msg, NULL);
17023
17024 if (msg != NULL)
17025 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017026}
17027
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017028/*
17029* xmlSchemaDeriveAndValidateFacets:
17030*
17031* Schema Component Constraint: Simple Type Restriction (Facets)
17032* (st-restrict-facets)
17033*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017034static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017035xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17036 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017037{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017038 xmlSchemaTypePtr base = type->baseType;
17039 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017040 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017041 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17042 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17043 fmininc = NULL, fmaxinc = NULL,
17044 fminexc = NULL, fmaxexc = NULL,
17045 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17046 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17047 bfmininc = NULL, bfmaxinc = NULL,
17048 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017049 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017050
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017051 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017052 * SPEC st-restrict-facets 1:
17053 * "The {variety} of R is the same as that of B."
17054 */
17055 /*
17056 * SPEC st-restrict-facets 2:
17057 * "If {variety} is atomic, the {primitive type definition}
17058 * of R is the same as that of B."
17059 *
17060 * NOTE: we leave 1 & 2 out for now, since this will be
17061 * satisfied by the derivation process.
17062 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17063 */
17064 /*
17065 * SPEC st-restrict-facets 3:
17066 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017067 * of B, eliminating duplicates. To eliminate duplicates,
17068 * when a facet of the same kind occurs in both S and the
17069 * {facets} of B, the one in the {facets} of B is not
17070 * included, with the exception of enumeration and pattern
17071 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017072 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017073 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017074
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017075 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17076 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017077
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017078 last = type->facetSet;
17079 if (last != NULL)
17080 while (last->next != NULL)
17081 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017082
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017083 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17084 facet = cur->facet;
17085 switch (facet->type) {
17086 case XML_SCHEMA_FACET_LENGTH:
17087 flength = facet; break;
17088 case XML_SCHEMA_FACET_MINLENGTH:
17089 fminlen = facet; break;
17090 case XML_SCHEMA_FACET_MININCLUSIVE:
17091 fmininc = facet; break;
17092 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17093 fminexc = facet; break;
17094 case XML_SCHEMA_FACET_MAXLENGTH:
17095 fmaxlen = facet; break;
17096 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17097 fmaxinc = facet; break;
17098 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17099 fmaxexc = facet; break;
17100 case XML_SCHEMA_FACET_TOTALDIGITS:
17101 ftotdig = facet; break;
17102 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17103 ffracdig = facet; break;
17104 default:
17105 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017106 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017107 }
17108 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17109 facet = cur->facet;
17110 switch (facet->type) {
17111 case XML_SCHEMA_FACET_LENGTH:
17112 bflength = facet; break;
17113 case XML_SCHEMA_FACET_MINLENGTH:
17114 bfminlen = facet; break;
17115 case XML_SCHEMA_FACET_MININCLUSIVE:
17116 bfmininc = facet; break;
17117 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17118 bfminexc = facet; break;
17119 case XML_SCHEMA_FACET_MAXLENGTH:
17120 bfmaxlen = facet; break;
17121 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17122 bfmaxinc = facet; break;
17123 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17124 bfmaxexc = facet; break;
17125 case XML_SCHEMA_FACET_TOTALDIGITS:
17126 bftotdig = facet; break;
17127 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17128 bffracdig = facet; break;
17129 default:
17130 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017131 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017132 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017133 /*
17134 * length and minLength or maxLength (2.2) + (3.2)
17135 */
17136 if (flength && (fminlen || fmaxlen)) {
17137 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17138 "either of 'minLength' or 'maxLength' to be specified on "
17139 "the same type definition")
17140 }
17141 /*
17142 * Mutual exclusions in the same derivation step.
17143 */
17144 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017145 /*
17146 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017147 */
17148 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17149 }
17150 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017151 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017152 * SCC "minInclusive and minExclusive"
17153 */
17154 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017155 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017156
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017157 if (flength && bflength) {
17158 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017159 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017160 * The values have to be equal.
17161 */
17162 res = xmlSchemaCompareValues(flength->val, bflength->val);
17163 if (res == -2)
17164 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017165 if (res != 0)
17166 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17167 if ((res != 0) && (bflength->fixed)) {
17168 FACET_RESTR_FIXED_ERR(flength)
17169 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017170
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017171 }
17172 if (fminlen && bfminlen) {
17173 /*
17174 * SCC "minLength valid restriction"
17175 * minLength >= BASE minLength
17176 */
17177 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17178 if (res == -2)
17179 goto internal_error;
17180 if (res == -1)
17181 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17182 if ((res != 0) && (bfminlen->fixed)) {
17183 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017184 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017185 }
17186 if (fmaxlen && bfmaxlen) {
17187 /*
17188 * SCC "maxLength valid restriction"
17189 * maxLength <= BASE minLength
17190 */
17191 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17192 if (res == -2)
17193 goto internal_error;
17194 if (res == 1)
17195 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17196 if ((res != 0) && (bfmaxlen->fixed)) {
17197 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017198 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017199 }
17200 /*
17201 * SCC "length and minLength or maxLength"
17202 */
17203 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017204 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017205 if (flength) {
17206 if (! fminlen)
17207 flength = bflength;
17208 if (fminlen) {
17209 /* (1.1) length >= minLength */
17210 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17211 if (res == -2)
17212 goto internal_error;
17213 if (res == -1)
17214 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17215 }
17216 if (! fmaxlen)
17217 fmaxlen = bfmaxlen;
17218 if (fmaxlen) {
17219 /* (2.1) length <= maxLength */
17220 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17221 if (res == -2)
17222 goto internal_error;
17223 if (res == 1)
17224 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17225 }
17226 }
17227 if (fmaxinc) {
17228 /*
17229 * "maxInclusive"
17230 */
17231 if (fmininc) {
17232 /* SCC "maxInclusive >= minInclusive" */
17233 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17234 if (res == -2)
17235 goto internal_error;
17236 if (res == -1) {
17237 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17238 }
17239 }
17240 /*
17241 * SCC "maxInclusive valid restriction"
17242 */
17243 if (bfmaxinc) {
17244 /* maxInclusive <= BASE maxInclusive */
17245 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17246 if (res == -2)
17247 goto internal_error;
17248 if (res == 1)
17249 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17250 if ((res != 0) && (bfmaxinc->fixed)) {
17251 FACET_RESTR_FIXED_ERR(fmaxinc)
17252 }
17253 }
17254 if (bfmaxexc) {
17255 /* maxInclusive < BASE maxExclusive */
17256 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17257 if (res == -2)
17258 goto internal_error;
17259 if (res != -1) {
17260 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17261 }
17262 }
17263 if (bfmininc) {
17264 /* maxInclusive >= BASE minInclusive */
17265 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17266 if (res == -2)
17267 goto internal_error;
17268 if (res == -1) {
17269 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17270 }
17271 }
17272 if (bfminexc) {
17273 /* maxInclusive > BASE minExclusive */
17274 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17275 if (res == -2)
17276 goto internal_error;
17277 if (res != 1) {
17278 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17279 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017280 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017281 }
17282 if (fmaxexc) {
17283 /*
17284 * "maxExclusive >= minExclusive"
17285 */
17286 if (fminexc) {
17287 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17288 if (res == -2)
17289 goto internal_error;
17290 if (res == -1) {
17291 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17292 }
17293 }
17294 /*
17295 * "maxExclusive valid restriction"
17296 */
17297 if (bfmaxexc) {
17298 /* maxExclusive <= BASE maxExclusive */
17299 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17300 if (res == -2)
17301 goto internal_error;
17302 if (res == 1) {
17303 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17304 }
17305 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017306 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017307 }
17308 }
17309 if (bfmaxinc) {
17310 /* maxExclusive <= BASE maxInclusive */
17311 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17312 if (res == -2)
17313 goto internal_error;
17314 if (res == 1) {
17315 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17316 }
17317 }
17318 if (bfmininc) {
17319 /* maxExclusive > BASE minInclusive */
17320 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17321 if (res == -2)
17322 goto internal_error;
17323 if (res != 1) {
17324 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17325 }
17326 }
17327 if (bfminexc) {
17328 /* maxExclusive > BASE minExclusive */
17329 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17330 if (res == -2)
17331 goto internal_error;
17332 if (res != 1) {
17333 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17334 }
17335 }
17336 }
17337 if (fminexc) {
17338 /*
17339 * "minExclusive < maxInclusive"
17340 */
17341 if (fmaxinc) {
17342 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17343 if (res == -2)
17344 goto internal_error;
17345 if (res != -1) {
17346 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17347 }
17348 }
17349 /*
17350 * "minExclusive valid restriction"
17351 */
17352 if (bfminexc) {
17353 /* minExclusive >= BASE minExclusive */
17354 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17355 if (res == -2)
17356 goto internal_error;
17357 if (res == -1) {
17358 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17359 }
17360 if ((res != 0) && (bfminexc->fixed)) {
17361 FACET_RESTR_FIXED_ERR(fminexc)
17362 }
17363 }
17364 if (bfmaxinc) {
17365 /* minExclusive <= BASE maxInclusive */
17366 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17367 if (res == -2)
17368 goto internal_error;
17369 if (res == 1) {
17370 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17371 }
17372 }
17373 if (bfmininc) {
17374 /* minExclusive >= BASE minInclusive */
17375 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17376 if (res == -2)
17377 goto internal_error;
17378 if (res == -1) {
17379 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17380 }
17381 }
17382 if (bfmaxexc) {
17383 /* minExclusive < BASE maxExclusive */
17384 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17385 if (res == -2)
17386 goto internal_error;
17387 if (res != -1) {
17388 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17389 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017390 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017391 }
17392 if (fmininc) {
17393 /*
17394 * "minInclusive < maxExclusive"
17395 */
17396 if (fmaxexc) {
17397 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17398 if (res == -2)
17399 goto internal_error;
17400 if (res != -1) {
17401 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17402 }
17403 }
17404 /*
17405 * "minExclusive valid restriction"
17406 */
17407 if (bfmininc) {
17408 /* minInclusive >= BASE minInclusive */
17409 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17410 if (res == -2)
17411 goto internal_error;
17412 if (res == -1) {
17413 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17414 }
17415 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017416 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017417 }
17418 }
17419 if (bfmaxinc) {
17420 /* minInclusive <= BASE maxInclusive */
17421 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17422 if (res == -2)
17423 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017424 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017425 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17426 }
17427 }
17428 if (bfminexc) {
17429 /* minInclusive > BASE minExclusive */
17430 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17431 if (res == -2)
17432 goto internal_error;
17433 if (res != 1)
17434 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17435 }
17436 if (bfmaxexc) {
17437 /* minInclusive < BASE maxExclusive */
17438 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17439 if (res == -2)
17440 goto internal_error;
17441 if (res != -1)
17442 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17443 }
17444 }
17445 if (ftotdig && bftotdig) {
17446 /*
17447 * SCC " totalDigits valid restriction"
17448 * totalDigits <= BASE totalDigits
17449 */
17450 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17451 if (res == -2)
17452 goto internal_error;
17453 if (res == 1)
17454 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17455 -1, 1, 1);
17456 if ((res != 0) && (bftotdig->fixed)) {
17457 FACET_RESTR_FIXED_ERR(ftotdig)
17458 }
17459 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017460 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017461 /*
17462 * SCC "fractionDigits valid restriction"
17463 * fractionDigits <= BASE fractionDigits
17464 */
17465 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17466 if (res == -2)
17467 goto internal_error;
17468 if (res == 1)
17469 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17470 -1, 1, 1);
17471 if ((res != 0) && (bffracdig->fixed)) {
17472 FACET_RESTR_FIXED_ERR(ffracdig)
17473 }
17474 }
17475 /*
17476 * SCC "fractionDigits less than or equal to totalDigits"
17477 */
17478 if (! ftotdig)
17479 ftotdig = bftotdig;
17480 if (! ffracdig)
17481 ffracdig = bffracdig;
17482 if (ftotdig && ffracdig) {
17483 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17484 if (res == -2)
17485 goto internal_error;
17486 if (res == 1)
17487 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17488 -1, 1, 0);
17489 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017490 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017491 * *Enumerations* won' be added here, since only the first set
17492 * of enumerations in the ancestor-or-self axis is used
17493 * for validation, plus we need to use the base type of those
17494 * enumerations for whitespace.
17495 *
17496 * *Patterns*: won't be add here, since they are ORed at
17497 * type level and ANDed at ancestor level. This will
17498 * happed during validation by walking the base axis
17499 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017500 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017501 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17502 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017503 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017504 * Special handling of enumerations and patterns.
17505 * TODO: hmm, they should not appear in the set, so remove this.
17506 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017507 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017508 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017509 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017510 /*
17511 * Search for a duplicate facet in the current type.
17512 */
17513 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017514 /* err = 0; */
17515 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017516 while (link != NULL) {
17517 facet = link->facet;
17518 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017519 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017520 case XML_SCHEMA_FACET_WHITESPACE:
17521 /*
17522 * The whitespace must be stronger.
17523 */
17524 if (facet->whitespace < bfacet->whitespace) {
17525 FACET_RESTR_ERR(flength,
17526 "The 'whitespace' value has to be equal to "
17527 "or stronger than the 'whitespace' value of "
17528 "the base type")
17529 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017530 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017531 (facet->whitespace != bfacet->whitespace)) {
17532 FACET_RESTR_FIXED_ERR(facet)
17533 }
17534 break;
17535 default:
17536 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017537 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017538 /* Duplicate found. */
17539 break;
17540 }
17541 link = link->next;
17542 }
17543 /*
17544 * If no duplicate was found: add the base types's facet
17545 * to the set.
17546 */
17547 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017548 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017549 xmlMalloc(sizeof(xmlSchemaFacetLink));
17550 if (link == NULL) {
17551 xmlSchemaPErrMemory(pctxt,
17552 "deriving facets, creating a facet link", NULL);
17553 return (-1);
17554 }
17555 link->facet = cur->facet;
17556 link->next = NULL;
17557 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017558 type->facetSet = link;
17559 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017560 last->next = link;
17561 last = link;
17562 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017563
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017564 }
17565
17566 return (0);
17567internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017568 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17569 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017570 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017571}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017572
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017573static int
17574xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17575 xmlSchemaTypePtr type)
17576{
17577 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17578 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017579 * The actual value is then formed by replacing any union type
17580 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017581 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017582 *
17583 * TODO: There's a bug entry at
17584 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17585 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017586 */
17587 link = type->memberTypes;
17588 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017589
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017590 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017591 xmlSchemaTypeFixup(link->type, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017592
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017593 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017594 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017595 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017596 link->type = subLink->type;
17597 if (subLink->next != NULL) {
17598 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017599 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017600 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017601 while (subLink != NULL) {
17602 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017603 xmlMalloc(sizeof(xmlSchemaTypeLink));
17604 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017605 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017606 NULL);
17607 return (-1);
17608 }
17609 newLink->type = subLink->type;
17610 prevLink->next = newLink;
17611 prevLink = newLink;
17612 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017613
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017614 subLink = subLink->next;
17615 }
17616 }
17617 }
17618 }
17619 link = link->next;
17620 }
17621 return (0);
17622}
17623
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017624static void
17625xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17626{
17627 int has = 0, needVal = 0, normVal = 0;
17628
17629 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17630 if (has) {
17631 needVal = (type->baseType->flags &
17632 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17633 normVal = (type->baseType->flags &
17634 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17635 }
17636 if (type->facets != NULL) {
17637 xmlSchemaFacetPtr fac;
17638
17639 for (fac = type->facets; fac != NULL; fac = fac->next) {
17640 switch (fac->type) {
17641 case XML_SCHEMA_FACET_WHITESPACE:
17642 break;
17643 case XML_SCHEMA_FACET_PATTERN:
17644 normVal = 1;
17645 has = 1;
17646 break;
17647 case XML_SCHEMA_FACET_ENUMERATION:
17648 needVal = 1;
17649 normVal = 1;
17650 has = 1;
17651 break;
17652 default:
17653 has = 1;
17654 break;
17655 }
17656 }
17657 }
17658 if (normVal)
17659 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17660 if (needVal)
17661 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17662 if (has)
17663 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17664
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017665 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017666 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17667 /*
17668 * OPTIMIZE VAL TODO: Some facets need a computed value.
17669 */
17670 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17671 (prim->builtInType != XML_SCHEMAS_STRING)) {
17672 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17673 }
17674 }
17675}
17676
17677static int
17678xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17679{
17680
17681
17682 /*
17683 * Evaluate the whitespace-facet value.
17684 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017685 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017686 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17687 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017688 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017689 return (0);
17690
17691 if (type->facetSet != NULL) {
17692 xmlSchemaFacetLinkPtr lin;
17693
17694 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17695 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17696 switch (lin->facet->whitespace) {
17697 case XML_SCHEMAS_FACET_PRESERVE:
17698 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17699 break;
17700 case XML_SCHEMAS_FACET_REPLACE:
17701 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17702 break;
17703 case XML_SCHEMAS_FACET_COLLAPSE:
17704 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17705 break;
17706 default:
17707 return (-1);
17708 }
17709 return (0);
17710 }
17711 }
17712 }
17713 /*
17714 * For all ·atomic· datatypes other than string (and types ·derived·
17715 * by ·restriction· from it) the value of whiteSpace is fixed to
17716 * collapse
17717 */
17718 {
17719 xmlSchemaTypePtr anc;
17720
17721 for (anc = type->baseType; anc != NULL &&
17722 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17723 anc = anc->baseType) {
17724
17725 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17726 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17727 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17728
17729 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17730 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17731 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17732
17733 } else
17734 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17735 break;
17736 }
17737 }
17738 return (0);
17739 }
17740 return (0);
17741}
17742
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017743static int
17744xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17745 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017746{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017747 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17748 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017749 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017750 return(0);
17751 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017752
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017753 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017754 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017755 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017756 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017757 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017758 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017759 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017760 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017761 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017762 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017763 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017764 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017765 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017766 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017767 * Corresponds to <simpleType><union>...
17768 */
17769 if (type->memberTypes == NULL) {
17770 /*
17771 * This one is really needed, so get out.
17772 */
17773 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17774 "union type has no member-types assigned");
17775 return(-1);
17776 }
17777 } else {
17778 /*
17779 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017780 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017781 if (type->baseType == NULL) {
17782 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17783 "type has no base-type assigned");
17784 return(-1);
17785 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017786 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017787 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17788 return(-1);
17789 /*
17790 * Variety
17791 * If the <restriction> alternative is chosen, then the
17792 * {variety} of the {base type definition}.
17793 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017794 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017795 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017796 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017797 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017798 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017799 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017800 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017801 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017802 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017803 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017804 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017805 * NOTE that we won't assign the memberTypes of the base,
17806 * since this will make trouble when freeing them; we will
17807 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017808 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017809 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017810 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017811 return(0);
17812}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017813
Daniel Veillard8651f532002-04-17 09:06:27 +000017814#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017815xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17816 xmlSchemaTypePtr type)
17817{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017818 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017819 xmlGenericError(xmlGenericErrorContext,
17820 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017821 type->node->doc->URL,
17822 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017823 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017824 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017825 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017826 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017827 switch (type->contentType) {
17828 case XML_SCHEMA_CONTENT_SIMPLE:
17829 xmlGenericError(xmlGenericErrorContext, "simple\n");
17830 break;
17831 case XML_SCHEMA_CONTENT_ELEMENTS:
17832 xmlGenericError(xmlGenericErrorContext, "elements\n");
17833 break;
17834 case XML_SCHEMA_CONTENT_UNKNOWN:
17835 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17836 break;
17837 case XML_SCHEMA_CONTENT_EMPTY:
17838 xmlGenericError(xmlGenericErrorContext, "empty\n");
17839 break;
17840 case XML_SCHEMA_CONTENT_MIXED:
17841 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017842 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017843 xmlGenericError(xmlGenericErrorContext,
17844 "mixed as emptiable particle\n");
17845 else
17846 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17847 break;
17848 /* Removed, since not used. */
17849 /*
17850 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17851 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17852 break;
17853 */
17854 case XML_SCHEMA_CONTENT_BASIC:
17855 xmlGenericError(xmlGenericErrorContext, "basic\n");
17856 break;
17857 default:
17858 xmlGenericError(xmlGenericErrorContext,
17859 "not registered !!!\n");
17860 break;
17861 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017862 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017863}
Daniel Veillard8651f532002-04-17 09:06:27 +000017864#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017865
17866/*
17867* 3.14.6 Constraints on Simple Type Definition Schema Components
17868*/
17869static int
17870xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17871 xmlSchemaTypePtr type)
17872{
17873 int res, olderrs = pctxt->nberrors;
17874
17875 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17876 return(-1);
17877
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017878 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017879 return(0);
17880
17881 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17882 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17883
17884 if (type->baseType == NULL) {
17885 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17886 "missing baseType");
17887 goto exit_failure;
17888 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017889 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017890 xmlSchemaTypeFixup(type->baseType, pctxt);
17891 /*
17892 * If a member type of a union is a union itself, we need to substitute
17893 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017894 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17895 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017896 */
17897 if ((type->memberTypes != NULL) &&
17898 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17899 return(-1);
17900 /*
17901 * SPEC src-simple-type 1
17902 * "The corresponding simple type definition, if any, must satisfy
17903 * the conditions set out in Constraints on Simple Type Definition
17904 * Schema Components (§3.14.6)."
17905 */
17906 /*
17907 * Schema Component Constraint: Simple Type Definition Properties Correct
17908 * (st-props-correct)
17909 */
17910 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17911 HFAILURE HERROR
17912 /*
17913 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17914 * (cos-st-restricts)
17915 */
17916 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17917 HFAILURE HERROR
17918 /*
17919 * TODO: Removed the error report, since it got annoying to get an
17920 * extra error report, if anything failed until now.
17921 * Enable this if needed.
17922 *
17923 * xmlSchemaPErr(ctxt, type->node,
17924 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17925 * "Simple type '%s' does not satisfy the constraints "
17926 * "on simple type definitions.\n",
17927 * type->name, NULL);
17928 */
17929 /*
17930 * Schema Component Constraint: Simple Type Restriction (Facets)
17931 * (st-restrict-facets)
17932 */
17933 res = xmlSchemaCheckFacetValues(type, pctxt);
17934 HFAILURE HERROR
17935 if ((type->facetSet != NULL) ||
17936 (type->baseType->facetSet != NULL)) {
17937 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17938 HFAILURE HERROR
17939 }
17940 /*
17941 * Whitespace value.
17942 */
17943 res = xmlSchemaTypeFixupWhitespace(type);
17944 HFAILURE HERROR
17945 xmlSchemaTypeFixupOptimFacets(type);
17946
17947exit_error:
17948#ifdef DEBUG_TYPE
17949 xmlSchemaDebugFixedType(pctxt, type);
17950#endif
17951 if (olderrs != pctxt->nberrors)
17952 return(pctxt->err);
17953 return(0);
17954
17955exit_failure:
17956#ifdef DEBUG_TYPE
17957 xmlSchemaDebugFixedType(pctxt, type);
17958#endif
17959 return(-1);
17960}
17961
17962static int
17963xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
17964 xmlSchemaTypePtr type)
17965{
17966 int res = 0, olderrs = pctxt->nberrors;
17967 xmlSchemaTypePtr baseType = type->baseType;
17968
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017969 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017970 return(0);
17971 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17972 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017973 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017974 "missing baseType");
17975 goto exit_failure;
17976 }
17977 /*
17978 * Fixup the base type.
17979 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017980 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017981 xmlSchemaTypeFixup(baseType, pctxt);
17982 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
17983 /*
17984 * Skip fixup if the base type is invalid.
17985 * TODO: Generate a warning!
17986 */
17987 return(0);
17988 }
17989 /*
17990 * This basically checks if the base type can be derived.
17991 */
17992 res = xmlSchemaCheckSRCCT(pctxt, type);
17993 HFAILURE HERROR
17994 /*
17995 * Fixup the content type.
17996 */
17997 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
17998 /*
17999 * Corresponds to <complexType><simpleContent>...
18000 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018001 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018002 (baseType->contentTypeDef != NULL) &&
18003 (WXS_IS_RESTRICTION(type))) {
18004 xmlSchemaTypePtr contentBase, content;
18005#ifdef ENABLE_NAMED_LOCALS
18006 char buf[30];
18007 const xmlChar *tmpname;
18008#endif
18009 /*
18010 * SPEC (1) If <restriction> + base type is <complexType>,
18011 * "whose own {content type} is a simple type..."
18012 */
18013 if (type->contentTypeDef != NULL) {
18014 /*
18015 * SPEC (1.1) "the simple type definition corresponding to the
18016 * <simpleType> among the [children] of <restriction> if there
18017 * is one;"
18018 * Note that this "<simpleType> among the [children]" was put
18019 * into ->contentTypeDef during parsing.
18020 */
18021 contentBase = type->contentTypeDef;
18022 type->contentTypeDef = NULL;
18023 } else {
18024 /*
18025 * (1.2) "...otherwise (<restriction> has no <simpleType>
18026 * among its [children]), the simple type definition which
18027 * is the {content type} of the ... base type."
18028 */
18029 contentBase = baseType->contentTypeDef;
18030 }
18031 /*
18032 * SPEC
18033 * "... a simple type definition which restricts the simple
18034 * type definition identified in clause 1.1 or clause 1.2
18035 * with a set of facet components"
18036 *
18037 * Create the anonymous simple type, which will be the content
18038 * type of the complex type.
18039 */
18040#ifdef ENABLE_NAMED_LOCALS
18041 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18042 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018043 content = xmlSchemaAddType(pctxt, pctxt->schema,
18044 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018045 type->node, 0);
18046#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018047 content = xmlSchemaAddType(pctxt, pctxt->schema,
18048 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018049 type->node, 0);
18050#endif
18051 if (content == NULL)
18052 goto exit_failure;
18053 /*
18054 * We will use the same node as for the <complexType>
18055 * to have it somehow anchored in the schema doc.
18056 */
18057 content->type = XML_SCHEMA_TYPE_SIMPLE;
18058 content->baseType = contentBase;
18059 /*
18060 * Move the facets, previously anchored on the
18061 * complexType during parsing.
18062 */
18063 content->facets = type->facets;
18064 type->facets = NULL;
18065 content->facetSet = type->facetSet;
18066 type->facetSet = NULL;
18067
18068 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018069 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018070 xmlSchemaTypeFixup(contentBase, pctxt);
18071 /*
18072 * Fixup the newly created type. We don't need to check
18073 * for circularity here.
18074 */
18075 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18076 HFAILURE HERROR
18077 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18078 HFAILURE HERROR
18079
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018080 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018081 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18082 (WXS_IS_RESTRICTION(type))) {
18083 /*
18084 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18085 * an emptiable particle, then a simple type definition which
18086 * restricts the <restriction>'s <simpleType> child.
18087 */
18088 if ((type->contentTypeDef == NULL) ||
18089 (type->contentTypeDef->baseType == NULL)) {
18090 /*
18091 * TODO: Check if this ever happens.
18092 */
18093 xmlSchemaPCustomErr(pctxt,
18094 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018095 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018096 "Internal error: xmlSchemaTypeFixup, "
18097 "complex type '%s': the <simpleContent><restriction> "
18098 "is missing a <simpleType> child, but was not catched "
18099 "by xmlSchemaCheckSRCCT()", type->name);
18100 goto exit_failure;
18101 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018102 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018103 /*
18104 * SPEC (3) If <extension> + base is <complexType> with
18105 * <simpleType> content, "...then the {content type} of that
18106 * complex type definition"
18107 */
18108 if (baseType->contentTypeDef == NULL) {
18109 /*
18110 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18111 * should have catched this already.
18112 */
18113 xmlSchemaPCustomErr(pctxt,
18114 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018115 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018116 "Internal error: xmlSchemaTypeFixup, "
18117 "complex type '%s': the <extension>ed base type is "
18118 "a complex type with no simple content type",
18119 type->name);
18120 goto exit_failure;
18121 }
18122 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018123 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018124 /*
18125 * SPEC (4) <extension> + base is <simpleType>
18126 * "... then that simple type definition"
18127 */
18128 type->contentTypeDef = baseType;
18129 } else {
18130 /*
18131 * TODO: Check if this ever happens.
18132 */
18133 xmlSchemaPCustomErr(pctxt,
18134 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018135 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018136 "Internal error: xmlSchemaTypeFixup, "
18137 "complex type '%s' with <simpleContent>: unhandled "
18138 "derivation case", type->name);
18139 goto exit_failure;
18140 }
18141 } else {
18142 int dummySequence = 0;
18143 xmlSchemaParticlePtr particle =
18144 (xmlSchemaParticlePtr) type->subtypes;
18145 /*
18146 * Corresponds to <complexType><complexContent>...
18147 *
18148 * NOTE that the effective mixed was already set during parsing of
18149 * <complexType> and <complexContent>; its flag value is
18150 * XML_SCHEMAS_TYPE_MIXED.
18151 *
18152 * Compute the "effective content":
18153 * (2.1.1) + (2.1.2) + (2.1.3)
18154 */
18155 if ((particle == NULL) ||
18156 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18157 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18158 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18159 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18160 (particle->minOccurs == 0))) &&
18161 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18162 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18163 /*
18164 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18165 * a particle whose properties are as follows:..."
18166 *
18167 * Empty sequence model group with
18168 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18169 * NOTE that we sill assign it the <complexType> node to
18170 * somehow anchor it in the doc.
18171 */
18172 if ((particle == NULL) ||
18173 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18174 /*
18175 * Create the particle.
18176 */
18177 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18178 type->node, 1, 1);
18179 if (particle == NULL)
18180 goto exit_failure;
18181 /*
18182 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018183 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018184 particle->children = (xmlSchemaTreeItemPtr)
18185 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18186 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18187 if (particle->children == NULL)
18188 goto exit_failure;
18189
18190 type->subtypes = (xmlSchemaTypePtr) particle;
18191 }
18192 dummySequence = 1;
18193 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18194 } else {
18195 /*
18196 * SPEC (2.1.5) "otherwise empty"
18197 */
18198 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18199 }
18200 } else {
18201 /*
18202 * SPEC (2.2) "otherwise the particle corresponding to the
18203 * <all>, <choice>, <group> or <sequence> among the
18204 * [children]."
18205 */
18206 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18207 }
18208 /*
18209 * Compute the "content type".
18210 */
18211 if (WXS_IS_RESTRICTION(type)) {
18212 /*
18213 * SPEC (3.1) "If <restriction>..."
18214 * (3.1.1) + (3.1.2) */
18215 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18216 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18217 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18218 }
18219 } else {
18220 /*
18221 * SPEC (3.2) "If <extension>..."
18222 */
18223 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18224 /*
18225 * SPEC (3.2.1)
18226 */
18227 type->contentType = baseType->contentType;
18228 type->subtypes = baseType->subtypes;
18229 /*
18230 * NOTE that the effective mixed is ignored here.
18231 */
18232 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18233 /*
18234 * SPEC (3.2.2)
18235 */
18236 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18237 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18238 } else {
18239 /*
18240 * SPEC (3.2.3)
18241 */
18242 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18243 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18244 /*
18245 * "A model group whose {compositor} is sequence and whose
18246 * {particles} are..."
18247 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018248 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18249 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18250 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18251 XML_SCHEMA_TYPE_ALL))
18252 {
18253 /*
18254 * SPEC cos-all-limited (1)
18255 */
18256 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18257 /* TODO: error code */
18258 XML_SCHEMAP_COS_ALL_LIMITED,
18259 WXS_ITEM_NODE(type), NULL,
18260 "The type has an 'all' model group in its "
18261 "{content type} and thus cannot be derived from "
18262 "a non-empty type, since this would produce a "
18263 "'sequence' model group containing the 'all' "
18264 "model group; 'all' model groups are not "
18265 "allowed to appear inside other model groups",
18266 NULL, NULL);
18267
18268 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18269 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18270 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18271 XML_SCHEMA_TYPE_ALL))
18272 {
18273 /*
18274 * SPEC cos-all-limited (1)
18275 */
18276 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18277 /* TODO: error code */
18278 XML_SCHEMAP_COS_ALL_LIMITED,
18279 WXS_ITEM_NODE(type), NULL,
18280 "A type cannot be derived by extension from a type "
18281 "which has an 'all' model group in its "
18282 "{content type}, since this would produce a "
18283 "'sequence' model group containing the 'all' "
18284 "model group; 'all' model groups are not "
18285 "allowed to appear inside other model groups",
18286 NULL, NULL);
18287
18288 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018289 xmlSchemaTreeItemPtr effectiveContent =
18290 (xmlSchemaTreeItemPtr) type->subtypes;
18291 /*
18292 * Create the particle.
18293 */
18294 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18295 type->node, 1, 1);
18296 if (particle == NULL)
18297 goto exit_failure;
18298 /*
18299 * Create the "sequence" model group.
18300 */
18301 particle->children = (xmlSchemaTreeItemPtr)
18302 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18303 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18304 if (particle->children == NULL)
18305 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018306 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018307 /*
18308 * SPEC "the particle of the {content type} of
18309 * the ... base ..."
18310 * Create a duplicate of the base type's particle
18311 * and assign its "term" to it.
18312 */
18313 particle->children->children =
18314 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18315 pctxt->schema, type->node,
18316 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18317 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18318 if (particle->children->children == NULL)
18319 goto exit_failure;
18320 particle = (xmlSchemaParticlePtr)
18321 particle->children->children;
18322 particle->children =
18323 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18324 /*
18325 * SPEC "followed by the ·effective content·."
18326 */
18327 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018328 /*
18329 * This all will result in:
18330 * new-particle
18331 * --> new-sequence(
18332 * new-particle
18333 * --> base-model,
18334 * this-particle
18335 * --> this-model
18336 * )
18337 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018338 } else {
18339 /*
18340 * This is the case when there is already an empty
18341 * <sequence> with minOccurs==maxOccurs==1.
18342 * Just add the base types's content type.
18343 * NOTE that, although we miss to add an intermediate
18344 * <sequence>, this should produce no difference to
18345 * neither the regex compilation of the content model,
18346 * nor to the complex type contraints.
18347 */
18348 particle->children->children =
18349 (xmlSchemaTreeItemPtr) baseType->subtypes;
18350 }
18351 }
18352 }
18353 }
18354 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018355 * Now fixup attribute uses:
18356 * - expand attr. group references
18357 * - intersect attribute wildcards
18358 * - inherit attribute uses of the base type
18359 * - inherit or union attr. wildcards if extending
18360 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018361 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018362 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018363 HFAILURE HERROR
18364 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018365 * Apply the complex type component constraints; this will not
18366 * check attributes, since this is done in
18367 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018368 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018369 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018370 HFAILURE HERROR
18371
18372#ifdef DEBUG_TYPE
18373 xmlSchemaDebugFixedType(pctxt, type);
18374#endif
18375 if (olderrs != pctxt->nberrors)
18376 return(pctxt->err);
18377 else
18378 return(0);
18379
18380exit_error:
18381 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18382#ifdef DEBUG_TYPE
18383 xmlSchemaDebugFixedType(pctxt, type);
18384#endif
18385 return(pctxt->err);
18386
18387exit_failure:
18388 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18389#ifdef DEBUG_TYPE
18390 xmlSchemaDebugFixedType(pctxt, type);
18391#endif
18392 return(-1);
18393}
18394
18395
18396/**
18397 * xmlSchemaTypeFixup:
18398 * @typeDecl: the schema type definition
18399 * @ctxt: the schema parser context
18400 *
18401 * Fixes the content model of the type.
18402 * URGENT TODO: We need an int result!
18403 */
18404static int
18405xmlSchemaTypeFixup(xmlSchemaTypePtr type,
18406 xmlSchemaParserCtxtPtr pctxt)
18407{
18408 if (type == NULL)
18409 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018410 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018411 return(0);
18412 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
18413 return(xmlSchemaFixupComplexType(pctxt, type));
18414 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
18415 return(xmlSchemaFixupSimpleTypeStageTwo(pctxt, type));
18416 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018417}
18418
18419/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018420 * xmlSchemaCheckFacet:
18421 * @facet: the facet
18422 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018423 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018424 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018425 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018426 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018427 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018428 * Returns 0 if valid, a positive error code if not valid and
18429 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018430 */
18431int
18432xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018433 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018434 xmlSchemaParserCtxtPtr pctxt,
18435 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018436{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018437 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018438
Daniel Veillardce682bc2004-11-05 17:22:25 +000018439 if ((facet == NULL) || (typeDecl == NULL))
18440 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018441 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018442 * TODO: will the parser context be given if used from
18443 * the relaxNG module?
18444 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018445 if (pctxt == NULL)
18446 ctxtGiven = 0;
18447 else
18448 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018449
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018450 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018451 case XML_SCHEMA_FACET_MININCLUSIVE:
18452 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18453 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018454 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18455 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018456 /*
18457 * Okay we need to validate the value
18458 * at that point.
18459 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018460 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018461
18462 /* 4.3.5.5 Constraints on enumeration Schema Components
18463 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018464 * It is an ·error· if any member of {value} is not in the
18465 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018466 *
18467 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018468 * The value ·must· be in the
18469 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018470 */
18471 /*
18472 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018473 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018474 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018475 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018476 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018477 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018478 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018479 */
18480 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18481 base = typeDecl->baseType;
18482 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018483 PERROR_INT("xmlSchemaCheckFacet",
18484 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018485 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018486 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018487 } else
18488 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018489
18490 if (! ctxtGiven) {
18491 /*
18492 * A context is needed if called from RelaxNG.
18493 */
18494 pctxt = xmlSchemaNewParserCtxt("*");
18495 if (pctxt == NULL)
18496 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018497 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018498 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018499 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018500 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018501 * facet->node is just the node holding the facet
18502 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018503 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018504 */
18505 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018506 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018507 facet->value, &(facet->val), 1, 1, 0);
18508 if (ret != 0) {
18509 if (ret < 0) {
18510 /* No error message for RelaxNG. */
18511 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018512 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018513 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18514 "Internal error: xmlSchemaCheckFacet, "
18515 "failed to validate the value '%s' of the "
18516 "facet '%s' against the base type",
18517 facet->value, xmlSchemaFacetTypeToString(facet->type));
18518 }
18519 goto internal_error;
18520 }
18521 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18522 /* No error message for RelaxNG. */
18523 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018524 xmlChar *str = NULL;
18525
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018526 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018527 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018528 "The value '%s' of the facet does not validate "
18529 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018530 facet->value,
18531 xmlSchemaFormatQName(&str,
18532 base->targetNamespace, base->name));
18533 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018534 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535 goto exit;
18536 } else if (facet->val == NULL) {
18537 if (ctxtGiven) {
18538 PERROR_INT("xmlSchemaCheckFacet",
18539 "value was not computed");
18540 }
18541 TODO
18542 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018543 break;
18544 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018545 case XML_SCHEMA_FACET_PATTERN:
18546 facet->regexp = xmlRegexpCompile(facet->value);
18547 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018548 ret = XML_SCHEMAP_REGEXP_INVALID;
18549 /* No error message for RelaxNG. */
18550 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018551 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018552 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018553 "The value '%s' of the facet 'pattern' is not a "
18554 "valid regular expression",
18555 facet->value, NULL);
18556 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018557 }
18558 break;
18559 case XML_SCHEMA_FACET_TOTALDIGITS:
18560 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18561 case XML_SCHEMA_FACET_LENGTH:
18562 case XML_SCHEMA_FACET_MAXLENGTH:
18563 case XML_SCHEMA_FACET_MINLENGTH:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018564 ret = xmlSchemaValidatePredefinedType(
18565 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18566 facet->value, &(facet->val));
18567 if (ret != 0) {
18568 if (ret < 0) {
18569 /* No error message for RelaxNG. */
18570 if (ctxtGiven) {
18571 PERROR_INT("xmlSchemaCheckFacet",
18572 "validating facet value");
18573 }
18574 goto internal_error;
18575 }
18576 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18577 /* No error message for RelaxNG. */
18578 if (ctxtGiven) {
18579 /* error code */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018580 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018581 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018582 "The value '%s' of the facet '%s' is not a valid "
18583 "'nonNegativeInteger'",
18584 facet->value,
18585 xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018586 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018587 }
18588 break;
18589 }
18590 case XML_SCHEMA_FACET_WHITESPACE:{
18591 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18592 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18593 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18594 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18595 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18596 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18597 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018598 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18599 /* No error message for RelaxNG. */
18600 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018601 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018602 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018603 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018604 "The value '%s' of the facet 'whitespace' is not "
18605 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018606 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018607 }
18608 }
18609 default:
18610 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018611 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018612exit:
18613 if ((! ctxtGiven) && (pctxt != NULL))
18614 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018615 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018616internal_error:
18617 if ((! ctxtGiven) && (pctxt != NULL))
18618 xmlSchemaFreeParserCtxt(pctxt);
18619 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018620}
18621
18622/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018623 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018624 * @typeDecl: the schema type definition
18625 * @ctxt: the schema parser context
18626 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018627 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018628 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018629static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018630xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018631 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018632{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018633 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018634 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018635 /*
18636 * NOTE: It is intended to use the facets list, instead
18637 * of facetSet.
18638 */
18639 if (typeDecl->facets != NULL) {
18640 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018641
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018642 /*
18643 * Temporarily assign the "schema" to the validation context
18644 * of the parser context. This is needed for NOTATION validation.
18645 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018646 if (pctxt->vctxt == NULL) {
18647 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18648 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018649 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018650 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018651 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018652 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18653 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018654 facet = facet->next;
18655 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018656 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018657 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018658 if (olderrs != pctxt->nberrors)
18659 return(pctxt->err);
18660 return(0);
18661exit_failure:
18662 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018663}
18664
18665/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018666 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018667 * @ctxtMGroup: the searched model group
18668 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018669 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018670 *
18671 * This one is intended to be used by
18672 * xmlSchemaCheckGroupDefCircular only.
18673 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018674 * Returns the particle with the circular model group definition reference,
18675 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018676 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018677static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018678xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018679 xmlSchemaTreeItemPtr particle)
18680{
18681 xmlSchemaTreeItemPtr circ = NULL;
18682 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018683 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018684
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018685 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018686 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018687 if (term == NULL)
18688 continue;
18689 switch (term->type) {
18690 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018691 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018692 if (gdef == groupDef)
18693 return (particle);
18694 /*
18695 * Mark this model group definition to avoid infinite
18696 * recursion on circular references not yet examined.
18697 */
18698 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18699 continue;
18700 if (gdef->children != NULL) {
18701 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18702 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18703 gdef->children->children);
18704 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18705 if (circ != NULL)
18706 return (circ);
18707 }
18708 break;
18709 case XML_SCHEMA_TYPE_SEQUENCE:
18710 case XML_SCHEMA_TYPE_CHOICE:
18711 case XML_SCHEMA_TYPE_ALL:
18712 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18713 if (circ != NULL)
18714 return (circ);
18715 break;
18716 default:
18717 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018718 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018719 }
18720 return (NULL);
18721}
18722
18723/**
18724 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018725 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018726 * @ctxt: the parser context
18727 * @name: the name
18728 *
18729 * Checks for circular references to model group definitions.
18730 */
18731static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018732xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018733 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018734{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018735 /*
18736 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018737 * 2 Circular groups are disallowed. That is, within the {particles}
18738 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018739 * is the group itself.
18740 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018741 if ((item == NULL) ||
18742 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18743 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018744 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018745 {
18746 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018747
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018748 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018749 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018750 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018751 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018752 * TODO: The error report is not adequate: this constraint
18753 * is defined for model groups but not definitions, but since
18754 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018755 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018756 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018757 */
18758 xmlSchemaPCustomErr(ctxt,
18759 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018760 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018761 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018762 "defined", xmlSchemaFormatQName(&str,
18763 item->targetNamespace, item->name));
18764 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018765 /*
18766 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018767 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018768 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018769 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018770 }
18771 }
18772}
18773
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018774/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018775 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018776 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018777 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018778 *
18779 * Assigns the model group of model group definitions to the "term"
18780 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018781 * In xmlSchemaResolveModelGroupParticleReferences the model group
18782 * definitions were assigned to the "term", since needed for the
18783 * circularity check.
18784 *
18785 * Schema Component Constraint:
18786 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018787 */
18788static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018789xmlSchemaModelGroupToModelGroupDefFixup(
18790 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18791 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018792{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018793 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18794
18795 while (particle != NULL) {
18796 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18797 ((WXS_PARTICLE_TERM(particle))->type !=
18798 XML_SCHEMA_TYPE_GROUP))
18799 {
18800 particle = WXS_PTC_CAST particle->next;
18801 continue;
18802 }
18803 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18804 /*
18805 * TODO: Remove the particle.
18806 */
18807 WXS_PARTICLE_TERM(particle) = NULL;
18808 particle = WXS_PTC_CAST particle->next;
18809 continue;
18810 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018811 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018812 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018813 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018814 WXS_PARTICLE_TERM(particle) =
18815 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18816
18817 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018818 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018819}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018820
18821/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018822 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018823 * @ctxtGr: the searched attribute group
18824 * @attr: the current attribute list to be processed
18825 *
18826 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018827 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018828 *
18829 * Returns the circular attribute grou reference, otherwise NULL.
18830 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018831static xmlSchemaQNameRefPtr
18832xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18833 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018834{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018835 xmlSchemaAttributeGroupPtr gr;
18836 xmlSchemaQNameRefPtr ref, circ;
18837 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018838 /*
18839 * We will search for an attribute group reference which
18840 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018841 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018842 for (i = 0; i < list->nbItems; i++) {
18843 ref = list->items[i];
18844 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18845 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18846 (ref->item != NULL))
18847 {
18848 gr = WXS_ATTR_GROUP_CAST ref->item;
18849 if (gr == ctxtGr)
18850 return(ref);
18851 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18852 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018853 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018854 * Mark as visited to avoid infinite recursion on
18855 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018856 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018857 if ((gr->attrUses) &&
18858 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18859 {
18860 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18861 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18862 (xmlSchemaItemListPtr) gr->attrUses);
18863 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18864 if (circ != NULL)
18865 return (circ);
18866 }
18867
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018868 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018869 }
18870 return (NULL);
18871}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018872
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018873/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018874 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018875 * attrGr: the attribute group definition
18876 * @ctxt: the parser context
18877 * @name: the name
18878 *
18879 * Checks for circular references of attribute groups.
18880 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018881static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018882xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018883 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018884{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018885 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018886 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018887 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018888 * 3 Circular group reference is disallowed outside <redefine>.
18889 * That is, unless this element information item's parent is
18890 * <redefine>, then among the [children], if any, there must
18891 * not be an <attributeGroup> with ref [attribute] which resolves
18892 * to the component corresponding to this <attributeGroup>. Indirect
18893 * circularity is also ruled out. That is, when QName resolution
18894 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18895 * any <attributeGroup>s with a ref [attribute] among the [children],
18896 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018897 * which resolves to the component corresponding to this <attributeGroup>.
18898 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018899 if (attrGr->attrUses == NULL)
18900 return(0);
18901 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18902 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018903 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018904 xmlSchemaQNameRefPtr circ;
18905
18906 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18907 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018908 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018909 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018910 /*
18911 * TODO: Report the referenced attr group as QName.
18912 */
18913 xmlSchemaPCustomErr(ctxt,
18914 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018915 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018916 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018917 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18918 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018919 /*
18920 * NOTE: We will cut the reference to avoid further
18921 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018922 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018923 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018924 circ->item = NULL;
18925 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018926 }
18927 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018928 return(0);
18929}
18930
18931static int
18932xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18933 xmlSchemaAttributeGroupPtr attrGr);
18934
18935/**
18936 * xmlSchemaExpandAttributeGroupRefs:
18937 * @pctxt: the parser context
18938 * @node: the node of the component holding the attribute uses
18939 * @completeWild: the intersected wildcard to be returned
18940 * @list: the attribute uses
18941 *
18942 * Substitutes contained attribute group references
18943 * for their attribute uses. Wilcards are intersected.
18944 * Attribute use prohibitions are removed from the list
18945 * and returned via the @prohibs list.
18946 * Pointlessness of attr. prohibs, if a matching attr. decl
18947 * is existent a well, are checked.
18948 */
18949static int
18950xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
18951 xmlSchemaBasicItemPtr item,
18952 xmlSchemaWildcardPtr *completeWild,
18953 xmlSchemaItemListPtr list,
18954 xmlSchemaItemListPtr prohibs)
18955{
18956 xmlSchemaAttributeGroupPtr gr;
18957 xmlSchemaAttributeUsePtr use;
18958 xmlSchemaItemListPtr sublist;
18959 int i, j;
18960 int created = (*completeWild == NULL) ? 0 : 1;
18961
18962 if (prohibs)
18963 prohibs->nbItems = 0;
18964
18965 for (i = 0; i < list->nbItems; i++) {
18966 use = list->items[i];
18967
18968 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
18969 if (prohibs == NULL) {
18970 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
18971 "unexpected attr prohibition found");
18972 return(-1);
18973 }
18974 /*
18975 * Remove from attribute uses.
18976 */
18977 if (xmlSchemaItemListRemove(list, i) == -1)
18978 return(-1);
18979 i--;
18980 /*
18981 * Note that duplicate prohibitions were already
18982 * handled at parsing time.
18983 */
18984 /*
18985 * Add to list of prohibitions.
18986 */
18987 xmlSchemaItemListAddSize(prohibs, 2, use);
18988 continue;
18989 }
18990 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18991 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
18992 {
18993 if ((WXS_QNAME_CAST use)->item == NULL)
18994 return(-1);
18995 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
18996 /*
18997 * Expand the referenced attr. group.
18998 * TODO: remove this, this is done in a previous step, so
18999 * already done here.
19000 */
19001 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19002 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19003 return(-1);
19004 }
19005 /*
19006 * Build the 'complete' wildcard; i.e. intersect multiple
19007 * wildcards.
19008 */
19009 if (gr->attributeWildcard != NULL) {
19010 if (*completeWild == NULL) {
19011 *completeWild = gr->attributeWildcard;
19012 } else {
19013 if (! created) {
19014 xmlSchemaWildcardPtr tmpWild;
19015
19016 /*
19017 * Copy the first encountered wildcard as context,
19018 * except for the annotation.
19019 *
19020 * Although the complete wildcard might not correspond
19021 * to any node in the schema, we will anchor it on
19022 * the node of the owner component.
19023 */
19024 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19025 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19026 WXS_ITEM_NODE(item));
19027 if (tmpWild == NULL)
19028 return(-1);
19029 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19030 tmpWild, *completeWild) == -1)
19031 return (-1);
19032 tmpWild->processContents = (*completeWild)->processContents;
19033 *completeWild = tmpWild;
19034 created = 1;
19035 }
19036
19037 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19038 gr->attributeWildcard) == -1)
19039 return(-1);
19040 }
19041 }
19042 /*
19043 * Just remove the reference if the referenced group does not
19044 * contain any attribute uses.
19045 */
19046 if (gr->attrUses == NULL) {
19047 if (xmlSchemaItemListRemove(list, i) == -1)
19048 return(-1);
19049 i--;
19050 continue;
19051 }
19052 /*
19053 * Add the attribute uses.
19054 */
19055 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19056 if (sublist->nbItems != 0) {
19057 list->items[i] = sublist->items[0];
19058 if (sublist->nbItems != 1) {
19059 for (j = 1; j < sublist->nbItems; j++) {
19060 i++;
19061 if (xmlSchemaItemListInsert(list,
19062 sublist->items[j], i) == -1)
19063 return(-1);
19064 }
19065 }
19066 }
19067 }
19068
19069 }
19070 /*
19071 * Handle pointless prohibitions of declared attributes.
19072 */
19073 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19074 xmlSchemaAttributeUseProhibPtr prohib;
19075
19076 for (i = prohibs->nbItems -1; i >= 0; i--) {
19077 prohib = prohibs->items[i];
19078 for (j = 0; j < list->nbItems; j++) {
19079 use = list->items[j];
19080
19081 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19082 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19083 {
19084 xmlChar *str = NULL;
19085
19086 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19087 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19088 prohib->node, NULL,
19089 "Skipping pointless attribute use prohibition "
19090 "'%s', since a corresponding attribute use "
19091 "exists already in the type definition",
19092 xmlSchemaFormatQName(&str,
19093 prohib->targetNamespace, prohib->name),
19094 NULL, NULL);
19095 FREE_AND_NULL(str);
19096 /*
19097 * Remove the prohibition.
19098 */
19099 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19100 return(-1);
19101 break;
19102 }
19103 }
19104 }
19105 }
19106 return(0);
19107}
19108
19109/**
19110 * xmlSchemaAttributeGroupExpandRefs:
19111 * @pctxt: the parser context
19112 * @attrGr: the attribute group definition
19113 *
19114 * Computation of:
19115 * {attribute uses} property
19116 * {attribute wildcard} property
19117 *
19118 * Substitutes contained attribute group references
19119 * for their attribute uses. Wilcards are intersected.
19120 */
19121static int
19122xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19123 xmlSchemaAttributeGroupPtr attrGr)
19124{
19125 if ((attrGr->attrUses == NULL) ||
19126 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19127 return(0);
19128
19129 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19130 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19131 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19132 return(-1);
19133 return(0);
19134}
19135
19136/**
19137 * xmlSchemaAttributeGroupExpandRefs:
19138 * @pctxt: the parser context
19139 * @attrGr: the attribute group definition
19140 *
19141 * Substitutes contained attribute group references
19142 * for their attribute uses. Wilcards are intersected.
19143 *
19144 * Schema Component Constraint:
19145 * Attribute Group Definition Properties Correct (ag-props-correct)
19146 */
19147static int
19148xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19149 xmlSchemaAttributeGroupPtr attrGr)
19150{
19151 /*
19152 * SPEC ag-props-correct
19153 * (1) "The values of the properties of an attribute group definition
19154 * must be as described in the property tableau in The Attribute
19155 * Group Definition Schema Component (§3.6.1), modulo the impact of
19156 * Missing Sub-components (§5.3);"
19157 */
19158
19159 if ((attrGr->attrUses != NULL) &&
19160 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19161 {
19162 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19163 xmlSchemaAttributeUsePtr use, tmp;
19164 int i, j, hasId = 0;
19165
19166 for (i = uses->nbItems -1; i >= 0; i--) {
19167 use = uses->items[i];
19168 /*
19169 * SPEC ag-props-correct
19170 * (2) "Two distinct members of the {attribute uses} must not have
19171 * {attribute declaration}s both of whose {name}s match and whose
19172 * {target namespace}s are identical."
19173 */
19174 if (i > 0) {
19175 for (j = i -1; j >= 0; j--) {
19176 tmp = uses->items[j];
19177 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19178 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19179 (WXS_ATTRUSE_DECL_TNS(use) ==
19180 WXS_ATTRUSE_DECL_TNS(tmp)))
19181 {
19182 xmlChar *str = NULL;
19183
19184 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19185 XML_SCHEMAP_AG_PROPS_CORRECT,
19186 attrGr->node, WXS_BASIC_CAST attrGr,
19187 "Duplicate %s",
19188 xmlSchemaGetComponentDesignation(&str, use),
19189 NULL);
19190 FREE_AND_NULL(str);
19191 /*
19192 * Remove the duplicate.
19193 */
19194 if (xmlSchemaItemListRemove(uses, i) == -1)
19195 return(-1);
19196 goto next_use;
19197 }
19198 }
19199 }
19200 /*
19201 * SPEC ag-props-correct
19202 * (3) "Two distinct members of the {attribute uses} must not have
19203 * {attribute declaration}s both of whose {type definition}s are or
19204 * are derived from ID."
19205 * TODO: Does 'derived' include member-types of unions?
19206 */
19207 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19208 if (xmlSchemaIsDerivedFromBuiltInType(
19209 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19210 {
19211 if (hasId) {
19212 xmlChar *str = NULL;
19213
19214 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19215 XML_SCHEMAP_AG_PROPS_CORRECT,
19216 attrGr->node, WXS_BASIC_CAST attrGr,
19217 "There must not exist more than one attribute "
19218 "declaration of type 'xs:ID' "
19219 "(or derived from 'xs:ID'). The %s violates this "
19220 "constraint",
19221 xmlSchemaGetComponentDesignation(&str, use),
19222 NULL);
19223 FREE_AND_NULL(str);
19224 if (xmlSchemaItemListRemove(uses, i) == -1)
19225 return(-1);
19226 }
19227 hasId = 1;
19228 }
19229 }
19230next_use: {}
19231 }
19232 }
19233 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019234}
19235
19236/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019237 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019238 * @attrgrpDecl: the schema attribute definition
19239 * @ctxt: the schema parser context
19240 * @name: the attribute name
19241 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019242 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019243 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019244static int
19245xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19246 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019247{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019248 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019249
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019250 if (ref->item != NULL)
19251 return(0);
19252 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19253 ref->name,
19254 ref->targetNamespace);
19255 if (group == NULL) {
19256 xmlSchemaPResCompAttrErr(ctxt,
19257 XML_SCHEMAP_SRC_RESOLVE,
19258 NULL, ref->node,
19259 "ref", ref->name, ref->targetNamespace,
19260 ref->itemType, NULL);
19261 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019262 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019263 ref->item = WXS_BASIC_CAST group;
19264 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019265}
19266
19267/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019268 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019269 * @item: an schema attribute declaration/use
19270 * @ctxt: a schema parser context
19271 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019272 *
19273 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019274 * Schema Component Constraint:
19275 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019276 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019277 * Validates the value constraints of an attribute declaration/use.
19278 * NOTE that this needs the simle type definitions to be already
19279 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019280 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019281static int
19282xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19283 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019284{
19285
19286 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019287 * SPEC a-props-correct (1)
19288 * "The values of the properties of an attribute declaration must
19289 * be as described in the property tableau in The Attribute
19290 * Declaration Schema Component (§3.2.1), modulo the impact of
19291 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019292 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019293
19294 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19295 return(0);
19296
19297 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019298 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019299
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019300 /*
19301 * SPEC a-props-correct (3)
19302 * "If the {type definition} is or is derived from ID then there
19303 * must not be a {value constraint}."
19304 */
19305 if (xmlSchemaIsDerivedFromBuiltInType(
19306 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19307 {
19308 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19309 XML_SCHEMAP_A_PROPS_CORRECT_3,
19310 NULL, WXS_BASIC_CAST attr,
19311 "Value constraints are not allowed if the type definition "
19312 "is or is derived from xs:ID",
19313 NULL, NULL);
19314 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019315 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019316 /*
19317 * SPEC a-props-correct (2)
19318 * "if there is a {value constraint}, the canonical lexical
19319 * representation of its value must be ·valid· with respect
19320 * to the {type definition} as defined in String Valid (§3.14.4)."
19321 * TODO: Don't care about the *cononical* stuff here, this requirement
19322 * will be removed in WXS 1.1 anyway.
19323 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019324 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019325 attr->node, WXS_ATTR_TYPEDEF(attr),
19326 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019327 1, 1, 0);
19328 if (ret != 0) {
19329 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019330 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019331 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019332 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019333 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019334 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019335 XML_SCHEMAP_A_PROPS_CORRECT_2,
19336 NULL, WXS_BASIC_CAST attr,
19337 "The value of the value constraint is not valid",
19338 NULL, NULL);
19339 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019340 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019341 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019342
19343 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019344}
19345
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019346static xmlSchemaElementPtr
19347xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19348 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019349{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019350 xmlSchemaElementPtr ret;
19351
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019352 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019353 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019354 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019355 return (ancestor);
19356
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019357 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019358 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019359 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019360 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019361 WXS_SUBST_HEAD(ancestor));
19362 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019363
19364 return (ret);
19365}
19366
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019367/**
19368 * xmlSchemaCheckElemPropsCorrect:
19369 * @ctxt: a schema parser context
19370 * @decl: the element declaration
19371 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019372 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019373 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019374 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019375 *
19376 * STATUS:
19377 * missing: (6)
19378 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019379static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019380xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19381 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019382{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019383 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019384 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019385 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019386 * SPEC (1) "The values of the properties of an element declaration
19387 * must be as described in the property tableau in The Element
19388 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19389 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019390 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019391 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19392 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019393
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019394 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019395 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019396 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019397 * affiliation}, then {scope} must be global."
19398 */
19399 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19400 xmlSchemaPCustomErr(pctxt,
19401 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019402 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019403 "Only global element declarations can have a "
19404 "substitution group affiliation", NULL);
19405 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019406 }
19407 /*
19408 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19409 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019410 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019411 * property."
19412 */
19413 if (head == elemDecl)
19414 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019415 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019416 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19417 else
19418 circ = NULL;
19419 if (circ != NULL) {
19420 xmlChar *strA = NULL, *strB = NULL;
19421
19422 xmlSchemaPCustomErrExt(pctxt,
19423 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019424 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019425 "The element declaration '%s' defines a circular "
19426 "substitution group to element declaration '%s'",
19427 xmlSchemaGetComponentQName(&strA, circ),
19428 xmlSchemaGetComponentQName(&strB, head),
19429 NULL);
19430 FREE_AND_NULL(strA)
19431 FREE_AND_NULL(strB)
19432 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19433 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019434 /*
19435 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019436 * the {type definition}
19437 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019438 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019439 * of the {substitution group exclusions} of the {substitution group
19440 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19441 * (if the {type definition} is complex) or as defined in
19442 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019443 * simple)."
19444 *
19445 * NOTE: {substitution group exclusions} means the values of the
19446 * attribute "final".
19447 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019448
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019449 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019450 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019451
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019452 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19453 set |= SUBSET_EXTENSION;
19454 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19455 set |= SUBSET_RESTRICTION;
19456
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019457 if (xmlSchemaCheckCOSDerivedOK(pctxt, typeDef,
19458 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019459 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19460
19461 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019462 xmlSchemaPCustomErrExt(pctxt,
19463 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019464 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019465 "The type definition '%s' was "
19466 "either rejected by the substitution group "
19467 "affiliation '%s', or not validly derived from its type "
19468 "definition '%s'",
19469 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019470 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019471 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019472 FREE_AND_NULL(strA)
19473 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019474 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019475 }
19476 }
19477 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019478 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019479 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019480 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019481 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019482 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019483 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019484 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019485 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019486 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019487 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019488 (WXS_IS_COMPLEX(typeDef) &&
19489 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019490 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19491 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019492
19493 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19494 xmlSchemaPCustomErr(pctxt,
19495 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019496 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019497 "The type definition (or type definition's content type) is or "
19498 "is derived from ID; value constraints are not allowed in "
19499 "conjunction with such a type definition", NULL);
19500 } else if (elemDecl->value != NULL) {
19501 int vcret;
19502 xmlNodePtr node = NULL;
19503
19504 /*
19505 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19506 * representation of its value must be ·valid· with respect to the
19507 * {type definition} as defined in Element Default Valid (Immediate)
19508 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019509 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019510 if (typeDef == NULL) {
19511 xmlSchemaPErr(pctxt, elemDecl->node,
19512 XML_SCHEMAP_INTERNAL,
19513 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19514 "type is missing... skipping validation of "
19515 "the value constraint", NULL, NULL);
19516 return (-1);
19517 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019518 if (elemDecl->node != NULL) {
19519 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19520 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19521 BAD_CAST "fixed");
19522 else
19523 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19524 BAD_CAST "default");
19525 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019526 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19527 typeDef, elemDecl->value, &(elemDecl->defVal));
19528 if (vcret != 0) {
19529 if (vcret < 0) {
19530 PERROR_INT("xmlSchemaElemCheckValConstr",
19531 "failed to validate the value constraint of an "
19532 "element declaration");
19533 return (-1);
19534 }
19535 return (vcret);
19536 }
19537 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019538
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019539 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019540}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019541
19542/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019543 * xmlSchemaCheckElemSubstGroup:
19544 * @ctxt: a schema parser context
19545 * @decl: the element declaration
19546 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019547 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019548 * Schema Component Constraint:
19549 * Substitution Group (cos-equiv-class)
19550 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019551 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019552 * a list will be built for each subst. group head, holding all direct
19553 * referents to this head.
19554 * NOTE that this function needs:
19555 * 1. circular subst. groups to be checked beforehand
19556 * 2. the declaration's type to be derived from the head's type
19557 *
19558 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019559 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019560 */
19561static void
19562xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19563 xmlSchemaElementPtr elemDecl)
19564{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019565 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019566 /* SPEC (1) "Its {abstract} is false." */
19567 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19568 return;
19569 {
19570 xmlSchemaElementPtr head;
19571 xmlSchemaTypePtr headType, type;
19572 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019573 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019574 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19575 * {disallowed substitutions} as the blocking constraint, as defined in
19576 * Substitution Group OK (Transitive) (§3.3.6)."
19577 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019578 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19579 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019580 set = 0;
19581 methSet = 0;
19582 /*
19583 * The blocking constraints.
19584 */
19585 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19586 continue;
19587 headType = head->subtypes;
19588 type = elemDecl->subtypes;
19589 if (headType == type)
19590 goto add_member;
19591 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19592 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19593 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19594 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19595 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019596 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019597 * "The set of all {derivation method}s involved in the
19598 * derivation of D's {type definition} from C's {type definition}
19599 * does not intersect with the union of the blocking constraint,
19600 * C's {prohibited substitutions} (if C is complex, otherwise the
19601 * empty set) and the {prohibited substitutions} (respectively the
19602 * empty set) of any intermediate {type definition}s in the
19603 * derivation of D's {type definition} from C's {type definition}."
19604 */
19605 /*
19606 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19607 * subst.head axis, the methSet does not need to be computed for
19608 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019609 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019610 /*
19611 * The set of all {derivation method}s involved in the derivation
19612 */
19613 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019614 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019615 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19616 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019617
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019618 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019619 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19620 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19621
19622 type = type->baseType;
19623 }
19624 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019625 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019626 * the head's type.
19627 */
19628 type = elemDecl->subtypes->baseType;
19629 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019630 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019631 if ((type->flags &
19632 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19633 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19634 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19635 if ((type->flags &
19636 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19637 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19638 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19639 } else
19640 break;
19641 if (type == headType)
19642 break;
19643 type = type->baseType;
19644 }
19645 if ((set != 0) &&
19646 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19647 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19648 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19649 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19650 continue;
19651 }
19652add_member:
19653 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19654 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19655 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19656 }
19657 }
19658}
19659
19660/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019661 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019662 * @item: an schema element declaration/particle
19663 * @ctxt: a schema parser context
19664 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019665 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019666 * Validates the value constraints of an element declaration.
19667 *
19668 * Fixes finish doing the computations on the element declarations.
19669 */
19670static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019671xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019672 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019673{
19674 if (elemDecl == NULL)
19675 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019676 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19677 return;
19678 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019679 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19680 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019681}
19682
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019683/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019684 * xmlSchemaResolveModelGroupParticleReferences:
19685 * @particle: a particle component
19686 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019687 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019688 * Resolves references of a model group's {particles} to
19689 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019690 */
19691static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019692xmlSchemaResolveModelGroupParticleReferences(
19693 xmlSchemaParserCtxtPtr ctxt,
19694 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019695{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019696 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19697 xmlSchemaQNameRefPtr ref;
19698 xmlSchemaBasicItemPtr refItem;
19699
19700 /*
19701 * URGENT TODO: Test this.
19702 */
19703 while (particle != NULL) {
19704 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19705 ((WXS_PARTICLE_TERM(particle))->type !=
19706 XML_SCHEMA_EXTRA_QNAMEREF))
19707 {
19708 goto next_particle;
19709 }
19710 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019711 /*
19712 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019713 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019714 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019715 particle->children = NULL;
19716
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019717 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19718 ref->itemType, ref->name, ref->targetNamespace);
19719 if (refItem == NULL) {
19720 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019721 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019722 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019723 /* TODO: remove the particle. */
19724 goto next_particle;
19725 }
19726 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19727 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19728 /* TODO: remove the particle. */
19729 goto next_particle;
19730 /*
19731 * NOTE that we will assign the model group definition
19732 * itself to the "term" of the particle. This will ease
19733 * the check for circular model group definitions. After
19734 * that the "term" will be assigned the model group of the
19735 * model group definition.
19736 */
19737 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19738 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019739 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019740 * SPEC cos-all-limited (1)
19741 * SPEC cos-all-limited (1.2)
19742 * "It appears only as the value of one or both of the
19743 * following properties:"
19744 * (1.1) "the {model group} property of a model group
19745 * definition."
19746 * (1.2) "the {term} property of a particle [... of] the "
19747 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019748 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019749 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19750 /* TODO: error code */
19751 XML_SCHEMAP_COS_ALL_LIMITED,
19752 WXS_ITEM_NODE(particle), NULL,
19753 "A model group definition is referenced, but "
19754 "it contains an 'all' model group, which "
19755 "cannot be contained by model groups",
19756 NULL, NULL);
19757 /* TODO: remove the particle. */
19758 goto next_particle;
19759 }
19760 particle->children = (xmlSchemaTreeItemPtr) refItem;
19761 } else {
19762 /*
19763 * TODO: Are referenced element declarations the only
19764 * other components we expect here?
19765 */
19766 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019767 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019768next_particle:
19769 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019770 }
19771}
19772
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019773static int
19774xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19775 xmlSchemaValPtr y)
19776{
19777 xmlSchemaTypePtr tx, ty, ptx, pty;
19778 int ret;
19779
19780 while (x != NULL) {
19781 /* Same types. */
19782 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19783 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19784 ptx = xmlSchemaGetPrimitiveType(tx);
19785 pty = xmlSchemaGetPrimitiveType(ty);
19786 /*
19787 * (1) if a datatype T' is ·derived· by ·restriction· from an
19788 * atomic datatype T then the ·value space· of T' is a subset of
19789 * the ·value space· of T. */
19790 /*
19791 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19792 * from a common atomic ancestor T then the ·value space·s of T'
19793 * and T'' may overlap.
19794 */
19795 if (ptx != pty)
19796 return(0);
19797 /*
19798 * We assume computed values to be normalized, so do a fast
19799 * string comparison for string based types.
19800 */
19801 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019802 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019803 if (! xmlStrEqual(
19804 xmlSchemaValueGetAsString(x),
19805 xmlSchemaValueGetAsString(y)))
19806 return (0);
19807 } else {
19808 ret = xmlSchemaCompareValuesWhtsp(
19809 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19810 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19811 if (ret == -2)
19812 return(-1);
19813 if (ret != 0)
19814 return(0);
19815 }
19816 /*
19817 * Lists.
19818 */
19819 x = xmlSchemaValueGetNext(x);
19820 if (x != NULL) {
19821 y = xmlSchemaValueGetNext(y);
19822 if (y == NULL)
19823 return (0);
19824 } else if (xmlSchemaValueGetNext(y) != NULL)
19825 return (0);
19826 else
19827 return (1);
19828 }
19829 return (0);
19830}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019831
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019832/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019833 * xmlSchemaResolveAttrUseReferences:
19834 * @item: an attribute use
19835 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019836 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019837 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019838 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019839static int
19840xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19841 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019842{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019843 if ((ctxt == NULL) || (ause == NULL))
19844 return(-1);
19845 if ((ause->attrDecl == NULL) ||
19846 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19847 return(0);
19848
19849 {
19850 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19851
19852 /*
19853 * TODO: Evaluate, what errors could occur if the declaration is not
19854 * found.
19855 */
19856 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19857 ref->name, ref->targetNamespace);
19858 if (ause->attrDecl == NULL) {
19859 xmlSchemaPResCompAttrErr(ctxt,
19860 XML_SCHEMAP_SRC_RESOLVE,
19861 WXS_BASIC_CAST ause, ause->node,
19862 "ref", ref->name, ref->targetNamespace,
19863 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19864 return(ctxt->err);;
19865 }
19866 }
19867 return(0);
19868}
19869
19870/**
19871 * xmlSchemaCheckAttrUsePropsCorrect:
19872 * @ctxt: a parser context
19873 * @use: an attribute use
19874 *
19875 * Schema Component Constraint:
19876 * Attribute Use Correct (au-props-correct)
19877 *
19878 */
19879static int
19880xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19881 xmlSchemaAttributeUsePtr use)
19882{
19883 if ((ctxt == NULL) || (use == NULL))
19884 return(-1);
19885 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19886 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19887 return(0);
19888
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019889 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019890 * SPEC au-props-correct (1)
19891 * "The values of the properties of an attribute use must be as
19892 * described in the property tableau in The Attribute Use Schema
19893 * Component (§3.5.1), modulo the impact of Missing
19894 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019895 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019896
19897 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19898 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19899 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19900 {
19901 xmlSchemaPCustomErr(ctxt,
19902 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19903 WXS_BASIC_CAST use, NULL,
19904 "The attribute declaration has a 'fixed' value constraint "
19905 ", thus the attribute use must also have a 'fixed' value "
19906 "constraint",
19907 NULL);
19908 return(ctxt->err);
19909 }
19910 /*
19911 * Compute and check the value constraint's value.
19912 */
19913 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19914 int ret;
19915 /*
19916 * TODO: The spec seems to be missing a check of the
19917 * value constraint of the attribute use. We will do it here.
19918 */
19919 /*
19920 * SPEC a-props-correct (3)
19921 */
19922 if (xmlSchemaIsDerivedFromBuiltInType(
19923 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19924 {
19925 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19926 XML_SCHEMAP_AU_PROPS_CORRECT,
19927 NULL, WXS_BASIC_CAST use,
19928 "Value constraints are not allowed if the type definition "
19929 "is or is derived from xs:ID",
19930 NULL, NULL);
19931 return(ctxt->err);
19932 }
19933
19934 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19935 use->node, WXS_ATTRUSE_TYPEDEF(use),
19936 use->defValue, &(use->defVal),
19937 1, 1, 0);
19938 if (ret != 0) {
19939 if (ret < 0) {
19940 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19941 "calling xmlSchemaVCheckCVCSimpleType()");
19942 return(-1);
19943 }
19944 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19945 XML_SCHEMAP_AU_PROPS_CORRECT,
19946 NULL, WXS_BASIC_CAST use,
19947 "The value of the value constraint is not valid",
19948 NULL, NULL);
19949 return(ctxt->err);
19950 }
19951 }
19952 /*
19953 * SPEC au-props-correct (2)
19954 * "If the {attribute declaration} has a fixed
19955 * {value constraint}, then if the attribute use itself has a
19956 * {value constraint}, it must also be fixed and its value must match
19957 * that of the {attribute declaration}'s {value constraint}."
19958 */
19959 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
19960 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19961 {
19962 if (! xmlSchemaAreValuesEqual(use->defVal,
19963 (WXS_ATTRUSE_DECL(use))->defVal))
19964 {
19965 xmlSchemaPCustomErr(ctxt,
19966 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19967 WXS_BASIC_CAST use, NULL,
19968 "The 'fixed' value constraint of the attribute use "
19969 "must match the attribute declaration's value "
19970 "constraint '%s'",
19971 (WXS_ATTRUSE_DECL(use))->defValue);
19972 }
19973 return(ctxt->err);
19974 }
19975 return(0);
19976}
19977
19978
19979
19980
19981/**
19982 * xmlSchemaResolveAttrTypeReferences:
19983 * @item: an attribute declaration
19984 * @ctxt: a parser context
19985 *
19986 * Resolves the referenced type definition component.
19987 */
19988static int
19989xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
19990 xmlSchemaParserCtxtPtr ctxt)
19991{
Daniel Veillard01fa6152004-06-29 17:04:39 +000019992 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019993 * The simple type definition corresponding to the <simpleType> element
19994 * information item in the [children], if present, otherwise the simple
19995 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000019996 * [attribute], if present, otherwise the ·simple ur-type definition·.
19997 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019998 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019999 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020000 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20001 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020002 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020003 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020004 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020005
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020006 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20007 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020008 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020009 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020010 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020011 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020012 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020013 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020014 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020015 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020016 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020017
Daniel Veillard3646d642004-06-02 19:19:14 +000020018 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020019 /*
20020 * The type defaults to the xs:anySimpleType.
20021 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020022 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20023 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020024 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020025}
20026
20027/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020028 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020029 * @idc: the identity-constraint definition
20030 * @ctxt: the schema parser context
20031 * @name: the attribute name
20032 *
20033 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020034 * Schema Component Constraint:
20035 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020036 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020037static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020038xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020039 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020040{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020041 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020042 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020043 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020044 idc->ref->item = (xmlSchemaBasicItemPtr)
20045 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20046 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020047 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020048 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020049 * TODO: It is actually not an error to fail to resolve
20050 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020051 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020052 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020053 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020054 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020055 "refer", idc->ref->name,
20056 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020057 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020058 return(pctxt->err);
20059 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20060 /*
20061 * SPEC c-props-correct (1)
20062 */
20063 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20064 XML_SCHEMAP_C_PROPS_CORRECT,
20065 NULL, WXS_BASIC_CAST idc,
20066 "The keyref references a keyref",
20067 NULL, NULL);
20068 idc->ref->item = NULL;
20069 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020070 } else {
20071 if (idc->nbFields !=
20072 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20073 xmlChar *str = NULL;
20074 xmlSchemaIDCPtr refer;
20075
20076 refer = (xmlSchemaIDCPtr) idc->ref->item;
20077 /*
20078 * SPEC c-props-correct(2)
20079 * "If the {identity-constraint category} is keyref,
20080 * the cardinality of the {fields} must equal that of
20081 * the {fields} of the {referenced key}.
20082 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020083 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020084 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020085 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020086 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020087 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020088 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020089 refer->name),
20090 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020091 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020092 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020093 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020094 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020095 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020096 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020097}
20098
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020099static int
20100xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20101 xmlSchemaParserCtxtPtr pctxt)
20102{
20103 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20104 prohib->targetNamespace) == NULL) {
20105
20106 xmlSchemaPResCompAttrErr(pctxt,
20107 XML_SCHEMAP_SRC_RESOLVE,
20108 NULL, prohib->node,
20109 "ref", prohib->name, prohib->targetNamespace,
20110 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20111 return(XML_SCHEMAP_SRC_RESOLVE);
20112 }
20113 return(0);
20114}
20115
20116#define WXS_REDEFINED_TYPE(c) \
20117(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20118
20119#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20120(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20121
20122#define WXS_REDEFINED_ATTR_GROUP(c) \
20123(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20124
20125static int
20126xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20127{
20128 int err = 0;
20129 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20130 xmlSchemaBasicItemPtr prev, item;
20131 int wasRedefined;
20132
20133 if (redef == NULL)
20134 return(0);
20135
20136 do {
20137 item = redef->item;
20138 /*
20139 * First try to locate the redefined component in the
20140 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020141 * NOTE: According to this schema bug entry:
20142 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20143 * it's not clear if the referenced component needs to originate
20144 * from the <redefine>d schema _document_ or the schema; the latter
20145 * would include all imported and included sub-schemas of the
20146 * <redefine>d schema. Currenlty we latter approach is used.
20147 * SUPPLEMENT: It seems that the WG moves towards the latter
20148 * approach, so we are doing it right.
20149 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020150 */
20151 prev = xmlSchemaFindRedefCompInGraph(
20152 redef->targetBucket, item->type,
20153 redef->refName, redef->refTargetNs);
20154 if (prev == NULL) {
20155 xmlChar *str = NULL;
20156 xmlNodePtr node;
20157
20158 /*
20159 * SPEC src-redefine:
20160 * (6.2.1) "The ·actual value· of its own name attribute plus
20161 * target namespace must successfully ·resolve· to a model
20162 * group definition in I."
20163 * (7.2.1) "The ·actual value· of its own name attribute plus
20164 * target namespace must successfully ·resolve· to an attribute
20165 * group definition in I."
20166
20167 *
20168 * Note that, if we are redefining with the use of references
20169 * to components, the spec assumes the src-resolve to be used;
20170 * but this won't assure that we search only *inside* the
20171 * redefined schema.
20172 */
20173 if (redef->reference)
20174 node = WXS_ITEM_NODE(redef->reference);
20175 else
20176 node = WXS_ITEM_NODE(item);
20177 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20178 /*
20179 * TODO: error code.
20180 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20181 * reference kind.
20182 */
20183 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
20184 "The %s to be redefined could not be found in "
20185 "the redefined schema",
20186 WXS_ITEM_TYPE_NAME(item),
20187 xmlSchemaFormatQName(&str, redef->refTargetNs,
20188 redef->refName));
20189 FREE_AND_NULL(str);
20190 err = pctxt->err;
20191 redef = redef->next;
20192 continue;
20193 }
20194 /*
20195 * TODO: Obtaining and setting the redefinition state is really
20196 * clumsy.
20197 */
20198 wasRedefined = 0;
20199 switch (item->type) {
20200 case XML_SCHEMA_TYPE_COMPLEX:
20201 case XML_SCHEMA_TYPE_SIMPLE:
20202 if ((WXS_TYPE_CAST prev)->flags &
20203 XML_SCHEMAS_TYPE_REDEFINED)
20204 {
20205 wasRedefined = 1;
20206 break;
20207 }
20208 /* Mark it as redefined. */
20209 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20210 /*
20211 * Assign the redefined type to the
20212 * base type of the redefining type.
20213 * TODO: How
20214 */
20215 ((xmlSchemaTypePtr) item)->baseType =
20216 (xmlSchemaTypePtr) prev;
20217 break;
20218 case XML_SCHEMA_TYPE_GROUP:
20219 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20220 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20221 {
20222 wasRedefined = 1;
20223 break;
20224 }
20225 /* Mark it as redefined. */
20226 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20227 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20228 if (redef->reference != NULL) {
20229 /*
20230 * Overwrite the QName-reference with the
20231 * referenced model group def.
20232 */
20233 (WXS_PTC_CAST redef->reference)->children =
20234 WXS_TREE_CAST prev;
20235 }
20236 redef->target = prev;
20237 break;
20238 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20239 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20240 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20241 {
20242 wasRedefined = 1;
20243 break;
20244 }
20245 (WXS_ATTR_GROUP_CAST prev)->flags |=
20246 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20247 if (redef->reference != NULL) {
20248 /*
20249 * Assign the redefined attribute group to the
20250 * QName-reference component.
20251 * This is the easy case, since we will just
20252 * expand the redefined group.
20253 */
20254 (WXS_QNAME_CAST redef->reference)->item = prev;
20255 redef->target = NULL;
20256 } else {
20257 /*
20258 * This is the complicated case: we need
20259 * to apply src-redefine (7.2.2) at a later
20260 * stage, i.e. when attribute group references
20261 * have beed expanded and simple types have
20262 * beed fixed.
20263 */
20264 redef->target = prev;
20265 }
20266 break;
20267 default:
20268 PERROR_INT("xmlSchemaResolveRedefReferences",
20269 "Unexpected redefined component type");
20270 return(-1);
20271 }
20272 if (wasRedefined) {
20273 xmlChar *str = NULL;
20274 xmlNodePtr node;
20275
20276 if (redef->reference)
20277 node = WXS_ITEM_NODE(redef->reference);
20278 else
20279 node = WXS_ITEM_NODE(redef->item);
20280
20281 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20282 /* TODO: error code. */
20283 XML_SCHEMAP_SRC_REDEFINE,
20284 node, NULL,
20285 "The referenced %s was already redefined. Multiple "
20286 "redefinition of the same component is not supported",
20287 xmlSchemaGetComponentDesignation(&str, prev),
20288 NULL);
20289 FREE_AND_NULL(str)
20290 err = pctxt->err;
20291 redef = redef->next;
20292 continue;
20293 }
20294 redef = redef->next;
20295 } while (redef != NULL);
20296
20297 return(err);
20298}
20299
20300static int
20301xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20302{
20303 int err = 0;
20304 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20305 xmlSchemaBasicItemPtr item;
20306
20307 if (redef == NULL)
20308 return(0);
20309
20310 do {
20311 if (redef->target == NULL) {
20312 redef = redef->next;
20313 continue;
20314 }
20315 item = redef->item;
20316
20317 switch (item->type) {
20318 case XML_SCHEMA_TYPE_SIMPLE:
20319 case XML_SCHEMA_TYPE_COMPLEX:
20320 /*
20321 * Since the spec wants the {name} of the redefined
20322 * type to be 'absent', we'll NULL it.
20323 */
20324 (WXS_TYPE_CAST redef->target)->name = NULL;
20325
20326 /*
20327 * TODO: Seems like there's nothing more to do. The normal
20328 * inheritance mechanism is used. But not 100% sure.
20329 */
20330 break;
20331 case XML_SCHEMA_TYPE_GROUP:
20332 /*
20333 * URGENT TODO:
20334 * SPEC src-redefine:
20335 * (6.2.2) "The {model group} of the model group definition
20336 * which corresponds to it per XML Representation of Model
20337 * Group Definition Schema Components (§3.7.2) must be a
20338 * ·valid restriction· of the {model group} of that model
20339 * group definition in I, as defined in Particle Valid
20340 * (Restriction) (§3.9.6)."
20341 */
20342 break;
20343 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20344 /*
20345 * SPEC src-redefine:
20346 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20347 * the attribute group definition which corresponds to it
20348 * per XML Representation of Attribute Group Definition Schema
20349 * Components (§3.6.2) must be ·valid restrictions· of the
20350 * {attribute uses} and {attribute wildcard} of that attribute
20351 * group definition in I, as defined in clause 2, clause 3 and
20352 * clause 4 of Derivation Valid (Restriction, Complex)
20353 * (§3.4.6) (where references to the base type definition are
20354 * understood as references to the attribute group definition
20355 * in I)."
20356 */
20357 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20358 XML_SCHEMA_ACTION_REDEFINE,
20359 item, redef->target,
20360 (WXS_ATTR_GROUP_CAST item)->attrUses,
20361 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20362 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20363 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20364 if (err == -1)
20365 return(-1);
20366 break;
20367 default:
20368 break;
20369 }
20370 redef = redef->next;
20371 } while (redef != NULL);
20372 return(0);
20373}
20374
20375
20376static int
20377xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20378 xmlSchemaBucketPtr bucket)
20379{
20380 xmlSchemaBasicItemPtr item;
20381 int err;
20382 xmlHashTablePtr *table;
20383 const xmlChar *name;
20384 int i;
20385
20386#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20387 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20388 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20389 else \
20390 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20391
20392 /*
20393 * Add global components to the schema's hash tables.
20394 * This is the place where duplicate components will be
20395 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020396 * TODO: I think normally we should support imports of the
20397 * same namespace from multiple locations. We don't do currently,
20398 * but if we do then according to:
20399 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20400 * we would need, if imported directly, to import redefined
20401 * components as well to be able to catch clashing components.
20402 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020403 */
20404 if (bucket == NULL)
20405 return(-1);
20406 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20407 return(0);
20408 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20409
20410 for (i = 0; i < bucket->globals->nbItems; i++) {
20411 item = bucket->globals->items[i];
20412 table = NULL;
20413 switch (item->type) {
20414 case XML_SCHEMA_TYPE_COMPLEX:
20415 case XML_SCHEMA_TYPE_SIMPLE:
20416 if (WXS_REDEFINED_TYPE(item))
20417 continue;
20418 name = (WXS_TYPE_CAST item)->name;
20419 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20420 break;
20421 case XML_SCHEMA_TYPE_ELEMENT:
20422 name = (WXS_ELEM_CAST item)->name;
20423 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20424 break;
20425 case XML_SCHEMA_TYPE_ATTRIBUTE:
20426 name = (WXS_ATTR_CAST item)->name;
20427 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20428 break;
20429 case XML_SCHEMA_TYPE_GROUP:
20430 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20431 continue;
20432 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20433 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20434 break;
20435 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20436 if (WXS_REDEFINED_ATTR_GROUP(item))
20437 continue;
20438 name = (WXS_ATTR_GROUP_CAST item)->name;
20439 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20440 break;
20441 case XML_SCHEMA_TYPE_IDC_KEY:
20442 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20443 case XML_SCHEMA_TYPE_IDC_KEYREF:
20444 name = (WXS_IDC_CAST item)->name;
20445 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20446 break;
20447 case XML_SCHEMA_TYPE_NOTATION:
20448 name = ((xmlSchemaNotationPtr) item)->name;
20449 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20450 break;
20451 default:
20452 PERROR_INT("xmlSchemaAddComponents",
20453 "Unexpected global component type");
20454 continue;
20455 }
20456 if (*table == NULL) {
20457 *table = xmlHashCreateDict(10, pctxt->dict);
20458 if (*table == NULL) {
20459 PERROR_INT("xmlSchemaAddComponents",
20460 "failed to create a component hash table");
20461 return(-1);
20462 }
20463 }
20464 err = xmlHashAddEntry(*table, name, item);
20465 if (err != 0) {
20466 xmlChar *str = NULL;
20467
20468 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20469 XML_SCHEMAP_REDEFINED_TYPE,
20470 WXS_ITEM_NODE(item),
20471 WXS_BASIC_CAST item,
20472 "A global %s '%s' does already exist",
20473 WXS_ITEM_TYPE_NAME(item),
20474 xmlSchemaGetComponentQName(&str, item));
20475 FREE_AND_NULL(str);
20476 }
20477 }
20478 /*
20479 * Process imported/included schemas.
20480 */
20481 if (bucket->relations != NULL) {
20482 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20483 do {
20484 if ((rel->bucket != NULL) &&
20485 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20486 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20487 return(-1);
20488 }
20489 rel = rel->next;
20490 } while (rel != NULL);
20491 }
20492 return(0);
20493}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020494
20495static int
20496xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20497{
20498 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20499 xmlSchemaTreeItemPtr item, *items;
20500 int nbItems, i;
20501
20502#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20503
20504 if ((con->pending == NULL) ||
20505 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020506 return(0);
20507
20508 /* TODO:
20509 * SPEC (src-redefine):
20510 * (6.2) "If it has no such self-reference, then all of the
20511 * following must be true:"
20512
20513 * (6.2.2) The {model group} of the model group definition which
20514 * corresponds to it per XML Representation of Model Group
20515 * Definition Schema Components (§3.7.2) must be a ·valid
20516 * restriction· of the {model group} of that model group definition
20517 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20518 */
20519 xmlSchemaCheckSRCRedefineFirst(pctxt);
20520
20521 /*
20522 * Add global components to the schemata's hash tables.
20523 */
20524 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020525
20526 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020527 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20528 nbItems = con->pending->nbItems;
20529 /*
20530 * Now that we have parsed *all* the schema document(s) and converted
20531 * them to schema components, we can resolve references, apply component
20532 * constraints, create the FSA from the content model, etc.
20533 */
20534 /*
20535 * Resolve references of..
20536 *
20537 * 1. element declarations:
20538 * - the type definition
20539 * - the substitution group affiliation
20540 * 2. simple/complex types:
20541 * - the base type definition
20542 * - the memberTypes of union types
20543 * - the itemType of list types
20544 * 3. attributes declarations and attribute uses:
20545 * - the type definition
20546 * - if an attribute use, then the attribute declaration
20547 * 4. attribute group references:
20548 * - the attribute group definition
20549 * 5. particles:
20550 * - the term of the particle (e.g. a model group)
20551 * 6. IDC key-references:
20552 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020553 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020554 */
20555 for (i = 0; i < nbItems; i++) {
20556 item = items[i];
20557 switch (item->type) {
20558 case XML_SCHEMA_TYPE_ELEMENT:
20559 xmlSchemaResolveElementReferences(
20560 (xmlSchemaElementPtr) item, pctxt);
20561 FIXHFAILURE;
20562 break;
20563 case XML_SCHEMA_TYPE_COMPLEX:
20564 case XML_SCHEMA_TYPE_SIMPLE:
20565 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020566 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020567 FIXHFAILURE;
20568 break;
20569 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020570 xmlSchemaResolveAttrTypeReferences(
20571 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020572 FIXHFAILURE;
20573 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020574 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20575 xmlSchemaResolveAttrUseReferences(
20576 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020577 FIXHFAILURE;
20578 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020579 case XML_SCHEMA_EXTRA_QNAMEREF:
20580 if ((WXS_QNAME_CAST item)->itemType ==
20581 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20582 {
20583 xmlSchemaResolveAttrGroupReferences(
20584 WXS_QNAME_CAST item, pctxt);
20585 }
20586 FIXHFAILURE;
20587 break;
20588 case XML_SCHEMA_TYPE_SEQUENCE:
20589 case XML_SCHEMA_TYPE_CHOICE:
20590 case XML_SCHEMA_TYPE_ALL:
20591 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20592 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020593 FIXHFAILURE;
20594 break;
20595 case XML_SCHEMA_TYPE_IDC_KEY:
20596 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20597 case XML_SCHEMA_TYPE_IDC_KEYREF:
20598 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020599 (xmlSchemaIDCPtr) item, pctxt);
20600 FIXHFAILURE;
20601 break;
20602 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20603 /*
20604 * Handle attribue prohibition which had a
20605 * "ref" attribute.
20606 */
20607 xmlSchemaResolveAttrUseProhibReferences(
20608 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020609 FIXHFAILURE;
20610 break;
20611 default:
20612 break;
20613 }
20614 }
20615 if (pctxt->nberrors != 0)
20616 goto exit_error;
20617
20618 /*
20619 * Now that all references are resolved we
20620 * can check for circularity of...
20621 * 1. the base axis of type definitions
20622 * 2. nested model group definitions
20623 * 3. nested attribute group definitions
20624 * TODO: check for circual substitution groups.
20625 */
20626 for (i = 0; i < nbItems; i++) {
20627 item = items[i];
20628 /*
20629 * Let's better stop on the first error here.
20630 */
20631 switch (item->type) {
20632 case XML_SCHEMA_TYPE_COMPLEX:
20633 case XML_SCHEMA_TYPE_SIMPLE:
20634 xmlSchemaCheckTypeDefCircular(
20635 (xmlSchemaTypePtr) item, pctxt);
20636 FIXHFAILURE;
20637 if (pctxt->nberrors != 0)
20638 goto exit_error;
20639 break;
20640 case XML_SCHEMA_TYPE_GROUP:
20641 xmlSchemaCheckGroupDefCircular(
20642 (xmlSchemaModelGroupDefPtr) item, pctxt);
20643 FIXHFAILURE;
20644 if (pctxt->nberrors != 0)
20645 goto exit_error;
20646 break;
20647 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20648 xmlSchemaCheckAttrGroupCircular(
20649 (xmlSchemaAttributeGroupPtr) item, pctxt);
20650 FIXHFAILURE;
20651 if (pctxt->nberrors != 0)
20652 goto exit_error;
20653 break;
20654 default:
20655 break;
20656 }
20657 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020658 if (pctxt->nberrors != 0)
20659 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020660 /*
20661 * Model group definition references:
20662 * Such a reference is reflected by a particle at the component
20663 * level. Until now the 'term' of such particles pointed
20664 * to the model group definition; this was done, in order to
20665 * ease circularity checks. Now we need to set the 'term' of
20666 * such particles to the model group of the model group definition.
20667 */
20668 for (i = 0; i < nbItems; i++) {
20669 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020670 switch (item->type) {
20671 case XML_SCHEMA_TYPE_SEQUENCE:
20672 case XML_SCHEMA_TYPE_CHOICE:
20673 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20674 WXS_MODEL_GROUP_CAST item);
20675 break;
20676 default:
20677 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020678 }
20679 }
20680 if (pctxt->nberrors != 0)
20681 goto exit_error;
20682 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020683 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020684 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020685 for (i = 0; i < nbItems; i++) {
20686 item = items[i];
20687 switch (item->type) {
20688 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20689 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20690 WXS_ATTR_GROUP_HAS_REFS(item))
20691 {
20692 xmlSchemaAttributeGroupExpandRefs(pctxt,
20693 WXS_ATTR_GROUP_CAST item);
20694 FIXHFAILURE;
20695 }
20696 break;
20697 default:
20698 break;
20699 }
20700 }
20701 if (pctxt->nberrors != 0)
20702 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020703 /*
20704 * First compute the variety of simple types. This is needed as
20705 * a seperate step, since otherwise we won't be able to detect
20706 * circular union types in all cases.
20707 */
20708 for (i = 0; i < nbItems; i++) {
20709 item = items[i];
20710 switch (item->type) {
20711 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020712 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020713 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20714 (xmlSchemaTypePtr) item);
20715 FIXHFAILURE;
20716 }
20717 break;
20718 default:
20719 break;
20720 }
20721 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020722 if (pctxt->nberrors != 0)
20723 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020724 /*
20725 * Detect circular union types. Note that this needs the variety to
20726 * be already computed.
20727 */
20728 for (i = 0; i < nbItems; i++) {
20729 item = items[i];
20730 switch (item->type) {
20731 case XML_SCHEMA_TYPE_SIMPLE:
20732 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20733 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20734 (xmlSchemaTypePtr) item);
20735 FIXHFAILURE;
20736 }
20737 break;
20738 default:
20739 break;
20740 }
20741 }
20742 if (pctxt->nberrors != 0)
20743 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020744
20745 /*
20746 * Do the complete type fixup for simple types.
20747 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020748 for (i = 0; i < nbItems; i++) {
20749 item = items[i];
20750 switch (item->type) {
20751 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020752 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20753 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20754 FIXHFAILURE;
20755 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020756 break;
20757 default:
20758 break;
20759 }
20760 }
20761 if (pctxt->nberrors != 0)
20762 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020763 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020764 * At this point we need all simple types to be builded and checked.
20765 */
20766 /*
20767 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020768 */
20769 for (i = 0; i < nbItems; i++) {
20770 item = items[i];
20771 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020772 case XML_SCHEMA_TYPE_ATTRIBUTE:
20773 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20774 FIXHFAILURE;
20775 break;
20776 default:
20777 break;
20778 }
20779 }
20780 if (pctxt->nberrors != 0)
20781 goto exit_error;
20782 /*
20783 * Apply constraints for attribute uses.
20784 */
20785 for (i = 0; i < nbItems; i++) {
20786 item = items[i];
20787 switch (item->type) {
20788 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20789 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20790 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20791 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020792 FIXHFAILURE;
20793 }
20794 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020795 default:
20796 break;
20797 }
20798 }
20799 if (pctxt->nberrors != 0)
20800 goto exit_error;
20801
20802 /*
20803 * Apply constraints for attribute group definitions.
20804 */
20805 for (i = 0; i < nbItems; i++) {
20806 item = items[i];
20807 switch (item->type) {
20808 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20809 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20810 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20811 {
20812 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20813 FIXHFAILURE;
20814 }
20815 break;
20816 default:
20817 break;
20818 }
20819 }
20820 if (pctxt->nberrors != 0)
20821 goto exit_error;
20822
20823 /*
20824 * Apply constraints for redefinitions.
20825 */
20826 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20827 xmlSchemaCheckSRCRedefineSecond(pctxt);
20828 if (pctxt->nberrors != 0)
20829 goto exit_error;
20830
20831 /*
20832 * Fixup complex types.
20833 */
20834 for (i = 0; i < nbItems; i++) {
20835 item = con->pending->items[i];
20836 switch (item->type) {
20837 case XML_SCHEMA_TYPE_COMPLEX:
20838 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20839 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20840 FIXHFAILURE;
20841 }
20842 break;
20843 default:
20844 break;
20845 }
20846 }
20847 if (pctxt->nberrors != 0)
20848 goto exit_error;
20849
20850 /*
20851 * The list could have changed, since xmlSchemaFixupComplexType()
20852 * will create particles and model groups in some cases.
20853 */
20854 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20855 nbItems = con->pending->nbItems;
20856
20857 /*
20858 * At this point all complex types need to be builded and checked.
20859 */
20860 /*
20861 * Apply some constraints for element declarations.
20862 */
20863 for (i = 0; i < nbItems; i++) {
20864 item = items[i];
20865 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020866 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020867
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020868 if ((((xmlSchemaElementPtr) item)->flags &
20869 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20870 xmlSchemaCheckElementDeclComponent(
20871 (xmlSchemaElementPtr) item, pctxt);
20872 FIXHFAILURE;
20873 }
20874 break;
20875 default:
20876 break;
20877 }
20878 }
20879 if (pctxt->nberrors != 0)
20880 goto exit_error;
20881 /*
20882 * Finally we can build the automaton from the content model of
20883 * complex types.
20884 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020885
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020886 for (i = 0; i < nbItems; i++) {
20887 item = items[i];
20888 switch (item->type) {
20889 case XML_SCHEMA_TYPE_COMPLEX:
20890 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020891 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020892 break;
20893 default:
20894 break;
20895 }
20896 }
20897 if (pctxt->nberrors != 0)
20898 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020899 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020900 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020901 */
20902 con->pending->nbItems = 0;
20903 return(0);
20904exit_error:
20905 con->pending->nbItems = 0;
20906 return(pctxt->err);
20907exit_failure:
20908 con->pending->nbItems = 0;
20909 return(-1);
20910}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020911/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020912 * xmlSchemaParse:
20913 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020914 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020915 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020916 * XML Shema struture which can be used to validate instances.
20917 * *WARNING* this interface is highly subject to change
20918 *
20919 * Returns the internal XML Schema structure built from the resource or
20920 * NULL in case of error
20921 */
20922xmlSchemaPtr
20923xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20924{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020925 xmlSchemaPtr schema = NULL;
20926 xmlSchemaBucketPtr bucket = NULL;
20927 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020928
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020929 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020930 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020931 * the API; i.e. not automatically by the validated instance document.
20932 */
20933
Daniel Veillard4255d502002-04-16 15:50:10 +000020934 xmlSchemaInitTypes();
20935
Daniel Veillard6045c902002-10-09 21:13:59 +000020936 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020937 return (NULL);
20938
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020939 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020940 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020941 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020942 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020943
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020944 /* Create the *main* schema. */
20945 schema = xmlSchemaNewSchema(ctxt);
20946 if (schema == NULL)
20947 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000020948 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020949 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020950 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020951 if (ctxt->constructor == NULL) {
20952 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
20953 if (ctxt->constructor == NULL)
20954 return(NULL);
20955 /* Take ownership of the constructor to be able to free it. */
20956 ctxt->ownsConstructor = 1;
20957 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020958 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020959 /*
20960 * Locate and add the schema document.
20961 */
20962 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
20963 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
20964 NULL, NULL, &bucket);
20965 if (res == -1)
20966 goto exit_failure;
20967 if (res != 0)
20968 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020969
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020970 if (bucket == NULL) {
20971 /* TODO: Error code, actually we failed to *locate* the schema. */
20972 if (ctxt->URL)
20973 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20974 NULL, NULL,
20975 "Failed to locate the main schema resource at '%s'",
20976 ctxt->URL, NULL);
20977 else
20978 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20979 NULL, NULL,
20980 "Failed to locate the main schema resource",
20981 NULL, NULL);
20982 goto exit;
20983 }
20984 /* Set the main schema bucket. */
20985 ctxt->constructor->bucket = bucket;
20986 ctxt->targetNamespace = bucket->targetNamespace;
20987 schema->targetNamespace = bucket->targetNamespace;
20988
20989 /* Then do the parsing for good. */
20990 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
20991 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020992 if (ctxt->nberrors != 0)
20993 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020994
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020995 schema->doc = bucket->doc;
20996 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020997
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020998 ctxt->schema = schema;
20999
21000 if (xmlSchemaFixupComponents(ctxt) == -1)
21001 goto exit_failure;
21002
21003 /*
21004 * TODO: This is not nice, since we cannot distinguish from the
21005 * result if there was an internal error or not.
21006 */
21007exit:
21008 if (ctxt->nberrors != 0) {
21009 if (schema) {
21010 xmlSchemaFree(schema);
21011 schema = NULL;
21012 }
21013 if (ctxt->constructor) {
21014 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21015 ctxt->constructor = NULL;
21016 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021017 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021018 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021019 ctxt->schema = NULL;
21020 return(schema);
21021exit_failure:
21022 /*
21023 * Quite verbose, but should catch internal errors, which were
21024 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021025 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021026 if (schema) {
21027 xmlSchemaFree(schema);
21028 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021029 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021030 if (ctxt->constructor) {
21031 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21032 ctxt->constructor = NULL;
21033 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021034 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021035 PERROR_INT2("xmlSchemaParse",
21036 "An internal error occured");
21037 ctxt->schema = NULL;
21038 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021039}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021040
Daniel Veillard4255d502002-04-16 15:50:10 +000021041/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021042 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021043 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021044 * @err: the error callback
21045 * @warn: the warning callback
21046 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021047 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021048 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021049 */
21050void
21051xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021052 xmlSchemaValidityErrorFunc err,
21053 xmlSchemaValidityWarningFunc warn, void *ctx)
21054{
Daniel Veillard4255d502002-04-16 15:50:10 +000021055 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021056 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021057 ctxt->error = err;
21058 ctxt->warning = warn;
21059 ctxt->userData = ctx;
21060}
21061
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021062/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021063 * xmlSchemaGetParserErrors:
21064 * @ctxt: a XMl-Schema parser context
21065 * @err: the error callback result
21066 * @warn: the warning callback result
21067 * @ctx: contextual data for the callbacks result
21068 *
21069 * Get the callback information used to handle errors for a parser context
21070 *
21071 * Returns -1 in case of failure, 0 otherwise
21072 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021073int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021074xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
21075 xmlSchemaValidityErrorFunc * err,
21076 xmlSchemaValidityWarningFunc * warn, void **ctx)
21077{
21078 if (ctxt == NULL)
21079 return(-1);
21080 if (err != NULL)
21081 *err = ctxt->error;
21082 if (warn != NULL)
21083 *warn = ctxt->warning;
21084 if (ctx != NULL)
21085 *ctx = ctxt->userData;
21086 return(0);
21087}
21088
21089/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021090 * xmlSchemaFacetTypeToString:
21091 * @type: the facet type
21092 *
21093 * Convert the xmlSchemaTypeType to a char string.
21094 *
21095 * Returns the char string representation of the facet type if the
21096 * type is a facet and an "Internal Error" string otherwise.
21097 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021098static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021099xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21100{
21101 switch (type) {
21102 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021103 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021104 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021105 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021106 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021107 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021108 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021109 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021110 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021111 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021112 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021113 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021114 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021115 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021116 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021117 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021118 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021119 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021120 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021121 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021122 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021123 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021124 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021125 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021126 default:
21127 break;
21128 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021129 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021130}
21131
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021132static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021133xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21134{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021135 /*
21136 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021137 * from xsd:string.
21138 */
21139 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021140 /*
21141 * Note that we assume a whitespace of preserve for anySimpleType.
21142 */
21143 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21144 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21145 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021146 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021147 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021148 else {
21149 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021150 * For all ·atomic· datatypes other than string (and types ·derived·
21151 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021152 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021153 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021154 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021155 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021156 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021157 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021158 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021159 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021160 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021161 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021162 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021163 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021164 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021165 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21166 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21167 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21168 return (XML_SCHEMA_WHITESPACE_REPLACE);
21169 else
21170 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021171 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021172 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021173}
21174
Daniel Veillard4255d502002-04-16 15:50:10 +000021175/************************************************************************
21176 * *
21177 * Simple type validation *
21178 * *
21179 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021180
Daniel Veillard4255d502002-04-16 15:50:10 +000021181
21182/************************************************************************
21183 * *
21184 * DOM Validation code *
21185 * *
21186 ************************************************************************/
21187
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021188/**
21189 * xmlSchemaAssembleByLocation:
21190 * @pctxt: a schema parser context
21191 * @vctxt: a schema validation context
21192 * @schema: the existing schema
21193 * @node: the node that fired the assembling
21194 * @nsName: the namespace name of the new schema
21195 * @location: the location of the schema
21196 *
21197 * Expands an existing schema by an additional schema.
21198 *
21199 * Returns 0 if the new schema is correct, a positive error code
21200 * number otherwise and -1 in case of an internal or API error.
21201 */
21202static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021203xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021204 xmlSchemaPtr schema,
21205 xmlNodePtr node,
21206 const xmlChar *nsName,
21207 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021208{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021209 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021210 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021211 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021212
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021213 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021214 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021215
21216 if (vctxt->pctxt == NULL) {
21217 VERROR_INT("xmlSchemaAssembleByLocation",
21218 "no parser context available");
21219 return(-1);
21220 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021221 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021222 if (pctxt->constructor == NULL) {
21223 PERROR_INT("xmlSchemaAssembleByLocation",
21224 "no constructor");
21225 return(-1);
21226 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021227 /*
21228 * Acquire the schema document.
21229 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021230 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21231 location, node);
21232 /*
21233 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21234 * the process will automatically change this to
21235 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21236 */
21237 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21238 location, NULL, NULL, 0, node, NULL, nsName,
21239 &bucket);
21240 if (ret != 0)
21241 return(ret);
21242 if (bucket == NULL) {
21243 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021244 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021245 return(-1);
21246 }
21247 /*
21248 * The first located schema will be handled as if all other
21249 * schemas imported by XSI were imported by this first schema.
21250 */
21251 if ((bucket != NULL) &&
21252 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21253 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21254 /*
21255 * TODO: Is this handled like an import? I.e. is it not an error
21256 * if the schema cannot be located?
21257 */
21258 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21259 return(0);
21260 /*
21261 * We will reuse the parser context for every schema imported
21262 * directly via XSI. So reset the context.
21263 */
21264 pctxt->nberrors = 0;
21265 pctxt->err = 0;
21266 pctxt->doc = bucket->doc;
21267
21268 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21269 if (ret == -1) {
21270 pctxt->doc = NULL;
21271 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021272 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021273 /* Paranoid error channelling. */
21274 if ((ret == 0) && (pctxt->nberrors != 0))
21275 ret = pctxt->err;
21276 if (pctxt->nberrors == 0) {
21277 /*
21278 * Only bother to fixup pending components, if there was
21279 * no error yet.
21280 */
21281 xmlSchemaFixupComponents(pctxt);
21282 vctxt->nberrors += pctxt->nberrors;
21283 } else {
21284 /* Add to validation error sum. */
21285 vctxt->nberrors += pctxt->nberrors;
21286 }
21287 pctxt->doc = NULL;
21288 return(ret);
21289exit_failure:
21290 pctxt->doc = NULL;
21291 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021292}
21293
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021294static xmlSchemaAttrInfoPtr
21295xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21296 int metaType)
21297{
21298 if (vctxt->nbAttrInfos == 0)
21299 return (NULL);
21300 {
21301 int i;
21302 xmlSchemaAttrInfoPtr iattr;
21303
21304 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21305 iattr = vctxt->attrInfos[i];
21306 if (iattr->metaType == metaType)
21307 return (iattr);
21308 }
21309
21310 }
21311 return (NULL);
21312}
21313
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021314/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021315 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021316 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021317 *
21318 * Expands an existing schema by an additional schema using
21319 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21320 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21321 * must be set to 1.
21322 *
21323 * Returns 0 if the new schema is correct, a positive error code
21324 * number otherwise and -1 in case of an internal or API error.
21325 */
21326static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021327xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021328{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021329 const xmlChar *cur, *end;
21330 const xmlChar *nsname = NULL, *location;
21331 int count = 0;
21332 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021333 xmlSchemaAttrInfoPtr iattr;
21334
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021335 /*
21336 * Parse the value; we will assume an even number of values
21337 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021338 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021339 * URGENT TODO: !! This needs to work for both
21340 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21341 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021342 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021343 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21344 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21345 if (iattr == NULL)
21346 xmlSchemaGetMetaAttrInfo(vctxt,
21347 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21348 if (iattr == NULL)
21349 return (0);
21350 cur = iattr->value;
21351 do {
21352 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021353 /*
21354 * Get the namespace name.
21355 */
21356 while (IS_BLANK_CH(*cur))
21357 cur++;
21358 end = cur;
21359 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21360 end++;
21361 if (end == cur)
21362 break;
21363 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021364 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021365 cur = end;
21366 }
21367 /*
21368 * Get the URI.
21369 */
21370 while (IS_BLANK_CH(*cur))
21371 cur++;
21372 end = cur;
21373 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21374 end++;
21375 if (end == cur)
21376 break;
21377 count++;
21378 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021379 cur = end;
21380 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21381 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021382 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021383 VERROR_INT("xmlSchemaAssembleByXSI",
21384 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021385 return (-1);
21386 }
21387 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021388 return (ret);
21389}
21390
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021391static const xmlChar *
21392xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21393 const xmlChar *prefix)
21394{
21395 if (vctxt->sax != NULL) {
21396 int i, j;
21397 xmlSchemaNodeInfoPtr inode;
21398
21399 for (i = vctxt->depth; i >= 0; i--) {
21400 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21401 inode = vctxt->elemInfos[i];
21402 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21403 if (((prefix == NULL) &&
21404 (inode->nsBindings[j] == NULL)) ||
21405 ((prefix != NULL) && xmlStrEqual(prefix,
21406 inode->nsBindings[j]))) {
21407
21408 /*
21409 * Note that the namespace bindings are already
21410 * in a string dict.
21411 */
21412 return (inode->nsBindings[j+1]);
21413 }
21414 }
21415 }
21416 }
21417 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021418#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021419 } else if (vctxt->reader != NULL) {
21420 xmlChar *nsName;
21421
21422 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21423 if (nsName != NULL) {
21424 const xmlChar *ret;
21425
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021426 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21427 xmlFree(nsName);
21428 return (ret);
21429 } else
21430 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021431#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021432 } else {
21433 xmlNsPtr ns;
21434
21435 if ((vctxt->inode->node == NULL) ||
21436 (vctxt->inode->node->doc == NULL)) {
21437 VERROR_INT("xmlSchemaLookupNamespace",
21438 "no node or node's doc avaliable");
21439 return (NULL);
21440 }
21441 ns = xmlSearchNs(vctxt->inode->node->doc,
21442 vctxt->inode->node, prefix);
21443 if (ns != NULL)
21444 return (ns->href);
21445 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021446 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021447}
21448
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021449/*
21450* This one works on the schema of the validation context.
21451*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021452static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021453xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21454 xmlSchemaPtr schema,
21455 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021456 const xmlChar *value,
21457 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021458 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021459{
21460 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021461
21462 if (vctxt && (vctxt->schema == NULL)) {
21463 VERROR_INT("xmlSchemaValidateNotation",
21464 "a schema is needed on the validation context");
21465 return (-1);
21466 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021467 ret = xmlValidateQName(value, 1);
21468 if (ret != 0)
21469 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021470 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021471 xmlChar *localName = NULL;
21472 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021473
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021474 localName = xmlSplitQName2(value, &prefix);
21475 if (prefix != NULL) {
21476 const xmlChar *nsName = NULL;
21477
21478 if (vctxt != NULL)
21479 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21480 else if (node != NULL) {
21481 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21482 if (ns != NULL)
21483 nsName = ns->href;
21484 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021485 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021486 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021487 return (1);
21488 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021489 if (nsName == NULL) {
21490 xmlFree(prefix);
21491 xmlFree(localName);
21492 return (1);
21493 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021494 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021495 if (valNeeded && (val != NULL)) {
21496 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21497 BAD_CAST xmlStrdup(nsName));
21498 if (*val == NULL)
21499 ret = -1;
21500 }
21501 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021502 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021503 xmlFree(prefix);
21504 xmlFree(localName);
21505 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021506 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021507 if (valNeeded && (val != NULL)) {
21508 (*val) = xmlSchemaNewNOTATIONValue(
21509 BAD_CAST xmlStrdup(value), NULL);
21510 if (*val == NULL)
21511 ret = -1;
21512 }
21513 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021514 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021515 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021516 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021517 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021518}
21519
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021520static int
21521xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21522 const xmlChar* lname,
21523 const xmlChar* nsname)
21524{
21525 int i;
21526
21527 lname = xmlDictLookup(vctxt->dict, lname, -1);
21528 if (lname == NULL)
21529 return(-1);
21530 if (nsname != NULL) {
21531 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21532 if (nsname == NULL)
21533 return(-1);
21534 }
21535 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21536 if ((vctxt->nodeQNames->items [i] == lname) &&
21537 (vctxt->nodeQNames->items[i +1] == nsname))
21538 /* Already there */
21539 return(i);
21540 }
21541 /* Add new entry. */
21542 i = vctxt->nodeQNames->nbItems;
21543 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21544 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21545 return(i);
21546}
21547
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021548/************************************************************************
21549 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021550 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021551 * *
21552 ************************************************************************/
21553
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021554/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021555 * xmlSchemaAugmentIDC:
21556 * @idcDef: the IDC definition
21557 *
21558 * Creates an augmented IDC definition item.
21559 *
21560 * Returns the item, or NULL on internal errors.
21561 */
21562static void
21563xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21564 xmlSchemaValidCtxtPtr vctxt)
21565{
21566 xmlSchemaIDCAugPtr aidc;
21567
21568 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21569 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021570 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021571 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21572 NULL);
21573 return;
21574 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021575 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021576 aidc->def = idcDef;
21577 aidc->next = NULL;
21578 if (vctxt->aidcs == NULL)
21579 vctxt->aidcs = aidc;
21580 else {
21581 aidc->next = vctxt->aidcs;
21582 vctxt->aidcs = aidc;
21583 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021584 /*
21585 * Save if we have keyrefs at all.
21586 */
21587 if ((vctxt->hasKeyrefs == 0) &&
21588 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21589 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021590}
21591
21592/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021593 * xmlSchemaIDCNewBinding:
21594 * @idcDef: the IDC definition of this binding
21595 *
21596 * Creates a new IDC binding.
21597 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021598 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021599 */
21600static xmlSchemaPSVIIDCBindingPtr
21601xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21602{
21603 xmlSchemaPSVIIDCBindingPtr ret;
21604
21605 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21606 sizeof(xmlSchemaPSVIIDCBinding));
21607 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021608 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021609 "allocating a PSVI IDC binding item", NULL);
21610 return (NULL);
21611 }
21612 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21613 ret->definition = idcDef;
21614 return (ret);
21615}
21616
21617/**
21618 * xmlSchemaIDCStoreNodeTableItem:
21619 * @vctxt: the WXS validation context
21620 * @item: the IDC node table item
21621 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021622 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021623 * They are stored to avoid copying them if IDC node-tables are merged
21624 * with corresponding parent IDC node-tables (bubbling).
21625 *
21626 * Returns 0 if succeeded, -1 on internal errors.
21627 */
21628static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021629xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021630 xmlSchemaPSVIIDCNodePtr item)
21631{
21632 /*
21633 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021634 */
21635 if (vctxt->idcNodes == NULL) {
21636 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021637 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21638 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021639 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021640 "allocating the IDC node table item list", NULL);
21641 return (-1);
21642 }
21643 vctxt->sizeIdcNodes = 20;
21644 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21645 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021646 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21647 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021648 sizeof(xmlSchemaPSVIIDCNodePtr));
21649 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021650 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021651 "re-allocating the IDC node table item list", NULL);
21652 return (-1);
21653 }
21654 }
21655 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021656
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021657 return (0);
21658}
21659
21660/**
21661 * xmlSchemaIDCStoreKey:
21662 * @vctxt: the WXS validation context
21663 * @item: the IDC key
21664 *
21665 * The validation context is used to store an IDC key.
21666 *
21667 * Returns 0 if succeeded, -1 on internal errors.
21668 */
21669static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021670xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021671 xmlSchemaPSVIIDCKeyPtr key)
21672{
21673 /*
21674 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021675 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021676 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021677 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021678 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21679 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021680 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021681 "allocating the IDC key storage list", NULL);
21682 return (-1);
21683 }
21684 vctxt->sizeIdcKeys = 40;
21685 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21686 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021687 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21688 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021689 sizeof(xmlSchemaPSVIIDCKeyPtr));
21690 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021691 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021692 "re-allocating the IDC key storage list", NULL);
21693 return (-1);
21694 }
21695 }
21696 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021697
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021698 return (0);
21699}
21700
21701/**
21702 * xmlSchemaIDCAppendNodeTableItem:
21703 * @bind: the IDC binding
21704 * @ntItem: the node-table item
21705 *
21706 * Appends the IDC node-table item to the binding.
21707 *
21708 * Returns 0 on success and -1 on internal errors.
21709 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021710static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021711xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21712 xmlSchemaPSVIIDCNodePtr ntItem)
21713{
21714 if (bind->nodeTable == NULL) {
21715 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021716 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021717 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21718 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021719 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021720 "allocating an array of IDC node-table items", NULL);
21721 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021722 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021723 } else if (bind->sizeNodes <= bind->nbNodes) {
21724 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021725 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21726 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021727 sizeof(xmlSchemaPSVIIDCNodePtr));
21728 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021729 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021730 "re-allocating an array of IDC node-table items", NULL);
21731 return(-1);
21732 }
21733 }
21734 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021735 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021736}
21737
21738/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021739 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021740 * @vctxt: the WXS validation context
21741 * @matcher: the IDC matcher
21742 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021743 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021744 * of the given matcher. If none found, a new one is created
21745 * and added to the IDC table.
21746 *
21747 * Returns an IDC binding or NULL on internal errors.
21748 */
21749static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021750xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021751 xmlSchemaIDCMatcherPtr matcher)
21752{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021753 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021754
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021755 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021756
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021757 if (ielem->idcTable == NULL) {
21758 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21759 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021760 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021761 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021762 } else {
21763 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021764
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021765 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021766 do {
21767 if (bind->definition == matcher->aidc->def)
21768 return(bind);
21769 if (bind->next == NULL) {
21770 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21771 if (bind->next == NULL)
21772 return (NULL);
21773 return(bind->next);
21774 }
21775 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021776 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021777 }
21778 return (NULL);
21779}
21780
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021781static xmlSchemaItemListPtr
21782xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21783 xmlSchemaIDCMatcherPtr matcher)
21784{
21785 if (matcher->targets == NULL)
21786 matcher->targets = xmlSchemaItemListCreate();
21787 return(matcher->targets);
21788}
21789
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021790/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021791 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021792 * @key: the IDC key
21793 *
21794 * Frees an IDC key together with its compiled value.
21795 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021796static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021797xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21798{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021799 if (key->val != NULL)
21800 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021801 xmlFree(key);
21802}
21803
21804/**
21805 * xmlSchemaIDCFreeBinding:
21806 *
21807 * Frees an IDC binding. Note that the node table-items
21808 * are not freed.
21809 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021810static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021811xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21812{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021813 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021814 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021815 if (bind->dupls != NULL)
21816 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021817 xmlFree(bind);
21818}
21819
21820/**
21821 * xmlSchemaIDCFreeIDCTable:
21822 * @bind: the first IDC binding in the list
21823 *
21824 * Frees an IDC table, i.e. all the IDC bindings in the list.
21825 */
21826static void
21827xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21828{
21829 xmlSchemaPSVIIDCBindingPtr prev;
21830
21831 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021832 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021833 bind = bind->next;
21834 xmlSchemaIDCFreeBinding(prev);
21835 }
21836}
21837
21838/**
21839 * xmlSchemaIDCFreeMatcherList:
21840 * @matcher: the first IDC matcher in the list
21841 *
21842 * Frees a list of IDC matchers.
21843 */
21844static void
21845xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21846{
21847 xmlSchemaIDCMatcherPtr next;
21848
21849 while (matcher != NULL) {
21850 next = matcher->next;
21851 if (matcher->keySeqs != NULL) {
21852 int i;
21853 for (i = 0; i < matcher->sizeKeySeqs; i++)
21854 if (matcher->keySeqs[i] != NULL)
21855 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021856 xmlFree(matcher->keySeqs);
21857 }
21858 if (matcher->targets != NULL) {
21859 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21860 int i;
21861 xmlSchemaPSVIIDCNodePtr idcNode;
21862 /*
21863 * Node-table items for keyrefs are not stored globally
21864 * to the validation context, since they are not bubbled.
21865 * We need to free them here.
21866 */
21867 for (i = 0; i < matcher->targets->nbItems; i++) {
21868 idcNode =
21869 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21870 xmlFree(idcNode->keys);
21871 xmlFree(idcNode);
21872 }
21873 }
21874 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021875 }
21876 xmlFree(matcher);
21877 matcher = next;
21878 }
21879}
21880
21881/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021882 * xmlSchemaIDCAddStateObject:
21883 * @vctxt: the WXS validation context
21884 * @matcher: the IDC matcher
21885 * @sel: the XPath information
21886 * @parent: the parent "selector" state object if any
21887 * @type: "selector" or "field"
21888 *
21889 * Creates/reuses and activates state objects for the given
21890 * XPath information; if the XPath expression consists of unions,
21891 * multiple state objects are created for every unioned expression.
21892 *
21893 * Returns 0 on success and -1 on internal errors.
21894 */
21895static int
21896xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21897 xmlSchemaIDCMatcherPtr matcher,
21898 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021899 int type)
21900{
21901 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021902
21903 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021904 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021905 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021906 if (vctxt->xpathStatePool != NULL) {
21907 sto = vctxt->xpathStatePool;
21908 vctxt->xpathStatePool = sto->next;
21909 sto->next = NULL;
21910 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021911 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021912 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021913 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021914 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21915 if (sto == NULL) {
21916 xmlSchemaVErrMemory(NULL,
21917 "allocating an IDC state object", NULL);
21918 return (-1);
21919 }
21920 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
21921 }
21922 /*
21923 * Add to global list.
21924 */
21925 if (vctxt->xpathStates != NULL)
21926 sto->next = vctxt->xpathStates;
21927 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021928
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021929 /*
21930 * Free the old xpath validation context.
21931 */
21932 if (sto->xpathCtxt != NULL)
21933 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
21934
21935 /*
21936 * Create a new XPath (pattern) validation context.
21937 */
21938 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
21939 (xmlPatternPtr) sel->xpathComp);
21940 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021941 VERROR_INT("xmlSchemaIDCAddStateObject",
21942 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021943 return (-1);
21944 }
21945 sto->type = type;
21946 sto->depth = vctxt->depth;
21947 sto->matcher = matcher;
21948 sto->sel = sel;
21949 sto->nbHistory = 0;
21950
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021951#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021952 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
21953 sto->sel->xpath);
21954#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021955 return (0);
21956}
21957
21958/**
21959 * xmlSchemaXPathEvaluate:
21960 * @vctxt: the WXS validation context
21961 * @nodeType: the nodeType of the current node
21962 *
21963 * Evaluates all active XPath state objects.
21964 *
21965 * Returns the number of IC "field" state objects which resolved to
21966 * this node, 0 if none resolved and -1 on internal errors.
21967 */
21968static int
21969xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021970 xmlElementType nodeType)
21971{
21972 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021973 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021974
21975 if (vctxt->xpathStates == NULL)
21976 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021977
21978 if (nodeType == XML_ATTRIBUTE_NODE)
21979 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021980#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021981 {
21982 xmlChar *str = NULL;
21983 xmlGenericError(xmlGenericErrorContext,
21984 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021985 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
21986 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021987 FREE_AND_NULL(str)
21988 }
21989#endif
21990 /*
21991 * Process all active XPath state objects.
21992 */
21993 first = vctxt->xpathStates;
21994 sto = first;
21995 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021996#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021997 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021998 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
21999 sto->matcher->aidc->def->name, sto->sel->xpath);
22000 else
22001 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22002 sto->matcher->aidc->def->name, sto->sel->xpath);
22003#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022004 if (nodeType == XML_ELEMENT_NODE)
22005 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022006 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022007 else
22008 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022009 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022010
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022011 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022012 VERROR_INT("xmlSchemaXPathEvaluate",
22013 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022014 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022015 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022016 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022017 goto next_sto;
22018 /*
22019 * Full match.
22020 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022021#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022022 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022023 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022024#endif
22025 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022026 * Register a match in the state object history.
22027 */
22028 if (sto->history == NULL) {
22029 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22030 if (sto->history == NULL) {
22031 xmlSchemaVErrMemory(NULL,
22032 "allocating the state object history", NULL);
22033 return(-1);
22034 }
22035 sto->sizeHistory = 10;
22036 } else if (sto->sizeHistory <= sto->nbHistory) {
22037 sto->sizeHistory *= 2;
22038 sto->history = (int *) xmlRealloc(sto->history,
22039 sto->sizeHistory * sizeof(int));
22040 if (sto->history == NULL) {
22041 xmlSchemaVErrMemory(NULL,
22042 "re-allocating the state object history", NULL);
22043 return(-1);
22044 }
22045 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022046 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022047
22048#ifdef DEBUG_IDC
22049 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22050 vctxt->depth);
22051#endif
22052
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022053 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22054 xmlSchemaIDCSelectPtr sel;
22055 /*
22056 * Activate state objects for the IDC fields of
22057 * the IDC selector.
22058 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022059#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022060 xmlGenericError(xmlGenericErrorContext, "IDC: "
22061 "activating field states\n");
22062#endif
22063 sel = sto->matcher->aidc->def->fields;
22064 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022065 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22066 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22067 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022068 sel = sel->next;
22069 }
22070 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22071 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022072 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022073 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022074#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022075 xmlGenericError(xmlGenericErrorContext,
22076 "IDC: key found\n");
22077#endif
22078 /*
22079 * Notify that the character value of this node is
22080 * needed.
22081 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022082 if (resolved == 0) {
22083 if ((vctxt->inode->flags &
22084 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22085 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22086 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022087 resolved++;
22088 }
22089next_sto:
22090 if (sto->next == NULL) {
22091 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022092 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022093 */
22094 head = first;
22095 sto = vctxt->xpathStates;
22096 } else
22097 sto = sto->next;
22098 }
22099 return (resolved);
22100}
22101
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022102static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022103xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022104 xmlChar **buf,
22105 xmlSchemaPSVIIDCKeyPtr *seq,
22106 int count)
22107{
22108 int i, res;
22109 const xmlChar *value = NULL;
22110
22111 *buf = xmlStrdup(BAD_CAST "[");
22112 for (i = 0; i < count; i++) {
22113 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022114 res = xmlSchemaGetCanonValueWhtsp(seq[i]->val, &value,
22115 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type));
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022116 if (res == 0)
22117 *buf = xmlStrcat(*buf, value);
22118 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022119 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22120 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022121 *buf = xmlStrcat(*buf, BAD_CAST "???");
22122 }
22123 if (i < count -1)
22124 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22125 else
22126 *buf = xmlStrcat(*buf, BAD_CAST "'");
22127 if (value != NULL) {
22128 xmlFree((xmlChar *) value);
22129 value = NULL;
22130 }
22131 }
22132 *buf = xmlStrcat(*buf, BAD_CAST "]");
22133
22134 return (BAD_CAST *buf);
22135}
22136
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022137/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022138 * xmlSchemaXPathPop:
22139 * @vctxt: the WXS validation context
22140 *
22141 * Pops all XPath states.
22142 *
22143 * Returns 0 on success and -1 on internal errors.
22144 */
22145static int
22146xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22147{
22148 xmlSchemaIDCStateObjPtr sto;
22149 int res;
22150
22151 if (vctxt->xpathStates == NULL)
22152 return(0);
22153 sto = vctxt->xpathStates;
22154 do {
22155 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22156 if (res == -1)
22157 return (-1);
22158 sto = sto->next;
22159 } while (sto != NULL);
22160 return(0);
22161}
22162
22163/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022164 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022165 * @vctxt: the WXS validation context
22166 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022167 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022168 *
22169 * Processes and pops the history items of the IDC state objects.
22170 * IDC key-sequences are validated/created on IDC bindings.
22171 *
22172 * Returns 0 on success and -1 on internal errors.
22173 */
22174static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022175xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022176 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022177{
22178 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022179 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022180 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022181 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022182
22183 if (vctxt->xpathStates == NULL)
22184 return (0);
22185 sto = vctxt->xpathStates;
22186
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022187#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022188 {
22189 xmlChar *str = NULL;
22190 xmlGenericError(xmlGenericErrorContext,
22191 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022192 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22193 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022194 FREE_AND_NULL(str)
22195 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022196#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022197 /*
22198 * Evaluate the state objects.
22199 */
22200 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022201 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22202 if (res == -1) {
22203 VERROR_INT("xmlSchemaXPathProcessHistory",
22204 "calling xmlStreamPop()");
22205 return (-1);
22206 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022207#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022208 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22209 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022210#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022211 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022212 goto deregister_check;
22213
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022214 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022215
22216 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022217 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022218 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022219 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022220 sto = sto->next;
22221 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022222 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022223 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022224 /*
22225 * NOTE: According to
22226 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22227 * ... the simple-content of complex types is also allowed.
22228 */
22229
22230 if (WXS_IS_COMPLEX(type)) {
22231 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22232 /*
22233 * Sanity check for complex types with simple content.
22234 */
22235 simpleType = type->contentTypeDef;
22236 if (simpleType == NULL) {
22237 VERROR_INT("xmlSchemaXPathProcessHistory",
22238 "field resolves to a CT with simple content "
22239 "but the CT is missing the ST definition");
22240 return (-1);
22241 }
22242 } else
22243 simpleType = NULL;
22244 } else
22245 simpleType = type;
22246 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022247 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022248
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022249 /*
22250 * Not qualified if the field resolves to a node of non
22251 * simple type.
22252 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022253 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022254 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022255 WXS_BASIC_CAST sto->matcher->aidc->def,
22256 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022257 "non-simple type",
22258 sto->sel->xpath,
22259 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22260 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022261 sto->nbHistory--;
22262 goto deregister_check;
22263 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022264
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022265 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022266 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022267 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022268 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022269 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022270 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022271 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022272 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022273 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022274 sto->nbHistory--;
22275 goto deregister_check;
22276 } else {
22277 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22278 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022279 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022280
22281 /*
22282 * The key will be anchored on the matcher's list of
22283 * key-sequences. The position in this list is determined
22284 * by the target node's depth relative to the matcher's
22285 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022286 *
22287 * Element Depth Pos List-entries
22288 * <scope> 0 NULL
22289 * <bar> 1 NULL
22290 * <target/> 2 2 target
22291 * <bar>
22292 * </scope>
22293 *
22294 * The size of the list is only dependant on the depth of
22295 * the tree.
22296 * An entry will be NULLed in selector_leave, i.e. when
22297 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022298 */
22299 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022300 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022301
22302 /*
22303 * Create/grow the array of key-sequences.
22304 */
22305 if (matcher->keySeqs == NULL) {
22306 if (pos > 9)
22307 matcher->sizeKeySeqs = pos * 2;
22308 else
22309 matcher->sizeKeySeqs = 10;
22310 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22311 xmlMalloc(matcher->sizeKeySeqs *
22312 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22313 if (matcher->keySeqs == NULL) {
22314 xmlSchemaVErrMemory(NULL,
22315 "allocating an array of key-sequences",
22316 NULL);
22317 return(-1);
22318 }
22319 memset(matcher->keySeqs, 0,
22320 matcher->sizeKeySeqs *
22321 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22322 } else if (pos >= matcher->sizeKeySeqs) {
22323 int i = matcher->sizeKeySeqs;
22324
22325 matcher->sizeKeySeqs *= 2;
22326 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22327 xmlRealloc(matcher->keySeqs,
22328 matcher->sizeKeySeqs *
22329 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022330 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022331 xmlSchemaVErrMemory(NULL,
22332 "reallocating an array of key-sequences",
22333 NULL);
22334 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022335 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022336 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022337 * The array needs to be NULLed.
22338 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022339 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022340 for (; i < matcher->sizeKeySeqs; i++)
22341 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022342 }
22343
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022344 /*
22345 * Get/create the key-sequence.
22346 */
22347 keySeq = matcher->keySeqs[pos];
22348 if (keySeq == NULL) {
22349 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022350 } else if (keySeq[idx] != NULL) {
22351 xmlChar *str = NULL;
22352 /*
22353 * cvc-identity-constraint:
22354 * 3 For each node in the ·target node set· all
22355 * of the {fields}, with that node as the context
22356 * node, evaluate to either an empty node-set or
22357 * a node-set with exactly one member, which must
22358 * have a simple type.
22359 *
22360 * The key was already set; report an error.
22361 */
22362 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22363 XML_SCHEMAV_CVC_IDC, NULL,
22364 WXS_BASIC_CAST matcher->aidc->def,
22365 "The XPath '%s' of a field of %s evaluates to a "
22366 "node-set with more than one member",
22367 sto->sel->xpath,
22368 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22369 FREE_AND_NULL(str);
22370 sto->nbHistory--;
22371 goto deregister_check;
22372 } else
22373 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022374
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022375create_sequence:
22376 /*
22377 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022378 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022379 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22380 matcher->aidc->def->nbFields *
22381 sizeof(xmlSchemaPSVIIDCKeyPtr));
22382 if (keySeq == NULL) {
22383 xmlSchemaVErrMemory(NULL,
22384 "allocating an IDC key-sequence", NULL);
22385 return(-1);
22386 }
22387 memset(keySeq, 0, matcher->aidc->def->nbFields *
22388 sizeof(xmlSchemaPSVIIDCKeyPtr));
22389 matcher->keySeqs[pos] = keySeq;
22390create_key:
22391 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022392 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022393 */
22394 if (key == NULL) {
22395 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22396 sizeof(xmlSchemaPSVIIDCKey));
22397 if (key == NULL) {
22398 xmlSchemaVErrMemory(NULL,
22399 "allocating a IDC key", NULL);
22400 xmlFree(keySeq);
22401 matcher->keySeqs[pos] = NULL;
22402 return(-1);
22403 }
22404 /*
22405 * Consume the compiled value.
22406 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022407 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022408 key->val = vctxt->inode->val;
22409 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022410 /*
22411 * Store the key in a global list.
22412 */
22413 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22414 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022415 return (-1);
22416 }
22417 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022418 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022419 }
22420 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022421
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022422 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022423 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022424 xmlSchemaPSVIIDCNodePtr ntItem;
22425 xmlSchemaIDCMatcherPtr matcher;
22426 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022427 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022428 int pos, i, j, nbKeys;
22429 /*
22430 * Here we have the following scenario:
22431 * An IDC 'selector' state object resolved to a target node,
22432 * during the time this target node was in the
22433 * ancestor-or-self axis, the 'field' state object(s) looked
22434 * out for matching nodes to create a key-sequence for this
22435 * target node. Now we are back to this target node and need
22436 * to put the key-sequence, together with the target node
22437 * itself, into the node-table of the corresponding IDC
22438 * binding.
22439 */
22440 matcher = sto->matcher;
22441 idc = matcher->aidc->def;
22442 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022443 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022444 /*
22445 * Check if the matcher has any key-sequences at all, plus
22446 * if it has a key-sequence for the current target node.
22447 */
22448 if ((matcher->keySeqs == NULL) ||
22449 (matcher->sizeKeySeqs <= pos)) {
22450 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22451 goto selector_key_error;
22452 else
22453 goto selector_leave;
22454 }
22455
22456 keySeq = &(matcher->keySeqs[pos]);
22457 if (*keySeq == NULL) {
22458 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22459 goto selector_key_error;
22460 else
22461 goto selector_leave;
22462 }
22463
22464 for (i = 0; i < nbKeys; i++) {
22465 if ((*keySeq)[i] == NULL) {
22466 /*
22467 * Not qualified, if not all fields did resolve.
22468 */
22469 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22470 /*
22471 * All fields of a "key" IDC must resolve.
22472 */
22473 goto selector_key_error;
22474 }
22475 goto selector_leave;
22476 }
22477 }
22478 /*
22479 * All fields did resolve.
22480 */
22481
22482 /*
22483 * 4.1 If the {identity-constraint category} is unique(/key),
22484 * then no two members of the ·qualified node set· have
22485 * ·key-sequences· whose members are pairwise equal, as
22486 * defined by Equal in [XML Schemas: Datatypes].
22487 *
22488 * Get the IDC binding from the matcher and check for
22489 * duplicate key-sequences.
22490 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022491#if 0
22492 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22493#endif
22494 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022495 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022496 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022497 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022498
22499 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022500 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022501 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022502 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022503 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022504 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022505 bkeySeq =
22506 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022507 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022508 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022509 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022510 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022511 if (res == -1) {
22512 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022513 } else if (res == 0) {
22514 /*
22515 * One of the keys differs, so the key-sequence
22516 * won't be equal; get out.
22517 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022518 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022519 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022520 }
22521 if (res == 1) {
22522 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022523 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022524 */
22525 break;
22526 }
22527 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022528 } while (i < targets->nbItems);
22529 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022530 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022531 /*
22532 * TODO: Try to report the key-sequence.
22533 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022534 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022535 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022536 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022537 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022538 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022539 (*keySeq), nbKeys),
22540 xmlSchemaGetIDCDesignation(&strB, idc));
22541 FREE_AND_NULL(str);
22542 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022543 goto selector_leave;
22544 }
22545 }
22546 /*
22547 * Add a node-table item to the IDC binding.
22548 */
22549 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22550 sizeof(xmlSchemaPSVIIDCNode));
22551 if (ntItem == NULL) {
22552 xmlSchemaVErrMemory(NULL,
22553 "allocating an IDC node-table item", NULL);
22554 xmlFree(*keySeq);
22555 *keySeq = NULL;
22556 return(-1);
22557 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022558 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022559
22560 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022561 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022562 */
22563 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22564 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22565 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022566 xmlFree(*keySeq);
22567 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022568 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022569 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022570 ntItem->nodeQNameID = -1;
22571 } else {
22572 /*
22573 * Save a cached QName for this node on the IDC node, to be
22574 * able to report it, even if the node is not saved.
22575 */
22576 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22577 vctxt->inode->localName, vctxt->inode->nsName);
22578 if (ntItem->nodeQNameID == -1) {
22579 xmlFree(ntItem);
22580 xmlFree(*keySeq);
22581 *keySeq = NULL;
22582 return (-1);
22583 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022584 }
22585 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022586 * Init the node-table item: Save the node, position and
22587 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022588 */
22589 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022590 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022591 ntItem->keys = *keySeq;
22592 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022593#if 0
22594 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22595#endif
22596 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022597 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22598 /*
22599 * Free the item, since keyref items won't be
22600 * put on a global list.
22601 */
22602 xmlFree(ntItem->keys);
22603 xmlFree(ntItem);
22604 }
22605 return (-1);
22606 }
22607
22608 goto selector_leave;
22609selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022610 {
22611 xmlChar *str = NULL;
22612 /*
22613 * 4.2.1 (KEY) The ·target node set· and the
22614 * ·qualified node set· are equal, that is, every
22615 * member of the ·target node set· is also a member
22616 * of the ·qualified node set· and vice versa.
22617 */
22618 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22619 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022620 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022621 "Not all fields of %s evaluate to a node",
22622 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22623 FREE_AND_NULL(str);
22624 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022625selector_leave:
22626 /*
22627 * Free the key-sequence if not added to the IDC table.
22628 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022629 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022630 xmlFree(*keySeq);
22631 *keySeq = NULL;
22632 }
22633 } /* if selector */
22634
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022635 sto->nbHistory--;
22636
22637deregister_check:
22638 /*
22639 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022640 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022641 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022642#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022643 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22644 sto->sel->xpath);
22645#endif
22646 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022647 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022648 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022649 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022650 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022651 nextsto = sto->next;
22652 /*
22653 * Unlink from the list of active XPath state objects.
22654 */
22655 vctxt->xpathStates = sto->next;
22656 sto->next = vctxt->xpathStatePool;
22657 /*
22658 * Link it to the pool of reusable state objects.
22659 */
22660 vctxt->xpathStatePool = sto;
22661 sto = nextsto;
22662 } else
22663 sto = sto->next;
22664 } /* while (sto != NULL) */
22665 return (0);
22666}
22667
22668/**
22669 * xmlSchemaIDCRegisterMatchers:
22670 * @vctxt: the WXS validation context
22671 * @elemDecl: the element declaration
22672 *
22673 * Creates helper objects to evaluate IDC selectors/fields
22674 * successively.
22675 *
22676 * Returns 0 if OK and -1 on internal errors.
22677 */
22678static int
22679xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22680 xmlSchemaElementPtr elemDecl)
22681{
22682 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22683 xmlSchemaIDCPtr idc, refIdc;
22684 xmlSchemaIDCAugPtr aidc;
22685
22686 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22687 if (idc == NULL)
22688 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022689
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022690#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022691 {
22692 xmlChar *str = NULL;
22693 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022694 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022695 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22696 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022697 FREE_AND_NULL(str)
22698 }
22699#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022700 if (vctxt->inode->idcMatchers != NULL) {
22701 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22702 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022703 return (-1);
22704 }
22705 do {
22706 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22707 /*
22708 * Since IDCs bubbles are expensive we need to know the
22709 * depth at which the bubbles should stop; this will be
22710 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022711 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022712 * be -1, indicating that no bubbles are needed.
22713 */
22714 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22715 if (refIdc != NULL) {
22716 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022717 * Remember that we have keyrefs on this node.
22718 */
22719 vctxt->inode->hasKeyrefs = 1;
22720 /*
22721 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022722 */
22723 aidc = vctxt->aidcs;
22724 while (aidc != NULL) {
22725 if (aidc->def == refIdc)
22726 break;
22727 aidc = aidc->next;
22728 }
22729 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022730 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022731 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022732 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022733 return (-1);
22734 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022735 if ((aidc->keyrefDepth == -1) ||
22736 (vctxt->depth < aidc->keyrefDepth))
22737 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022738 }
22739 }
22740 /*
22741 * Lookup the augmented IDC item for the IDC definition.
22742 */
22743 aidc = vctxt->aidcs;
22744 while (aidc != NULL) {
22745 if (aidc->def == idc)
22746 break;
22747 aidc = aidc->next;
22748 }
22749 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022750 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22751 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022752 return (-1);
22753 }
22754 /*
22755 * Create an IDC matcher for every IDC definition.
22756 */
22757 matcher = (xmlSchemaIDCMatcherPtr)
22758 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22759 if (matcher == NULL) {
22760 xmlSchemaVErrMemory(vctxt,
22761 "allocating an IDC matcher", NULL);
22762 return (-1);
22763 }
22764 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22765 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022766 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022767 else
22768 last->next = matcher;
22769 last = matcher;
22770
22771 matcher->type = IDC_MATCHER;
22772 matcher->depth = vctxt->depth;
22773 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022774 matcher->idcType = aidc->def->type;
22775#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022776 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22777#endif
22778 /*
22779 * Init the automaton state object.
22780 */
22781 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022782 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022783 return (-1);
22784
22785 idc = idc->next;
22786 } while (idc != NULL);
22787 return (0);
22788}
22789
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022790static int
22791xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22792 xmlSchemaNodeInfoPtr ielem)
22793{
22794 xmlSchemaPSVIIDCBindingPtr bind;
22795 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22796 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22797 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22798
22799 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22800 /* vctxt->createIDCNodeTables */
22801 while (matcher != NULL) {
22802 /*
22803 * Skip keyref IDCs and empty IDC target-lists.
22804 */
22805 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22806 WXS_ILIST_IS_EMPTY(matcher->targets))
22807 {
22808 matcher = matcher->next;
22809 continue;
22810 }
22811 /*
22812 * If we _want_ the IDC node-table to be created in any case
22813 * then do so. Otherwise create them only if keyrefs need them.
22814 */
22815 if ((! vctxt->createIDCNodeTables) &&
22816 ((matcher->aidc->keyrefDepth == -1) ||
22817 (matcher->aidc->keyrefDepth > vctxt->depth)))
22818 {
22819 matcher = matcher->next;
22820 continue;
22821 }
22822 /*
22823 * Get/create the IDC binding on this element for the IDC definition.
22824 */
22825 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22826
22827 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22828 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22829 nbDupls = bind->dupls->nbItems;
22830 } else {
22831 dupls = NULL;
22832 nbDupls = 0;
22833 }
22834 if (bind->nodeTable != NULL) {
22835 nbNodeTable = bind->nbNodes;
22836 } else {
22837 nbNodeTable = 0;
22838 }
22839
22840 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22841 /*
22842 * Transfer all IDC target-nodes to the IDC node-table.
22843 */
22844 bind->nodeTable =
22845 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22846 bind->sizeNodes = matcher->targets->sizeItems;
22847 bind->nbNodes = matcher->targets->nbItems;
22848
22849 matcher->targets->items = NULL;
22850 matcher->targets->sizeItems = 0;
22851 matcher->targets->nbItems = 0;
22852 } else {
22853 /*
22854 * Compare the key-sequences and add to the IDC node-table.
22855 */
22856 nbTargets = matcher->targets->nbItems;
22857 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22858 nbFields = matcher->aidc->def->nbFields;
22859 i = 0;
22860 do {
22861 keys = targets[i]->keys;
22862 if (nbDupls) {
22863 /*
22864 * Search in already found duplicates first.
22865 */
22866 j = 0;
22867 do {
22868 if (nbFields == 1) {
22869 res = xmlSchemaAreValuesEqual(keys[0]->val,
22870 dupls[j]->keys[0]->val);
22871 if (res == -1)
22872 goto internal_error;
22873 if (res == 1) {
22874 /*
22875 * Equal key-sequence.
22876 */
22877 goto next_target;
22878 }
22879 } else {
22880 res = 0;
22881 ntkeys = dupls[j]->keys;
22882 for (k = 0; k < nbFields; k++) {
22883 res = xmlSchemaAreValuesEqual(keys[k]->val,
22884 ntkeys[k]->val);
22885 if (res == -1)
22886 goto internal_error;
22887 if (res == 0) {
22888 /*
22889 * One of the keys differs.
22890 */
22891 break;
22892 }
22893 }
22894 if (res == 1) {
22895 /*
22896 * Equal key-sequence found.
22897 */
22898 goto next_target;
22899 }
22900 }
22901 j++;
22902 } while (j < nbDupls);
22903 }
22904 if (nbNodeTable) {
22905 j = 0;
22906 do {
22907 if (nbFields == 1) {
22908 res = xmlSchemaAreValuesEqual(keys[0]->val,
22909 bind->nodeTable[j]->keys[0]->val);
22910 if (res == -1)
22911 goto internal_error;
22912 if (res == 0) {
22913 /*
22914 * The key-sequence differs.
22915 */
22916 goto next_node_table_entry;
22917 }
22918 } else {
22919 res = 0;
22920 ntkeys = bind->nodeTable[j]->keys;
22921 for (k = 0; k < nbFields; k++) {
22922 res = xmlSchemaAreValuesEqual(keys[k]->val,
22923 ntkeys[k]->val);
22924 if (res == -1)
22925 goto internal_error;
22926 if (res == 0) {
22927 /*
22928 * One of the keys differs.
22929 */
22930 goto next_node_table_entry;
22931 }
22932 }
22933 }
22934 /*
22935 * Add the duplicate to the list of duplicates.
22936 */
22937 if (bind->dupls == NULL) {
22938 bind->dupls = xmlSchemaItemListCreate();
22939 if (bind->dupls == NULL)
22940 goto internal_error;
22941 }
22942 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
22943 goto internal_error;
22944 /*
22945 * Remove the duplicate entry from the IDC node-table.
22946 */
22947 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
22948 bind->nbNodes--;
22949
22950 goto next_target;
22951
22952next_node_table_entry:
22953 j++;
22954 } while (j < nbNodeTable);
22955 }
22956 /*
22957 * If everything is fine, then add the IDC target-node to
22958 * the IDC node-table.
22959 */
22960 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
22961 goto internal_error;
22962
22963next_target:
22964 i++;
22965 } while (i < nbTargets);
22966 }
22967 matcher = matcher->next;
22968 }
22969 return(0);
22970
22971internal_error:
22972 return(-1);
22973}
22974
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022975/**
22976 * xmlSchemaBubbleIDCNodeTables:
22977 * @depth: the current tree depth
22978 *
22979 * Merges IDC bindings of an element at @depth into the corresponding IDC
22980 * bindings of its parent element. If a duplicate note-table entry is found,
22981 * both, the parent node-table entry and child entry are discarded from the
22982 * node-table of the parent.
22983 *
22984 * Returns 0 if OK and -1 on internal errors.
22985 */
22986static int
22987xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
22988{
22989 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022990 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
22991 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022992 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022993 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022994
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022995 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022996 if (bind == NULL) {
22997 /* Fine, no table, no bubbles. */
22998 return (0);
22999 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023000
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023001 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23002 /*
23003 * Walk all bindings; create new or add to existing bindings.
23004 * Remove duplicate key-sequences.
23005 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023006 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023007
23008 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23009 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023010 /*
23011 * Check if the key/unique IDC table needs to be bubbled.
23012 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023013 if (! vctxt->createIDCNodeTables) {
23014 aidc = vctxt->aidcs;
23015 do {
23016 if (aidc->def == bind->definition) {
23017 if ((aidc->keyrefDepth == -1) ||
23018 (aidc->keyrefDepth >= vctxt->depth)) {
23019 goto next_binding;
23020 }
23021 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023022 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023023 aidc = aidc->next;
23024 } while (aidc != NULL);
23025 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023026
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023027 if (parTable != NULL)
23028 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023029 /*
23030 * Search a matching parent binding for the
23031 * IDC definition.
23032 */
23033 while (parBind != NULL) {
23034 if (parBind->definition == bind->definition)
23035 break;
23036 parBind = parBind->next;
23037 }
23038
23039 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023040 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023041 * Compare every node-table entry of the child node,
23042 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023043 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023044 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023045
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023046 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23047 oldDupls = parBind->dupls->nbItems;
23048 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23049 } else {
23050 dupls = NULL;
23051 oldDupls = 0;
23052 }
23053
23054 parNodes = parBind->nodeTable;
23055 nbFields = bind->definition->nbFields;
23056
23057 for (i = 0; i < bind->nbNodes; i++) {
23058 node = bind->nodeTable[i];
23059 if (node == NULL)
23060 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023061 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023062 * ...with every key-sequence of the parent node, already
23063 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023064 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023065 if (oldDupls) {
23066 j = 0;
23067 while (j < oldDupls) {
23068 if (nbFields == 1) {
23069 ret = xmlSchemaAreValuesEqual(
23070 node->keys[0]->val,
23071 dupls[j]->keys[0]->val);
23072 if (ret == -1)
23073 goto internal_error;
23074 if (ret == 0) {
23075 j++;
23076 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023077 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023078 } else {
23079 parNode = dupls[j];
23080 for (k = 0; k < nbFields; k++) {
23081 ret = xmlSchemaAreValuesEqual(
23082 node->keys[k]->val,
23083 parNode->keys[k]->val);
23084 if (ret == -1)
23085 goto internal_error;
23086 if (ret == 0)
23087 break;
23088 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023089 }
23090 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023091 /* Duplicate found. */
23092 break;
23093 j++;
23094 }
23095 if (j != oldDupls) {
23096 /* Duplicate found. Skip this entry. */
23097 continue;
23098 }
23099 }
23100 /*
23101 * ... and with every key-sequence of the parent node.
23102 */
23103 if (oldNum) {
23104 j = 0;
23105 while (j < oldNum) {
23106 if (nbFields == 1) {
23107 ret = xmlSchemaAreValuesEqual(
23108 node->keys[0]->val,
23109 parNodes[j]->keys[0]->val);
23110 if (ret == -1)
23111 goto internal_error;
23112 if (ret == 0) {
23113 j++;
23114 continue;
23115 }
23116 } else {
23117 parNode = parNodes[j];
23118 for (k = 0; k < nbFields; k++) {
23119 ret = xmlSchemaAreValuesEqual(
23120 node->keys[k]->val,
23121 parNode->keys[k]->val);
23122 if (ret == -1)
23123 goto internal_error;
23124 if (ret == 0)
23125 break;
23126 }
23127 }
23128 if (ret == 1)
23129 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023130 break;
23131 j++;
23132 }
23133 if (j != oldNum) {
23134 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023135 * Handle duplicates. Move the duplicate in
23136 * the parent's node-table to the list of
23137 * duplicates.
23138 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023139 oldNum--;
23140 parBind->nbNodes--;
23141 /*
23142 * Move last old item to pos of duplicate.
23143 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023144 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023145
23146 if (parBind->nbNodes != oldNum) {
23147 /*
23148 * If new items exist, move last new item to
23149 * last of old items.
23150 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023151 parNodes[oldNum] =
23152 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023153 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023154 if (parBind->dupls == NULL) {
23155 parBind->dupls = xmlSchemaItemListCreate();
23156 if (parBind->dupls == NULL)
23157 goto internal_error;
23158 }
23159 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023160 } else {
23161 /*
23162 * Add the node-table entry (node and key-sequence) of
23163 * the child node to the node table of the parent node.
23164 */
23165 if (parBind->nodeTable == NULL) {
23166 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023167 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023168 if (parBind->nodeTable == NULL) {
23169 xmlSchemaVErrMemory(NULL,
23170 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023171 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023172 }
23173 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023174 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023175 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023176 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23177 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23178 sizeof(xmlSchemaPSVIIDCNodePtr));
23179 if (parBind->nodeTable == NULL) {
23180 xmlSchemaVErrMemory(NULL,
23181 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023182 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023183 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023184 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023185 /*
23186 * Append the new node-table entry to the 'new node-table
23187 * entries' section.
23188 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023189 parBind->nodeTable[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023190 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023191
23192 }
23193
23194 }
23195 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023196 /*
23197 * No binding for the IDC was found: create a new one and
23198 * copy all node-tables.
23199 */
23200 parBind = xmlSchemaIDCNewBinding(bind->definition);
23201 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023202 goto internal_error;
23203
23204 /*
23205 * TODO: Hmm, how to optimize the initial number of
23206 * allocated entries?
23207 */
23208 if (bind->nbNodes != 0) {
23209 /*
23210 * Add all IDC node-table entries.
23211 */
23212 if (! vctxt->psviExposeIDCNodeTables) {
23213 /*
23214 * Just move the entries.
23215 * NOTE: this is quite save here, since
23216 * all the keyref lookups have already been
23217 * performed.
23218 */
23219 parBind->nodeTable = bind->nodeTable;
23220 bind->nodeTable = NULL;
23221 parBind->sizeNodes = bind->sizeNodes;
23222 bind->sizeNodes = 0;
23223 parBind->nbNodes = bind->nbNodes;
23224 bind->nbNodes = 0;
23225 } else {
23226 /*
23227 * Copy the entries.
23228 */
23229 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23230 xmlMalloc(bind->nbNodes *
23231 sizeof(xmlSchemaPSVIIDCNodePtr));
23232 if (parBind->nodeTable == NULL) {
23233 xmlSchemaVErrMemory(NULL,
23234 "allocating an array of IDC node-table "
23235 "items", NULL);
23236 xmlSchemaIDCFreeBinding(parBind);
23237 goto internal_error;
23238 }
23239 parBind->sizeNodes = bind->nbNodes;
23240 parBind->nbNodes = bind->nbNodes;
23241 memcpy(parBind->nodeTable, bind->nodeTable,
23242 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23243 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023244 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023245 if (bind->dupls) {
23246 /*
23247 * Move the duplicates.
23248 */
23249 if (parBind->dupls != NULL)
23250 xmlSchemaItemListFree(parBind->dupls);
23251 parBind->dupls = bind->dupls;
23252 bind->dupls = NULL;
23253 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023254 if (*parTable == NULL)
23255 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023256 else {
23257 parBind->next = *parTable;
23258 *parTable = parBind;
23259 }
23260 }
23261
23262next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023263 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023264 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023265 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023266
23267internal_error:
23268 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023269}
23270
23271/**
23272 * xmlSchemaCheckCVCIDCKeyRef:
23273 * @vctxt: the WXS validation context
23274 * @elemDecl: the element declaration
23275 *
23276 * Check the cvc-idc-keyref constraints.
23277 */
23278static int
23279xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23280{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023281 xmlSchemaIDCMatcherPtr matcher;
23282 xmlSchemaPSVIIDCBindingPtr bind;
23283
23284 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023285 /*
23286 * Find a keyref.
23287 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023288 while (matcher != NULL) {
23289 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23290 matcher->targets &&
23291 matcher->targets->nbItems)
23292 {
23293 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023294 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023295 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023296
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023297 nbFields = matcher->aidc->def->nbFields;
23298
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023299 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023300 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023301 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023302 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023303 while (bind != NULL) {
23304 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023305 bind->definition)
23306 break;
23307 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023308 }
23309 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023310 /*
23311 * Search for a matching key-sequences.
23312 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023313 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023314 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023315 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023316 if (bind != NULL) {
23317 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023318 for (j = 0; j < bind->nbNodes; j++) {
23319 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023320 for (k = 0; k < nbFields; k++) {
23321 res = xmlSchemaAreValuesEqual(keys[k]->val,
23322 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023323 if (res == 0)
23324 break;
23325 else if (res == -1) {
23326 return (-1);
23327 }
23328 }
23329 if (res == 1) {
23330 /*
23331 * Match found.
23332 */
23333 break;
23334 }
23335 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023336 if ((res == 0) && hasDupls) {
23337 /*
23338 * Search in duplicates
23339 */
23340 for (j = 0; j < bind->dupls->nbItems; j++) {
23341 keys = ((xmlSchemaPSVIIDCNodePtr)
23342 bind->dupls->items[j])->keys;
23343 for (k = 0; k < nbFields; k++) {
23344 res = xmlSchemaAreValuesEqual(keys[k]->val,
23345 refKeys[k]->val);
23346 if (res == 0)
23347 break;
23348 else if (res == -1) {
23349 return (-1);
23350 }
23351 }
23352 if (res == 1) {
23353 /*
23354 * Match in duplicates found.
23355 */
23356 xmlChar *str = NULL, *strB = NULL;
23357 xmlSchemaKeyrefErr(vctxt,
23358 XML_SCHEMAV_CVC_IDC, refNode,
23359 (xmlSchemaTypePtr) matcher->aidc->def,
23360 "More than one match found for "
23361 "key-sequence %s of keyref '%s'",
23362 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23363 refNode->keys, nbFields),
23364 xmlSchemaGetComponentQName(&strB,
23365 matcher->aidc->def));
23366 FREE_AND_NULL(str);
23367 FREE_AND_NULL(strB);
23368 break;
23369 }
23370 }
23371 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023372 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023373
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023374 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023375 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023376 xmlSchemaKeyrefErr(vctxt,
23377 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023378 (xmlSchemaTypePtr) matcher->aidc->def,
23379 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023380 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023381 refNode->keys, nbFields),
23382 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023383 FREE_AND_NULL(str);
23384 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023385 }
23386 }
23387 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023388 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023389 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023390 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023391 return (0);
23392}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023393
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023394/************************************************************************
23395 * *
23396 * XML Reader validation code *
23397 * *
23398 ************************************************************************/
23399
23400static xmlSchemaAttrInfoPtr
23401xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023402{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023403 xmlSchemaAttrInfoPtr iattr;
23404 /*
23405 * Grow/create list of attribute infos.
23406 */
23407 if (vctxt->attrInfos == NULL) {
23408 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23409 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23410 vctxt->sizeAttrInfos = 1;
23411 if (vctxt->attrInfos == NULL) {
23412 xmlSchemaVErrMemory(vctxt,
23413 "allocating attribute info list", NULL);
23414 return (NULL);
23415 }
23416 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23417 vctxt->sizeAttrInfos++;
23418 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23419 xmlRealloc(vctxt->attrInfos,
23420 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23421 if (vctxt->attrInfos == NULL) {
23422 xmlSchemaVErrMemory(vctxt,
23423 "re-allocating attribute info list", NULL);
23424 return (NULL);
23425 }
23426 } else {
23427 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23428 if (iattr->localName != NULL) {
23429 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23430 "attr info not cleared");
23431 return (NULL);
23432 }
23433 iattr->nodeType = XML_ATTRIBUTE_NODE;
23434 return (iattr);
23435 }
23436 /*
23437 * Create an attribute info.
23438 */
23439 iattr = (xmlSchemaAttrInfoPtr)
23440 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23441 if (iattr == NULL) {
23442 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23443 return (NULL);
23444 }
23445 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23446 iattr->nodeType = XML_ATTRIBUTE_NODE;
23447 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23448
23449 return (iattr);
23450}
23451
23452static int
23453xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23454 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023455 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023456 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023457 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023458 int ownedNames,
23459 xmlChar *value,
23460 int ownedValue)
23461{
23462 xmlSchemaAttrInfoPtr attr;
23463
23464 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23465 if (attr == NULL) {
23466 VERROR_INT("xmlSchemaPushAttribute",
23467 "calling xmlSchemaGetFreshAttrInfo()");
23468 return (-1);
23469 }
23470 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023471 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023472 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23473 attr->localName = localName;
23474 attr->nsName = nsName;
23475 if (ownedNames)
23476 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23477 /*
23478 * Evaluate if it's an XSI attribute.
23479 */
23480 if (nsName != NULL) {
23481 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23482 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23483 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23484 }
23485 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23486 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23487 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23488 }
23489 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23490 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23491 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23492 }
23493 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23494 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23495 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23496 }
23497 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23498 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23499 }
23500 }
23501 attr->value = value;
23502 if (ownedValue)
23503 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23504 if (attr->metaType != 0)
23505 attr->state = XML_SCHEMAS_ATTR_META;
23506 return (0);
23507}
23508
23509static void
23510xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23511{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023512 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023513 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23514 FREE_AND_NULL(ielem->localName);
23515 FREE_AND_NULL(ielem->nsName);
23516 } else {
23517 ielem->localName = NULL;
23518 ielem->nsName = NULL;
23519 }
23520 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23521 FREE_AND_NULL(ielem->value);
23522 } else {
23523 ielem->value = NULL;
23524 }
23525 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023526 /*
23527 * PSVI TODO: Be careful not to free it when the value is
23528 * exposed via PSVI.
23529 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023530 xmlSchemaFreeValue(ielem->val);
23531 ielem->val = NULL;
23532 }
23533 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023534 /*
23535 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23536 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023537 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23538 ielem->idcMatchers = NULL;
23539 }
23540 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023541 /*
23542 * OPTIMIZE TODO: Use a pool of IDC tables??.
23543 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023544 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23545 ielem->idcTable = NULL;
23546 }
23547 if (ielem->regexCtxt != NULL) {
23548 xmlRegFreeExecCtxt(ielem->regexCtxt);
23549 ielem->regexCtxt = NULL;
23550 }
23551 if (ielem->nsBindings != NULL) {
23552 xmlFree((xmlChar **)ielem->nsBindings);
23553 ielem->nsBindings = NULL;
23554 ielem->nbNsBindings = 0;
23555 ielem->sizeNsBindings = 0;
23556 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023557}
23558
23559/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023560 * xmlSchemaGetFreshElemInfo:
23561 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023562 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023563 * Creates/reuses and initializes the element info item for
23564 * the currect tree depth.
23565 *
23566 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023567 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023568static xmlSchemaNodeInfoPtr
23569xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023570{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023571 xmlSchemaNodeInfoPtr info = NULL;
23572
23573 if (vctxt->depth > vctxt->sizeElemInfos) {
23574 VERROR_INT("xmlSchemaGetFreshElemInfo",
23575 "inconsistent depth encountered");
23576 return (NULL);
23577 }
23578 if (vctxt->elemInfos == NULL) {
23579 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23580 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23581 if (vctxt->elemInfos == NULL) {
23582 xmlSchemaVErrMemory(vctxt,
23583 "allocating the element info array", NULL);
23584 return (NULL);
23585 }
23586 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23587 vctxt->sizeElemInfos = 10;
23588 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23589 int i = vctxt->sizeElemInfos;
23590
23591 vctxt->sizeElemInfos *= 2;
23592 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23593 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23594 sizeof(xmlSchemaNodeInfoPtr));
23595 if (vctxt->elemInfos == NULL) {
23596 xmlSchemaVErrMemory(vctxt,
23597 "re-allocating the element info array", NULL);
23598 return (NULL);
23599 }
23600 /*
23601 * We need the new memory to be NULLed.
23602 * TODO: Use memset instead?
23603 */
23604 for (; i < vctxt->sizeElemInfos; i++)
23605 vctxt->elemInfos[i] = NULL;
23606 } else
23607 info = vctxt->elemInfos[vctxt->depth];
23608
23609 if (info == NULL) {
23610 info = (xmlSchemaNodeInfoPtr)
23611 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23612 if (info == NULL) {
23613 xmlSchemaVErrMemory(vctxt,
23614 "allocating an element info", NULL);
23615 return (NULL);
23616 }
23617 vctxt->elemInfos[vctxt->depth] = info;
23618 } else {
23619 if (info->localName != NULL) {
23620 VERROR_INT("xmlSchemaGetFreshElemInfo",
23621 "elem info has not been cleared");
23622 return (NULL);
23623 }
23624 }
23625 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23626 info->nodeType = XML_ELEMENT_NODE;
23627 info->depth = vctxt->depth;
23628
23629 return (info);
23630}
23631
23632#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23633#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23634#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23635
23636static int
23637xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23638 xmlNodePtr node,
23639 xmlSchemaTypePtr type,
23640 xmlSchemaValType valType,
23641 const xmlChar * value,
23642 xmlSchemaValPtr val,
23643 unsigned long length,
23644 int fireErrors)
23645{
23646 int ret, error = 0;
23647
23648 xmlSchemaTypePtr tmpType;
23649 xmlSchemaFacetLinkPtr facetLink;
23650 xmlSchemaFacetPtr facet;
23651 unsigned long len = 0;
23652 xmlSchemaWhitespaceValueType ws;
23653
23654 /*
23655 * In Libxml2, derived built-in types have currently no explicit facets.
23656 */
23657 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023658 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023659
23660 /*
23661 * NOTE: Do not jump away, if the facetSet of the given type is
23662 * empty: until now, "pattern" and "enumeration" facets of the
23663 * *base types* need to be checked as well.
23664 */
23665 if (type->facetSet == NULL)
23666 goto pattern_and_enum;
23667
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023668 if (! WXS_IS_ATOMIC(type)) {
23669 if (WXS_IS_LIST(type))
23670 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023671 else
23672 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023673 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023674 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023675 * Whitespace handling is only of importance for string-based
23676 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023677 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023678 tmpType = xmlSchemaGetPrimitiveType(type);
23679 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023680 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023681 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23682 } else
23683 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23684 /*
23685 * If the value was not computed (for string or
23686 * anySimpleType based types), then use the provided
23687 * type.
23688 */
23689 if (val == NULL)
23690 valType = valType;
23691 else
23692 valType = xmlSchemaGetValType(val);
23693
23694 ret = 0;
23695 for (facetLink = type->facetSet; facetLink != NULL;
23696 facetLink = facetLink->next) {
23697 /*
23698 * Skip the pattern "whiteSpace": it is used to
23699 * format the character content beforehand.
23700 */
23701 switch (facetLink->facet->type) {
23702 case XML_SCHEMA_FACET_WHITESPACE:
23703 case XML_SCHEMA_FACET_PATTERN:
23704 case XML_SCHEMA_FACET_ENUMERATION:
23705 continue;
23706 case XML_SCHEMA_FACET_LENGTH:
23707 case XML_SCHEMA_FACET_MINLENGTH:
23708 case XML_SCHEMA_FACET_MAXLENGTH:
23709 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23710 valType, value, val, &len, ws);
23711 break;
23712 default:
23713 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23714 valType, value, val, ws);
23715 break;
23716 }
23717 if (ret < 0) {
23718 AERROR_INT("xmlSchemaValidateFacets",
23719 "validating against a atomic type facet");
23720 return (-1);
23721 } else if (ret > 0) {
23722 if (fireErrors)
23723 xmlSchemaFacetErr(actxt, ret, node,
23724 value, len, type, facetLink->facet, NULL, NULL, NULL);
23725 else
23726 return (ret);
23727 if (error == 0)
23728 error = ret;
23729 }
23730 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023731 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023732
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023733WXS_IS_LIST:
23734 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023735 goto pattern_and_enum;
23736 /*
23737 * "length", "minLength" and "maxLength" of list types.
23738 */
23739 ret = 0;
23740 for (facetLink = type->facetSet; facetLink != NULL;
23741 facetLink = facetLink->next) {
23742
23743 switch (facetLink->facet->type) {
23744 case XML_SCHEMA_FACET_LENGTH:
23745 case XML_SCHEMA_FACET_MINLENGTH:
23746 case XML_SCHEMA_FACET_MAXLENGTH:
23747 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23748 value, length, NULL);
23749 break;
23750 default:
23751 continue;
23752 }
23753 if (ret < 0) {
23754 AERROR_INT("xmlSchemaValidateFacets",
23755 "validating against a list type facet");
23756 return (-1);
23757 } else if (ret > 0) {
23758 if (fireErrors)
23759 xmlSchemaFacetErr(actxt, ret, node,
23760 value, length, type, facetLink->facet, NULL, NULL, NULL);
23761 else
23762 return (ret);
23763 if (error == 0)
23764 error = ret;
23765 }
23766 ret = 0;
23767 }
23768
23769pattern_and_enum:
23770 if (error >= 0) {
23771 int found = 0;
23772 /*
23773 * Process enumerations. Facet values are in the value space
23774 * of the defining type's base type. This seems to be a bug in the
23775 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23776 * Only the first set of enumerations in the ancestor-or-self axis
23777 * is used for validation.
23778 */
23779 ret = 0;
23780 tmpType = type;
23781 do {
23782 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23783 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23784 continue;
23785 found = 1;
23786 ret = xmlSchemaAreValuesEqual(facet->val, val);
23787 if (ret == 1)
23788 break;
23789 else if (ret < 0) {
23790 AERROR_INT("xmlSchemaValidateFacets",
23791 "validating against an enumeration facet");
23792 return (-1);
23793 }
23794 }
23795 if (ret != 0)
23796 break;
23797 tmpType = tmpType->baseType;
23798 } while ((tmpType != NULL) &&
23799 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23800 if (found && (ret == 0)) {
23801 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23802 if (fireErrors) {
23803 xmlSchemaFacetErr(actxt, ret, node,
23804 value, 0, type, NULL, NULL, NULL, NULL);
23805 } else
23806 return (ret);
23807 if (error == 0)
23808 error = ret;
23809 }
23810 }
23811
23812 if (error >= 0) {
23813 int found;
23814 /*
23815 * Process patters. Pattern facets are ORed at type level
23816 * and ANDed if derived. Walk the base type axis.
23817 */
23818 tmpType = type;
23819 facet = NULL;
23820 do {
23821 found = 0;
23822 for (facetLink = tmpType->facetSet; facetLink != NULL;
23823 facetLink = facetLink->next) {
23824 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23825 continue;
23826 found = 1;
23827 /*
23828 * NOTE that for patterns, @value needs to be the
23829 * normalized vaule.
23830 */
23831 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23832 if (ret == 1)
23833 break;
23834 else if (ret < 0) {
23835 AERROR_INT("xmlSchemaValidateFacets",
23836 "validating against a pattern facet");
23837 return (-1);
23838 } else {
23839 /*
23840 * Save the last non-validating facet.
23841 */
23842 facet = facetLink->facet;
23843 }
23844 }
23845 if (found && (ret != 1)) {
23846 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23847 if (fireErrors) {
23848 xmlSchemaFacetErr(actxt, ret, node,
23849 value, 0, type, facet, NULL, NULL, NULL);
23850 } else
23851 return (ret);
23852 if (error == 0)
23853 error = ret;
23854 break;
23855 }
23856 tmpType = tmpType->baseType;
23857 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23858 }
23859
23860 return (error);
23861}
23862
23863static xmlChar *
23864xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23865 const xmlChar *value)
23866{
23867 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23868 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23869 return (xmlSchemaCollapseString(value));
23870 case XML_SCHEMA_WHITESPACE_REPLACE:
23871 return (xmlSchemaWhiteSpaceReplace(value));
23872 default:
23873 return (NULL);
23874 }
23875}
23876
23877static int
23878xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23879 const xmlChar *value,
23880 xmlSchemaValPtr *val,
23881 int valNeeded)
23882{
23883 int ret;
23884 const xmlChar *nsName;
23885 xmlChar *local, *prefix = NULL;
23886
23887 ret = xmlValidateQName(value, 1);
23888 if (ret != 0) {
23889 if (ret == -1) {
23890 VERROR_INT("xmlSchemaValidateQName",
23891 "calling xmlValidateQName()");
23892 return (-1);
23893 }
23894 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23895 }
23896 /*
23897 * NOTE: xmlSplitQName2 will always return a duplicated
23898 * strings.
23899 */
23900 local = xmlSplitQName2(value, &prefix);
23901 if (local == NULL)
23902 local = xmlStrdup(value);
23903 /*
23904 * OPTIMIZE TODO: Use flags for:
23905 * - is there any namespace binding?
23906 * - is there a default namespace?
23907 */
23908 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23909
23910 if (prefix != NULL) {
23911 xmlFree(prefix);
23912 /*
23913 * A namespace must be found if the prefix is
23914 * NOT NULL.
23915 */
23916 if (nsName == NULL) {
23917 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023918 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023919 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023920 "The QName value '%s' has no "
23921 "corresponding namespace declaration in "
23922 "scope", value, NULL);
23923 if (local != NULL)
23924 xmlFree(local);
23925 return (ret);
23926 }
23927 }
23928 if (valNeeded && val) {
23929 if (nsName != NULL)
23930 *val = xmlSchemaNewQNameValue(
23931 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
23932 else
23933 *val = xmlSchemaNewQNameValue(NULL,
23934 BAD_CAST local);
23935 } else
23936 xmlFree(local);
23937 return (0);
23938}
23939
23940/*
23941* cvc-simple-type
23942*/
23943static int
23944xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
23945 xmlNodePtr node,
23946 xmlSchemaTypePtr type,
23947 const xmlChar *value,
23948 xmlSchemaValPtr *retVal,
23949 int fireErrors,
23950 int normalize,
23951 int isNormalized)
23952{
23953 int ret = 0, valNeeded = (retVal) ? 1 : 0;
23954 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023955 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023956 xmlChar *normValue = NULL;
23957
23958#define NORMALIZE(atype) \
23959 if ((! isNormalized) && \
23960 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
23961 normValue = xmlSchemaNormalizeValue(atype, value); \
23962 if (normValue != NULL) \
23963 value = normValue; \
23964 isNormalized = 1; \
23965 }
23966
23967 if ((retVal != NULL) && (*retVal != NULL)) {
23968 xmlSchemaFreeValue(*retVal);
23969 *retVal = NULL;
23970 }
23971 /*
23972 * 3.14.4 Simple Type Definition Validation Rules
23973 * Validation Rule: String Valid
23974 */
23975 /*
23976 * 1 It is schema-valid with respect to that definition as defined
23977 * by Datatype Valid in [XML Schemas: Datatypes].
23978 */
23979 /*
23980 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
23981 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
23982 * the string must be a ·declared entity name·.
23983 */
23984 /*
23985 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
23986 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
23987 * then every whitespace-delimited substring of the string must be a ·declared
23988 * entity name·.
23989 */
23990 /*
23991 * 2.3 otherwise no further condition applies.
23992 */
23993 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
23994 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000023995 if (value == NULL)
23996 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023997 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023998 xmlSchemaTypePtr biType; /* The built-in type. */
23999 /*
24000 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24001 * a literal in the ·lexical space· of {base type definition}"
24002 */
24003 /*
24004 * Whitespace-normalize.
24005 */
24006 NORMALIZE(type);
24007 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24008 /*
24009 * Get the built-in type.
24010 */
24011 biType = type->baseType;
24012 while ((biType != NULL) &&
24013 (biType->type != XML_SCHEMA_TYPE_BASIC))
24014 biType = biType->baseType;
24015
24016 if (biType == NULL) {
24017 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24018 "could not get the built-in type");
24019 goto internal_error;
24020 }
24021 } else
24022 biType = type;
24023 /*
24024 * NOTATIONs need to be processed here, since they need
24025 * to lookup in the hashtable of NOTATION declarations of the schema.
24026 */
24027 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24028 switch (biType->builtInType) {
24029 case XML_SCHEMAS_NOTATION:
24030 ret = xmlSchemaValidateNotation(
24031 (xmlSchemaValidCtxtPtr) actxt,
24032 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24033 NULL, value, &val, valNeeded);
24034 break;
24035 case XML_SCHEMAS_QNAME:
24036 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24037 value, &val, valNeeded);
24038 break;
24039 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024040 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024041 if (valNeeded)
24042 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24043 value, &val, NULL);
24044 else
24045 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24046 value, NULL, NULL);
24047 break;
24048 }
24049 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24050 switch (biType->builtInType) {
24051 case XML_SCHEMAS_NOTATION:
24052 ret = xmlSchemaValidateNotation(NULL,
24053 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24054 value, &val, valNeeded);
24055 break;
24056 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024057 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024058 if (valNeeded)
24059 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24060 value, &val, node);
24061 else
24062 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24063 value, NULL, node);
24064 break;
24065 }
24066 } else {
24067 /*
24068 * Validation via a public API is not implemented yet.
24069 */
24070 TODO
24071 goto internal_error;
24072 }
24073 if (ret != 0) {
24074 if (ret < 0) {
24075 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24076 "validating against a built-in type");
24077 goto internal_error;
24078 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024079 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024080 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24081 else
24082 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24083 }
24084 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24085 /*
24086 * Check facets.
24087 */
24088 ret = xmlSchemaValidateFacets(actxt, node, type,
24089 (xmlSchemaValType) biType->builtInType, value, val,
24090 0, fireErrors);
24091 if (ret != 0) {
24092 if (ret < 0) {
24093 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24094 "validating facets of atomic simple type");
24095 goto internal_error;
24096 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024097 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024098 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24099 else
24100 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24101 }
24102 }
24103 if (fireErrors && (ret > 0))
24104 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024105 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024106
24107 xmlSchemaTypePtr itemType;
24108 const xmlChar *cur, *end;
24109 xmlChar *tmpValue = NULL;
24110 unsigned long len = 0;
24111 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24112 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24113 * of white space separated tokens, each of which ·match·es a literal
24114 * in the ·lexical space· of {item type definition}
24115 */
24116 /*
24117 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24118 * the list type has an enum or pattern facet.
24119 */
24120 NORMALIZE(type);
24121 /*
24122 * VAL TODO: Optimize validation of empty values.
24123 * VAL TODO: We do not have computed values for lists.
24124 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024125 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024126 cur = value;
24127 do {
24128 while (IS_BLANK_CH(*cur))
24129 cur++;
24130 end = cur;
24131 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24132 end++;
24133 if (end == cur)
24134 break;
24135 tmpValue = xmlStrndup(cur, end - cur);
24136 len++;
24137
24138 if (valNeeded)
24139 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24140 tmpValue, &curVal, fireErrors, 0, 1);
24141 else
24142 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24143 tmpValue, NULL, fireErrors, 0, 1);
24144 FREE_AND_NULL(tmpValue);
24145 if (curVal != NULL) {
24146 /*
24147 * Add to list of computed values.
24148 */
24149 if (val == NULL)
24150 val = curVal;
24151 else
24152 xmlSchemaValueAppend(prevVal, curVal);
24153 prevVal = curVal;
24154 curVal = NULL;
24155 }
24156 if (ret != 0) {
24157 if (ret < 0) {
24158 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24159 "validating an item of list simple type");
24160 goto internal_error;
24161 }
24162 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24163 break;
24164 }
24165 cur = end;
24166 } while (*cur != 0);
24167 FREE_AND_NULL(tmpValue);
24168 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24169 /*
24170 * Apply facets (pattern, enumeration).
24171 */
24172 ret = xmlSchemaValidateFacets(actxt, node, type,
24173 XML_SCHEMAS_UNKNOWN, value, val,
24174 len, fireErrors);
24175 if (ret != 0) {
24176 if (ret < 0) {
24177 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24178 "validating facets of list simple type");
24179 goto internal_error;
24180 }
24181 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24182 }
24183 }
24184 if (fireErrors && (ret > 0)) {
24185 /*
24186 * Report the normalized value.
24187 */
24188 normalize = 1;
24189 NORMALIZE(type);
24190 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24191 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024192 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024193 xmlSchemaTypeLinkPtr memberLink;
24194 /*
24195 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24196 * not apply directly; however, the normalization behavior of ·union·
24197 * types is controlled by the value of whiteSpace on that one of the
24198 * ·memberTypes· against which the ·union· is successfully validated.
24199 *
24200 * This means that the value is normalized by the first validating
24201 * member type, then the facets of the union type are applied. This
24202 * needs changing of the value!
24203 */
24204
24205 /*
24206 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24207 * literal in the ·lexical space· of at least one member of
24208 * {member type definitions}
24209 */
24210 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24211 if (memberLink == NULL) {
24212 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24213 "union simple type has no member types");
24214 goto internal_error;
24215 }
24216 /*
24217 * Always normalize union type values, since we currently
24218 * cannot store the whitespace information with the value
24219 * itself; otherwise a later value-comparison would be
24220 * not possible.
24221 */
24222 while (memberLink != NULL) {
24223 if (valNeeded)
24224 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24225 memberLink->type, value, &val, 0, 1, 0);
24226 else
24227 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24228 memberLink->type, value, NULL, 0, 1, 0);
24229 if (ret <= 0)
24230 break;
24231 memberLink = memberLink->next;
24232 }
24233 if (ret != 0) {
24234 if (ret < 0) {
24235 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24236 "validating members of union simple type");
24237 goto internal_error;
24238 }
24239 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24240 }
24241 /*
24242 * Apply facets (pattern, enumeration).
24243 */
24244 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24245 /*
24246 * The normalization behavior of ·union· types is controlled by
24247 * the value of whiteSpace on that one of the ·memberTypes·
24248 * against which the ·union· is successfully validated.
24249 */
24250 NORMALIZE(memberLink->type);
24251 ret = xmlSchemaValidateFacets(actxt, node, type,
24252 XML_SCHEMAS_UNKNOWN, value, val,
24253 0, fireErrors);
24254 if (ret != 0) {
24255 if (ret < 0) {
24256 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24257 "validating facets of union simple type");
24258 goto internal_error;
24259 }
24260 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24261 }
24262 }
24263 if (fireErrors && (ret > 0))
24264 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24265 }
24266
24267 if (normValue != NULL)
24268 xmlFree(normValue);
24269 if (ret == 0) {
24270 if (retVal != NULL)
24271 *retVal = val;
24272 else if (val != NULL)
24273 xmlSchemaFreeValue(val);
24274 } else if (val != NULL)
24275 xmlSchemaFreeValue(val);
24276 return (ret);
24277internal_error:
24278 if (normValue != NULL)
24279 xmlFree(normValue);
24280 if (val != NULL)
24281 xmlSchemaFreeValue(val);
24282 return (-1);
24283}
24284
24285static int
24286xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24287 const xmlChar *value,
24288 const xmlChar **nsName,
24289 const xmlChar **localName)
24290{
24291 int ret = 0;
24292
24293 if ((nsName == NULL) || (localName == NULL))
24294 return (-1);
24295 *nsName = NULL;
24296 *localName = NULL;
24297
24298 ret = xmlValidateQName(value, 1);
24299 if (ret == -1)
24300 return (-1);
24301 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024302 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024303 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24304 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24305 return (1);
24306 }
24307 {
24308 xmlChar *local = NULL;
24309 xmlChar *prefix;
24310
24311 /*
24312 * NOTE: xmlSplitQName2 will return a duplicated
24313 * string.
24314 */
24315 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024316 if (local == NULL)
24317 *localName = xmlDictLookup(vctxt->dict, value, -1);
24318 else {
24319 *localName = xmlDictLookup(vctxt->dict, local, -1);
24320 xmlFree(local);
24321 }
24322
24323 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24324
24325 if (prefix != NULL) {
24326 xmlFree(prefix);
24327 /*
24328 * A namespace must be found if the prefix is NOT NULL.
24329 */
24330 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024331 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024332 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024333 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024334 "The QName value '%s' has no "
24335 "corresponding namespace declaration in scope",
24336 value, NULL);
24337 return (2);
24338 }
24339 }
24340 }
24341 return (0);
24342}
24343
24344static int
24345xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24346 xmlSchemaAttrInfoPtr iattr,
24347 xmlSchemaTypePtr *localType,
24348 xmlSchemaElementPtr elemDecl)
24349{
24350 int ret = 0;
24351 /*
24352 * cvc-elt (3.3.4) : (4)
24353 * AND
24354 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24355 * (1.2.1.2.1) - (1.2.1.2.4)
24356 * Handle 'xsi:type'.
24357 */
24358 if (localType == NULL)
24359 return (-1);
24360 *localType = NULL;
24361 if (iattr == NULL)
24362 return (0);
24363 else {
24364 const xmlChar *nsName = NULL, *local = NULL;
24365 /*
24366 * TODO: We should report a *warning* that the type was overriden
24367 * by the instance.
24368 */
24369 ACTIVATE_ATTRIBUTE(iattr);
24370 /*
24371 * (cvc-elt) (3.3.4) : (4.1)
24372 * (cvc-assess-elt) (1.2.1.2.2)
24373 */
24374 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24375 &nsName, &local);
24376 if (ret != 0) {
24377 if (ret < 0) {
24378 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24379 "calling xmlSchemaQNameExpand() to validate the "
24380 "attribute 'xsi:type'");
24381 goto internal_error;
24382 }
24383 goto exit;
24384 }
24385 /*
24386 * (cvc-elt) (3.3.4) : (4.2)
24387 * (cvc-assess-elt) (1.2.1.2.3)
24388 */
24389 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24390 if (*localType == NULL) {
24391 xmlChar *str = NULL;
24392
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024393 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024394 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024395 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024396 "The QName value '%s' of the xsi:type attribute does not "
24397 "resolve to a type definition",
24398 xmlSchemaFormatQName(&str, nsName, local), NULL);
24399 FREE_AND_NULL(str);
24400 ret = vctxt->err;
24401 goto exit;
24402 }
24403 if (elemDecl != NULL) {
24404 int set = 0;
24405
24406 /*
24407 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24408 * "The ·local type definition· must be validly
24409 * derived from the {type definition} given the union of
24410 * the {disallowed substitutions} and the {type definition}'s
24411 * {prohibited substitutions}, as defined in
24412 * Type Derivation OK (Complex) (§3.4.6)
24413 * (if it is a complex type definition),
24414 * or given {disallowed substitutions} as defined in Type
24415 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24416 * definition)."
24417 *
24418 * {disallowed substitutions}: the "block" on the element decl.
24419 * {prohibited substitutions}: the "block" on the type def.
24420 */
24421 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24422 (elemDecl->subtypes->flags &
24423 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24424 set |= SUBSET_EXTENSION;
24425
24426 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24427 (elemDecl->subtypes->flags &
24428 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24429 set |= SUBSET_RESTRICTION;
24430
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024431 if ((vctxt->pctxt == NULL) &&
24432 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24433 return (-1);
24434 if (xmlSchemaCheckCOSDerivedOK(vctxt->pctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024435 elemDecl->subtypes, set) != 0) {
24436 xmlChar *str = NULL;
24437
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024438 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024439 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24440 "The type definition '%s', specified by xsi:type, is "
24441 "blocked or not validly derived from the type definition "
24442 "of the element declaration",
24443 xmlSchemaFormatQName(&str,
24444 (*localType)->targetNamespace,
24445 (*localType)->name),
24446 NULL);
24447 FREE_AND_NULL(str);
24448 ret = vctxt->err;
24449 *localType = NULL;
24450 }
24451 }
24452 }
24453exit:
24454 ACTIVATE_ELEM;
24455 return (ret);
24456internal_error:
24457 ACTIVATE_ELEM;
24458 return (-1);
24459}
24460
24461static int
24462xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24463{
24464 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024465 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024466
24467 /*
24468 * cvc-elt (3.3.4) : 1
24469 */
24470 if (elemDecl == NULL) {
24471 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24472 "No matching declaration available");
24473 return (vctxt->err);
24474 }
24475 /*
24476 * cvc-elt (3.3.4) : 2
24477 */
24478 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24479 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24480 "The element declaration is abstract");
24481 return (vctxt->err);
24482 }
24483 if (actualType == NULL) {
24484 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24485 "The type definition is absent");
24486 return (XML_SCHEMAV_CVC_TYPE_1);
24487 }
24488 if (vctxt->nbAttrInfos != 0) {
24489 int ret;
24490 xmlSchemaAttrInfoPtr iattr;
24491 /*
24492 * cvc-elt (3.3.4) : 3
24493 * Handle 'xsi:nil'.
24494 */
24495 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24496 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24497 if (iattr) {
24498 ACTIVATE_ATTRIBUTE(iattr);
24499 /*
24500 * Validate the value.
24501 */
24502 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024503 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024504 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24505 iattr->value, &(iattr->val), 1, 0, 0);
24506 ACTIVATE_ELEM;
24507 if (ret < 0) {
24508 VERROR_INT("xmlSchemaValidateElemDecl",
24509 "calling xmlSchemaVCheckCVCSimpleType() to "
24510 "validate the attribute 'xsi:nil'");
24511 return (-1);
24512 }
24513 if (ret == 0) {
24514 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24515 /*
24516 * cvc-elt (3.3.4) : 3.1
24517 */
24518 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24519 "The element is not 'nillable'");
24520 /* Does not return an error on purpose. */
24521 } else {
24522 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24523 /*
24524 * cvc-elt (3.3.4) : 3.2.2
24525 */
24526 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24527 (elemDecl->value != NULL)) {
24528 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24529 "The element cannot be 'nilled' because "
24530 "there is a fixed value constraint defined "
24531 "for it");
24532 /* Does not return an error on purpose. */
24533 } else
24534 vctxt->inode->flags |=
24535 XML_SCHEMA_ELEM_INFO_NILLED;
24536 }
24537 }
24538 }
24539 }
24540 /*
24541 * cvc-elt (3.3.4) : 4
24542 * Handle 'xsi:type'.
24543 */
24544 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24545 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24546 if (iattr) {
24547 xmlSchemaTypePtr localType = NULL;
24548
24549 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24550 elemDecl);
24551 if (ret != 0) {
24552 if (ret == -1) {
24553 VERROR_INT("xmlSchemaValidateElemDecl",
24554 "calling xmlSchemaProcessXSIType() to "
24555 "process the attribute 'xsi:type'");
24556 return (-1);
24557 }
24558 /* Does not return an error on purpose. */
24559 }
24560 if (localType != NULL) {
24561 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24562 actualType = localType;
24563 }
24564 }
24565 }
24566 /*
24567 * IDC: Register identity-constraint XPath matchers.
24568 */
24569 if ((elemDecl->idcs != NULL) &&
24570 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24571 return (-1);
24572 /*
24573 * No actual type definition.
24574 */
24575 if (actualType == NULL) {
24576 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24577 "The type definition is absent");
24578 return (XML_SCHEMAV_CVC_TYPE_1);
24579 }
24580 /*
24581 * Remember the actual type definition.
24582 */
24583 vctxt->inode->typeDef = actualType;
24584
24585 return (0);
24586}
24587
24588static int
24589xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24590{
24591 xmlSchemaAttrInfoPtr iattr;
24592 int ret = 0, i;
24593
24594 /*
24595 * SPEC cvc-type (3.1.1)
24596 * "The attributes of must be empty, excepting those whose namespace
24597 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24598 * whose local name is one of type, nil, schemaLocation or
24599 * noNamespaceSchemaLocation."
24600 */
24601 if (vctxt->nbAttrInfos == 0)
24602 return (0);
24603 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24604 iattr = vctxt->attrInfos[i];
24605 if (! iattr->metaType) {
24606 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024607 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024608 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24609 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24610 }
24611 }
24612 ACTIVATE_ELEM
24613 return (ret);
24614}
24615
24616/*
24617* Cleanup currently used attribute infos.
24618*/
24619static void
24620xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24621{
24622 int i;
24623 xmlSchemaAttrInfoPtr attr;
24624
24625 if (vctxt->nbAttrInfos == 0)
24626 return;
24627 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24628 attr = vctxt->attrInfos[i];
24629 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24630 if (attr->localName != NULL)
24631 xmlFree((xmlChar *) attr->localName);
24632 if (attr->nsName != NULL)
24633 xmlFree((xmlChar *) attr->nsName);
24634 }
24635 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24636 if (attr->value != NULL)
24637 xmlFree((xmlChar *) attr->value);
24638 }
24639 if (attr->val != NULL) {
24640 xmlSchemaFreeValue(attr->val);
24641 attr->val = NULL;
24642 }
24643 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24644 }
24645 vctxt->nbAttrInfos = 0;
24646}
24647
24648/*
24649* 3.4.4 Complex Type Definition Validation Rules
24650* Element Locally Valid (Complex Type) (cvc-complex-type)
24651* 3.2.4 Attribute Declaration Validation Rules
24652* Validation Rule: Attribute Locally Valid (cvc-attribute)
24653* Attribute Locally Valid (Use) (cvc-au)
24654*
24655* Only "assessed" attribute information items will be visible to
24656* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24657*/
24658static int
24659xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24660{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024661 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24662 xmlSchemaItemListPtr attrUseList;
24663 xmlSchemaAttributeUsePtr attrUse = NULL;
24664 xmlSchemaAttributePtr attrDecl = NULL;
24665 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24666 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024667 int xpathRes = 0, res, wildIDs = 0, fixed;
24668
24669 /*
24670 * SPEC (cvc-attribute)
24671 * (1) "The declaration must not be ·absent· (see Missing
24672 * Sub-components (§5.3) for how this can fail to be
24673 * the case)."
24674 * (2) "Its {type definition} must not be absent."
24675 *
24676 * NOTE (1) + (2): This is not handled here, since we currently do not
24677 * allow validation against schemas which have missing sub-components.
24678 *
24679 * SPEC (cvc-complex-type)
24680 * (3) "For each attribute information item in the element information
24681 * item's [attributes] excepting those whose [namespace name] is
24682 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24683 * [local name] is one of type, nil, schemaLocation or
24684 * noNamespaceSchemaLocation, the appropriate case among the following
24685 * must be true:
24686 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024687 */
24688 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24689 /*
24690 * @nbAttrs is the number of attributes present in the instance.
24691 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024692 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024693 if (attrUseList != NULL)
24694 nbUses = attrUseList->nbItems;
24695 else
24696 nbUses = 0;
24697 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024698 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024699 attrUse = attrUseList->items[i];
24700 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24701 for (j = 0; j < nbAttrs; j++) {
24702 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024703 /*
24704 * SPEC (cvc-complex-type) (3)
24705 * Skip meta attributes.
24706 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024707 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024708 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024709 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024710 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024711 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024712 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024713 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024714 continue;
24715 found = 1;
24716 /*
24717 * SPEC (cvc-complex-type)
24718 * (3.1) "If there is among the {attribute uses} an attribute
24719 * use with an {attribute declaration} whose {name} matches
24720 * the attribute information item's [local name] and whose
24721 * {target namespace} is identical to the attribute information
24722 * item's [namespace name] (where an ·absent· {target namespace}
24723 * is taken to be identical to a [namespace name] with no value),
24724 * then the attribute information must be ·valid· with respect
24725 * to that attribute use as per Attribute Locally Valid (Use)
24726 * (§3.5.4). In this case the {attribute declaration} of that
24727 * attribute use is the ·context-determined declaration· for the
24728 * attribute information item with respect to Schema-Validity
24729 * Assessment (Attribute) (§3.2.4) and
24730 * Assessment Outcome (Attribute) (§3.2.5).
24731 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024732 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24733 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024734 /*
24735 * Context-determined declaration.
24736 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024737 iattr->decl = attrDecl;
24738 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024739 break;
24740 }
24741
24742 if (found)
24743 continue;
24744
24745 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24746 /*
24747 * Handle non-existent, required attributes.
24748 *
24749 * SPEC (cvc-complex-type)
24750 * (4) "The {attribute declaration} of each attribute use in
24751 * the {attribute uses} whose {required} is true matches one
24752 * of the attribute information items in the element information
24753 * item's [attributes] as per clause 3.1 above."
24754 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024755 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24756 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024757 VERROR_INT(
24758 "xmlSchemaVAttributesComplex",
24759 "calling xmlSchemaGetFreshAttrInfo()");
24760 return (-1);
24761 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024762 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24763 tmpiattr->use = attrUse;
24764 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024765 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24766 ((attrUse->defValue != NULL) ||
24767 (attrDecl->defValue != NULL))) {
24768 /*
24769 * Handle non-existent, optional, default/fixed attributes.
24770 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024771 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24772 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024773 VERROR_INT(
24774 "xmlSchemaVAttributesComplex",
24775 "calling xmlSchemaGetFreshAttrInfo()");
24776 return (-1);
24777 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024778 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24779 tmpiattr->use = attrUse;
24780 tmpiattr->decl = attrDecl;
24781 tmpiattr->typeDef = attrDecl->subtypes;
24782 tmpiattr->localName = attrDecl->name;
24783 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024784 }
24785 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024786
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024787 if (vctxt->nbAttrInfos == 0)
24788 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024789 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024790 /*
24791 * Validate against the wildcard.
24792 */
24793 if (type->attributeWildcard != NULL) {
24794 /*
24795 * SPEC (cvc-complex-type)
24796 * (3.2.1) "There must be an {attribute wildcard}."
24797 */
24798 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024799 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024800 /*
24801 * SPEC (cvc-complex-type) (3)
24802 * Skip meta attributes.
24803 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024804 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024805 continue;
24806 /*
24807 * SPEC (cvc-complex-type)
24808 * (3.2.2) "The attribute information item must be ·valid· with
24809 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24810 *
24811 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24812 * "... its [namespace name] must be ·valid· with respect to
24813 * the wildcard constraint, as defined in Wildcard allows
24814 * Namespace Name (§3.10.4)."
24815 */
24816 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024817 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024818 /*
24819 * Handle processContents.
24820 *
24821 * SPEC (cvc-wildcard):
24822 * processContents | context-determined declaration:
24823 * "strict" "mustFind"
24824 * "lax" "none"
24825 * "skip" "skip"
24826 */
24827 if (type->attributeWildcard->processContents ==
24828 XML_SCHEMAS_ANY_SKIP) {
24829 /*
24830 * context-determined declaration = "skip"
24831 *
24832 * SPEC PSVI Assessment Outcome (Attribute)
24833 * [validity] = "notKnown"
24834 * [validation attempted] = "none"
24835 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024836 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024837 continue;
24838 }
24839 /*
24840 * Find an attribute declaration.
24841 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024842 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24843 iattr->localName, iattr->nsName);
24844 if (iattr->decl != NULL) {
24845 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024846 /*
24847 * SPEC (cvc-complex-type)
24848 * (5) "Let [Definition:] the wild IDs be the set of
24849 * all attribute information item to which clause 3.2
24850 * applied and whose ·validation· resulted in a
24851 * ·context-determined declaration· of mustFind or no
24852 * ·context-determined declaration· at all, and whose
24853 * [local name] and [namespace name] resolve (as
24854 * defined by QName resolution (Instance) (§3.15.4)) to
24855 * an attribute declaration whose {type definition} is
24856 * or is derived from ID. Then all of the following
24857 * must be true:"
24858 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024859 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024860 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024861 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024862 /*
24863 * SPEC (5.1) "There must be no more than one
24864 * item in ·wild IDs·."
24865 */
24866 if (wildIDs != 0) {
24867 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024868 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024869 TODO
24870 continue;
24871 }
24872 wildIDs++;
24873 /*
24874 * SPEC (cvc-complex-type)
24875 * (5.2) "If ·wild IDs· is non-empty, there must not
24876 * be any attribute uses among the {attribute uses}
24877 * whose {attribute declaration}'s {type definition}
24878 * is or is derived from ID."
24879 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024880 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024881 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024882 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024883 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024884 /* URGENT VAL TODO: implement */
24885 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024886 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024887 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024888 }
24889 }
24890 }
24891 } else if (type->attributeWildcard->processContents ==
24892 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024893 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024894 /*
24895 * SPEC PSVI Assessment Outcome (Attribute)
24896 * [validity] = "notKnown"
24897 * [validation attempted] = "none"
24898 */
24899 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024900 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024901 }
24902 }
24903 }
24904 }
24905
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024906 if (vctxt->nbAttrInfos == 0)
24907 return (0);
24908
24909 /*
24910 * Validate values, create default attributes, evaluate IDCs.
24911 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024912 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024913 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024914 /*
24915 * VAL TODO: Note that we won't try to resolve IDCs to
24916 * "lax" and "skip" validated attributes. Check what to
24917 * do in this case.
24918 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024919 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
24920 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024921 continue;
24922 /*
24923 * VAL TODO: What to do if the type definition is missing?
24924 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024925 if (iattr->typeDef == NULL) {
24926 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024927 continue;
24928 }
24929
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024930 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000024931 fixed = 0;
24932 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024933
24934 if (vctxt->xpathStates != NULL) {
24935 /*
24936 * Evaluate IDCs.
24937 */
24938 xpathRes = xmlSchemaXPathEvaluate(vctxt,
24939 XML_ATTRIBUTE_NODE);
24940 if (xpathRes == -1) {
24941 VERROR_INT("xmlSchemaVAttributesComplex",
24942 "calling xmlSchemaXPathEvaluate()");
24943 goto internal_error;
24944 }
24945 }
24946
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024947 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024948 /*
24949 * Default/fixed attributes.
24950 */
24951 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024952 if (iattr->use->defValue != NULL) {
24953 iattr->value = (xmlChar *) iattr->use->defValue;
24954 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024955 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024956 iattr->value = (xmlChar *) iattr->decl->defValue;
24957 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024958 }
24959 /*
24960 * IDCs will consume the precomputed default value,
24961 * so we need to clone it.
24962 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024963 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024964 VERROR_INT("xmlSchemaVAttributesComplex",
24965 "default/fixed value on an attribute use was "
24966 "not precomputed");
24967 goto internal_error;
24968 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024969 iattr->val = xmlSchemaCopyValue(iattr->val);
24970 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024971 VERROR_INT("xmlSchemaVAttributesComplex",
24972 "calling xmlSchemaCopyValue()");
24973 goto internal_error;
24974 }
24975 }
24976 /*
24977 * PSVI: Add the default attribute to the current element.
24978 * VAL TODO: Should we use the *normalized* value? This currently
24979 * uses the *initial* value.
24980 */
24981 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024982 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024983 xmlChar *normValue;
24984 const xmlChar *value;
24985
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024986 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024987 /*
24988 * Normalize the value.
24989 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024990 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
24991 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024992 if (normValue != NULL)
24993 value = BAD_CAST normValue;
24994
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024995 if (iattr->nsName == NULL) {
24996 if (xmlNewProp(iattr->node->parent,
24997 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024998 VERROR_INT("xmlSchemaVAttributesComplex",
24999 "callling xmlNewProp()");
25000 if (normValue != NULL)
25001 xmlFree(normValue);
25002 goto internal_error;
25003 }
25004 } else {
25005 xmlNsPtr ns;
25006
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025007 ns = xmlSearchNsByHref(iattr->node->doc,
25008 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025009 if (ns == NULL) {
25010 xmlChar prefix[12];
25011 int counter = 0;
25012
25013 /*
25014 * Create a namespace declaration on the validation
25015 * root node if no namespace declaration is in scope.
25016 */
25017 do {
25018 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025019 ns = xmlSearchNs(iattr->node->doc,
25020 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025021 if (counter > 1000) {
25022 VERROR_INT(
25023 "xmlSchemaVAttributesComplex",
25024 "could not compute a ns prefix for a "
25025 "default/fixed attribute");
25026 if (normValue != NULL)
25027 xmlFree(normValue);
25028 goto internal_error;
25029 }
25030 } while (ns != NULL);
25031 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025032 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025033 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025034 /*
25035 * TODO:
25036 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25037 * If we have QNames: do we need to ensure there's a
25038 * prefix defined for the QName?
25039 */
25040 xmlNewNsProp(iattr->node->parent, ns,
25041 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025042 }
25043 if (normValue != NULL)
25044 xmlFree(normValue);
25045 }
25046 /*
25047 * Go directly to IDC evaluation.
25048 */
25049 goto eval_idcs;
25050 }
25051 /*
25052 * Validate the value.
25053 */
25054 if (vctxt->value != NULL) {
25055 /*
25056 * Free last computed value; just for safety reasons.
25057 */
25058 xmlSchemaFreeValue(vctxt->value);
25059 vctxt->value = NULL;
25060 }
25061 /*
25062 * Note that the attribute *use* can be unavailable, if
25063 * the attribute was a wild attribute.
25064 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025065 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25066 ((iattr->use != NULL) &&
25067 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025068 fixed = 1;
25069 else
25070 fixed = 0;
25071 /*
25072 * SPEC (cvc-attribute)
25073 * (3) "The item's ·normalized value· must be locally ·valid·
25074 * with respect to that {type definition} as per
25075 * String Valid (§3.14.4)."
25076 *
25077 * VAL TODO: Do we already have the
25078 * "normalized attribute value" here?
25079 */
25080 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025081 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025082 /*
25083 * Request a computed value.
25084 */
25085 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025086 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025087 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025088 1, 1, 0);
25089 } else {
25090 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025091 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025092 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025093 1, 0, 0);
25094 }
25095
25096 if (res != 0) {
25097 if (res == -1) {
25098 VERROR_INT("xmlSchemaVAttributesComplex",
25099 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25100 goto internal_error;
25101 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025102 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025103 /*
25104 * SPEC PSVI Assessment Outcome (Attribute)
25105 * [validity] = "invalid"
25106 */
25107 goto eval_idcs;
25108 }
25109
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025110 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025111 /*
25112 * SPEC Attribute Locally Valid (Use) (cvc-au)
25113 * "For an attribute information item to be·valid·
25114 * with respect to an attribute use its *normalized*
25115 * value· must match the *canonical* lexical
25116 * representation of the attribute use's {value
25117 * constraint}value, if it is present and fixed."
25118 *
25119 * VAL TODO: The requirement for the *canonical* value
25120 * will be removed in XML Schema 1.1.
25121 */
25122 /*
25123 * SPEC Attribute Locally Valid (cvc-attribute)
25124 * (4) "The item's *actual* value· must match the *value* of
25125 * the {value constraint}, if it is present and fixed."
25126 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025127 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025128 /* VAL TODO: A value was not precomputed. */
25129 TODO
25130 goto eval_idcs;
25131 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025132 if ((iattr->use != NULL) &&
25133 (iattr->use->defValue != NULL)) {
25134 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025135 /* VAL TODO: A default value was not precomputed. */
25136 TODO
25137 goto eval_idcs;
25138 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025139 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025140 /*
25141 if (xmlSchemaCompareValuesWhtsp(attr->val,
25142 (xmlSchemaWhitespaceValueType) ws,
25143 attr->use->defVal,
25144 (xmlSchemaWhitespaceValueType) ws) != 0) {
25145 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025146 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25147 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025148 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025149 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025150 /* VAL TODO: A default value was not precomputed. */
25151 TODO
25152 goto eval_idcs;
25153 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025154 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025155 /*
25156 if (xmlSchemaCompareValuesWhtsp(attr->val,
25157 (xmlSchemaWhitespaceValueType) ws,
25158 attrDecl->defVal,
25159 (xmlSchemaWhitespaceValueType) ws) != 0) {
25160 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025161 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25162 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025163 }
25164 /*
25165 * [validity] = "valid"
25166 */
25167 }
25168eval_idcs:
25169 /*
25170 * Evaluate IDCs.
25171 */
25172 if (xpathRes) {
25173 if (xmlSchemaXPathProcessHistory(vctxt,
25174 vctxt->depth +1) == -1) {
25175 VERROR_INT("xmlSchemaVAttributesComplex",
25176 "calling xmlSchemaXPathEvaluate()");
25177 goto internal_error;
25178 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025179 } else if (vctxt->xpathStates != NULL)
25180 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025181 }
25182
25183 /*
25184 * Report errors.
25185 */
25186 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025187 iattr = vctxt->attrInfos[i];
25188 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25189 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25190 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25191 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025192 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025193 ACTIVATE_ATTRIBUTE(iattr);
25194 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025195 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25196 xmlChar *str = NULL;
25197 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025198 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025199 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25200 "The attribute '%s' is required but missing",
25201 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025202 iattr->decl->targetNamespace,
25203 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025204 NULL);
25205 FREE_AND_NULL(str)
25206 break;
25207 }
25208 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25209 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25210 "The type definition is absent");
25211 break;
25212 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025213 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025214 XML_SCHEMAV_CVC_AU, NULL, NULL,
25215 "The value '%s' does not match the fixed "
25216 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025217 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025218 break;
25219 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25220 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25221 "No matching global attribute declaration available, but "
25222 "demanded by the strict wildcard");
25223 break;
25224 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025225 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025226 break;
25227 /*
25228 * MAYBE VAL TODO: One might report different error messages
25229 * for the following errors.
25230 */
25231 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025232 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025233 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025234 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025235 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025236 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025237 }
25238 break;
25239 default:
25240 break;
25241 }
25242 }
25243
25244 ACTIVATE_ELEM;
25245 return (0);
25246internal_error:
25247 ACTIVATE_ELEM;
25248 return (-1);
25249}
25250
25251static int
25252xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25253 int *skip)
25254{
25255 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25256 /*
25257 * The namespace of the element was already identified to be
25258 * matching the wildcard.
25259 */
25260 if ((skip == NULL) || (wild == NULL) ||
25261 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25262 VERROR_INT("xmlSchemaValidateElemWildcard",
25263 "bad arguments");
25264 return (-1);
25265 }
25266 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025267 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25268 /*
25269 * URGENT VAL TODO: Either we need to position the stream to the
25270 * next sibling, or walk the whole subtree.
25271 */
25272 *skip = 1;
25273 return (0);
25274 }
25275 {
25276 xmlSchemaElementPtr decl = NULL;
25277
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025278 decl = xmlSchemaGetElem(vctxt->schema,
25279 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025280 if (decl != NULL) {
25281 vctxt->inode->decl = decl;
25282 return (0);
25283 }
25284 }
25285 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25286 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025287 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025288 "No matching global element declaration available, but "
25289 "demanded by the strict wildcard");
25290 return (vctxt->err);
25291 }
25292 if (vctxt->nbAttrInfos != 0) {
25293 xmlSchemaAttrInfoPtr iattr;
25294 /*
25295 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25296 * (1.2.1.2.1) - (1.2.1.2.3 )
25297 *
25298 * Use the xsi:type attribute for the type definition.
25299 */
25300 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25301 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25302 if (iattr != NULL) {
25303 if (xmlSchemaProcessXSIType(vctxt, iattr,
25304 &(vctxt->inode->typeDef), NULL) == -1) {
25305 VERROR_INT("xmlSchemaValidateElemWildcard",
25306 "calling xmlSchemaProcessXSIType() to "
25307 "process the attribute 'xsi:nil'");
25308 return (-1);
25309 }
25310 /*
25311 * Don't return an error on purpose.
25312 */
25313 return (0);
25314 }
25315 }
25316 /*
25317 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25318 *
25319 * Fallback to "anyType".
25320 */
25321 vctxt->inode->typeDef =
25322 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25323 return (0);
25324}
25325
25326/*
25327* xmlSchemaCheckCOSValidDefault:
25328*
25329* This will be called if: not nilled, no content and a default/fixed
25330* value is provided.
25331*/
25332
25333static int
25334xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25335 const xmlChar *value,
25336 xmlSchemaValPtr *val)
25337{
25338 int ret = 0;
25339 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25340
25341 /*
25342 * cos-valid-default:
25343 * Schema Component Constraint: Element Default Valid (Immediate)
25344 * For a string to be a valid default with respect to a type
25345 * definition the appropriate case among the following must be true:
25346 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025347 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025348 /*
25349 * Complex type.
25350 *
25351 * SPEC (2.1) "its {content type} must be a simple type definition
25352 * or mixed."
25353 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25354 * type}'s particle must be ·emptiable· as defined by
25355 * Particle Emptiable (§3.9.6)."
25356 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025357 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25358 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25359 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025360 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25361 /* NOTE that this covers (2.2.2) as well. */
25362 VERROR(ret, NULL,
25363 "For a string to be a valid default, the type definition "
25364 "must be a simple type or a complex type with simple content "
25365 "or mixed content and a particle emptiable");
25366 return(ret);
25367 }
25368 }
25369 /*
25370 * 1 If the type definition is a simple type definition, then the string
25371 * must be ·valid· with respect to that definition as defined by String
25372 * Valid (§3.14.4).
25373 *
25374 * AND
25375 *
25376 * 2.2.1 If the {content type} is a simple type definition, then the
25377 * string must be ·valid· with respect to that simple type definition
25378 * as defined by String Valid (§3.14.4).
25379 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025380 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025381
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025382 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025383 NULL, inode->typeDef, value, val, 1, 1, 0);
25384
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025385 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025386
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025387 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025388 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25389 }
25390 if (ret < 0) {
25391 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25392 "calling xmlSchemaVCheckCVCSimpleType()");
25393 }
25394 return (ret);
25395}
25396
25397static void
25398xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25399 const xmlChar * name ATTRIBUTE_UNUSED,
25400 xmlSchemaElementPtr item,
25401 xmlSchemaNodeInfoPtr inode)
25402{
25403 inode->decl = item;
25404#ifdef DEBUG_CONTENT
25405 {
25406 xmlChar *str = NULL;
25407
25408 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25409 xmlGenericError(xmlGenericErrorContext,
25410 "AUTOMATON callback for '%s' [declaration]\n",
25411 xmlSchemaFormatQName(&str,
25412 inode->localName, inode->nsName));
25413 } else {
25414 xmlGenericError(xmlGenericErrorContext,
25415 "AUTOMATON callback for '%s' [wildcard]\n",
25416 xmlSchemaFormatQName(&str,
25417 inode->localName, inode->nsName));
25418
25419 }
25420 FREE_AND_NULL(str)
25421 }
25422#endif
25423}
25424
25425static int
25426xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025427{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025428 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25429 if (vctxt->inode == NULL) {
25430 VERROR_INT("xmlSchemaValidatorPushElem",
25431 "calling xmlSchemaGetFreshElemInfo()");
25432 return (-1);
25433 }
25434 vctxt->nbAttrInfos = 0;
25435 return (0);
25436}
25437
25438static int
25439xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25440 xmlSchemaNodeInfoPtr inode,
25441 xmlSchemaTypePtr type,
25442 const xmlChar *value)
25443{
25444 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25445 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025446 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025447 type, value, &(inode->val), 1, 1, 0));
25448 else
25449 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025450 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025451 type, value, NULL, 1, 0, 0));
25452}
25453
25454
25455
25456/*
25457* Process END of element.
25458*/
25459static int
25460xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25461{
25462 int ret = 0;
25463 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25464
25465 if (vctxt->nbAttrInfos != 0)
25466 xmlSchemaClearAttrInfos(vctxt);
25467 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25468 /*
25469 * This element was not expected;
25470 * we will not validate child elements of broken parents.
25471 * Skip validation of all content of the parent.
25472 */
25473 vctxt->skipDepth = vctxt->depth -1;
25474 goto end_elem;
25475 }
25476 if ((inode->typeDef == NULL) ||
25477 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25478 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025479 * 1. the type definition might be missing if the element was
25480 * error prone
25481 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025482 */
25483 goto end_elem;
25484 }
25485 /*
25486 * Check the content model.
25487 */
25488 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25489 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25490
25491 /*
25492 * Workaround for "anyType".
25493 */
25494 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25495 goto character_content;
25496
25497 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25498 xmlChar *values[10];
25499 int terminal, nbval = 10, nbneg;
25500
25501 if (inode->regexCtxt == NULL) {
25502 /*
25503 * Create the regex context.
25504 */
25505 inode->regexCtxt =
25506 xmlRegNewExecCtxt(inode->typeDef->contModel,
25507 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25508 vctxt);
25509 if (inode->regexCtxt == NULL) {
25510 VERROR_INT("xmlSchemaValidatorPopElem",
25511 "failed to create a regex context");
25512 goto internal_error;
25513 }
25514#ifdef DEBUG_AUTOMATA
25515 xmlGenericError(xmlGenericErrorContext,
25516 "AUTOMATON create on '%s'\n", inode->localName);
25517#endif
25518 }
25519 /*
25520 * Get hold of the still expected content, since a further
25521 * call to xmlRegExecPushString() will loose this information.
25522 */
25523 xmlRegExecNextValues(inode->regexCtxt,
25524 &nbval, &nbneg, &values[0], &terminal);
25525 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25526 if (ret <= 0) {
25527 /*
25528 * Still missing something.
25529 */
25530 ret = 1;
25531 inode->flags |=
25532 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025533 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025534 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25535 "Missing child element(s)",
25536 nbval, nbneg, values);
25537#ifdef DEBUG_AUTOMATA
25538 xmlGenericError(xmlGenericErrorContext,
25539 "AUTOMATON missing ERROR on '%s'\n",
25540 inode->localName);
25541#endif
25542 } else {
25543 /*
25544 * Content model is satisfied.
25545 */
25546 ret = 0;
25547#ifdef DEBUG_AUTOMATA
25548 xmlGenericError(xmlGenericErrorContext,
25549 "AUTOMATON succeeded on '%s'\n",
25550 inode->localName);
25551#endif
25552 }
25553
25554 }
25555 }
25556 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25557 goto end_elem;
25558
25559character_content:
25560
25561 if (vctxt->value != NULL) {
25562 xmlSchemaFreeValue(vctxt->value);
25563 vctxt->value = NULL;
25564 }
25565 /*
25566 * Check character content.
25567 */
25568 if (inode->decl == NULL) {
25569 /*
25570 * Speedup if no declaration exists.
25571 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025572 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025573 ret = xmlSchemaVCheckINodeDataType(vctxt,
25574 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025575 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025576 ret = xmlSchemaVCheckINodeDataType(vctxt,
25577 inode, inode->typeDef->contentTypeDef,
25578 inode->value);
25579 }
25580 if (ret < 0) {
25581 VERROR_INT("xmlSchemaValidatorPopElem",
25582 "calling xmlSchemaVCheckCVCSimpleType()");
25583 goto internal_error;
25584 }
25585 goto end_elem;
25586 }
25587 /*
25588 * cvc-elt (3.3.4) : 5
25589 * The appropriate case among the following must be true:
25590 */
25591 /*
25592 * cvc-elt (3.3.4) : 5.1
25593 * If the declaration has a {value constraint},
25594 * the item has neither element nor character [children] and
25595 * clause 3.2 has not applied, then all of the following must be true:
25596 */
25597 if ((inode->decl->value != NULL) &&
25598 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25599 (! INODE_NILLED(inode))) {
25600 /*
25601 * cvc-elt (3.3.4) : 5.1.1
25602 * If the ·actual type definition· is a ·local type definition·
25603 * then the canonical lexical representation of the {value constraint}
25604 * value must be a valid default for the ·actual type definition· as
25605 * defined in Element Default Valid (Immediate) (§3.3.6).
25606 */
25607 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025608 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025609 * NOTE: Although the *canonical* value is stated, it is not
25610 * relevant if canonical or not. Additionally XML Schema 1.1
25611 * will removed this requirement as well.
25612 */
25613 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25614
25615 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25616 inode->decl->value, &(inode->val));
25617 if (ret != 0) {
25618 if (ret < 0) {
25619 VERROR_INT("xmlSchemaValidatorPopElem",
25620 "calling xmlSchemaCheckCOSValidDefault()");
25621 goto internal_error;
25622 }
25623 goto end_elem;
25624 }
25625 /*
25626 * Stop here, to avoid redundant validation of the value
25627 * (see following).
25628 */
25629 goto default_psvi;
25630 }
25631 /*
25632 * cvc-elt (3.3.4) : 5.1.2
25633 * The element information item with the canonical lexical
25634 * representation of the {value constraint} value used as its
25635 * ·normalized value· must be ·valid· with respect to the
25636 * ·actual type definition· as defined by Element Locally Valid (Type)
25637 * (§3.3.4).
25638 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025639 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025640 ret = xmlSchemaVCheckINodeDataType(vctxt,
25641 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025642 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025643 ret = xmlSchemaVCheckINodeDataType(vctxt,
25644 inode, inode->typeDef->contentTypeDef,
25645 inode->decl->value);
25646 }
25647 if (ret != 0) {
25648 if (ret < 0) {
25649 VERROR_INT("xmlSchemaValidatorPopElem",
25650 "calling xmlSchemaVCheckCVCSimpleType()");
25651 goto internal_error;
25652 }
25653 goto end_elem;
25654 }
25655
25656default_psvi:
25657 /*
25658 * PSVI: Create a text node on the instance element.
25659 */
25660 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25661 (inode->node != NULL)) {
25662 xmlNodePtr textChild;
25663 xmlChar *normValue;
25664 /*
25665 * VAL TODO: Normalize the value.
25666 */
25667 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25668 inode->decl->value);
25669 if (normValue != NULL) {
25670 textChild = xmlNewText(BAD_CAST normValue);
25671 xmlFree(normValue);
25672 } else
25673 textChild = xmlNewText(inode->decl->value);
25674 if (textChild == NULL) {
25675 VERROR_INT("xmlSchemaValidatorPopElem",
25676 "calling xmlNewText()");
25677 goto internal_error;
25678 } else
25679 xmlAddChild(inode->node, textChild);
25680 }
25681
25682 } else if (! INODE_NILLED(inode)) {
25683 /*
25684 * 5.2.1 The element information item must be ·valid· with respect
25685 * to the ·actual type definition· as defined by Element Locally
25686 * Valid (Type) (§3.3.4).
25687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025688 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025689 /*
25690 * SPEC (cvc-type) (3.1)
25691 * "If the type definition is a simple type definition, ..."
25692 * (3.1.3) "If clause 3.2 of Element Locally Valid
25693 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25694 * must be ·valid· with respect to the type definition as defined
25695 * by String Valid (§3.14.4).
25696 */
25697 ret = xmlSchemaVCheckINodeDataType(vctxt,
25698 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025699 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025700 /*
25701 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25702 * definition, then the element information item must be
25703 * ·valid· with respect to the type definition as per
25704 * Element Locally Valid (Complex Type) (§3.4.4);"
25705 *
25706 * SPEC (cvc-complex-type) (2.2)
25707 * "If the {content type} is a simple type definition, ...
25708 * the ·normalized value· of the element information item is
25709 * ·valid· with respect to that simple type definition as
25710 * defined by String Valid (§3.14.4)."
25711 */
25712 ret = xmlSchemaVCheckINodeDataType(vctxt,
25713 inode, inode->typeDef->contentTypeDef, inode->value);
25714 }
25715 if (ret != 0) {
25716 if (ret < 0) {
25717 VERROR_INT("xmlSchemaValidatorPopElem",
25718 "calling xmlSchemaVCheckCVCSimpleType()");
25719 goto internal_error;
25720 }
25721 goto end_elem;
25722 }
25723 /*
25724 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25725 * not applied, all of the following must be true:
25726 */
25727 if ((inode->decl->value != NULL) &&
25728 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25729
25730 /*
25731 * TODO: We will need a computed value, when comparison is
25732 * done on computed values.
25733 */
25734 /*
25735 * 5.2.2.1 The element information item must have no element
25736 * information item [children].
25737 */
25738 if (inode->flags &
25739 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25740 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25741 VERROR(ret, NULL,
25742 "The content must not containt element nodes since "
25743 "there is a fixed value constraint");
25744 goto end_elem;
25745 } else {
25746 /*
25747 * 5.2.2.2 The appropriate case among the following must
25748 * be true:
25749 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025750 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025751 /*
25752 * 5.2.2.2.1 If the {content type} of the ·actual type
25753 * definition· is mixed, then the *initial value* of the
25754 * item must match the canonical lexical representation
25755 * of the {value constraint} value.
25756 *
25757 * ... the *initial value* of an element information
25758 * item is the string composed of, in order, the
25759 * [character code] of each character information item in
25760 * the [children] of that element information item.
25761 */
25762 if (! xmlStrEqual(inode->value, inode->decl->value)){
25763 /*
25764 * VAL TODO: Report invalid & expected values as well.
25765 * VAL TODO: Implement the canonical stuff.
25766 */
25767 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025768 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025769 ret, NULL, NULL,
25770 "The initial value '%s' does not match the fixed "
25771 "value constraint '%s'",
25772 inode->value, inode->decl->value);
25773 goto end_elem;
25774 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025775 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025776 /*
25777 * 5.2.2.2.2 If the {content type} of the ·actual type
25778 * definition· is a simple type definition, then the
25779 * *actual value* of the item must match the canonical
25780 * lexical representation of the {value constraint} value.
25781 */
25782 /*
25783 * VAL TODO: *actual value* is the normalized value, impl.
25784 * this.
25785 * VAL TODO: Report invalid & expected values as well.
25786 * VAL TODO: Implement a comparison with the computed values.
25787 */
25788 if (! xmlStrEqual(inode->value,
25789 inode->decl->value)) {
25790 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025791 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025792 ret, NULL, NULL,
25793 "The actual value '%s' does not match the fixed "
25794 "value constraint '%s'",
25795 inode->value,
25796 inode->decl->value);
25797 goto end_elem;
25798 }
25799 }
25800 }
25801 }
25802 }
25803
25804end_elem:
25805 if (vctxt->depth < 0) {
25806 /* TODO: raise error? */
25807 return (0);
25808 }
25809 if (vctxt->depth == vctxt->skipDepth)
25810 vctxt->skipDepth = -1;
25811 /*
25812 * Evaluate the history of XPath state objects.
25813 */
25814 if (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1)
25815 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025816 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025817 * MAYBE TODO:
25818 * SPEC (6) "The element information item must be ·valid· with
25819 * respect to each of the {identity-constraint definitions} as per
25820 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025821 */
25822 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025823 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25824 * need to be built in any case.
25825 * We will currently build IDC node-tables and bubble them only if
25826 * keyrefs do exist.
25827 */
25828
25829 /*
25830 * Add the current IDC target-nodes to the IDC node-tables.
25831 */
25832 if ((inode->idcMatchers != NULL) &&
25833 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25834 {
25835 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25836 goto internal_error;
25837 }
25838 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025839 * Validate IDC keyrefs.
25840 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025841 if (vctxt->inode->hasKeyrefs)
25842 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25843 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025844 /*
25845 * Merge/free the IDC table.
25846 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025847 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025848#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025849 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025850 inode->nsName,
25851 inode->localName,
25852 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025853#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025854 if ((vctxt->depth > 0) &&
25855 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25856 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025857 /*
25858 * Merge the IDC node table with the table of the parent node.
25859 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025860 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25861 goto internal_error;
25862 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025863 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025864 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025865 * Clear the current ielem.
25866 * VAL TODO: Don't free the PSVI IDC tables if they are
25867 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025868 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025869 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025870 /*
25871 * Skip further processing if we are on the validation root.
25872 */
25873 if (vctxt->depth == 0) {
25874 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025875 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025876 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025877 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025878 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025879 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025880 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025881 if (vctxt->aidcs != NULL) {
25882 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25883 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025884 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025885 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025886 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025887 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025888 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025889 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025890 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025891 }
25892 aidc = aidc->next;
25893 } while (aidc != NULL);
25894 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025895 vctxt->depth--;
25896 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025897 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025898 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025899 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25900 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025901 return (ret);
25902
25903internal_error:
25904 vctxt->err = -1;
25905 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000025906}
25907
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025908/*
25909* 3.4.4 Complex Type Definition Validation Rules
25910* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
25911*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000025912static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025913xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000025914{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025915 xmlSchemaNodeInfoPtr pielem;
25916 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000025917 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000025918
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025919 if (vctxt->depth <= 0) {
25920 VERROR_INT("xmlSchemaValidateChildElem",
25921 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025922 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025923 }
25924 pielem = vctxt->elemInfos[vctxt->depth -1];
25925 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
25926 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025927 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025928 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025929 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025930 if (INODE_NILLED(pielem)) {
25931 /*
25932 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
25933 */
25934 ACTIVATE_PARENT_ELEM;
25935 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
25936 VERROR(ret, NULL,
25937 "Neither character nor element content is allowed, "
25938 "because the element was 'nilled'");
25939 ACTIVATE_ELEM;
25940 goto unexpected_elem;
25941 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025942
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025943 ptype = pielem->typeDef;
25944
25945 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
25946 /*
25947 * Workaround for "anyType": we have currently no content model
25948 * assigned for "anyType", so handle it explicitely.
25949 * "anyType" has an unbounded, lax "any" wildcard.
25950 */
25951 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
25952 vctxt->inode->localName,
25953 vctxt->inode->nsName);
25954
25955 if (vctxt->inode->decl == NULL) {
25956 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025957 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025958 * Process "xsi:type".
25959 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025960 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025961 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25962 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25963 if (iattr != NULL) {
25964 ret = xmlSchemaProcessXSIType(vctxt, iattr,
25965 &(vctxt->inode->typeDef), NULL);
25966 if (ret != 0) {
25967 if (ret == -1) {
25968 VERROR_INT("xmlSchemaValidateChildElem",
25969 "calling xmlSchemaProcessXSIType() to "
25970 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025971 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000025972 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025973 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000025974 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025975 } else {
25976 /*
25977 * Fallback to "anyType".
25978 *
25979 * SPEC (cvc-assess-elt)
25980 * "If the item cannot be ·strictly assessed·, [...]
25981 * an element information item's schema validity may be laxly
25982 * assessed if its ·context-determined declaration· is not
25983 * skip by ·validating· with respect to the ·ur-type
25984 * definition· as per Element Locally Valid (Type) (§3.3.4)."
25985 */
25986 vctxt->inode->typeDef =
25987 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025988 }
25989 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025990 return (0);
25991 }
25992
25993 switch (ptype->contentType) {
25994 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025995 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025996 * SPEC (2.1) "If the {content type} is empty, then the
25997 * element information item has no character or element
25998 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025999 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026000 ACTIVATE_PARENT_ELEM
26001 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26002 VERROR(ret, NULL,
26003 "Element content is not allowed, "
26004 "because the content type is empty");
26005 ACTIVATE_ELEM
26006 goto unexpected_elem;
26007 break;
26008
26009 case XML_SCHEMA_CONTENT_MIXED:
26010 case XML_SCHEMA_CONTENT_ELEMENTS: {
26011 xmlRegExecCtxtPtr regexCtxt;
26012 xmlChar *values[10];
26013 int terminal, nbval = 10, nbneg;
26014
26015 /* VAL TODO: Optimized "anyType" validation.*/
26016
26017 if (ptype->contModel == NULL) {
26018 VERROR_INT("xmlSchemaValidateChildElem",
26019 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026020 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026021 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026022 /*
26023 * Safety belf for evaluation if the cont. model was already
26024 * examined to be invalid.
26025 */
26026 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26027 VERROR_INT("xmlSchemaValidateChildElem",
26028 "validating elem, but elem content is already invalid");
26029 return (-1);
26030 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026031
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026032 regexCtxt = pielem->regexCtxt;
26033 if (regexCtxt == NULL) {
26034 /*
26035 * Create the regex context.
26036 */
26037 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26038 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26039 vctxt);
26040 if (regexCtxt == NULL) {
26041 VERROR_INT("xmlSchemaValidateChildElem",
26042 "failed to create a regex context");
26043 return (-1);
26044 }
26045 pielem->regexCtxt = regexCtxt;
26046#ifdef DEBUG_AUTOMATA
26047 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26048 pielem->localName);
26049#endif
26050 }
26051
26052 /*
26053 * SPEC (2.4) "If the {content type} is element-only or mixed,
26054 * then the sequence of the element information item's
26055 * element information item [children], if any, taken in
26056 * order, is ·valid· with respect to the {content type}'s
26057 * particle, as defined in Element Sequence Locally Valid
26058 * (Particle) (§3.9.4)."
26059 */
26060 ret = xmlRegExecPushString2(regexCtxt,
26061 vctxt->inode->localName,
26062 vctxt->inode->nsName,
26063 vctxt->inode);
26064#ifdef DEBUG_AUTOMATA
26065 if (ret < 0)
26066 xmlGenericError(xmlGenericErrorContext,
26067 "AUTOMATON push ERROR for '%s' on '%s'\n",
26068 vctxt->inode->localName, pielem->localName);
26069 else
26070 xmlGenericError(xmlGenericErrorContext,
26071 "AUTOMATON push OK for '%s' on '%s'\n",
26072 vctxt->inode->localName, pielem->localName);
26073#endif
26074 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26075 VERROR_INT("xmlSchemaValidateChildElem",
26076 "calling xmlRegExecPushString2()");
26077 return (-1);
26078 }
26079 if (ret < 0) {
26080 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26081 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026082 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026083 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26084 "This element is not expected",
26085 nbval, nbneg, values);
26086 ret = vctxt->err;
26087 goto unexpected_elem;
26088 } else
26089 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026090 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026091 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026092 case XML_SCHEMA_CONTENT_SIMPLE:
26093 case XML_SCHEMA_CONTENT_BASIC:
26094 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026095 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026096 /*
26097 * SPEC (cvc-complex-type) (2.2)
26098 * "If the {content type} is a simple type definition, then
26099 * the element information item has no element information
26100 * item [children], ..."
26101 */
26102 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26103 VERROR(ret, NULL, "Element content is not allowed, "
26104 "because the content type is a simple type definition");
26105 } else {
26106 /*
26107 * SPEC (cvc-type) (3.1.2) "The element information item must
26108 * have no element information item [children]."
26109 */
26110 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26111 VERROR(ret, NULL, "Element content is not allowed, "
26112 "because the type definition is simple");
26113 }
26114 ACTIVATE_ELEM
26115 ret = vctxt->err;
26116 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026117 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026118
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026119 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026120 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026121 }
26122 return (ret);
26123unexpected_elem:
26124 /*
26125 * Pop this element and set the skipDepth to skip
26126 * all further content of the parent element.
26127 */
26128 vctxt->skipDepth = vctxt->depth;
26129 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26130 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26131 return (ret);
26132}
26133
26134#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26135#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26136#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26137
26138static int
26139xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26140 int nodeType, const xmlChar *value, int len,
26141 int mode, int *consumed)
26142{
26143 /*
26144 * Unfortunately we have to duplicate the text sometimes.
26145 * OPTIMIZE: Maybe we could skip it, if:
26146 * 1. content type is simple
26147 * 2. whitespace is "collapse"
26148 * 3. it consists of whitespace only
26149 *
26150 * Process character content.
26151 */
26152 if (consumed != NULL)
26153 *consumed = 0;
26154 if (INODE_NILLED(vctxt->inode)) {
26155 /*
26156 * SPEC cvc-elt (3.3.4 - 3.2.1)
26157 * "The element information item must have no character or
26158 * element information item [children]."
26159 */
26160 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26161 "Neither character nor element content is allowed "
26162 "because the element is 'nilled'");
26163 return (vctxt->err);
26164 }
26165 /*
26166 * SPEC (2.1) "If the {content type} is empty, then the
26167 * element information item has no character or element
26168 * information item [children]."
26169 */
26170 if (vctxt->inode->typeDef->contentType ==
26171 XML_SCHEMA_CONTENT_EMPTY) {
26172 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26173 "Character content is not allowed, "
26174 "because the content type is empty");
26175 return (vctxt->err);
26176 }
26177
26178 if (vctxt->inode->typeDef->contentType ==
26179 XML_SCHEMA_CONTENT_ELEMENTS) {
26180 if ((nodeType != XML_TEXT_NODE) ||
26181 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26182 /*
26183 * SPEC cvc-complex-type (2.3)
26184 * "If the {content type} is element-only, then the
26185 * element information item has no character information
26186 * item [children] other than those whose [character
26187 * code] is defined as a white space in [XML 1.0 (Second
26188 * Edition)]."
26189 */
26190 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26191 "Character content other than whitespace is not allowed "
26192 "because the content type is 'element-only'");
26193 return (vctxt->err);
26194 }
26195 return (0);
26196 }
26197
26198 if ((value == NULL) || (value[0] == 0))
26199 return (0);
26200 /*
26201 * Save the value.
26202 * NOTE that even if the content type is *mixed*, we need the
26203 * *initial value* for default/fixed value constraints.
26204 */
26205 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26206 ((vctxt->inode->decl == NULL) ||
26207 (vctxt->inode->decl->value == NULL)))
26208 return (0);
26209
26210 if (vctxt->inode->value == NULL) {
26211 /*
26212 * Set the value.
26213 */
26214 switch (mode) {
26215 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26216 /*
26217 * When working on a tree.
26218 */
26219 vctxt->inode->value = value;
26220 break;
26221 case XML_SCHEMA_PUSH_TEXT_CREATED:
26222 /*
26223 * When working with the reader.
26224 * The value will be freed by the element info.
26225 */
26226 vctxt->inode->value = value;
26227 if (consumed != NULL)
26228 *consumed = 1;
26229 vctxt->inode->flags |=
26230 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26231 break;
26232 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26233 /*
26234 * When working with SAX.
26235 * The value will be freed by the element info.
26236 */
26237 if (len != -1)
26238 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26239 else
26240 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26241 vctxt->inode->flags |=
26242 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26243 break;
26244 default:
26245 break;
26246 }
26247 } else {
26248 /*
26249 * Concat the value.
26250 */
26251 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026252 vctxt->inode->value = BAD_CAST xmlStrncat(
26253 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026254 } else {
26255 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026256 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026257 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26258 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026259 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026260
26261 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026262}
26263
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026264static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026265xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026266{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026267 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026268
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026269 if ((vctxt->skipDepth != -1) &&
26270 (vctxt->depth >= vctxt->skipDepth)) {
26271 VERROR_INT("xmlSchemaValidateElem",
26272 "in skip-state");
26273 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026274 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026275 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026276 /*
26277 * URGENT TODO: Better to fully stop validation
26278 * if there was an error during dynamic schema construction.
26279 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026280 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26281 goto internal_error;
26282 }
26283 if (vctxt->depth > 0) {
26284 /*
26285 * Validate this element against the content model
26286 * of the parent.
26287 */
26288 ret = xmlSchemaValidateChildElem(vctxt);
26289 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026290 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026291 VERROR_INT("xmlSchemaValidateElem",
26292 "calling xmlSchemaStreamValidateChildElement()");
26293 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026294 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026295 goto exit;
26296 }
26297 if (vctxt->depth == vctxt->skipDepth)
26298 goto exit;
26299 if ((vctxt->inode->decl == NULL) &&
26300 (vctxt->inode->typeDef == NULL)) {
26301 VERROR_INT("xmlSchemaValidateElem",
26302 "the child element was valid but neither the "
26303 "declaration nor the type was set");
26304 goto internal_error;
26305 }
26306 } else {
26307 /*
26308 * Get the declaration of the validation root.
26309 */
26310 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26311 vctxt->inode->localName,
26312 vctxt->inode->nsName);
26313 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026314 ret = XML_SCHEMAV_CVC_ELT_1;
26315 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026316 "No matching global declaration available "
26317 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026318 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026319 }
26320 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026321
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026322 if (vctxt->inode->decl == NULL)
26323 goto type_validation;
26324
26325 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26326 int skip;
26327 /*
26328 * Wildcards.
26329 */
26330 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26331 if (ret != 0) {
26332 if (ret < 0) {
26333 VERROR_INT("xmlSchemaValidateElem",
26334 "calling xmlSchemaValidateElemWildcard()");
26335 goto internal_error;
26336 }
26337 goto exit;
26338 }
26339 if (skip) {
26340 vctxt->skipDepth = vctxt->depth;
26341 goto exit;
26342 }
26343 /*
26344 * The declaration might be set by the wildcard validation,
26345 * when the processContents is "lax" or "strict".
26346 */
26347 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26348 /*
26349 * Clear the "decl" field to not confuse further processing.
26350 */
26351 vctxt->inode->decl = NULL;
26352 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026353 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026354 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026355 /*
26356 * Validate against the declaration.
26357 */
26358 ret = xmlSchemaValidateElemDecl(vctxt);
26359 if (ret != 0) {
26360 if (ret < 0) {
26361 VERROR_INT("xmlSchemaValidateElem",
26362 "calling xmlSchemaValidateElemDecl()");
26363 goto internal_error;
26364 }
26365 goto exit;
26366 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026367 /*
26368 * Validate against the type definition.
26369 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026370type_validation:
26371
26372 if (vctxt->inode->typeDef == NULL) {
26373 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26374 ret = XML_SCHEMAV_CVC_TYPE_1;
26375 VERROR(ret, NULL,
26376 "The type definition is absent");
26377 goto exit;
26378 }
26379 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26380 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26381 ret = XML_SCHEMAV_CVC_TYPE_2;
26382 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026383 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026384 goto exit;
26385 }
26386 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026387 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026388 * during validation against the declaration. This must be done
26389 * _before_ attribute validation.
26390 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026391 if (vctxt->xpathStates != NULL) {
26392 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
26393 if (ret == -1) {
26394 VERROR_INT("xmlSchemaValidateElem",
26395 "calling xmlSchemaXPathEvaluate()");
26396 goto internal_error;
26397 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026398 }
26399 /*
26400 * Validate attributes.
26401 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026402 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026403 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026404 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026405
26406 ret = xmlSchemaVAttributesComplex(vctxt);
26407 }
26408 } else if (vctxt->nbAttrInfos != 0) {
26409
26410 ret = xmlSchemaVAttributesSimple(vctxt);
26411 }
26412 /*
26413 * Clear registered attributes.
26414 */
26415 if (vctxt->nbAttrInfos != 0)
26416 xmlSchemaClearAttrInfos(vctxt);
26417 if (ret == -1) {
26418 VERROR_INT("xmlSchemaValidateElem",
26419 "calling attributes validation");
26420 goto internal_error;
26421 }
26422 /*
26423 * Don't return an error if attributes are invalid on purpose.
26424 */
26425 ret = 0;
26426
26427exit:
26428 if (ret != 0)
26429 vctxt->skipDepth = vctxt->depth;
26430 return (ret);
26431internal_error:
26432 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026433}
26434
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026435#ifdef XML_SCHEMA_READER_ENABLED
26436static int
26437xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026438{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026439 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26440 int depth, nodeType, ret = 0, consumed;
26441 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026442
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026443 vctxt->depth = -1;
26444 ret = xmlTextReaderRead(vctxt->reader);
26445 /*
26446 * Move to the document element.
26447 */
26448 while (ret == 1) {
26449 nodeType = xmlTextReaderNodeType(vctxt->reader);
26450 if (nodeType == XML_ELEMENT_NODE)
26451 goto root_found;
26452 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026453 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026454 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026455
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026456root_found:
26457
26458 do {
26459 depth = xmlTextReaderDepth(vctxt->reader);
26460 nodeType = xmlTextReaderNodeType(vctxt->reader);
26461
26462 if (nodeType == XML_ELEMENT_NODE) {
26463
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026464 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026465 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26466 VERROR_INT("xmlSchemaVReaderWalk",
26467 "calling xmlSchemaValidatorPushElem()");
26468 goto internal_error;
26469 }
26470 ielem = vctxt->inode;
26471 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26472 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26473 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26474 /*
26475 * Is the element empty?
26476 */
26477 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26478 if (ret == -1) {
26479 VERROR_INT("xmlSchemaVReaderWalk",
26480 "calling xmlTextReaderIsEmptyElement()");
26481 goto internal_error;
26482 }
26483 if (ret) {
26484 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26485 }
26486 /*
26487 * Register attributes.
26488 */
26489 vctxt->nbAttrInfos = 0;
26490 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26491 if (ret == -1) {
26492 VERROR_INT("xmlSchemaVReaderWalk",
26493 "calling xmlTextReaderMoveToFirstAttribute()");
26494 goto internal_error;
26495 }
26496 if (ret == 1) {
26497 do {
26498 /*
26499 * VAL TODO: How do we know that the reader works on a
26500 * node tree, to be able to pass a node here?
26501 */
26502 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26503 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26504 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26505 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26506
26507 VERROR_INT("xmlSchemaVReaderWalk",
26508 "calling xmlSchemaValidatorPushAttribute()");
26509 goto internal_error;
26510 }
26511 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26512 if (ret == -1) {
26513 VERROR_INT("xmlSchemaVReaderWalk",
26514 "calling xmlTextReaderMoveToFirstAttribute()");
26515 goto internal_error;
26516 }
26517 } while (ret == 1);
26518 /*
26519 * Back to element position.
26520 */
26521 ret = xmlTextReaderMoveToElement(vctxt->reader);
26522 if (ret == -1) {
26523 VERROR_INT("xmlSchemaVReaderWalk",
26524 "calling xmlTextReaderMoveToElement()");
26525 goto internal_error;
26526 }
26527 }
26528 /*
26529 * Validate the element.
26530 */
26531 ret= xmlSchemaValidateElem(vctxt);
26532 if (ret != 0) {
26533 if (ret == -1) {
26534 VERROR_INT("xmlSchemaVReaderWalk",
26535 "calling xmlSchemaValidateElem()");
26536 goto internal_error;
26537 }
26538 goto exit;
26539 }
26540 if (vctxt->depth == vctxt->skipDepth) {
26541 int curDepth;
26542 /*
26543 * Skip all content.
26544 */
26545 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26546 ret = xmlTextReaderRead(vctxt->reader);
26547 curDepth = xmlTextReaderDepth(vctxt->reader);
26548 while ((ret == 1) && (curDepth != depth)) {
26549 ret = xmlTextReaderRead(vctxt->reader);
26550 curDepth = xmlTextReaderDepth(vctxt->reader);
26551 }
26552 if (ret < 0) {
26553 /*
26554 * VAL TODO: A reader error occured; what to do here?
26555 */
26556 ret = 1;
26557 goto exit;
26558 }
26559 }
26560 goto leave_elem;
26561 }
26562 /*
26563 * READER VAL TODO: Is an END_ELEM really never called
26564 * if the elem is empty?
26565 */
26566 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26567 goto leave_elem;
26568 } else if (nodeType == END_ELEM) {
26569 /*
26570 * Process END of element.
26571 */
26572leave_elem:
26573 ret = xmlSchemaValidatorPopElem(vctxt);
26574 if (ret != 0) {
26575 if (ret < 0) {
26576 VERROR_INT("xmlSchemaVReaderWalk",
26577 "calling xmlSchemaValidatorPopElem()");
26578 goto internal_error;
26579 }
26580 goto exit;
26581 }
26582 if (vctxt->depth >= 0)
26583 ielem = vctxt->inode;
26584 else
26585 ielem = NULL;
26586 } else if ((nodeType == XML_TEXT_NODE) ||
26587 (nodeType == XML_CDATA_SECTION_NODE) ||
26588 (nodeType == WHTSP) ||
26589 (nodeType == SIGN_WHTSP)) {
26590 /*
26591 * Process character content.
26592 */
26593 xmlChar *value;
26594
26595 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26596 nodeType = XML_TEXT_NODE;
26597
26598 value = xmlTextReaderValue(vctxt->reader);
26599 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26600 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26601 if (! consumed)
26602 xmlFree(value);
26603 if (ret == -1) {
26604 VERROR_INT("xmlSchemaVReaderWalk",
26605 "calling xmlSchemaVPushText()");
26606 goto internal_error;
26607 }
26608 } else if ((nodeType == XML_ENTITY_NODE) ||
26609 (nodeType == XML_ENTITY_REF_NODE)) {
26610 /*
26611 * VAL TODO: What to do with entities?
26612 */
26613 TODO
26614 }
26615 /*
26616 * Read next node.
26617 */
26618 ret = xmlTextReaderRead(vctxt->reader);
26619 } while (ret == 1);
26620
26621exit:
26622 return (ret);
26623internal_error:
26624 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026625}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026626#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026627
26628/************************************************************************
26629 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026630 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026631 * *
26632 ************************************************************************/
26633
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026634/*
26635* Process text content.
26636*/
26637static void
26638xmlSchemaSAXHandleText(void *ctx,
26639 const xmlChar * ch,
26640 int len)
26641{
26642 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26643
26644 if (vctxt->depth < 0)
26645 return;
26646 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26647 return;
26648 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26649 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26650 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26651 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26652 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26653 "calling xmlSchemaVPushText()");
26654 vctxt->err = -1;
26655 xmlStopParser(vctxt->parserCtxt);
26656 }
26657}
26658
26659/*
26660* Process CDATA content.
26661*/
26662static void
26663xmlSchemaSAXHandleCDataSection(void *ctx,
26664 const xmlChar * ch,
26665 int len)
26666{
26667 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26668
26669 if (vctxt->depth < 0)
26670 return;
26671 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26672 return;
26673 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26674 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26675 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26676 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26677 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26678 "calling xmlSchemaVPushText()");
26679 vctxt->err = -1;
26680 xmlStopParser(vctxt->parserCtxt);
26681 }
26682}
26683
26684static void
26685xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26686 const xmlChar * name ATTRIBUTE_UNUSED)
26687{
26688 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26689
26690 if (vctxt->depth < 0)
26691 return;
26692 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26693 return;
26694 /* SAX VAL TODO: What to do here? */
26695 TODO
26696}
26697
26698static void
26699xmlSchemaSAXHandleStartElementNs(void *ctx,
26700 const xmlChar * localname,
26701 const xmlChar * prefix ATTRIBUTE_UNUSED,
26702 const xmlChar * URI,
26703 int nb_namespaces,
26704 const xmlChar ** namespaces,
26705 int nb_attributes,
26706 int nb_defaulted ATTRIBUTE_UNUSED,
26707 const xmlChar ** attributes)
26708{
26709 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26710 int ret;
26711 xmlSchemaNodeInfoPtr ielem;
26712 int i, j;
26713
26714 /*
26715 * SAX VAL TODO: What to do with nb_defaulted?
26716 */
26717 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026718 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026719 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026720 vctxt->depth++;
26721 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026722 return;
26723 /*
26724 * Push the element.
26725 */
26726 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26727 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26728 "calling xmlSchemaValidatorPushElem()");
26729 goto internal_error;
26730 }
26731 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026732 /*
26733 * TODO: Is this OK?
26734 */
26735 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026736 ielem->localName = localname;
26737 ielem->nsName = URI;
26738 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26739 /*
26740 * Register namespaces on the elem info.
26741 */
26742 if (nb_namespaces != 0) {
26743 /*
26744 * Although the parser builds its own namespace list,
26745 * we have no access to it, so we'll use an own one.
26746 */
26747 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26748 /*
26749 * Store prefix and namespace name.
26750 */
26751 if (ielem->nsBindings == NULL) {
26752 ielem->nsBindings =
26753 (const xmlChar **) xmlMalloc(10 *
26754 sizeof(const xmlChar *));
26755 if (ielem->nsBindings == NULL) {
26756 xmlSchemaVErrMemory(vctxt,
26757 "allocating namespace bindings for SAX validation",
26758 NULL);
26759 goto internal_error;
26760 }
26761 ielem->nbNsBindings = 0;
26762 ielem->sizeNsBindings = 5;
26763 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26764 ielem->sizeNsBindings *= 2;
26765 ielem->nsBindings =
26766 (const xmlChar **) xmlRealloc(
26767 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026768 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026769 if (ielem->nsBindings == NULL) {
26770 xmlSchemaVErrMemory(vctxt,
26771 "re-allocating namespace bindings for SAX validation",
26772 NULL);
26773 goto internal_error;
26774 }
26775 }
26776
26777 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26778 if (namespaces[j+1][0] == 0) {
26779 /*
26780 * Handle xmlns="".
26781 */
26782 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26783 } else
26784 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26785 namespaces[j+1];
26786 ielem->nbNsBindings++;
26787 }
26788 }
26789 /*
26790 * Register attributes.
26791 * SAX VAL TODO: We are not adding namespace declaration
26792 * attributes yet.
26793 */
26794 if (nb_attributes != 0) {
26795 xmlChar *value;
26796
26797 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26798 /*
26799 * Duplicate the value.
26800 */
26801 value = xmlStrndup(attributes[j+3],
26802 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026803 /*
26804 * TODO: Set the node line.
26805 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026806 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026807 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026808 value, 1);
26809 if (ret == -1) {
26810 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26811 "calling xmlSchemaValidatorPushAttribute()");
26812 goto internal_error;
26813 }
26814 }
26815 }
26816 /*
26817 * Validate the element.
26818 */
26819 ret = xmlSchemaValidateElem(vctxt);
26820 if (ret != 0) {
26821 if (ret == -1) {
26822 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26823 "calling xmlSchemaValidateElem()");
26824 goto internal_error;
26825 }
26826 goto exit;
26827 }
26828
26829exit:
26830 return;
26831internal_error:
26832 vctxt->err = -1;
26833 xmlStopParser(vctxt->parserCtxt);
26834 return;
26835}
26836
26837static void
26838xmlSchemaSAXHandleEndElementNs(void *ctx,
26839 const xmlChar * localname ATTRIBUTE_UNUSED,
26840 const xmlChar * prefix ATTRIBUTE_UNUSED,
26841 const xmlChar * URI ATTRIBUTE_UNUSED)
26842{
26843 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26844 int res;
26845
26846 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026847 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026848 */
26849 if (vctxt->skipDepth != -1) {
26850 if (vctxt->depth > vctxt->skipDepth) {
26851 vctxt->depth--;
26852 return;
26853 } else
26854 vctxt->skipDepth = -1;
26855 }
26856 /*
26857 * SAX VAL TODO: Just a temporary check.
26858 */
26859 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26860 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26861 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26862 "elem pop mismatch");
26863 }
26864 res = xmlSchemaValidatorPopElem(vctxt);
26865 if (res != 0) {
26866 if (res < 0) {
26867 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26868 "calling xmlSchemaValidatorPopElem()");
26869 goto internal_error;
26870 }
26871 goto exit;
26872 }
26873exit:
26874 return;
26875internal_error:
26876 vctxt->err = -1;
26877 xmlStopParser(vctxt->parserCtxt);
26878 return;
26879}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026880
Daniel Veillard4255d502002-04-16 15:50:10 +000026881/************************************************************************
26882 * *
26883 * Validation interfaces *
26884 * *
26885 ************************************************************************/
26886
26887/**
26888 * xmlSchemaNewValidCtxt:
26889 * @schema: a precompiled XML Schemas
26890 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026891 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026892 *
26893 * Returns the validation context or NULL in case of error
26894 */
26895xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026896xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26897{
Daniel Veillard4255d502002-04-16 15:50:10 +000026898 xmlSchemaValidCtxtPtr ret;
26899
26900 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26901 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026902 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026903 return (NULL);
26904 }
26905 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026906 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026907 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000026908 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026909 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000026910 return (ret);
26911}
26912
26913/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026914 * xmlSchemaClearValidCtxt:
26915 * @ctxt: the schema validation context
26916 *
26917 * Free the resources associated to the schema validation context;
26918 * leaves some fields alive intended for reuse of the context.
26919 */
26920static void
26921xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
26922{
26923 if (vctxt == NULL)
26924 return;
26925
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026926 /*
26927 * TODO: Should we clear the flags?
26928 * Might be problematic if one reuses the context
26929 * and assumes that the options remain the same.
26930 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026931 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026932 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026933 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026934#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026935 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026936#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026937 vctxt->hasKeyrefs = 0;
26938
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026939 if (vctxt->value != NULL) {
26940 xmlSchemaFreeValue(vctxt->value);
26941 vctxt->value = NULL;
26942 }
26943 /*
26944 * Augmented IDC information.
26945 */
26946 if (vctxt->aidcs != NULL) {
26947 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
26948 do {
26949 next = cur->next;
26950 xmlFree(cur);
26951 cur = next;
26952 } while (cur != NULL);
26953 vctxt->aidcs = NULL;
26954 }
26955 if (vctxt->idcNodes != NULL) {
26956 int i;
26957 xmlSchemaPSVIIDCNodePtr item;
26958
26959 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026960 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026961 xmlFree(item->keys);
26962 xmlFree(item);
26963 }
26964 xmlFree(vctxt->idcNodes);
26965 vctxt->idcNodes = NULL;
26966 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026967 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026968 * Note that we won't delete the XPath state pool here.
26969 */
26970 if (vctxt->xpathStates != NULL) {
26971 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
26972 vctxt->xpathStates = NULL;
26973 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026974 /*
26975 * Attribute info.
26976 */
26977 if (vctxt->nbAttrInfos != 0) {
26978 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026979 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026980 /*
26981 * Element info.
26982 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026983 if (vctxt->elemInfos != NULL) {
26984 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026985 xmlSchemaNodeInfoPtr ei;
26986
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026987 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026988 ei = vctxt->elemInfos[i];
26989 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026990 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026991 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026992 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026993 }
26994 xmlSchemaItemListClear(vctxt->nodeQNames);
26995 /* Recreate the dict. */
26996 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026997 /*
26998 * TODO: Is is save to recreate it? Do we have a scenario
26999 * where the user provides the dict?
27000 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027001 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027002}
27003
27004/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027005 * xmlSchemaFreeValidCtxt:
27006 * @ctxt: the schema validation context
27007 *
27008 * Free the resources associated to the schema validation context
27009 */
27010void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027011xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27012{
Daniel Veillard4255d502002-04-16 15:50:10 +000027013 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027014 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027015 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027016 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027017 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027018 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027019 if (ctxt->idcNodes != NULL) {
27020 int i;
27021 xmlSchemaPSVIIDCNodePtr item;
27022
27023 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027024 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027025 xmlFree(item->keys);
27026 xmlFree(item);
27027 }
27028 xmlFree(ctxt->idcNodes);
27029 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027030 if (ctxt->idcKeys != NULL) {
27031 int i;
27032 for (i = 0; i < ctxt->nbIdcKeys; i++)
27033 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27034 xmlFree(ctxt->idcKeys);
27035 }
27036
27037 if (ctxt->xpathStates != NULL)
27038 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27039 if (ctxt->xpathStatePool != NULL)
27040 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27041
27042 /*
27043 * Augmented IDC information.
27044 */
27045 if (ctxt->aidcs != NULL) {
27046 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27047 do {
27048 next = cur->next;
27049 xmlFree(cur);
27050 cur = next;
27051 } while (cur != NULL);
27052 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027053 if (ctxt->attrInfos != NULL) {
27054 int i;
27055 xmlSchemaAttrInfoPtr attr;
27056
27057 /* Just a paranoid call to the cleanup. */
27058 if (ctxt->nbAttrInfos != 0)
27059 xmlSchemaClearAttrInfos(ctxt);
27060 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27061 attr = ctxt->attrInfos[i];
27062 xmlFree(attr);
27063 }
27064 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027065 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027066 if (ctxt->elemInfos != NULL) {
27067 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027068 xmlSchemaNodeInfoPtr ei;
27069
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027070 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027071 ei = ctxt->elemInfos[i];
27072 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027073 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027074 xmlSchemaClearElemInfo(ei);
27075 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027076 }
27077 xmlFree(ctxt->elemInfos);
27078 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027079 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027080 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027081 if (ctxt->dict != NULL)
27082 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027083 xmlFree(ctxt);
27084}
27085
27086/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027087 * xmlSchemaIsValid:
27088 * @ctxt: the schema validation context
27089 *
27090 * Check if any error was detected during validation.
27091 *
27092 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27093 * of internal error.
27094 */
27095int
27096xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27097{
27098 if (ctxt == NULL)
27099 return(-1);
27100 return(ctxt->err == 0);
27101}
27102
27103/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027104 * xmlSchemaSetValidErrors:
27105 * @ctxt: a schema validation context
27106 * @err: the error function
27107 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027108 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027109 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027110 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027111 */
27112void
27113xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027114 xmlSchemaValidityErrorFunc err,
27115 xmlSchemaValidityWarningFunc warn, void *ctx)
27116{
Daniel Veillard4255d502002-04-16 15:50:10 +000027117 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027118 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027119 ctxt->error = err;
27120 ctxt->warning = warn;
27121 ctxt->userData = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027122 if (ctxt->pctxt != NULL)
27123 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027124}
27125
27126/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027127 * xmlSchemaSetValidStructuredErrors:
27128 * @ctxt: a schema validation context
27129 * @serror: the structured error function
27130 * @ctx: the functions context
27131 *
27132 * Set the structured error callback
27133 */
27134void
27135xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
27136 xmlStructuredErrorFunc serror, void *ctx)
27137{
27138 if (ctxt == NULL)
27139 return;
27140 ctxt->serror = serror;
27141 ctxt->error = NULL;
27142 ctxt->warning = NULL;
27143 ctxt->userData = ctx;
27144}
27145
27146/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027147 * xmlSchemaGetValidErrors:
27148 * @ctxt: a XML-Schema validation context
27149 * @err: the error function result
27150 * @warn: the warning function result
27151 * @ctx: the functions context result
27152 *
27153 * Get the error and warning callback informations
27154 *
27155 * Returns -1 in case of error and 0 otherwise
27156 */
27157int
27158xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
27159 xmlSchemaValidityErrorFunc * err,
27160 xmlSchemaValidityWarningFunc * warn, void **ctx)
27161{
27162 if (ctxt == NULL)
27163 return (-1);
27164 if (err != NULL)
27165 *err = ctxt->error;
27166 if (warn != NULL)
27167 *warn = ctxt->warning;
27168 if (ctx != NULL)
27169 *ctx = ctxt->userData;
27170 return (0);
27171}
27172
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027173
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027174/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027175 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027176 * @ctxt: a schema validation context
27177 * @options: a combination of xmlSchemaValidOption
27178 *
27179 * Sets the options to be used during the validation.
27180 *
27181 * Returns 0 in case of success, -1 in case of an
27182 * API error.
27183 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027184int
27185xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27186 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027187
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027188{
27189 int i;
27190
27191 if (ctxt == NULL)
27192 return (-1);
27193 /*
27194 * WARNING: Change the start value if adding to the
27195 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027196 * TODO: Is there an other, more easy to maintain,
27197 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027198 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027199 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027200 if (options & 1<<i)
27201 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027202 }
27203 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027204 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027205}
27206
27207/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027208 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027209 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027210 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027211 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027212 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027213 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027214 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027215int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027216xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027217
27218{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027219 if (ctxt == NULL)
27220 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027221 else
27222 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027223}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027224
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027225static int
27226xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27227{
27228 xmlAttrPtr attr;
27229 int ret = 0;
27230 xmlSchemaNodeInfoPtr ielem = NULL;
27231 xmlNodePtr node, valRoot;
27232 const xmlChar *nsName;
27233
27234 /* DOC VAL TODO: Move this to the start function. */
27235 valRoot = xmlDocGetRootElement(vctxt->doc);
27236 if (valRoot == NULL) {
27237 /* VAL TODO: Error code? */
27238 VERROR(1, NULL, "The document has no document element");
27239 return (1);
27240 }
27241 vctxt->depth = -1;
27242 vctxt->validationRoot = valRoot;
27243 node = valRoot;
27244 while (node != NULL) {
27245 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27246 goto next_sibling;
27247 if (node->type == XML_ELEMENT_NODE) {
27248
27249 /*
27250 * Init the node-info.
27251 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027252 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027253 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27254 goto internal_error;
27255 ielem = vctxt->inode;
27256 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027257 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027258 ielem->localName = node->name;
27259 if (node->ns != NULL)
27260 ielem->nsName = node->ns->href;
27261 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27262 /*
27263 * Register attributes.
27264 * DOC VAL TODO: We do not register namespace declaration
27265 * attributes yet.
27266 */
27267 vctxt->nbAttrInfos = 0;
27268 if (node->properties != NULL) {
27269 attr = node->properties;
27270 do {
27271 if (attr->ns != NULL)
27272 nsName = attr->ns->href;
27273 else
27274 nsName = NULL;
27275 ret = xmlSchemaValidatorPushAttribute(vctxt,
27276 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027277 /*
27278 * Note that we give it the line number of the
27279 * parent element.
27280 */
27281 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027282 attr->name, nsName, 0,
27283 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27284 if (ret == -1) {
27285 VERROR_INT("xmlSchemaDocWalk",
27286 "calling xmlSchemaValidatorPushAttribute()");
27287 goto internal_error;
27288 }
27289 attr = attr->next;
27290 } while (attr);
27291 }
27292 /*
27293 * Validate the element.
27294 */
27295 ret = xmlSchemaValidateElem(vctxt);
27296 if (ret != 0) {
27297 if (ret == -1) {
27298 VERROR_INT("xmlSchemaDocWalk",
27299 "calling xmlSchemaValidateElem()");
27300 goto internal_error;
27301 }
27302 /*
27303 * Don't stop validation; just skip the content
27304 * of this element.
27305 */
27306 goto leave_node;
27307 }
27308 if ((vctxt->skipDepth != -1) &&
27309 (vctxt->depth >= vctxt->skipDepth))
27310 goto leave_node;
27311 } else if ((node->type == XML_TEXT_NODE) ||
27312 (node->type == XML_CDATA_SECTION_NODE)) {
27313 /*
27314 * Process character content.
27315 */
27316 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27317 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27318 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27319 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27320 if (ret < 0) {
27321 VERROR_INT("xmlSchemaVDocWalk",
27322 "calling xmlSchemaVPushText()");
27323 goto internal_error;
27324 }
27325 /*
27326 * DOC VAL TODO: Should we skip further validation of the
27327 * element content here?
27328 */
27329 } else if ((node->type == XML_ENTITY_NODE) ||
27330 (node->type == XML_ENTITY_REF_NODE)) {
27331 /*
27332 * DOC VAL TODO: What to do with entities?
27333 */
27334 TODO
27335 } else {
27336 goto leave_node;
27337 /*
27338 * DOC VAL TODO: XInclude nodes, etc.
27339 */
27340 }
27341 /*
27342 * Walk the doc.
27343 */
27344 if (node->children != NULL) {
27345 node = node->children;
27346 continue;
27347 }
27348leave_node:
27349 if (node->type == XML_ELEMENT_NODE) {
27350 /*
27351 * Leaving the scope of an element.
27352 */
27353 if (node != vctxt->inode->node) {
27354 VERROR_INT("xmlSchemaVDocWalk",
27355 "element position mismatch");
27356 goto internal_error;
27357 }
27358 ret = xmlSchemaValidatorPopElem(vctxt);
27359 if (ret != 0) {
27360 if (ret < 0) {
27361 VERROR_INT("xmlSchemaVDocWalk",
27362 "calling xmlSchemaValidatorPopElem()");
27363 goto internal_error;
27364 }
27365 }
27366 if (node == valRoot)
27367 goto exit;
27368 }
27369next_sibling:
27370 if (node->next != NULL)
27371 node = node->next;
27372 else {
27373 node = node->parent;
27374 goto leave_node;
27375 }
27376 }
27377
27378exit:
27379 return (ret);
27380internal_error:
27381 return (-1);
27382}
27383
27384static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027385xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027386 /*
27387 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027388 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027389 vctxt->err = 0;
27390 vctxt->nberrors = 0;
27391 vctxt->depth = -1;
27392 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027393 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027394 vctxt->hasKeyrefs = 0;
27395#ifdef ENABLE_IDC_NODE_TABLES
27396 vctxt->createIDCNodeTables = 1;
27397#else
27398 vctxt->createIDCNodeTables = 0;
27399#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027400 /*
27401 * Create a schema + parser if necessary.
27402 */
27403 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027404 xmlSchemaParserCtxtPtr pctxt;
27405
27406 vctxt->xsiAssemble = 1;
27407 /*
27408 * If not schema was given then we will create a schema
27409 * dynamically using XSI schema locations.
27410 *
27411 * Create the schema parser context.
27412 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027413 if ((vctxt->pctxt == NULL) &&
27414 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27415 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027416 pctxt = vctxt->pctxt;
27417 pctxt->xsiAssemble = 1;
27418 /*
27419 * Create the schema.
27420 */
27421 vctxt->schema = xmlSchemaNewSchema(pctxt);
27422 if (vctxt->schema == NULL)
27423 return (-1);
27424 /*
27425 * Create the schema construction context.
27426 */
27427 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27428 if (pctxt->constructor == NULL)
27429 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027430 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027431 /*
27432 * Take ownership of the constructor to be able to free it.
27433 */
27434 pctxt->ownsConstructor = 1;
27435 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027436 /*
27437 * Augment the IDC definitions.
27438 */
27439 if (vctxt->schema->idcDef != NULL) {
27440 xmlHashScan(vctxt->schema->idcDef,
27441 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27442 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027443 return(0);
27444}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027445
Daniel Veillardf10ae122005-07-10 19:03:16 +000027446static void
27447xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027448 if (vctxt->xsiAssemble) {
27449 if (vctxt->schema != NULL) {
27450 xmlSchemaFree(vctxt->schema);
27451 vctxt->schema = NULL;
27452 }
27453 }
27454 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027455}
27456
27457static int
27458xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27459{
27460 int ret = 0;
27461
27462 if (xmlSchemaPreRun(vctxt) < 0)
27463 return(-1);
27464
27465 if (vctxt->doc != NULL) {
27466 /*
27467 * Tree validation.
27468 */
27469 ret = xmlSchemaVDocWalk(vctxt);
27470#ifdef LIBXML_READER_ENABLED
27471 } else if (vctxt->reader != NULL) {
27472 /*
27473 * XML Reader validation.
27474 */
27475#ifdef XML_SCHEMA_READER_ENABLED
27476 ret = xmlSchemaVReaderWalk(vctxt);
27477#endif
27478#endif
27479 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27480 /*
27481 * SAX validation.
27482 */
27483 ret = xmlParseDocument(vctxt->parserCtxt);
27484 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027485 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027486 "no instance to validate");
27487 ret = -1;
27488 }
27489
27490 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027491 if (ret == 0)
27492 ret = vctxt->err;
27493 return (ret);
27494}
27495
27496/**
27497 * xmlSchemaValidateOneElement:
27498 * @ctxt: a schema validation context
27499 * @elem: an element node
27500 *
27501 * Validate a branch of a tree, starting with the given @elem.
27502 *
27503 * Returns 0 if the element and its subtree is valid, a positive error
27504 * code number otherwise and -1 in case of an internal or API error.
27505 */
27506int
27507xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27508{
27509 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27510 return (-1);
27511
27512 if (ctxt->schema == NULL)
27513 return (-1);
27514
27515 ctxt->doc = elem->doc;
27516 ctxt->node = elem;
27517 ctxt->validationRoot = elem;
27518 return(xmlSchemaVStart(ctxt));
27519}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027520
Daniel Veillard259f0df2004-08-18 09:13:18 +000027521/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027522 * xmlSchemaValidateDoc:
27523 * @ctxt: a schema validation context
27524 * @doc: a parsed document tree
27525 *
27526 * Validate a document tree in memory.
27527 *
27528 * Returns 0 if the document is schemas valid, a positive error code
27529 * number otherwise and -1 in case of internal or API error.
27530 */
27531int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027532xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27533{
Daniel Veillard4255d502002-04-16 15:50:10 +000027534 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027535 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027536
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027537 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027538 ctxt->node = xmlDocGetRootElement(doc);
27539 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027540 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027541 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27542 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027543 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027544 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027545 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027546 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027547 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027548}
27549
Daniel Veillardcdc82732005-07-08 15:04:06 +000027550
27551/************************************************************************
27552 * *
27553 * Function and data for SAX streaming API *
27554 * *
27555 ************************************************************************/
27556typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27557typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27558
27559struct _xmlSchemaSplitSAXData {
27560 xmlSAXHandlerPtr user_sax;
27561 void *user_data;
27562 xmlSchemaValidCtxtPtr ctxt;
27563 xmlSAXHandlerPtr schemas_sax;
27564};
27565
Daniel Veillard971771e2005-07-09 17:32:57 +000027566#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27567
27568struct _xmlSchemaSAXPlug {
27569 unsigned int magic;
27570
27571 /* the original callbacks informations */
27572 xmlSAXHandlerPtr *user_sax_ptr;
27573 xmlSAXHandlerPtr user_sax;
27574 void **user_data_ptr;
27575 void *user_data;
27576
27577 /* the block plugged back and validation informations */
27578 xmlSAXHandler schemas_sax;
27579 xmlSchemaValidCtxtPtr ctxt;
27580};
27581
Daniel Veillardcdc82732005-07-08 15:04:06 +000027582/* All those functions just bounces to the user provided SAX handlers */
27583static void
27584internalSubsetSplit(void *ctx, const xmlChar *name,
27585 const xmlChar *ExternalID, const xmlChar *SystemID)
27586{
Daniel Veillard971771e2005-07-09 17:32:57 +000027587 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027588 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27589 (ctxt->user_sax->internalSubset != NULL))
27590 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27591 SystemID);
27592}
27593
27594static int
27595isStandaloneSplit(void *ctx)
27596{
Daniel Veillard971771e2005-07-09 17:32:57 +000027597 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027598 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27599 (ctxt->user_sax->isStandalone != NULL))
27600 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27601 return(0);
27602}
27603
27604static int
27605hasInternalSubsetSplit(void *ctx)
27606{
Daniel Veillard971771e2005-07-09 17:32:57 +000027607 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027608 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27609 (ctxt->user_sax->hasInternalSubset != NULL))
27610 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27611 return(0);
27612}
27613
27614static int
27615hasExternalSubsetSplit(void *ctx)
27616{
Daniel Veillard971771e2005-07-09 17:32:57 +000027617 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027618 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27619 (ctxt->user_sax->hasExternalSubset != NULL))
27620 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27621 return(0);
27622}
27623
27624static void
27625externalSubsetSplit(void *ctx, const xmlChar *name,
27626 const xmlChar *ExternalID, const xmlChar *SystemID)
27627{
Daniel Veillard971771e2005-07-09 17:32:57 +000027628 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027629 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27630 (ctxt->user_sax->internalSubset != NULL))
27631 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27632 SystemID);
27633}
27634
27635static xmlParserInputPtr
27636resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27637{
Daniel Veillard971771e2005-07-09 17:32:57 +000027638 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027639 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27640 (ctxt->user_sax->resolveEntity != NULL))
27641 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27642 systemId));
27643 return(NULL);
27644}
27645
27646static xmlEntityPtr
27647getEntitySplit(void *ctx, const xmlChar *name)
27648{
Daniel Veillard971771e2005-07-09 17:32:57 +000027649 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027650 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27651 (ctxt->user_sax->getEntity != NULL))
27652 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27653 return(NULL);
27654}
27655
27656static xmlEntityPtr
27657getParameterEntitySplit(void *ctx, const xmlChar *name)
27658{
Daniel Veillard971771e2005-07-09 17:32:57 +000027659 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027660 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27661 (ctxt->user_sax->getParameterEntity != NULL))
27662 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27663 return(NULL);
27664}
27665
27666
27667static void
27668entityDeclSplit(void *ctx, const xmlChar *name, int type,
27669 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27670{
Daniel Veillard971771e2005-07-09 17:32:57 +000027671 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027672 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27673 (ctxt->user_sax->entityDecl != NULL))
27674 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27675 systemId, content);
27676}
27677
27678static void
27679attributeDeclSplit(void *ctx, const xmlChar * elem,
27680 const xmlChar * name, int type, int def,
27681 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27682{
Daniel Veillard971771e2005-07-09 17:32:57 +000027683 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027684 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27685 (ctxt->user_sax->attributeDecl != NULL)) {
27686 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27687 def, defaultValue, tree);
27688 } else {
27689 xmlFreeEnumeration(tree);
27690 }
27691}
27692
27693static void
27694elementDeclSplit(void *ctx, const xmlChar *name, int type,
27695 xmlElementContentPtr content)
27696{
Daniel Veillard971771e2005-07-09 17:32:57 +000027697 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027698 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27699 (ctxt->user_sax->elementDecl != NULL))
27700 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27701}
27702
27703static void
27704notationDeclSplit(void *ctx, const xmlChar *name,
27705 const xmlChar *publicId, const xmlChar *systemId)
27706{
Daniel Veillard971771e2005-07-09 17:32:57 +000027707 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027708 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27709 (ctxt->user_sax->notationDecl != NULL))
27710 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27711 systemId);
27712}
27713
27714static void
27715unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27716 const xmlChar *publicId, const xmlChar *systemId,
27717 const xmlChar *notationName)
27718{
Daniel Veillard971771e2005-07-09 17:32:57 +000027719 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027720 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27721 (ctxt->user_sax->unparsedEntityDecl != NULL))
27722 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27723 systemId, notationName);
27724}
27725
27726static void
27727setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27728{
Daniel Veillard971771e2005-07-09 17:32:57 +000027729 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027730 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27731 (ctxt->user_sax->setDocumentLocator != NULL))
27732 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27733}
27734
27735static void
27736startDocumentSplit(void *ctx)
27737{
Daniel Veillard971771e2005-07-09 17:32:57 +000027738 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027739 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27740 (ctxt->user_sax->startDocument != NULL))
27741 ctxt->user_sax->startDocument(ctxt->user_data);
27742}
27743
27744static void
27745endDocumentSplit(void *ctx)
27746{
Daniel Veillard971771e2005-07-09 17:32:57 +000027747 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027748 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27749 (ctxt->user_sax->endDocument != NULL))
27750 ctxt->user_sax->endDocument(ctxt->user_data);
27751}
27752
27753static void
27754processingInstructionSplit(void *ctx, const xmlChar *target,
27755 const xmlChar *data)
27756{
Daniel Veillard971771e2005-07-09 17:32:57 +000027757 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027758 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27759 (ctxt->user_sax->processingInstruction != NULL))
27760 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27761}
27762
27763static void
27764commentSplit(void *ctx, const xmlChar *value)
27765{
Daniel Veillard971771e2005-07-09 17:32:57 +000027766 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027767 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27768 (ctxt->user_sax->comment != NULL))
27769 ctxt->user_sax->comment(ctxt->user_data, value);
27770}
27771
27772/*
27773 * Varargs error callbacks to the user application, harder ...
27774 */
27775
Daniel Veillardffa3c742005-07-21 13:24:09 +000027776static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027777warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027778 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027779 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27780 (ctxt->user_sax->warning != NULL)) {
27781 TODO
27782 }
27783}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027784static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027785errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027786 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027787 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27788 (ctxt->user_sax->error != NULL)) {
27789 TODO
27790 }
27791}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027792static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027793fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027794 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027795 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27796 (ctxt->user_sax->fatalError != NULL)) {
27797 TODO
27798 }
27799}
27800
27801/*
27802 * Those are function where both the user handler and the schemas handler
27803 * need to be called.
27804 */
27805static void
27806charactersSplit(void *ctx, const xmlChar *ch, int len)
27807{
Daniel Veillard971771e2005-07-09 17:32:57 +000027808 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027809 if (ctxt == NULL)
27810 return;
27811 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27812 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27813 if (ctxt->ctxt != NULL)
27814 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27815}
27816
27817static void
27818ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27819{
Daniel Veillard971771e2005-07-09 17:32:57 +000027820 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027821 if (ctxt == NULL)
27822 return;
27823 if ((ctxt->user_sax != NULL) &&
27824 (ctxt->user_sax->ignorableWhitespace != NULL))
27825 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27826 if (ctxt->ctxt != NULL)
27827 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27828}
27829
27830static void
27831cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27832{
Daniel Veillard971771e2005-07-09 17:32:57 +000027833 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027834 if (ctxt == NULL)
27835 return;
27836 if ((ctxt->user_sax != NULL) &&
27837 (ctxt->user_sax->ignorableWhitespace != NULL))
27838 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27839 if (ctxt->ctxt != NULL)
27840 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27841}
27842
27843static void
27844referenceSplit(void *ctx, const xmlChar *name)
27845{
Daniel Veillard971771e2005-07-09 17:32:57 +000027846 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027847 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27848 (ctxt->user_sax->reference != NULL))
27849 ctxt->user_sax->reference(ctxt->user_data, name);
27850 if (ctxt->ctxt != NULL)
27851 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27852}
27853
27854static void
27855startElementNsSplit(void *ctx, const xmlChar * localname,
27856 const xmlChar * prefix, const xmlChar * URI,
27857 int nb_namespaces, const xmlChar ** namespaces,
27858 int nb_attributes, int nb_defaulted,
27859 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027860 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027861 if (ctxt == NULL)
27862 return;
27863 if ((ctxt->user_sax != NULL) &&
27864 (ctxt->user_sax->startElementNs != NULL))
27865 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27866 URI, nb_namespaces, namespaces,
27867 nb_attributes, nb_defaulted,
27868 attributes);
27869 if (ctxt->ctxt != NULL)
27870 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27871 URI, nb_namespaces, namespaces,
27872 nb_attributes, nb_defaulted,
27873 attributes);
27874}
27875
27876static void
27877endElementNsSplit(void *ctx, const xmlChar * localname,
27878 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027879 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027880 if (ctxt == NULL)
27881 return;
27882 if ((ctxt->user_sax != NULL) &&
27883 (ctxt->user_sax->endElementNs != NULL))
27884 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27885 if (ctxt->ctxt != NULL)
27886 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27887}
27888
Daniel Veillard4255d502002-04-16 15:50:10 +000027889/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027890 * xmlSchemaSAXPlug:
27891 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027892 * @sax: a pointer to the original xmlSAXHandlerPtr
27893 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027894 *
27895 * Plug a SAX based validation layer in a SAX parsing event flow.
27896 * The original @saxptr and @dataptr data are replaced by new pointers
27897 * but the calls to the original will be maintained.
27898 *
27899 * Returns a pointer to a data structure needed to unplug the validation layer
27900 * or NULL in case of errors.
27901 */
27902xmlSchemaSAXPlugPtr
27903xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
27904 xmlSAXHandlerPtr *sax, void **user_data)
27905{
27906 xmlSchemaSAXPlugPtr ret;
27907 xmlSAXHandlerPtr old_sax;
27908
27909 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
27910 return(NULL);
27911
27912 /*
27913 * We only allow to plug into SAX2 event streams
27914 */
27915 old_sax = *sax;
27916 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
27917 return(NULL);
27918 if ((old_sax != NULL) &&
27919 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
27920 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
27921 return(NULL);
27922
27923 /*
27924 * everything seems right allocate the local data needed for that layer
27925 */
27926 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
27927 if (ret == NULL) {
27928 return(NULL);
27929 }
27930 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
27931 ret->magic = XML_SAX_PLUG_MAGIC;
27932 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
27933 ret->ctxt = ctxt;
27934 ret->user_sax_ptr = sax;
27935 ret->user_sax = old_sax;
27936 if (old_sax == NULL) {
27937 /*
27938 * go direct, no need for the split block and functions.
27939 */
27940 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
27941 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
27942 /*
27943 * Note that we use the same text-function for both, to prevent
27944 * the parser from testing for ignorable whitespace.
27945 */
27946 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
27947 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
27948
27949 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
27950 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
27951
27952 ret->user_data = ctxt;
27953 *user_data = ctxt;
27954 } else {
27955 /*
27956 * for each callback unused by Schemas initialize it to the Split
27957 * routine only if non NULL in the user block, this can speed up
27958 * things at the SAX level.
27959 */
27960 if (old_sax->internalSubset != NULL)
27961 ret->schemas_sax.internalSubset = internalSubsetSplit;
27962 if (old_sax->isStandalone != NULL)
27963 ret->schemas_sax.isStandalone = isStandaloneSplit;
27964 if (old_sax->hasInternalSubset != NULL)
27965 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
27966 if (old_sax->hasExternalSubset != NULL)
27967 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
27968 if (old_sax->resolveEntity != NULL)
27969 ret->schemas_sax.resolveEntity = resolveEntitySplit;
27970 if (old_sax->getEntity != NULL)
27971 ret->schemas_sax.getEntity = getEntitySplit;
27972 if (old_sax->entityDecl != NULL)
27973 ret->schemas_sax.entityDecl = entityDeclSplit;
27974 if (old_sax->notationDecl != NULL)
27975 ret->schemas_sax.notationDecl = notationDeclSplit;
27976 if (old_sax->attributeDecl != NULL)
27977 ret->schemas_sax.attributeDecl = attributeDeclSplit;
27978 if (old_sax->elementDecl != NULL)
27979 ret->schemas_sax.elementDecl = elementDeclSplit;
27980 if (old_sax->unparsedEntityDecl != NULL)
27981 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
27982 if (old_sax->setDocumentLocator != NULL)
27983 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
27984 if (old_sax->startDocument != NULL)
27985 ret->schemas_sax.startDocument = startDocumentSplit;
27986 if (old_sax->endDocument != NULL)
27987 ret->schemas_sax.endDocument = endDocumentSplit;
27988 if (old_sax->processingInstruction != NULL)
27989 ret->schemas_sax.processingInstruction = processingInstructionSplit;
27990 if (old_sax->comment != NULL)
27991 ret->schemas_sax.comment = commentSplit;
27992 if (old_sax->warning != NULL)
27993 ret->schemas_sax.warning = warningSplit;
27994 if (old_sax->error != NULL)
27995 ret->schemas_sax.error = errorSplit;
27996 if (old_sax->fatalError != NULL)
27997 ret->schemas_sax.fatalError = fatalErrorSplit;
27998 if (old_sax->getParameterEntity != NULL)
27999 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28000 if (old_sax->externalSubset != NULL)
28001 ret->schemas_sax.externalSubset = externalSubsetSplit;
28002
28003 /*
28004 * the 6 schemas callback have to go to the splitter functions
28005 * Note that we use the same text-function for ignorableWhitespace
28006 * if possible, to prevent the parser from testing for ignorable
28007 * whitespace.
28008 */
28009 ret->schemas_sax.characters = charactersSplit;
28010 if ((old_sax->ignorableWhitespace != NULL) &&
28011 (old_sax->ignorableWhitespace != old_sax->characters))
28012 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28013 else
28014 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28015 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28016 ret->schemas_sax.reference = referenceSplit;
28017 ret->schemas_sax.startElementNs = startElementNsSplit;
28018 ret->schemas_sax.endElementNs = endElementNsSplit;
28019
28020 ret->user_data_ptr = user_data;
28021 ret->user_data = *user_data;
28022 *user_data = ret;
28023 }
28024
28025 /*
28026 * plug the pointers back.
28027 */
28028 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028029 ctxt->sax = *sax;
28030 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28031 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028032 return(ret);
28033}
28034
28035/**
28036 * xmlSchemaSAXUnplug:
28037 * @plug: a data structure returned by xmlSchemaSAXPlug
28038 *
28039 * Unplug a SAX based validation layer in a SAX parsing event flow.
28040 * The original pointers used in the call are restored.
28041 *
28042 * Returns 0 in case of success and -1 in case of failure.
28043 */
28044int
28045xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28046{
28047 xmlSAXHandlerPtr *sax;
28048 void **user_data;
28049
28050 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28051 return(-1);
28052 plug->magic = 0;
28053
Daniel Veillardf10ae122005-07-10 19:03:16 +000028054 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028055 /* restore the data */
28056 sax = plug->user_sax_ptr;
28057 *sax = plug->user_sax;
28058 if (plug->user_sax != NULL) {
28059 user_data = plug->user_data_ptr;
28060 *user_data = plug->user_data;
28061 }
28062
28063 /* free and return */
28064 xmlFree(plug);
28065 return(0);
28066}
28067
28068/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028069 * xmlSchemaValidateStream:
28070 * @ctxt: a schema validation context
28071 * @input: the input to use for reading the data
28072 * @enc: an optional encoding information
28073 * @sax: a SAX handler for the resulting events
28074 * @user_data: the context to provide to the SAX handler.
28075 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028076 * Validate an input based on a flow of SAX event from the parser
28077 * and forward the events to the @sax handler with the provided @user_data
28078 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028079 *
28080 * Returns 0 if the document is schemas valid, a positive error code
28081 * number otherwise and -1 in case of internal or API error.
28082 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028083int
Daniel Veillard4255d502002-04-16 15:50:10 +000028084xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028085 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28086 xmlSAXHandlerPtr sax, void *user_data)
28087{
Daniel Veillard971771e2005-07-09 17:32:57 +000028088 xmlSchemaSAXPlugPtr plug = NULL;
28089 xmlSAXHandlerPtr old_sax = NULL;
28090 xmlParserCtxtPtr pctxt = NULL;
28091 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028092 int ret;
28093
Daniel Veillard4255d502002-04-16 15:50:10 +000028094 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028095 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028096
Daniel Veillardcdc82732005-07-08 15:04:06 +000028097 /*
28098 * prepare the parser
28099 */
28100 pctxt = xmlNewParserCtxt();
28101 if (pctxt == NULL)
28102 return (-1);
28103 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028104 pctxt->sax = sax;
28105 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028106#if 0
28107 if (options)
28108 xmlCtxtUseOptions(pctxt, options);
28109#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028110 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028111
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028112 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028113 if (inputStream == NULL) {
28114 ret = -1;
28115 goto done;
28116 }
28117 inputPush(pctxt, inputStream);
28118 ctxt->parserCtxt = pctxt;
28119 ctxt->input = input;
28120
28121 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028122 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028123 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028124 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28125 if (plug == NULL) {
28126 ret = -1;
28127 goto done;
28128 }
28129 ctxt->input = input;
28130 ctxt->enc = enc;
28131 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028132 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28133 ret = xmlSchemaVStart(ctxt);
28134
28135 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28136 ret = ctxt->parserCtxt->errNo;
28137 if (ret == 0)
28138 ret = 1;
28139 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028140
28141done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028142 ctxt->parserCtxt = NULL;
28143 ctxt->sax = NULL;
28144 ctxt->input = NULL;
28145 if (plug != NULL) {
28146 xmlSchemaSAXUnplug(plug);
28147 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028148 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028149 if (pctxt != NULL) {
28150 pctxt->sax = old_sax;
28151 xmlFreeParserCtxt(pctxt);
28152 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028153 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028154}
28155
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028156/**
28157 * xmlSchemaValidateFile:
28158 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028159 * @filename: the URI of the instance
28160 * @options: a future set of options, currently unused
28161 *
28162 * Do a schemas validation of the given resource, it will use the
28163 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028164 *
28165 * Returns 0 if the document is valid, a positive error code
28166 * number otherwise and -1 in case of an internal or API error.
28167 */
28168int
28169xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028170 const char * filename,
28171 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028172{
28173 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028174 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028175
28176 if ((ctxt == NULL) || (filename == NULL))
28177 return (-1);
28178
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028179 input = xmlParserInputBufferCreateFilename(filename,
28180 XML_CHAR_ENCODING_NONE);
28181 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028182 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028183 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28184 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028185 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028186}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028187
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028188#define bottom_xmlschemas
28189#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028190#endif /* LIBXML_SCHEMAS_ENABLED */