blob: dbefae78300fe4a681afb58f6167021107a5de5c [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. Buchcik65c2f1d2005-10-17 12:39:58 +0000899 int appliedXPath; /* Indicates that an XPath has been applied. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000900};
901
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000902#define XML_SCHEMAS_ATTR_UNKNOWN 1
903#define XML_SCHEMAS_ATTR_ASSESSED 2
904#define XML_SCHEMAS_ATTR_PROHIBITED 3
905#define XML_SCHEMAS_ATTR_ERR_MISSING 4
906#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
907#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
908#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
909#define XML_SCHEMAS_ATTR_DEFAULT 8
910#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
911#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
912#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
913#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
914#define XML_SCHEMAS_ATTR_WILD_SKIP 13
915#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
916#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
917#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
918#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000919/*
920* @metaType values of xmlSchemaAttrInfo.
921*/
922#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
923#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
924#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
925#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
926#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000927
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000928typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
929typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
930struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000931 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000932 xmlNodePtr node;
933 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000934 const xmlChar *localName;
935 const xmlChar *nsName;
936 const xmlChar *value;
937 xmlSchemaValPtr val; /* the pre-computed value if any */
938 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
939 int flags; /* combination of node info flags */
940
941 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000942 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000943 int state;
944 int metaType;
945 const xmlChar *vcValue; /* the value constraint value */
946 xmlSchemaNodeInfoPtr parent;
947};
948
949
950#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000951/**
952 * xmlSchemaValidCtxt:
953 *
954 * A Schemas validation context
955 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000956struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000957 int type;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000958 void *userData; /* user specific data block */
959 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000960 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000961 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000962
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000963 xmlSchemaPtr schema; /* The schema in use */
964 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000965 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000966 xmlCharEncoding enc;
967 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000968 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000969 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000970
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000971 int err;
972 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000973
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000974 xmlNodePtr node;
975 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000976 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000977
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000978 xmlRegExecCtxtPtr regexp;
979 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000980
Daniel Veillardc0826a72004-08-10 14:17:33 +0000981 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000982 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000983 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000984 xmlSchemaParserCtxtPtr pctxt;
985 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000986
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000987 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000988 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000989 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000990 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000991
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000992 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
993
994 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
995 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000996
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000997 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
998 int nbIdcNodes;
999 int sizeIdcNodes;
1000
1001 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1002 int nbIdcKeys;
1003 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001004
1005 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001006
1007 xmlDictPtr dict;
1008
Daniel Veillard39e5c892005-07-03 22:48:50 +00001009#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001010 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001011#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001012
1013 xmlSchemaAttrInfoPtr *attrInfos;
1014 int nbAttrInfos;
1015 int sizeAttrInfos;
1016
1017 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001018 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001019 int hasKeyrefs;
1020 int createIDCNodeTables;
1021 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001022};
1023
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001024/**
1025 * xmlSchemaSubstGroup:
1026 *
1027 *
1028 */
1029typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1030typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1031struct _xmlSchemaSubstGroup {
1032 xmlSchemaElementPtr head;
1033 xmlSchemaItemListPtr members;
1034};
1035
Daniel Veillard4255d502002-04-16 15:50:10 +00001036/************************************************************************
1037 * *
1038 * Some predeclarations *
1039 * *
1040 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001041
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001042static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1043 xmlSchemaPtr schema,
1044 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001045static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1046 xmlSchemaPtr schema,
1047 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001048static int
1049xmlSchemaTypeFixup(xmlSchemaTypePtr type,
1050 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001051static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001052xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1053static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001054xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1055 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001056static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001057xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1058 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001059static void
1060xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001061static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001062xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001063static xmlSchemaTreeItemPtr
1064xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1065 xmlNodePtr node, xmlSchemaTypeType type,
1066 int withParticle);
1067static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001068xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001069static xmlSchemaTypeLinkPtr
1070xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001071static void
1072xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1073 const char *funcName,
1074 const char *message);
1075static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001076xmlSchemaCheckCOSSTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
1077 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001078 xmlSchemaTypePtr baseType,
1079 int subset);
1080static void
1081xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001082 xmlSchemaParserCtxtPtr ctxt);
1083static void
1084xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001085static xmlSchemaQNameRefPtr
1086xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1087 xmlSchemaPtr schema,
1088 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001089
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001090/************************************************************************
1091 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001092 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001093 * *
1094 ************************************************************************/
1095
1096/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001097 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001098 * @type: the type of the schema item
1099 *
1100 * Returns the component name of a schema item.
1101 */
1102static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001103xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001104{
1105 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001106 case XML_SCHEMA_TYPE_BASIC:
1107 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001108 case XML_SCHEMA_TYPE_SIMPLE:
1109 return(BAD_CAST "simple type definition");
1110 case XML_SCHEMA_TYPE_COMPLEX:
1111 return(BAD_CAST "complex type definition");
1112 case XML_SCHEMA_TYPE_ELEMENT:
1113 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001114 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1115 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001116 case XML_SCHEMA_TYPE_ATTRIBUTE:
1117 return(BAD_CAST "attribute declaration");
1118 case XML_SCHEMA_TYPE_GROUP:
1119 return(BAD_CAST "model group definition");
1120 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1121 return(BAD_CAST "attribute group definition");
1122 case XML_SCHEMA_TYPE_NOTATION:
1123 return(BAD_CAST "notation declaration");
1124 case XML_SCHEMA_TYPE_SEQUENCE:
1125 return(BAD_CAST "model group (sequence)");
1126 case XML_SCHEMA_TYPE_CHOICE:
1127 return(BAD_CAST "model group (choice)");
1128 case XML_SCHEMA_TYPE_ALL:
1129 return(BAD_CAST "model group (all)");
1130 case XML_SCHEMA_TYPE_PARTICLE:
1131 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001132 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001133 return(BAD_CAST "unique identity-constraint");
1134 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001135 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001136 return(BAD_CAST "key identity-constraint");
1137 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001138 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001139 return(BAD_CAST "keyref identity-constraint");
1140 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001141 case XML_SCHEMA_TYPE_ANY:
1142 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001143 case XML_SCHEMA_EXTRA_QNAMEREF:
1144 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001145 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1146 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001147 default:
1148 return(BAD_CAST "Not a schema component");
1149 }
1150}
1151
1152/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001153 * xmlSchemaGetComponentTypeStr:
1154 * @type: the type of the schema item
1155 *
1156 * Returns the component name of a schema item.
1157 */
1158static const xmlChar *
1159xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1160{
1161 switch (item->type) {
1162 case XML_SCHEMA_TYPE_BASIC:
1163 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1164 return(BAD_CAST "complex type definition");
1165 else
1166 return(BAD_CAST "simple type definition");
1167 default:
1168 return(xmlSchemaItemTypeToStr(item->type));
1169 }
1170}
1171
1172/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001173 * xmlSchemaGetComponentNode:
1174 * @item: a schema component
1175 *
1176 * Returns node associated with the schema component.
1177 * NOTE that such a node need not be available; plus, a component's
1178 * node need not to reflect the component directly, since there is no
1179 * one-to-one relationship between the XML Schema representation and
1180 * the component representation.
1181 */
1182static xmlNodePtr
1183xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1184{
1185 switch (item->type) {
1186 case XML_SCHEMA_TYPE_ELEMENT:
1187 return (((xmlSchemaElementPtr) item)->node);
1188 case XML_SCHEMA_TYPE_ATTRIBUTE:
1189 return (((xmlSchemaAttributePtr) item)->node);
1190 case XML_SCHEMA_TYPE_COMPLEX:
1191 case XML_SCHEMA_TYPE_SIMPLE:
1192 return (((xmlSchemaTypePtr) item)->node);
1193 case XML_SCHEMA_TYPE_ANY:
1194 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1195 return (((xmlSchemaWildcardPtr) item)->node);
1196 case XML_SCHEMA_TYPE_PARTICLE:
1197 return (((xmlSchemaParticlePtr) item)->node);
1198 case XML_SCHEMA_TYPE_SEQUENCE:
1199 case XML_SCHEMA_TYPE_CHOICE:
1200 case XML_SCHEMA_TYPE_ALL:
1201 return (((xmlSchemaModelGroupPtr) item)->node);
1202 case XML_SCHEMA_TYPE_GROUP:
1203 return (((xmlSchemaModelGroupDefPtr) item)->node);
1204 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1205 return (((xmlSchemaAttributeGroupPtr) item)->node);
1206 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1207 case XML_SCHEMA_TYPE_IDC_KEY:
1208 case XML_SCHEMA_TYPE_IDC_KEYREF:
1209 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001210 case XML_SCHEMA_EXTRA_QNAMEREF:
1211 return(((xmlSchemaQNameRefPtr) item)->node);
1212 /* TODO: What to do with NOTATIONs?
1213 case XML_SCHEMA_TYPE_NOTATION:
1214 return (((xmlSchemaNotationPtr) item)->node);
1215 */
1216 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1217 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001218 default:
1219 return (NULL);
1220 }
1221}
1222
1223#if 0
1224/**
1225 * xmlSchemaGetNextComponent:
1226 * @item: a schema component
1227 *
1228 * Returns the next sibling of the schema component.
1229 */
1230static xmlSchemaBasicItemPtr
1231xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1232{
1233 switch (item->type) {
1234 case XML_SCHEMA_TYPE_ELEMENT:
1235 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1236 case XML_SCHEMA_TYPE_ATTRIBUTE:
1237 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1238 case XML_SCHEMA_TYPE_COMPLEX:
1239 case XML_SCHEMA_TYPE_SIMPLE:
1240 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1241 case XML_SCHEMA_TYPE_ANY:
1242 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1243 return (NULL);
1244 case XML_SCHEMA_TYPE_PARTICLE:
1245 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1246 case XML_SCHEMA_TYPE_SEQUENCE:
1247 case XML_SCHEMA_TYPE_CHOICE:
1248 case XML_SCHEMA_TYPE_ALL:
1249 return (NULL);
1250 case XML_SCHEMA_TYPE_GROUP:
1251 return (NULL);
1252 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1253 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1254 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1255 case XML_SCHEMA_TYPE_IDC_KEY:
1256 case XML_SCHEMA_TYPE_IDC_KEYREF:
1257 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1258 default:
1259 return (NULL);
1260 }
1261}
1262#endif
1263
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001264
1265/**
1266 * xmlSchemaFormatQName:
1267 * @buf: the string buffer
1268 * @namespaceName: the namespace name
1269 * @localName: the local name
1270 *
1271 * Returns the given QName in the format "{namespaceName}localName" or
1272 * just "localName" if @namespaceName is NULL.
1273 *
1274 * Returns the localName if @namespaceName is NULL, a formatted
1275 * string otherwise.
1276 */
1277static const xmlChar*
1278xmlSchemaFormatQName(xmlChar **buf,
1279 const xmlChar *namespaceName,
1280 const xmlChar *localName)
1281{
1282 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001283 if (namespaceName != NULL) {
1284 *buf = xmlStrdup(BAD_CAST "{");
1285 *buf = xmlStrcat(*buf, namespaceName);
1286 *buf = xmlStrcat(*buf, BAD_CAST "}");
1287 }
1288 if (localName != NULL) {
1289 if (namespaceName == NULL)
1290 return(localName);
1291 *buf = xmlStrcat(*buf, localName);
1292 } else {
1293 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
1294 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001295 return ((const xmlChar *) *buf);
1296}
1297
1298static const xmlChar*
1299xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1300{
1301 if (ns != NULL)
1302 return (xmlSchemaFormatQName(buf, ns->href, localName));
1303 else
1304 return (xmlSchemaFormatQName(buf, NULL, localName));
1305}
1306
1307static const xmlChar *
1308xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1309{
1310 switch (item->type) {
1311 case XML_SCHEMA_TYPE_ELEMENT:
1312 return (((xmlSchemaElementPtr) item)->name);
1313 case XML_SCHEMA_TYPE_ATTRIBUTE:
1314 return (((xmlSchemaAttributePtr) item)->name);
1315 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1316 return (((xmlSchemaAttributeGroupPtr) item)->name);
1317 case XML_SCHEMA_TYPE_BASIC:
1318 case XML_SCHEMA_TYPE_SIMPLE:
1319 case XML_SCHEMA_TYPE_COMPLEX:
1320 return (((xmlSchemaTypePtr) item)->name);
1321 case XML_SCHEMA_TYPE_GROUP:
1322 return (((xmlSchemaModelGroupDefPtr) item)->name);
1323 case XML_SCHEMA_TYPE_IDC_KEY:
1324 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1325 case XML_SCHEMA_TYPE_IDC_KEYREF:
1326 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001327 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1328 if (WXS_ATTRUSE_DECL(item) != NULL) {
1329 return(xmlSchemaGetComponentName(
1330 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1331 } else
1332 return(NULL);
1333 case XML_SCHEMA_EXTRA_QNAMEREF:
1334 return (((xmlSchemaQNameRefPtr) item)->name);
1335 case XML_SCHEMA_TYPE_NOTATION:
1336 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001337 default:
1338 /*
1339 * Other components cannot have names.
1340 */
1341 break;
1342 }
1343 return (NULL);
1344}
1345
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001346#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1347#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1348/*
1349static const xmlChar *
1350xmlSchemaGetQNameRefName(void *ref)
1351{
1352 return(((xmlSchemaQNameRefPtr) ref)->name);
1353}
1354
1355static const xmlChar *
1356xmlSchemaGetQNameRefTargetNs(void *ref)
1357{
1358 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1359}
1360*/
1361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001362static const xmlChar *
1363xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1364{
1365 switch (item->type) {
1366 case XML_SCHEMA_TYPE_ELEMENT:
1367 return (((xmlSchemaElementPtr) item)->targetNamespace);
1368 case XML_SCHEMA_TYPE_ATTRIBUTE:
1369 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1370 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1371 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1372 case XML_SCHEMA_TYPE_BASIC:
1373 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1374 case XML_SCHEMA_TYPE_SIMPLE:
1375 case XML_SCHEMA_TYPE_COMPLEX:
1376 return (((xmlSchemaTypePtr) item)->targetNamespace);
1377 case XML_SCHEMA_TYPE_GROUP:
1378 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1379 case XML_SCHEMA_TYPE_IDC_KEY:
1380 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1381 case XML_SCHEMA_TYPE_IDC_KEYREF:
1382 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001383 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1384 if (WXS_ATTRUSE_DECL(item) != NULL) {
1385 return(xmlSchemaGetComponentTargetNs(
1386 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1387 }
1388 /* TODO: Will returning NULL break something? */
1389 break;
1390 case XML_SCHEMA_EXTRA_QNAMEREF:
1391 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1392 case XML_SCHEMA_TYPE_NOTATION:
1393 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001394 default:
1395 /*
1396 * Other components cannot have names.
1397 */
1398 break;
1399 }
1400 return (NULL);
1401}
1402
1403static const xmlChar*
1404xmlSchemaGetComponentQName(xmlChar **buf,
1405 void *item)
1406{
1407 return (xmlSchemaFormatQName(buf,
1408 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1409 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1410}
1411
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001412static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001413xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001414{
1415 xmlChar *str = NULL;
1416
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001417 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001418 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001419 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1420 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001421 *buf = xmlStrcat(*buf, BAD_CAST "'");
1422 FREE_AND_NULL(str);
1423 return(*buf);
1424}
1425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001426static const xmlChar*
1427xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1428{
1429 return(xmlSchemaGetComponentDesignation(buf, idc));
1430}
1431
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001432/**
1433 * xmlSchemaWildcardPCToString:
1434 * @pc: the type of processContents
1435 *
1436 * Returns a string representation of the type of
1437 * processContents.
1438 */
1439static const xmlChar *
1440xmlSchemaWildcardPCToString(int pc)
1441{
1442 switch (pc) {
1443 case XML_SCHEMAS_ANY_SKIP:
1444 return (BAD_CAST "skip");
1445 case XML_SCHEMAS_ANY_LAX:
1446 return (BAD_CAST "lax");
1447 case XML_SCHEMAS_ANY_STRICT:
1448 return (BAD_CAST "strict");
1449 default:
1450 return (BAD_CAST "invalid process contents");
1451 }
1452}
1453
1454/**
1455 * xmlSchemaGetCanonValueWhtspExt:
1456 * @val: the precomputed value
1457 * @retValue: the returned value
1458 * @ws: the whitespace type of the value
1459 *
1460 * Get a the cononical representation of the value.
1461 * The caller has to free the returned retValue.
1462 *
1463 * Returns 0 if the value could be built and -1 in case of
1464 * API errors or if the value type is not supported yet.
1465 */
1466static int
1467xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1468 xmlSchemaWhitespaceValueType ws,
1469 xmlChar **retValue)
1470{
1471 int list;
1472 xmlSchemaValType valType;
1473 const xmlChar *value, *value2 = NULL;
1474
1475
1476 if ((retValue == NULL) || (val == NULL))
1477 return (-1);
1478 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1479 *retValue = NULL;
1480 do {
1481 value = NULL;
1482 valType = xmlSchemaGetValType(val);
1483 switch (valType) {
1484 case XML_SCHEMAS_STRING:
1485 case XML_SCHEMAS_NORMSTRING:
1486 case XML_SCHEMAS_ANYSIMPLETYPE:
1487 value = xmlSchemaValueGetAsString(val);
1488 if (value != NULL) {
1489 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1490 value2 = xmlSchemaCollapseString(value);
1491 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1492 value2 = xmlSchemaWhiteSpaceReplace(value);
1493 if (value2 != NULL)
1494 value = value2;
1495 }
1496 break;
1497 default:
1498 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1499 if (value2 != NULL)
1500 xmlFree((xmlChar *) value2);
1501 goto internal_error;
1502 }
1503 value = value2;
1504 }
1505 if (*retValue == NULL)
1506 if (value == NULL) {
1507 if (! list)
1508 *retValue = xmlStrdup(BAD_CAST "");
1509 } else
1510 *retValue = xmlStrdup(value);
1511 else if (value != NULL) {
1512 /* List. */
1513 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1514 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1515 }
1516 FREE_AND_NULL(value2)
1517 val = xmlSchemaValueGetNext(val);
1518 } while (val != NULL);
1519
1520 return (0);
1521internal_error:
1522 if (*retValue != NULL)
1523 xmlFree((xmlChar *) (*retValue));
1524 if (value2 != NULL)
1525 xmlFree((xmlChar *) value2);
1526 return (-1);
1527}
1528
1529/**
1530 * xmlSchemaFormatItemForReport:
1531 * @buf: the string buffer
1532 * @itemDes: the designation of the item
1533 * @itemName: the name of the item
1534 * @item: the item as an object
1535 * @itemNode: the node of the item
1536 * @local: the local name
1537 * @parsing: if the function is used during the parse
1538 *
1539 * Returns a representation of the given item used
1540 * for error reports.
1541 *
1542 * The following order is used to build the resulting
1543 * designation if the arguments are not NULL:
1544 * 1a. If itemDes not NULL -> itemDes
1545 * 1b. If (itemDes not NULL) and (itemName not NULL)
1546 * -> itemDes + itemName
1547 * 2. If the preceding was NULL and (item not NULL) -> item
1548 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1549 *
1550 * If the itemNode is an attribute node, the name of the attribute
1551 * will be appended to the result.
1552 *
1553 * Returns the formatted string and sets @buf to the resulting value.
1554 */
1555static xmlChar*
1556xmlSchemaFormatItemForReport(xmlChar **buf,
1557 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001558 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001559 xmlNodePtr itemNode)
1560{
1561 xmlChar *str = NULL;
1562 int named = 1;
1563
1564 if (*buf != NULL) {
1565 xmlFree(*buf);
1566 *buf = NULL;
1567 }
1568
1569 if (itemDes != NULL) {
1570 *buf = xmlStrdup(itemDes);
1571 } else if (item != NULL) {
1572 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001573 case XML_SCHEMA_TYPE_BASIC: {
1574 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1575
1576 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001577 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001578 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001579 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001580 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001581 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1582 else
1583 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001584 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001585 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001586 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001587 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001588 case XML_SCHEMA_TYPE_SIMPLE: {
1589 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1590
1591 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001592 *buf = xmlStrdup(BAD_CAST"");
1593 } else {
1594 *buf = xmlStrdup(BAD_CAST "local ");
1595 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001596 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001597 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001598 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001599 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001600 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1602 else
1603 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001604 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001605 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001606 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001607 *buf = xmlStrcat(*buf, BAD_CAST "'");
1608 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001609 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001610 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001611 case XML_SCHEMA_TYPE_COMPLEX: {
1612 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1613
1614 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001615 *buf = xmlStrdup(BAD_CAST "");
1616 else
1617 *buf = xmlStrdup(BAD_CAST "local ");
1618 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001619 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001620 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001621 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001622 *buf = xmlStrcat(*buf, BAD_CAST "'");
1623 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001624 }
1625 break;
1626 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1627 xmlSchemaAttributeUsePtr ause;
1628
1629 ause = WXS_ATTR_USE_CAST item;
1630 *buf = xmlStrdup(BAD_CAST "attribute use ");
1631 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1632 *buf = xmlStrcat(*buf, BAD_CAST "'");
1633 *buf = xmlStrcat(*buf,
1634 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1635 FREE_AND_NULL(str)
1636 *buf = xmlStrcat(*buf, BAD_CAST "'");
1637 } else {
1638 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1639 }
1640 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001641 break;
1642 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1643 xmlSchemaAttributePtr attr;
1644
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001645 attr = (xmlSchemaAttributePtr) item;
1646 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1647 *buf = xmlStrcat(*buf, BAD_CAST " '");
1648 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1649 attr->targetNamespace, attr->name));
1650 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001651 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001652 }
1653 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001654 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1655 xmlSchemaGetComponentDesignation(buf, item);
1656 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001657 case XML_SCHEMA_TYPE_ELEMENT: {
1658 xmlSchemaElementPtr elem;
1659
1660 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001661 *buf = xmlStrdup(BAD_CAST "element decl.");
1662 *buf = xmlStrcat(*buf, BAD_CAST " '");
1663 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1664 elem->targetNamespace, elem->name));
1665 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001666 }
1667 break;
1668 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1669 case XML_SCHEMA_TYPE_IDC_KEY:
1670 case XML_SCHEMA_TYPE_IDC_KEYREF:
1671 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1672 *buf = xmlStrdup(BAD_CAST "unique '");
1673 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1674 *buf = xmlStrdup(BAD_CAST "key '");
1675 else
1676 *buf = xmlStrdup(BAD_CAST "keyRef '");
1677 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1678 *buf = xmlStrcat(*buf, BAD_CAST "'");
1679 break;
1680 case XML_SCHEMA_TYPE_ANY:
1681 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1682 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1683 ((xmlSchemaWildcardPtr) item)->processContents));
1684 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1685 break;
1686 case XML_SCHEMA_FACET_MININCLUSIVE:
1687 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1688 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1689 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1690 case XML_SCHEMA_FACET_TOTALDIGITS:
1691 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1692 case XML_SCHEMA_FACET_PATTERN:
1693 case XML_SCHEMA_FACET_ENUMERATION:
1694 case XML_SCHEMA_FACET_WHITESPACE:
1695 case XML_SCHEMA_FACET_LENGTH:
1696 case XML_SCHEMA_FACET_MAXLENGTH:
1697 case XML_SCHEMA_FACET_MINLENGTH:
1698 *buf = xmlStrdup(BAD_CAST "facet '");
1699 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1700 *buf = xmlStrcat(*buf, BAD_CAST "'");
1701 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001702 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001703 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001704 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001705 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001706 *buf = xmlStrcat(*buf, BAD_CAST "'");
1707 FREE_AND_NULL(str)
1708 }
1709 break;
1710 case XML_SCHEMA_TYPE_SEQUENCE:
1711 case XML_SCHEMA_TYPE_CHOICE:
1712 case XML_SCHEMA_TYPE_ALL:
1713 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001714 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1715 break;
1716 case XML_SCHEMA_TYPE_NOTATION: {
1717 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1718 *buf = xmlStrcat(*buf, BAD_CAST " '");
1719 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1720 *buf = xmlStrcat(*buf, BAD_CAST "'");
1721 FREE_AND_NULL(str);
1722 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001723 default:
1724 named = 0;
1725 }
1726 } else
1727 named = 0;
1728
1729 if ((named == 0) && (itemNode != NULL)) {
1730 xmlNodePtr elem;
1731
1732 if (itemNode->type == XML_ATTRIBUTE_NODE)
1733 elem = itemNode->parent;
1734 else
1735 elem = itemNode;
1736 *buf = xmlStrdup(BAD_CAST "Element '");
1737 if (elem->ns != NULL) {
1738 *buf = xmlStrcat(*buf,
1739 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1740 FREE_AND_NULL(str)
1741 } else
1742 *buf = xmlStrcat(*buf, elem->name);
1743 *buf = xmlStrcat(*buf, BAD_CAST "'");
1744
1745 }
1746 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1747 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1748 if (itemNode->ns != NULL) {
1749 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1750 itemNode->ns->href, itemNode->name));
1751 FREE_AND_NULL(str)
1752 } else
1753 *buf = xmlStrcat(*buf, itemNode->name);
1754 *buf = xmlStrcat(*buf, BAD_CAST "'");
1755 }
1756 FREE_AND_NULL(str)
1757
1758 return (*buf);
1759}
1760
1761/**
1762 * xmlSchemaFormatFacetEnumSet:
1763 * @buf: the string buffer
1764 * @type: the type holding the enumeration facets
1765 *
1766 * Builds a string consisting of all enumeration elements.
1767 *
1768 * Returns a string of all enumeration elements.
1769 */
1770static const xmlChar *
1771xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1772 xmlChar **buf, xmlSchemaTypePtr type)
1773{
1774 xmlSchemaFacetPtr facet;
1775 xmlSchemaWhitespaceValueType ws;
1776 xmlChar *value = NULL;
1777 int res;
1778
1779 if (*buf != NULL)
1780 xmlFree(*buf);
1781 *buf = NULL;
1782
1783 do {
1784 /*
1785 * Use the whitespace type of the base type.
1786 */
1787 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1788 for (facet = type->facets; facet != NULL; facet = facet->next) {
1789 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1790 continue;
1791 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1792 ws, &value);
1793 if (res == -1) {
1794 xmlSchemaInternalErr(actxt,
1795 "xmlSchemaFormatFacetEnumSet",
1796 "compute the canonical lexical representation");
1797 if (*buf != NULL)
1798 xmlFree(*buf);
1799 *buf = NULL;
1800 return (NULL);
1801 }
1802 if (*buf == NULL)
1803 *buf = xmlStrdup(BAD_CAST "'");
1804 else
1805 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1806 *buf = xmlStrcat(*buf, BAD_CAST value);
1807 *buf = xmlStrcat(*buf, BAD_CAST "'");
1808 if (value != NULL) {
1809 xmlFree((xmlChar *)value);
1810 value = NULL;
1811 }
1812 }
1813 type = type->baseType;
1814 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1815
1816 return ((const xmlChar *) *buf);
1817}
1818
1819/************************************************************************
1820 * *
1821 * Error functions *
1822 * *
1823 ************************************************************************/
1824
1825#if 0
1826static void
1827xmlSchemaErrMemory(const char *msg)
1828{
1829 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1830 msg);
1831}
1832#endif
1833
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001834static void
1835xmlSchemaPSimpleErr(const char *msg)
1836{
1837 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1838 msg);
1839}
1840
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001841/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001842 * xmlSchemaPErrMemory:
1843 * @node: a context node
1844 * @extra: extra informations
1845 *
1846 * Handle an out of memory condition
1847 */
1848static void
1849xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1850 const char *extra, xmlNodePtr node)
1851{
1852 if (ctxt != NULL)
1853 ctxt->nberrors++;
1854 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1855 extra);
1856}
1857
1858/**
1859 * xmlSchemaPErr:
1860 * @ctxt: the parsing context
1861 * @node: the context node
1862 * @error: the error code
1863 * @msg: the error message
1864 * @str1: extra data
1865 * @str2: extra data
1866 *
1867 * Handle a parser error
1868 */
1869static void
1870xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1871 const char *msg, const xmlChar * str1, const xmlChar * str2)
1872{
1873 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001874 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001875 void *data = NULL;
1876
1877 if (ctxt != NULL) {
1878 ctxt->nberrors++;
1879 channel = ctxt->error;
1880 data = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001881 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001882 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001883 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001884 error, XML_ERR_ERROR, NULL, 0,
1885 (const char *) str1, (const char *) str2, NULL, 0, 0,
1886 msg, str1, str2);
1887}
1888
1889/**
1890 * xmlSchemaPErr2:
1891 * @ctxt: the parsing context
1892 * @node: the context node
1893 * @node: the current child
1894 * @error: the error code
1895 * @msg: the error message
1896 * @str1: extra data
1897 * @str2: extra data
1898 *
1899 * Handle a parser error
1900 */
1901static void
1902xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1903 xmlNodePtr child, int error,
1904 const char *msg, const xmlChar * str1, const xmlChar * str2)
1905{
1906 if (child != NULL)
1907 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1908 else
1909 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1910}
1911
Daniel Veillard01fa6152004-06-29 17:04:39 +00001912
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001913/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001914 * xmlSchemaPErrExt:
1915 * @ctxt: the parsing context
1916 * @node: the context node
1917 * @error: the error code
1918 * @strData1: extra data
1919 * @strData2: extra data
1920 * @strData3: extra data
1921 * @msg: the message
1922 * @str1: extra parameter for the message display
1923 * @str2: extra parameter for the message display
1924 * @str3: extra parameter for the message display
1925 * @str4: extra parameter for the message display
1926 * @str5: extra parameter for the message display
1927 *
1928 * Handle a parser error
1929 */
1930static void
1931xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1932 const xmlChar * strData1, const xmlChar * strData2,
1933 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1934 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1935 const xmlChar * str5)
1936{
1937
1938 xmlGenericErrorFunc channel = NULL;
1939 xmlStructuredErrorFunc schannel = NULL;
1940 void *data = NULL;
1941
1942 if (ctxt != NULL) {
1943 ctxt->nberrors++;
1944 channel = ctxt->error;
1945 data = ctxt->userData;
1946 schannel = ctxt->serror;
1947 }
1948 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1949 error, XML_ERR_ERROR, NULL, 0,
1950 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001951 (const char *) strData3, 0, 0, msg, str1, str2,
1952 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001953}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001954
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001955/************************************************************************
1956 * *
1957 * Allround error functions *
1958 * *
1959 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001960
1961/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001962 * xmlSchemaVTypeErrMemory:
1963 * @node: a context node
1964 * @extra: extra informations
1965 *
1966 * Handle an out of memory condition
1967 */
1968static void
1969xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1970 const char *extra, xmlNodePtr node)
1971{
1972 if (ctxt != NULL) {
1973 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001974 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001975 }
1976 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1977 extra);
1978}
1979
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001980static void
1981xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1982 const char *msg, const xmlChar *str)
1983{
1984 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1985 msg, (const char *) str);
1986}
1987
1988#define WXS_ERROR_TYPE_ERROR 1
1989#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001990/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001991 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001992 * @ctxt: the validation context
1993 * @node: the context node
1994 * @error: the error code
1995 * @msg: the error message
1996 * @str1: extra data
1997 * @str2: extra data
1998 * @str3: extra data
1999 *
2000 * Handle a validation error
2001 */
2002static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002003xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002004 xmlErrorLevel errorLevel,
2005 int error, xmlNodePtr node, int line, const char *msg,
2006 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002007 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002008{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002009 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002010 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002011 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002012
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002013 if (ctxt != NULL) {
2014 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2015 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002016 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002017 if (errorLevel != XML_ERR_WARNING) {
2018 vctxt->nberrors++;
2019 vctxt->err = error;
2020 channel = vctxt->error;
2021 } else {
2022 channel = vctxt->warning;
2023 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002024 schannel = vctxt->serror;
2025 data = vctxt->userData;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002026
2027 /*
2028 * Error node. If we specify a line number, then
2029 * do not channel any node to the error function.
2030 */
2031 if (line == 0) {
2032 if ((node == NULL) &&
2033 (vctxt->depth >= 0) &&
2034 (vctxt->inode != NULL)) {
2035 node = vctxt->inode->node;
2036 }
2037 /*
2038 * Get filename and line if no node-tree.
2039 */
2040 if ((node == NULL) &&
2041 (vctxt->parserCtxt != NULL) &&
2042 (vctxt->parserCtxt->input != NULL)) {
2043 file = vctxt->parserCtxt->input->filename;
2044 line = vctxt->parserCtxt->input->line;
2045 }
2046 } else {
2047 /*
2048 * Override the given node's (if any) position
2049 * and channel only the given line number.
2050 */
2051 node = NULL;
2052 /*
2053 * Get filename.
2054 */
2055 if (vctxt->doc != NULL)
2056 file = (const char *) vctxt->doc->URL;
2057 else if ((vctxt->parserCtxt != NULL) &&
2058 (vctxt->parserCtxt->input != NULL))
2059 file = vctxt->parserCtxt->input->filename;
2060 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002061 __xmlRaiseError(schannel, channel, data, ctxt,
2062 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002063 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002064 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002065 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002066
2067 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2068 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002069 if (errorLevel != XML_ERR_WARNING) {
2070 pctxt->nberrors++;
2071 pctxt->err = error;
2072 channel = pctxt->error;
2073 } else {
2074 channel = pctxt->warning;
2075 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002076 schannel = pctxt->serror;
2077 data = pctxt->userData;
2078 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002079 node, XML_FROM_SCHEMASP, error,
2080 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002081 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002082 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002083 } else {
2084 TODO
2085 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002086 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002087}
2088
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002089/**
2090 * xmlSchemaErr3:
2091 * @ctxt: the validation context
2092 * @node: the context node
2093 * @error: the error code
2094 * @msg: the error message
2095 * @str1: extra data
2096 * @str2: extra data
2097 * @str3: extra data
2098 *
2099 * Handle a validation error
2100 */
2101static void
2102xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2103 int error, xmlNodePtr node, const char *msg,
2104 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2105{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002106 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2107 msg, str1, str2, str3, NULL);
2108}
2109
2110static void
2111xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2112 int error, xmlNodePtr node, const char *msg,
2113 const xmlChar *str1, const xmlChar *str2,
2114 const xmlChar *str3, const xmlChar *str4)
2115{
2116 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2117 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002118}
2119
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002120static void
2121xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2122 int error, xmlNodePtr node, const char *msg,
2123 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002124{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002125 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002126}
2127
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002128static xmlChar *
2129xmlSchemaFormatNodeForError(xmlChar ** msg,
2130 xmlSchemaAbstractCtxtPtr actxt,
2131 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002132{
2133 xmlChar *str = NULL;
2134
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002135 *msg = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002136 if (node != NULL) {
2137 /*
2138 * Work on tree nodes.
2139 */
2140 if (node->type == XML_ATTRIBUTE_NODE) {
2141 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002142
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002143 *msg = xmlStrdup(BAD_CAST "Element '");
2144 if (elem->ns != NULL)
2145 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2146 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002147 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002148 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2149 NULL, elem->name));
2150 FREE_AND_NULL(str);
2151 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2152 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2153 } else {
2154 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002155 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002156 if (node->ns != NULL)
2157 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2158 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002159 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002160 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2161 NULL, node->name));
2162 FREE_AND_NULL(str);
2163 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2164 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2165 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2166 /*
2167 * Work on node infos.
2168 */
2169 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2170 xmlSchemaNodeInfoPtr ielem =
2171 vctxt->elemInfos[vctxt->depth];
2172
2173 *msg = xmlStrdup(BAD_CAST "Element '");
2174 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2175 ielem->nsName, ielem->localName));
2176 FREE_AND_NULL(str);
2177 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2178 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2179 } else {
2180 *msg = xmlStrdup(BAD_CAST "Element '");
2181 }
2182 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2183 vctxt->inode->nsName, vctxt->inode->localName));
2184 FREE_AND_NULL(str);
2185 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002186 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2187 /*
2188 * Hmm, no node while parsing?
2189 * Return an empty string, in case NULL will break something.
2190 */
2191 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002192 } else {
2193 TODO
2194 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002195 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002196 /*
2197 * VAL TODO: The output of the given schema component is currently
2198 * disabled.
2199 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002200#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002201 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2202 *msg = xmlStrcat(*msg, BAD_CAST " [");
2203 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2204 NULL, type, NULL, 0));
2205 FREE_AND_NULL(str)
2206 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002207 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002208#endif
2209 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002210}
2211
Daniel Veillardc0826a72004-08-10 14:17:33 +00002212static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002213xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002214 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002215 const char *message,
2216 const xmlChar *str1,
2217 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002218{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002219 xmlChar *msg = NULL;
2220
2221 msg = xmlStrdup(BAD_CAST "Internal error: ");
2222 msg = xmlStrcat(msg, BAD_CAST funcName);
2223 msg = xmlStrcat(msg, BAD_CAST ", ");
2224 msg = xmlStrcat(msg, BAD_CAST message);
2225 msg = xmlStrcat(msg, BAD_CAST ".\n");
2226
2227 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2228 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002229 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002230
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002231 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002232 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002233 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002234
2235 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002236}
2237
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002238static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002239xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2240 const char *funcName,
2241 const char *message)
2242{
2243 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2244}
2245
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002246#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247static void
2248xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2249 const char *funcName,
2250 const char *message,
2251 const xmlChar *str1,
2252 const xmlChar *str2)
2253{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002254 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002255 str1, str2);
2256}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002257#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002258
2259static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002260xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2261 xmlParserErrors error,
2262 xmlNodePtr node,
2263 xmlSchemaBasicItemPtr item,
2264 const char *message,
2265 const xmlChar *str1, const xmlChar *str2,
2266 const xmlChar *str3, const xmlChar *str4)
2267{
2268 xmlChar *msg = NULL;
2269
2270 if ((node == NULL) && (item != NULL) &&
2271 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2272 node = WXS_ITEM_NODE(item);
2273 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2274 msg = xmlStrcat(msg, BAD_CAST ": ");
2275 } else
2276 xmlSchemaFormatNodeForError(&msg, actxt, node);
2277 msg = xmlStrcat(msg, (const xmlChar *) message);
2278 msg = xmlStrcat(msg, BAD_CAST ".\n");
2279 xmlSchemaErr4(actxt, error, node,
2280 (const char *) msg, str1, str2, str3, str4);
2281 FREE_AND_NULL(msg)
2282}
2283
2284static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002285xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2286 xmlParserErrors error,
2287 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002288 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002289 const char *message,
2290 const xmlChar *str1,
2291 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002292{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002293 xmlSchemaCustomErr4(actxt, error, node, item,
2294 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002295}
2296
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002297
2298
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002299static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002300xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2301 xmlParserErrors error,
2302 xmlNodePtr node,
2303 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2304 const char *message,
2305 const xmlChar *str1,
2306 const xmlChar *str2,
2307 const xmlChar *str3)
2308{
2309 xmlChar *msg = NULL;
2310
2311 xmlSchemaFormatNodeForError(&msg, actxt, node);
2312 msg = xmlStrcat(msg, (const xmlChar *) message);
2313 msg = xmlStrcat(msg, BAD_CAST ".\n");
2314
2315 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002316 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2317 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002318
2319 FREE_AND_NULL(msg)
2320}
2321
2322
2323
2324static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002325xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2326 xmlParserErrors error,
2327 xmlSchemaPSVIIDCNodePtr idcNode,
2328 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2329 const char *message,
2330 const xmlChar *str1,
2331 const xmlChar *str2)
2332{
2333 xmlChar *msg = NULL, *qname = NULL;
2334
2335 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2336 msg = xmlStrcat(msg, (const xmlChar *) message);
2337 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002338 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002339 error, NULL, idcNode->nodeLine, (const char *) msg,
2340 xmlSchemaFormatQName(&qname,
2341 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2342 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002343 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002344 FREE_AND_NULL(qname);
2345 FREE_AND_NULL(msg);
2346}
2347
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002348static int
2349xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2350 xmlNodePtr node)
2351{
2352 if (node != NULL)
2353 return (node->type);
2354 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2355 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2356 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2357 return (-1);
2358}
2359
2360static int
2361xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2362{
2363 switch (item->type) {
2364 case XML_SCHEMA_TYPE_COMPLEX:
2365 case XML_SCHEMA_TYPE_SIMPLE:
2366 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2367 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002368 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002369 case XML_SCHEMA_TYPE_GROUP:
2370 return (1);
2371 case XML_SCHEMA_TYPE_ELEMENT:
2372 if ( ((xmlSchemaElementPtr) item)->flags &
2373 XML_SCHEMAS_ELEM_GLOBAL)
2374 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002375 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002376 case XML_SCHEMA_TYPE_ATTRIBUTE:
2377 if ( ((xmlSchemaAttributePtr) item)->flags &
2378 XML_SCHEMAS_ATTR_GLOBAL)
2379 return(1);
2380 break;
2381 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002382 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002383 return(1);
2384 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002385 return (0);
2386}
2387
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002388static void
2389xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2390 xmlParserErrors error,
2391 xmlNodePtr node,
2392 const xmlChar *value,
2393 xmlSchemaTypePtr type,
2394 int displayValue)
2395{
2396 xmlChar *msg = NULL;
2397
2398 xmlSchemaFormatNodeForError(&msg, actxt, node);
2399
2400 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2401 XML_ATTRIBUTE_NODE))
2402 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2403 else
2404 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2405 "value of ");
2406
2407 if (! xmlSchemaIsGlobalItem(type))
2408 msg = xmlStrcat(msg, BAD_CAST "the local ");
2409 else
2410 msg = xmlStrcat(msg, BAD_CAST "the ");
2411
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002412 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002413 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002414 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002415 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002416 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002417 msg = xmlStrcat(msg, BAD_CAST "union type");
2418
2419 if (xmlSchemaIsGlobalItem(type)) {
2420 xmlChar *str = NULL;
2421 msg = xmlStrcat(msg, BAD_CAST " '");
2422 if (type->builtInType != 0) {
2423 msg = xmlStrcat(msg, BAD_CAST "xs:");
2424 msg = xmlStrcat(msg, type->name);
2425 } else
2426 msg = xmlStrcat(msg,
2427 xmlSchemaFormatQName(&str,
2428 type->targetNamespace, type->name));
2429 msg = xmlStrcat(msg, BAD_CAST "'");
2430 FREE_AND_NULL(str);
2431 }
2432 msg = xmlStrcat(msg, BAD_CAST ".\n");
2433 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2434 XML_ATTRIBUTE_NODE))
2435 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2436 else
2437 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2438 FREE_AND_NULL(msg)
2439}
2440
Daniel Veillardc0826a72004-08-10 14:17:33 +00002441static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002442xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2443 xmlSchemaNodeInfoPtr ni,
2444 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002445{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002446 if (node != NULL) {
2447 if (node->ns != NULL)
2448 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2449 else
2450 return (xmlSchemaFormatQName(str, NULL, node->name));
2451 } else if (ni != NULL)
2452 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2453 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002454}
2455
Daniel Veillardc0826a72004-08-10 14:17:33 +00002456static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002457xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2458 xmlParserErrors error,
2459 xmlSchemaAttrInfoPtr ni,
2460 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002461{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002462 xmlChar *msg = NULL, *str = NULL;
2463
2464 xmlSchemaFormatNodeForError(&msg, actxt, node);
2465 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2466 xmlSchemaErr(actxt, error, node, (const char *) msg,
2467 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2468 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002469 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002470 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002471}
2472
Daniel Veillardc0826a72004-08-10 14:17:33 +00002473static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002474xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2475 xmlParserErrors error,
2476 xmlNodePtr node,
2477 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002478 const char *message,
2479 int nbval,
2480 int nbneg,
2481 xmlChar **values)
2482{
2483 xmlChar *str = NULL, *msg = NULL;
2484 xmlChar *localName, *nsName;
2485 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00002486 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002487
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002488 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002489 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002490 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002491 /*
2492 * Note that is does not make sense to report that we have a
2493 * wildcard here, since the wildcard might be unfolded into
2494 * multiple transitions.
2495 */
2496 if (nbval + nbneg > 0) {
2497 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002498 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002499 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002500 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002501 nsName = NULL;
2502
2503 for (i = 0; i < nbval + nbneg; i++) {
2504 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002505 if (cur == NULL)
2506 continue;
2507 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2508 (cur[3] == ' ')) {
2509 is_not = 1;
2510 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002511 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002512 } else {
2513 is_not = 0;
2514 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002515 /*
2516 * Get the local name.
2517 */
2518 localName = NULL;
2519
2520 end = cur;
2521 if (*end == '*') {
2522 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002523 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002524 } else {
2525 while ((*end != 0) && (*end != '|'))
2526 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002527 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002528 }
2529 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002530 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002531 /*
2532 * Skip "*|*" if they come with negated expressions, since
2533 * they represent the same negated wildcard.
2534 */
2535 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2536 /*
2537 * Get the namespace name.
2538 */
2539 cur = end;
2540 if (*end == '*') {
2541 nsName = xmlStrdup(BAD_CAST "{*}");
2542 } else {
2543 while (*end != 0)
2544 end++;
2545
2546 if (i >= nbval)
2547 nsName = xmlStrdup(BAD_CAST "{##other:");
2548 else
2549 nsName = xmlStrdup(BAD_CAST "{");
2550
2551 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2552 nsName = xmlStrcat(nsName, BAD_CAST "}");
2553 }
2554 str = xmlStrcat(str, BAD_CAST nsName);
2555 FREE_AND_NULL(nsName)
2556 } else {
2557 FREE_AND_NULL(localName);
2558 continue;
2559 }
2560 }
2561 str = xmlStrcat(str, BAD_CAST localName);
2562 FREE_AND_NULL(localName);
2563
2564 if (i < nbval + nbneg -1)
2565 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002566 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002567 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002568 msg = xmlStrcat(msg, BAD_CAST str);
2569 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002570 } else
2571 msg = xmlStrcat(msg, BAD_CAST "\n");
2572 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002573 xmlFree(msg);
2574}
2575
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002576static void
2577xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2578 xmlParserErrors error,
2579 xmlNodePtr node,
2580 const xmlChar *value,
2581 unsigned long length,
2582 xmlSchemaTypePtr type,
2583 xmlSchemaFacetPtr facet,
2584 const char *message,
2585 const xmlChar *str1,
2586 const xmlChar *str2)
2587{
2588 xmlChar *str = NULL, *msg = NULL;
2589 xmlSchemaTypeType facetType;
2590 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2591
2592 xmlSchemaFormatNodeForError(&msg, actxt, node);
2593 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2594 facetType = XML_SCHEMA_FACET_ENUMERATION;
2595 /*
2596 * If enumerations are validated, one must not expect the
2597 * facet to be given.
2598 */
2599 } else
2600 facetType = facet->type;
2601 msg = xmlStrcat(msg, BAD_CAST "[");
2602 msg = xmlStrcat(msg, BAD_CAST "facet '");
2603 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2604 msg = xmlStrcat(msg, BAD_CAST "'] ");
2605 if (message == NULL) {
2606 /*
2607 * Use a default message.
2608 */
2609 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2610 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2611 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2612
2613 char len[25], actLen[25];
2614
2615 /* FIXME, TODO: What is the max expected string length of the
2616 * this value?
2617 */
2618 if (nodeType == XML_ATTRIBUTE_NODE)
2619 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2620 else
2621 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2622
2623 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2624 snprintf(actLen, 24, "%lu", length);
2625
2626 if (facetType == XML_SCHEMA_FACET_LENGTH)
2627 msg = xmlStrcat(msg,
2628 BAD_CAST "this differs from the allowed length of '%s'.\n");
2629 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2630 msg = xmlStrcat(msg,
2631 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2632 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2633 msg = xmlStrcat(msg,
2634 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2635
2636 if (nodeType == XML_ATTRIBUTE_NODE)
2637 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2638 value, (const xmlChar *) actLen, (const xmlChar *) len);
2639 else
2640 xmlSchemaErr(actxt, error, node, (const char *) msg,
2641 (const xmlChar *) actLen, (const xmlChar *) len);
2642
2643 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2644 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2645 "of the set {%s}.\n");
2646 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2647 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2648 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2649 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2650 "by the pattern '%s'.\n");
2651 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2652 facet->value);
2653 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2654 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2655 "minimum value allowed ('%s').\n");
2656 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2657 facet->value);
2658 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2659 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2660 "maximum value allowed ('%s').\n");
2661 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2662 facet->value);
2663 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2664 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2665 "'%s'.\n");
2666 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2667 facet->value);
2668 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2669 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be more than "
2670 "'%s'.\n");
2671 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2672 facet->value);
2673 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2674 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2675 "digits than are allowed ('%s').\n");
2676 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2677 facet->value);
2678 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2679 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2680 "digits than are allowed ('%s').\n");
2681 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2682 facet->value);
2683 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2684 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2685 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2686 } else {
2687 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2688 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2689 }
2690 } else {
2691 msg = xmlStrcat(msg, (const xmlChar *) message);
2692 msg = xmlStrcat(msg, BAD_CAST ".\n");
2693 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2694 }
2695 FREE_AND_NULL(str)
2696 xmlFree(msg);
2697}
2698
2699#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002700 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002701
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002702#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002703
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002704#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2705#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002706
2707#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2708
2709
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002710/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002711 * xmlSchemaPMissingAttrErr:
2712 * @ctxt: the schema validation context
2713 * @ownerDes: the designation of the owner
2714 * @ownerName: the name of the owner
2715 * @ownerItem: the owner as a schema object
2716 * @ownerElem: the owner as an element node
2717 * @node: the parent element node of the missing attribute node
2718 * @type: the corresponding type of the attribute node
2719 *
2720 * Reports an illegal attribute.
2721 */
2722static void
2723xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002724 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002725 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002726 xmlNodePtr ownerElem,
2727 const char *name,
2728 const char *message)
2729{
2730 xmlChar *des = NULL;
2731
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002732 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2733
Daniel Veillardc0826a72004-08-10 14:17:33 +00002734 if (message != NULL)
2735 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002736 else
2737 xmlSchemaPErr(ctxt, ownerElem, error,
2738 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002739 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002740 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002741}
2742
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002743
Daniel Veillardc0826a72004-08-10 14:17:33 +00002744/**
2745 * xmlSchemaPResCompAttrErr:
2746 * @ctxt: the schema validation context
2747 * @error: the error code
2748 * @ownerDes: the designation of the owner
2749 * @ownerItem: the owner as a schema object
2750 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002751 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002752 * @refName: the referenced local name
2753 * @refURI: the referenced namespace URI
2754 * @message: optional message
2755 *
2756 * Used to report QName attribute values that failed to resolve
2757 * to schema components.
2758 */
2759static void
2760xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002761 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002762 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002763 xmlNodePtr ownerElem,
2764 const char *name,
2765 const xmlChar *refName,
2766 const xmlChar *refURI,
2767 xmlSchemaTypeType refType,
2768 const char *refTypeStr)
2769{
2770 xmlChar *des = NULL, *strA = NULL;
2771
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002772 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002773 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002774 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002775 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002776 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002777 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2778 "%s.\n", BAD_CAST des, BAD_CAST name,
2779 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002780 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002781 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002782 FREE_AND_NULL(strA)
2783}
2784
William M. Brack2f2a6632004-08-20 23:09:47 +00002785/**
2786 * xmlSchemaPCustomAttrErr:
2787 * @ctxt: the schema parser context
2788 * @error: the error code
2789 * @ownerDes: the designation of the owner
2790 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002791 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002792 *
2793 * Reports an illegal attribute during the parse.
2794 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002795static void
2796xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002797 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002798 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002799 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002800 xmlAttrPtr attr,
2801 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002802{
2803 xmlChar *des = NULL;
2804
2805 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002806 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002807 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002808 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002809 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002810 } else
2811 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002812 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002813 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002814 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2815 if (ownerDes == NULL)
2816 FREE_AND_NULL(des);
2817}
2818
2819/**
2820 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002821 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002822 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002823 * @ownerDes: the designation of the attribute's owner
2824 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002825 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002826 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002827 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002828 */
2829static void
2830xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002831 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002832 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002833 xmlAttrPtr attr)
2834{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002835 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002836
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002837 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2838 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2839 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2840 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2841 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002842 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002843 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002844}
2845
William M. Brack2f2a6632004-08-20 23:09:47 +00002846/**
2847 * xmlSchemaPCustomErr:
2848 * @ctxt: the schema parser context
2849 * @error: the error code
2850 * @itemDes: the designation of the schema item
2851 * @item: the schema item
2852 * @itemElem: the node of the schema item
2853 * @message: the error message
2854 * @str1: an optional param for the error message
2855 * @str2: an optional param for the error message
2856 * @str3: an optional param for the error message
2857 *
2858 * Reports an error during parsing.
2859 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002860static void
2861xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002862 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002863 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002864 xmlNodePtr itemElem,
2865 const char *message,
2866 const xmlChar *str1,
2867 const xmlChar *str2,
2868 const xmlChar *str3)
2869{
2870 xmlChar *des = NULL, *msg = NULL;
2871
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002872 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002873 msg = xmlStrdup(BAD_CAST "%s: ");
2874 msg = xmlStrcat(msg, (const xmlChar *) message);
2875 msg = xmlStrcat(msg, BAD_CAST ".\n");
2876 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002877 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002878 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002879 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002880 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002881 FREE_AND_NULL(msg);
2882}
2883
William M. Brack2f2a6632004-08-20 23:09:47 +00002884/**
2885 * xmlSchemaPCustomErr:
2886 * @ctxt: the schema parser context
2887 * @error: the error code
2888 * @itemDes: the designation of the schema item
2889 * @item: the schema item
2890 * @itemElem: the node of the schema item
2891 * @message: the error message
2892 * @str1: the optional param for the error message
2893 *
2894 * Reports an error during parsing.
2895 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002896static void
2897xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002898 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002899 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002900 xmlNodePtr itemElem,
2901 const char *message,
2902 const xmlChar *str1)
2903{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002904 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002905 str1, NULL, NULL);
2906}
2907
William M. Brack2f2a6632004-08-20 23:09:47 +00002908/**
2909 * xmlSchemaPAttrUseErr:
2910 * @ctxt: the schema parser context
2911 * @error: the error code
2912 * @itemDes: the designation of the schema type
2913 * @item: the schema type
2914 * @itemElem: the node of the schema type
2915 * @attr: the invalid schema attribute
2916 * @message: the error message
2917 * @str1: the optional param for the error message
2918 *
2919 * Reports an attribute use error during parsing.
2920 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002921static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002922xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002923 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002924 xmlNodePtr node,
2925 xmlSchemaBasicItemPtr ownerItem,
2926 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002927 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002928 const xmlChar *str1, const xmlChar *str2,
2929 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002930{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002931 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002932
2933 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2934 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002935 msg = xmlStrcat(msg,
2936 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002937 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002938 FREE_AND_NULL(str);
2939 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002940 msg = xmlStrcat(msg, (const xmlChar *) message);
2941 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002942 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2943 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002944 xmlFree(msg);
2945}
2946
William M. Brack2f2a6632004-08-20 23:09:47 +00002947/**
2948 * xmlSchemaPIllegalFacetAtomicErr:
2949 * @ctxt: the schema parser context
2950 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002951 * @type: the schema type
2952 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002953 * @facet: the illegal facet
2954 *
2955 * Reports an illegal facet for atomic simple types.
2956 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002957static void
2958xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002959 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002960 xmlSchemaTypePtr type,
2961 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002962 xmlSchemaFacetPtr facet)
2963{
2964 xmlChar *des = NULL, *strT = NULL;
2965
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002966 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2967 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002968 "%s: The facet '%s' is not allowed on types derived from the "
2969 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002970 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002971 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002972 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002973 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002974 FREE_AND_NULL(strT);
2975}
2976
William M. Brack2f2a6632004-08-20 23:09:47 +00002977/**
2978 * xmlSchemaPIllegalFacetListUnionErr:
2979 * @ctxt: the schema parser context
2980 * @error: the error code
2981 * @itemDes: the designation of the schema item involved
2982 * @item: the schema item involved
2983 * @facet: the illegal facet
2984 *
2985 * Reports an illegal facet for <list> and <union>.
2986 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002987static void
2988xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002989 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002990 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002991 xmlSchemaFacetPtr facet)
2992{
2993 xmlChar *des = NULL, *strT = NULL;
2994
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002995 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
2996 type->node);
2997 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002998 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002999 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003000 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003001 FREE_AND_NULL(strT);
3002}
3003
3004/**
3005 * xmlSchemaPMutualExclAttrErr:
3006 * @ctxt: the schema validation context
3007 * @error: the error code
3008 * @elemDes: the designation of the parent element node
3009 * @attr: the bad attribute node
3010 * @type: the corresponding type of the attribute node
3011 *
3012 * Reports an illegal attribute.
3013 */
3014static void
3015xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3016 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003017 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003018 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003019 const char *name1,
3020 const char *name2)
3021{
3022 xmlChar *des = NULL;
3023
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003024 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003025 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003026 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003027 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003028 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003029}
3030
3031/**
3032 * xmlSchemaPSimpleTypeErr:
3033 * @ctxt: the schema validation context
3034 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003035 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003036 * @ownerDes: the designation of the owner
3037 * @ownerItem: the schema object if existent
3038 * @node: the validated node
3039 * @value: the validated value
3040 *
3041 * Reports a simple type validation error.
3042 * TODO: Should this report the value of an element as well?
3043 */
3044static void
3045xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3046 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003047 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003048 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003049 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003050 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003051 const xmlChar *value,
3052 const char *message,
3053 const xmlChar *str1,
3054 const xmlChar *str2)
3055{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003056 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003057
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003058 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003059 if (message == NULL) {
3060 /*
3061 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003062 */
3063 if (type != NULL) {
3064 if (node->type == XML_ATTRIBUTE_NODE)
3065 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3066 else
3067 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3068 "valid value of ");
3069 if (! xmlSchemaIsGlobalItem(type))
3070 msg = xmlStrcat(msg, BAD_CAST "the local ");
3071 else
3072 msg = xmlStrcat(msg, BAD_CAST "the ");
3073
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003074 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003075 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003076 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003077 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003078 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003079 msg = xmlStrcat(msg, BAD_CAST "union type");
3080
3081 if (xmlSchemaIsGlobalItem(type)) {
3082 xmlChar *str = NULL;
3083 msg = xmlStrcat(msg, BAD_CAST " '");
3084 if (type->builtInType != 0) {
3085 msg = xmlStrcat(msg, BAD_CAST "xs:");
3086 msg = xmlStrcat(msg, type->name);
3087 } else
3088 msg = xmlStrcat(msg,
3089 xmlSchemaFormatQName(&str,
3090 type->targetNamespace, type->name));
3091 msg = xmlStrcat(msg, BAD_CAST "'.");
3092 FREE_AND_NULL(str);
3093 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003094 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003095 if (node->type == XML_ATTRIBUTE_NODE)
3096 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3097 else
3098 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3099 "valid.");
3100 }
3101 if (expected) {
3102 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3103 msg = xmlStrcat(msg, BAD_CAST expected);
3104 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3105 } else
3106 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003107 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003108 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3109 else
3110 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3111 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003112 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003113 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003114 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003115 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003116 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003117 /* Cleanup. */
3118 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003119}
3120
William M. Brack2f2a6632004-08-20 23:09:47 +00003121/**
3122 * xmlSchemaPContentErr:
3123 * @ctxt: the schema parser context
3124 * @error: the error code
3125 * @onwerDes: the designation of the holder of the content
3126 * @ownerItem: the owner item of the holder of the content
3127 * @ownerElem: the node of the holder of the content
3128 * @child: the invalid child node
3129 * @message: the optional error message
3130 * @content: the optional string describing the correct content
3131 *
3132 * Reports an error concerning the content of a schema element.
3133 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003134static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003135xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003136 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003137 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003138 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003139 xmlNodePtr child,
3140 const char *message,
3141 const char *content)
3142{
3143 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003144
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003145 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003146 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003147 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3148 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003149 BAD_CAST des, BAD_CAST message);
3150 else {
3151 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003152 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3153 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003154 BAD_CAST des, BAD_CAST content);
3155 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003156 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3157 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003158 BAD_CAST des, NULL);
3159 }
3160 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003161 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003162}
3163
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003164/************************************************************************
3165 * *
3166 * Streamable error functions *
3167 * *
3168 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003169
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003170
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003171
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003172
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003173/************************************************************************
3174 * *
3175 * Validation helper functions *
3176 * *
3177 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003178
Daniel Veillardc0826a72004-08-10 14:17:33 +00003179
Daniel Veillard4255d502002-04-16 15:50:10 +00003180/************************************************************************
3181 * *
3182 * Allocation functions *
3183 * *
3184 ************************************************************************/
3185
3186/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003187 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003188 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003189 *
3190 * Allocate a new Schema structure.
3191 *
3192 * Returns the newly allocated structure or NULL in case or error
3193 */
3194static xmlSchemaPtr
3195xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3196{
3197 xmlSchemaPtr ret;
3198
3199 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3200 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003201 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003202 return (NULL);
3203 }
3204 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003205 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003206 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003207
3208 return (ret);
3209}
3210
3211/**
3212 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003213 *
3214 * Allocate a new Facet structure.
3215 *
3216 * Returns the newly allocated structure or NULL in case or error
3217 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003218xmlSchemaFacetPtr
3219xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003220{
3221 xmlSchemaFacetPtr ret;
3222
3223 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3224 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003225 return (NULL);
3226 }
3227 memset(ret, 0, sizeof(xmlSchemaFacet));
3228
3229 return (ret);
3230}
3231
3232/**
3233 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003234 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003235 * @node: a node
3236 *
3237 * Allocate a new annotation structure.
3238 *
3239 * Returns the newly allocated structure or NULL in case or error
3240 */
3241static xmlSchemaAnnotPtr
3242xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3243{
3244 xmlSchemaAnnotPtr ret;
3245
3246 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3247 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003248 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003249 return (NULL);
3250 }
3251 memset(ret, 0, sizeof(xmlSchemaAnnot));
3252 ret->content = node;
3253 return (ret);
3254}
3255
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003256static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003257xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003258{
3259 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003260
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003261 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3262 if (ret == NULL) {
3263 xmlSchemaPErrMemory(NULL,
3264 "allocating an item list structure", NULL);
3265 return (NULL);
3266 }
3267 memset(ret, 0, sizeof(xmlSchemaItemList));
3268 return (ret);
3269}
3270
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003271static void
3272xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3273{
3274 if (list->items != NULL) {
3275 xmlFree(list->items);
3276 list->items = NULL;
3277 }
3278 list->nbItems = 0;
3279 list->sizeItems = 0;
3280}
3281
3282static int
3283xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3284{
3285 if (list->items == NULL) {
3286 list->items = (void **) xmlMalloc(
3287 20 * sizeof(void *));
3288 if (list->items == NULL) {
3289 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3290 return(-1);
3291 }
3292 list->sizeItems = 20;
3293 } else if (list->sizeItems <= list->nbItems) {
3294 list->sizeItems *= 2;
3295 list->items = (void **) xmlRealloc(list->items,
3296 list->sizeItems * sizeof(void *));
3297 if (list->items == NULL) {
3298 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3299 list->sizeItems = 0;
3300 return(-1);
3301 }
3302 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003303 list->items[list->nbItems++] = item;
3304 return(0);
3305}
3306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003307static int
3308xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3309 int initialSize,
3310 void *item)
3311{
3312 if (list->items == NULL) {
3313 if (initialSize <= 0)
3314 initialSize = 1;
3315 list->items = (void **) xmlMalloc(
3316 initialSize * sizeof(void *));
3317 if (list->items == NULL) {
3318 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3319 return(-1);
3320 }
3321 list->sizeItems = initialSize;
3322 } else if (list->sizeItems <= list->nbItems) {
3323 list->sizeItems *= 2;
3324 list->items = (void **) xmlRealloc(list->items,
3325 list->sizeItems * sizeof(void *));
3326 if (list->items == NULL) {
3327 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3328 list->sizeItems = 0;
3329 return(-1);
3330 }
3331 }
3332 list->items[list->nbItems++] = item;
3333 return(0);
3334}
3335
3336static int
3337xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3338{
3339 if (list->items == NULL) {
3340 list->items = (void **) xmlMalloc(
3341 20 * sizeof(void *));
3342 if (list->items == NULL) {
3343 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3344 return(-1);
3345 }
3346 list->sizeItems = 20;
3347 } else if (list->sizeItems <= list->nbItems) {
3348 list->sizeItems *= 2;
3349 list->items = (void **) xmlRealloc(list->items,
3350 list->sizeItems * sizeof(void *));
3351 if (list->items == NULL) {
3352 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3353 list->sizeItems = 0;
3354 return(-1);
3355 }
3356 }
3357 /*
3358 * Just append if the index is greater/equal than the item count.
3359 */
3360 if (idx >= list->nbItems) {
3361 list->items[list->nbItems++] = item;
3362 } else {
3363 int i;
3364 for (i = list->nbItems; i > idx; i--)
3365 list->items[i] = list->items[i-1];
3366 list->items[idx] = item;
3367 list->nbItems++;
3368 }
3369 return(0);
3370}
3371
3372#if 0 /* enable if ever needed */
3373static int
3374xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3375 int initialSize,
3376 void *item,
3377 int idx)
3378{
3379 if (list->items == NULL) {
3380 if (initialSize <= 0)
3381 initialSize = 1;
3382 list->items = (void **) xmlMalloc(
3383 initialSize * sizeof(void *));
3384 if (list->items == NULL) {
3385 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3386 return(-1);
3387 }
3388 list->sizeItems = initialSize;
3389 } else if (list->sizeItems <= list->nbItems) {
3390 list->sizeItems *= 2;
3391 list->items = (void **) xmlRealloc(list->items,
3392 list->sizeItems * sizeof(void *));
3393 if (list->items == NULL) {
3394 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3395 list->sizeItems = 0;
3396 return(-1);
3397 }
3398 }
3399 /*
3400 * Just append if the index is greater/equal than the item count.
3401 */
3402 if (idx >= list->nbItems) {
3403 list->items[list->nbItems++] = item;
3404 } else {
3405 int i;
3406 for (i = list->nbItems; i > idx; i--)
3407 list->items[i] = list->items[i-1];
3408 list->items[idx] = item;
3409 list->nbItems++;
3410 }
3411 return(0);
3412}
3413#endif
3414
3415static int
3416xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3417{
3418 int i;
3419 if ((list->items == NULL) || (idx >= list->nbItems)) {
3420 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3421 "index error.\n");
3422 return(-1);
3423 }
3424
3425 if (list->nbItems == 1) {
3426 /* TODO: Really free the list? */
3427 xmlFree(list->items);
3428 list->items = NULL;
3429 list->nbItems = 0;
3430 list->sizeItems = 0;
3431 } else if (list->nbItems -1 == idx) {
3432 list->nbItems--;
3433 } else {
3434 for (i = idx; i < list->nbItems -1; i++)
3435 list->items[i] = list->items[i+1];
3436 list->nbItems--;
3437 }
3438 return(0);
3439}
3440
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003441/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003442 * xmlSchemaItemListFree:
3443 * @annot: a schema type structure
3444 *
3445 * Deallocate a annotation structure
3446 */
3447static void
3448xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3449{
3450 if (list == NULL)
3451 return;
3452 if (list->items != NULL)
3453 xmlFree(list->items);
3454 xmlFree(list);
3455}
3456
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003457static void
3458xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003459{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003460 if (bucket == NULL)
3461 return;
3462 if (bucket->globals != NULL) {
3463 xmlSchemaComponentListFree(bucket->globals);
3464 xmlSchemaItemListFree(bucket->globals);
3465 }
3466 if (bucket->locals != NULL) {
3467 xmlSchemaComponentListFree(bucket->locals);
3468 xmlSchemaItemListFree(bucket->locals);
3469 }
3470 if (bucket->relations != NULL) {
3471 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3472 do {
3473 prev = cur;
3474 cur = cur->next;
3475 xmlFree(prev);
3476 } while (cur != NULL);
3477 }
3478 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3479 xmlFreeDoc(bucket->doc);
3480 }
3481 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003482 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3483 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003484 }
3485 xmlFree(bucket);
3486}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003487
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003488static xmlSchemaBucketPtr
3489xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003490 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003491{
3492 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003493 int size;
3494 xmlSchemaPtr mainSchema;
3495
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003496 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003497 PERROR_INT("xmlSchemaBucketCreate",
3498 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003499 return(NULL);
3500 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003501 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003502 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003503 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003504 size = sizeof(xmlSchemaInclude);
3505 else
3506 size = sizeof(xmlSchemaImport);
3507 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3508 if (ret == NULL) {
3509 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3510 return(NULL);
3511 }
3512 memset(ret, 0, size);
3513 ret->targetNamespace = targetNamespace;
3514 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003515 ret->globals = xmlSchemaItemListCreate();
3516 if (ret->globals == NULL) {
3517 xmlFree(ret);
3518 return(NULL);
3519 }
3520 ret->locals = xmlSchemaItemListCreate();
3521 if (ret->locals == NULL) {
3522 xmlFree(ret);
3523 return(NULL);
3524 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003525 /*
3526 * The following will assure that only the first bucket is marked as
3527 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3528 * For each following import buckets an xmlSchema will be created.
3529 */
3530 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003531 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003532 PERROR_INT("xmlSchemaBucketCreate",
3533 "first bucket but it's an include or redefine");
3534 xmlSchemaBucketFree(ret);
3535 return(NULL);
3536 }
3537 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3538 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3539 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003540 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3541 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003542 } else {
3543 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3544 PERROR_INT("xmlSchemaBucketCreate",
3545 "main bucket but it's not the first one");
3546 xmlSchemaBucketFree(ret);
3547 return(NULL);
3548 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3549 /*
3550 * Create a schema for imports.
3551 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003552 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3553 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003554 xmlSchemaBucketFree(ret);
3555 return(NULL);
3556 }
3557 }
3558 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003559 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003560 int res;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003561 /* Imports go into the "schemasImports" slot of the main *schema*. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003562 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003563 mainSchema->schemasImports = xmlHashCreateDict(5,
3564 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003565 if (mainSchema->schemasImports == NULL) {
3566 xmlSchemaBucketFree(ret);
3567 return(NULL);
3568 }
3569 }
3570 if (targetNamespace == NULL)
3571 res = xmlHashAddEntry(mainSchema->schemasImports,
3572 XML_SCHEMAS_NO_NAMESPACE, ret);
3573 else
3574 res = xmlHashAddEntry(mainSchema->schemasImports,
3575 targetNamespace, ret);
3576 if (res != 0) {
3577 PERROR_INT("xmlSchemaBucketCreate",
3578 "failed to add the schema bucket to the hash");
3579 xmlSchemaBucketFree(ret);
3580 return(NULL);
3581 }
3582 } else {
3583 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003584 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3585 WXS_INCBUCKET(ret)->ownerImport =
3586 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003587 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003588 WXS_INCBUCKET(ret)->ownerImport =
3589 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003590
3591 /* Includes got into the "includes" slot of the *main* schema. */
3592 if (mainSchema->includes == NULL) {
3593 mainSchema->includes = xmlSchemaItemListCreate();
3594 if (mainSchema->includes == NULL) {
3595 xmlSchemaBucketFree(ret);
3596 return(NULL);
3597 }
3598 }
3599 xmlSchemaItemListAdd(mainSchema->includes, ret);
3600 }
3601 /*
3602 * Add to list of all buckets; this is used for lookup
3603 * during schema construction time only.
3604 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003605 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003606 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003607 return(ret);
3608}
3609
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003610static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003611xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003612{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003613 if (*list == NULL) {
3614 *list = xmlSchemaItemListCreate();
3615 if (*list == NULL)
3616 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003617 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003618 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003619 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003620}
3621
3622/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003623 * xmlSchemaFreeAnnot:
3624 * @annot: a schema type structure
3625 *
3626 * Deallocate a annotation structure
3627 */
3628static void
3629xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3630{
3631 if (annot == NULL)
3632 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003633 if (annot->next == NULL) {
3634 xmlFree(annot);
3635 } else {
3636 xmlSchemaAnnotPtr prev;
3637
3638 do {
3639 prev = annot;
3640 annot = annot->next;
3641 xmlFree(prev);
3642 } while (annot != NULL);
3643 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003644}
3645
3646/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003647 * xmlSchemaFreeNotation:
3648 * @schema: a schema notation structure
3649 *
3650 * Deallocate a Schema Notation structure.
3651 */
3652static void
3653xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3654{
3655 if (nota == NULL)
3656 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003657 xmlFree(nota);
3658}
3659
3660/**
3661 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003662 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003663 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003664 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003665 */
3666static void
3667xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3668{
3669 if (attr == NULL)
3670 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003671 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003672 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003673 if (attr->defVal != NULL)
3674 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003675 xmlFree(attr);
3676}
3677
3678/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003679 * xmlSchemaFreeAttributeUse:
3680 * @use: an attribute use
3681 *
3682 * Deallocates an attribute use structure.
3683 */
3684static void
3685xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3686{
3687 if (use == NULL)
3688 return;
3689 if (use->annot != NULL)
3690 xmlSchemaFreeAnnot(use->annot);
3691 if (use->defVal != NULL)
3692 xmlSchemaFreeValue(use->defVal);
3693 xmlFree(use);
3694}
3695
3696/**
3697 * xmlSchemaFreeAttributeUseProhib:
3698 * @prohib: an attribute use prohibition
3699 *
3700 * Deallocates an attribute use structure.
3701 */
3702static void
3703xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3704{
3705 if (prohib == NULL)
3706 return;
3707 xmlFree(prohib);
3708}
3709
3710/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003711 * xmlSchemaFreeWildcardNsSet:
3712 * set: a schema wildcard namespace
3713 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003714 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003715 */
3716static void
3717xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3718{
3719 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003720
Daniel Veillard3646d642004-06-02 19:19:14 +00003721 while (set != NULL) {
3722 next = set->next;
3723 xmlFree(set);
3724 set = next;
3725 }
3726}
3727
3728/**
3729 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003730 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003731 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003732 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003733 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003734void
Daniel Veillard3646d642004-06-02 19:19:14 +00003735xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3736{
3737 if (wildcard == NULL)
3738 return;
3739 if (wildcard->annot != NULL)
3740 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003741 if (wildcard->nsSet != NULL)
3742 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3743 if (wildcard->negNsSet != NULL)
3744 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003745 xmlFree(wildcard);
3746}
3747
3748/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003749 * xmlSchemaFreeAttributeGroup:
3750 * @schema: a schema attribute group structure
3751 *
3752 * Deallocate a Schema Attribute Group structure.
3753 */
3754static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003755xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003756{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003757 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003758 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003759 if (attrGr->annot != NULL)
3760 xmlSchemaFreeAnnot(attrGr->annot);
3761 if (attrGr->attrUses != NULL)
3762 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3763 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003764}
3765
3766/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003767 * xmlSchemaFreeQNameRef:
3768 * @item: a QName reference structure
3769 *
3770 * Deallocatea a QName reference structure.
3771 */
3772static void
3773xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3774{
3775 xmlFree(item);
3776}
3777
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003778/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003779 * xmlSchemaFreeTypeLinkList:
3780 * @alink: a type link
3781 *
3782 * Deallocate a list of types.
3783 */
3784static void
3785xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3786{
3787 xmlSchemaTypeLinkPtr next;
3788
3789 while (link != NULL) {
3790 next = link->next;
3791 xmlFree(link);
3792 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003793 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003794}
3795
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003796static void
3797xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3798{
3799 xmlSchemaIDCStateObjPtr next;
3800 while (sto != NULL) {
3801 next = sto->next;
3802 if (sto->history != NULL)
3803 xmlFree(sto->history);
3804 if (sto->xpathCtxt != NULL)
3805 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3806 xmlFree(sto);
3807 sto = next;
3808 }
3809}
3810
3811/**
3812 * xmlSchemaFreeIDC:
3813 * @idc: a identity-constraint definition
3814 *
3815 * Deallocates an identity-constraint definition.
3816 */
3817static void
3818xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3819{
3820 xmlSchemaIDCSelectPtr cur, prev;
3821
3822 if (idcDef == NULL)
3823 return;
3824 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003825 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003826 /* Selector */
3827 if (idcDef->selector != NULL) {
3828 if (idcDef->selector->xpathComp != NULL)
3829 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3830 xmlFree(idcDef->selector);
3831 }
3832 /* Fields */
3833 if (idcDef->fields != NULL) {
3834 cur = idcDef->fields;
3835 do {
3836 prev = cur;
3837 cur = cur->next;
3838 if (prev->xpathComp != NULL)
3839 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003840 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003841 } while (cur != NULL);
3842 }
3843 xmlFree(idcDef);
3844}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003845
Daniel Veillard01fa6152004-06-29 17:04:39 +00003846/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003847 * xmlSchemaFreeElement:
3848 * @schema: a schema element structure
3849 *
3850 * Deallocate a Schema Element structure.
3851 */
3852static void
3853xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3854{
3855 if (elem == NULL)
3856 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003857 if (elem->annot != NULL)
3858 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003859 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003860 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003861 if (elem->defVal != NULL)
3862 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003863 xmlFree(elem);
3864}
3865
3866/**
3867 * xmlSchemaFreeFacet:
3868 * @facet: a schema facet structure
3869 *
3870 * Deallocate a Schema Facet structure.
3871 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003872void
Daniel Veillard4255d502002-04-16 15:50:10 +00003873xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3874{
3875 if (facet == NULL)
3876 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003877 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003878 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003879 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003880 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003881 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003882 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003883 xmlFree(facet);
3884}
3885
3886/**
3887 * xmlSchemaFreeType:
3888 * @type: a schema type structure
3889 *
3890 * Deallocate a Schema Type structure.
3891 */
3892void
3893xmlSchemaFreeType(xmlSchemaTypePtr type)
3894{
3895 if (type == NULL)
3896 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003897 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003898 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003900 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003901
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003902 facet = type->facets;
3903 while (facet != NULL) {
3904 next = facet->next;
3905 xmlSchemaFreeFacet(facet);
3906 facet = next;
3907 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003908 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003909 if (type->attrUses != NULL)
3910 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003911 if (type->memberTypes != NULL)
3912 xmlSchemaFreeTypeLinkList(type->memberTypes);
3913 if (type->facetSet != NULL) {
3914 xmlSchemaFacetLinkPtr next, link;
3915
3916 link = type->facetSet;
3917 do {
3918 next = link->next;
3919 xmlFree(link);
3920 link = next;
3921 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003922 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003923 if (type->contModel != NULL)
3924 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003925 xmlFree(type);
3926}
3927
3928/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003929 * xmlSchemaFreeModelGroupDef:
3930 * @item: a schema model group definition
3931 *
3932 * Deallocates a schema model group definition.
3933 */
3934static void
3935xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3936{
3937 if (item->annot != NULL)
3938 xmlSchemaFreeAnnot(item->annot);
3939 xmlFree(item);
3940}
3941
3942/**
3943 * xmlSchemaFreeModelGroup:
3944 * @item: a schema model group
3945 *
3946 * Deallocates a schema model group structure.
3947 */
3948static void
3949xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3950{
3951 if (item->annot != NULL)
3952 xmlSchemaFreeAnnot(item->annot);
3953 xmlFree(item);
3954}
3955
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003956static void
3957xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3958{
3959 if ((list == NULL) || (list->nbItems == 0))
3960 return;
3961 {
3962 xmlSchemaTreeItemPtr item;
3963 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3964 int i;
3965
3966 for (i = 0; i < list->nbItems; i++) {
3967 item = items[i];
3968 if (item == NULL)
3969 continue;
3970 switch (item->type) {
3971 case XML_SCHEMA_TYPE_SIMPLE:
3972 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003973 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003974 break;
3975 case XML_SCHEMA_TYPE_ATTRIBUTE:
3976 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003977 break;
3978 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
3979 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
3980 break;
3981 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
3982 xmlSchemaFreeAttributeUseProhib(
3983 (xmlSchemaAttributeUseProhibPtr) item);
3984 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003985 case XML_SCHEMA_TYPE_ELEMENT:
3986 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
3987 break;
3988 case XML_SCHEMA_TYPE_PARTICLE:
3989 if (item->annot != NULL)
3990 xmlSchemaFreeAnnot(item->annot);
3991 xmlFree(item);
3992 break;
3993 case XML_SCHEMA_TYPE_SEQUENCE:
3994 case XML_SCHEMA_TYPE_CHOICE:
3995 case XML_SCHEMA_TYPE_ALL:
3996 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
3997 break;
3998 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
3999 xmlSchemaFreeAttributeGroup(
4000 (xmlSchemaAttributeGroupPtr) item);
4001 break;
4002 case XML_SCHEMA_TYPE_GROUP:
4003 xmlSchemaFreeModelGroupDef(
4004 (xmlSchemaModelGroupDefPtr) item);
4005 break;
4006 case XML_SCHEMA_TYPE_ANY:
4007 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4008 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4009 break;
4010 case XML_SCHEMA_TYPE_IDC_KEY:
4011 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4012 case XML_SCHEMA_TYPE_IDC_KEYREF:
4013 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4014 break;
4015 case XML_SCHEMA_TYPE_NOTATION:
4016 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4017 break;
4018 case XML_SCHEMA_EXTRA_QNAMEREF:
4019 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4020 break;
4021 default: {
4022 /* TODO: This should never be hit. */
4023 xmlSchemaPSimpleInternalErr(NULL,
4024 "Internal error: xmlSchemaComponentListFree, "
4025 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004026 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004027 }
4028 break;
4029 }
4030 }
4031 list->nbItems = 0;
4032 }
4033}
4034
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004035/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004036 * xmlSchemaFree:
4037 * @schema: a schema structure
4038 *
4039 * Deallocate a Schema structure.
4040 */
4041void
4042xmlSchemaFree(xmlSchemaPtr schema)
4043{
4044 if (schema == NULL)
4045 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004046 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004047 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004048 TODO
4049 /*
4050 * Note that those slots are not responsible for freeing
4051 * schema components anymore; this will now be done by
4052 * the schema buckets.
4053 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004054 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004055 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004056 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004057 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004058 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004059 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004060 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004061 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004062 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004063 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004064 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004065 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004066 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004067 xmlHashFree(schema->idcDef, NULL);
4068
Daniel Veillard1d913862003-11-21 00:28:39 +00004069 if (schema->schemasImports != NULL)
4070 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004071 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004072 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004073 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4074 int i;
4075 for (i = 0; i < list->nbItems; i++) {
4076 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4077 }
4078 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004079 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004080 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004081 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004082 /* Never free the doc here, since this will be done by the buckets. */
4083
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004084 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004085 xmlFree(schema);
4086}
4087
4088/************************************************************************
4089 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004090 * Debug functions *
4091 * *
4092 ************************************************************************/
4093
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004094#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004095
Daniel Veillard4255d502002-04-16 15:50:10 +00004096/**
4097 * xmlSchemaElementDump:
4098 * @elem: an element
4099 * @output: the file output
4100 *
4101 * Dump the element
4102 */
4103static void
4104xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004105 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004106 const xmlChar * namespace ATTRIBUTE_UNUSED,
4107 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004108{
4109 if (elem == NULL)
4110 return;
4111
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004112
4113 fprintf(output, "Element");
4114 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4115 fprintf(output, " (global)");
4116 fprintf(output, ": %s ", elem->name);
4117 if (namespace != NULL)
4118 fprintf(output, "ns %s", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004119 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004120#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004121 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004122 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004123 if (elem->maxOccurs >= UNBOUNDED)
4124 fprintf(output, "max: unbounded\n");
4125 else if (elem->maxOccurs != 1)
4126 fprintf(output, "max: %d\n", elem->maxOccurs);
4127 else
4128 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004129 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004130#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004131 /*
4132 * Misc other properties.
4133 */
4134 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4135 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4136 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004137 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004138 fprintf(output, " props: ");
4139 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4140 fprintf(output, "[fixed] ");
4141 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4142 fprintf(output, "[default] ");
4143 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4144 fprintf(output, "[abstract] ");
4145 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4146 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004147 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004148 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004149 /*
4150 * Default/fixed value.
4151 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004152 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004153 fprintf(output, " value: '%s'\n", elem->value);
4154 /*
4155 * Type.
4156 */
4157 if (elem->namedType != NULL) {
4158 fprintf(output, " type: %s ", elem->namedType);
4159 if (elem->namedTypeNs != NULL)
4160 fprintf(output, "ns %s\n", elem->namedTypeNs);
4161 else
4162 fprintf(output, "\n");
4163 }
4164 /*
4165 * Substitution group.
4166 */
4167 if (elem->substGroup != NULL) {
4168 fprintf(output, " substitutionGroup: %s ", elem->substGroup);
4169 if (elem->substGroupNs != NULL)
4170 fprintf(output, "ns %s\n", elem->substGroupNs);
4171 else
4172 fprintf(output, "\n");
4173 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004174}
4175
4176/**
4177 * xmlSchemaAnnotDump:
4178 * @output: the file output
4179 * @annot: a annotation
4180 *
4181 * Dump the annotation
4182 */
4183static void
4184xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4185{
4186 xmlChar *content;
4187
4188 if (annot == NULL)
4189 return;
4190
4191 content = xmlNodeGetContent(annot->content);
4192 if (content != NULL) {
4193 fprintf(output, " Annot: %s\n", content);
4194 xmlFree(content);
4195 } else
4196 fprintf(output, " Annot: empty\n");
4197}
4198
4199/**
4200 * xmlSchemaTypeDump:
4201 * @output: the file output
4202 * @type: a type structure
4203 *
4204 * Dump a SchemaType structure
4205 */
4206static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004207xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4208{
4209 xmlChar *str = NULL;
4210 xmlSchemaTreeItemPtr term;
4211 char shift[100];
4212 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004213
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004214 if (particle == NULL)
4215 return;
4216 for (i = 0;((i < depth) && (i < 25));i++)
4217 shift[2 * i] = shift[2 * i + 1] = ' ';
4218 shift[2 * i] = shift[2 * i + 1] = 0;
4219 fprintf(output, shift);
4220 if (particle->children == NULL) {
4221 fprintf(output, "MISSING particle term\n");
4222 return;
4223 }
4224 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004225 if (term == NULL) {
4226 fprintf(output, "(NULL)");
4227 } else {
4228 switch (term->type) {
4229 case XML_SCHEMA_TYPE_ELEMENT:
4230 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4231 ((xmlSchemaElementPtr)term)->targetNamespace,
4232 ((xmlSchemaElementPtr)term)->name));
4233 break;
4234 case XML_SCHEMA_TYPE_SEQUENCE:
4235 fprintf(output, "SEQUENCE");
4236 break;
4237 case XML_SCHEMA_TYPE_CHOICE:
4238 fprintf(output, "CHOICE");
4239 break;
4240 case XML_SCHEMA_TYPE_ALL:
4241 fprintf(output, "ALL");
4242 break;
4243 case XML_SCHEMA_TYPE_ANY:
4244 fprintf(output, "ANY");
4245 break;
4246 default:
4247 fprintf(output, "UNKNOWN\n");
4248 return;
4249 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004250 }
4251 if (particle->minOccurs != 1)
4252 fprintf(output, " min: %d", particle->minOccurs);
4253 if (particle->maxOccurs >= UNBOUNDED)
4254 fprintf(output, " max: unbounded");
4255 else if (particle->maxOccurs != 1)
4256 fprintf(output, " max: %d", particle->maxOccurs);
4257 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004258 if (term &&
4259 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4260 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4261 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4262 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004263 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4264 output, depth +1);
4265 }
4266 if (particle->next != NULL)
4267 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4268 output, depth);
4269}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004270
4271/**
4272 * xmlSchemaAttrUsesDump:
4273 * @uses: attribute uses list
4274 * @output: the file output
4275 *
4276 * Dumps a list of attribute use components.
4277 */
4278static void
4279xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4280{
4281 xmlSchemaAttributeUsePtr use;
4282 xmlSchemaAttributeUseProhibPtr prohib;
4283 xmlSchemaQNameRefPtr ref;
4284 const xmlChar *name, *tns;
4285 xmlChar *str = NULL;
4286 int i;
4287
4288 if ((uses == NULL) || (uses->nbItems == 0))
4289 return;
4290
4291 fprintf(output, "uses:\n");
4292 for (i = 0; i < uses->nbItems; i++) {
4293 use = uses->items[i];
4294 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4295 fprintf(output, " [prohibition] ");
4296 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4297 name = prohib->name;
4298 tns = prohib->targetNamespace;
4299 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4300 fprintf(output, " [reference] ");
4301 ref = (xmlSchemaQNameRefPtr) use;
4302 name = ref->name;
4303 tns = ref->targetNamespace;
4304 } else {
4305 fprintf(output, " [use] ");
4306 name = WXS_ATTRUSE_DECL_NAME(use);
4307 tns = WXS_ATTRUSE_DECL_TNS(use);
4308 }
4309 fprintf(output, "%s\n",
4310 (const char *) xmlSchemaFormatQName(&str, tns, name));
4311 FREE_AND_NULL(str);
4312 }
4313}
4314
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004315/**
4316 * xmlSchemaTypeDump:
4317 * @output: the file output
4318 * @type: a type structure
4319 *
4320 * Dump a SchemaType structure
4321 */
4322static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004323xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4324{
4325 if (type == NULL) {
4326 fprintf(output, "Type: NULL\n");
4327 return;
4328 }
4329 fprintf(output, "Type: ");
4330 if (type->name != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004331 fprintf(output, "%s ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004332 else
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004333 fprintf(output, "no name ");
4334 if (type->targetNamespace != NULL)
4335 fprintf(output, "ns %s ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004336 switch (type->type) {
4337 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004338 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004339 break;
4340 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004341 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004342 break;
4343 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004344 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004345 break;
4346 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004347 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004348 break;
4349 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004350 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004351 break;
4352 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004353 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004354 break;
4355 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004356 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004357 break;
4358 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004359 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004360 break;
4361 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004362 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004363 break;
4364 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004365 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004366 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004367 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004368 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004369 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004370 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004371 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004372 break;
4373 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004374 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004375 break;
4376 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004377 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004378 break;
4379 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004380 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004381 break;
4382 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004383 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004384 break;
4385 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004386 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004387 break;
4388 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004389 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004390 break;
4391 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004392 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004393 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004394 }
4395 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004396 if (type->base != NULL) {
4397 fprintf(output, " base type: %s", type->base);
4398 if (type->baseNs != NULL)
4399 fprintf(output, " ns %s\n", type->baseNs);
4400 else
4401 fprintf(output, "\n");
4402 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004403 if (type->attrUses != NULL)
4404 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004405 if (type->annot != NULL)
4406 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004407#ifdef DUMP_CONTENT_MODEL
4408 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4409 (type->subtypes != NULL)) {
4410 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4411 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004412 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004413#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004414}
4415
4416/**
4417 * xmlSchemaDump:
4418 * @output: the file output
4419 * @schema: a schema structure
4420 *
4421 * Dump a Schema structure.
4422 */
4423void
4424xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4425{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004426 if (output == NULL)
4427 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004428 if (schema == NULL) {
4429 fprintf(output, "Schemas: NULL\n");
4430 return;
4431 }
4432 fprintf(output, "Schemas: ");
4433 if (schema->name != NULL)
4434 fprintf(output, "%s, ", schema->name);
4435 else
4436 fprintf(output, "no name, ");
4437 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004438 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004439 else
4440 fprintf(output, "no target namespace");
4441 fprintf(output, "\n");
4442 if (schema->annot != NULL)
4443 xmlSchemaAnnotDump(output, schema->annot);
4444
4445 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4446 output);
4447 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004448 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004449}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004450
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004451#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004452/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004453 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004454 * @vctxt: the WXS validation context
4455 *
4456 * Displays the current IDC table for debug purposes.
4457 */
4458static void
4459xmlSchemaDebugDumpIDCTable(FILE * output,
4460 const xmlChar *namespaceName,
4461 const xmlChar *localName,
4462 xmlSchemaPSVIIDCBindingPtr bind)
4463{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004464 xmlChar *str = NULL;
4465 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004466 xmlSchemaPSVIIDCNodePtr tab;
4467 xmlSchemaPSVIIDCKeyPtr key;
4468 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004469
4470 fprintf(output, "IDC: TABLES on %s\n",
4471 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004472 FREE_AND_NULL(str)
4473
4474 if (bind == NULL)
4475 return;
4476 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004477 fprintf(output, "IDC: BINDING %s\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004478 xmlSchemaGetComponentQName(&str,
4479 bind->definition));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004480 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004481 for (i = 0; i < bind->nbNodes; i++) {
4482 tab = bind->nodeTable[i];
4483 fprintf(output, " ( ");
4484 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004485 key = tab->keys[j];
4486 if ((key != NULL) && (key->val != NULL)) {
4487 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004488 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004489 fprintf(output, "\"%s\" ", value);
4490 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004491 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004492 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004493 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004494 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004495 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004496 else
4497 fprintf(output, "(key missing), ");
4498 }
4499 fprintf(output, ")\n");
4500 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004501 if (bind->dupls && bind->dupls->nbItems) {
4502 fprintf(output, "IDC: dupls:\n");
4503 for (i = 0; i < bind->dupls->nbItems; i++) {
4504 tab = bind->dupls->items[i];
4505 fprintf(output, " ( ");
4506 for (j = 0; j < bind->definition->nbFields; j++) {
4507 key = tab->keys[j];
4508 if ((key != NULL) && (key->val != NULL)) {
4509 res = xmlSchemaGetCanonValue(key->val, &value);
4510 if (res >= 0)
4511 fprintf(output, "\"%s\" ", value);
4512 else
4513 fprintf(output, "CANON-VALUE-FAILED ");
4514 if (res == 0)
4515 FREE_AND_NULL(value)
4516 } else if (key != NULL)
4517 fprintf(output, "(no val), ");
4518 else
4519 fprintf(output, "(key missing), ");
4520 }
4521 fprintf(output, ")\n");
4522 }
4523 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004524 bind = bind->next;
4525 } while (bind != NULL);
4526}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004527#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004528#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004529
4530/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004531 * *
4532 * Utilities *
4533 * *
4534 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004535
Daniel Veillardc0826a72004-08-10 14:17:33 +00004536/**
4537 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004538 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004539 * @name: the name of the attribute
4540 *
4541 * Seeks an attribute with a name of @name in
4542 * no namespace.
4543 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004544 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004545 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004546static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004547xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004548{
4549 xmlAttrPtr prop;
4550
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004551 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004552 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004553 prop = node->properties;
4554 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004555 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004556 return(prop);
4557 prop = prop->next;
4558 }
4559 return (NULL);
4560}
4561
4562/**
4563 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004564 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004565 * @uri: the uri
4566 * @name: the name of the attribute
4567 *
4568 * Seeks an attribute with a local name of @name and
4569 * a namespace URI of @uri.
4570 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004571 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004572 */
4573static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004574xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004575{
4576 xmlAttrPtr prop;
4577
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004578 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004579 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004580 prop = node->properties;
4581 while (prop != NULL) {
4582 if ((prop->ns != NULL) &&
4583 xmlStrEqual(prop->name, BAD_CAST name) &&
4584 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004585 return(prop);
4586 prop = prop->next;
4587 }
4588 return (NULL);
4589}
4590
4591static const xmlChar *
4592xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4593{
4594 xmlChar *val;
4595 const xmlChar *ret;
4596
4597 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004598 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004599 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004600 ret = xmlDictLookup(ctxt->dict, val, -1);
4601 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004602 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004603}
4604
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004605static const xmlChar *
4606xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4607{
4608 return((const xmlChar*) xmlNodeGetContent(node));
4609}
4610
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004611/**
4612 * xmlSchemaGetProp:
4613 * @ctxt: the parser context
4614 * @node: the node
4615 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004616 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004617 * Read a attribute value and internalize the string
4618 *
4619 * Returns the string or NULL if not present.
4620 */
4621static const xmlChar *
4622xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4623 const char *name)
4624{
4625 xmlChar *val;
4626 const xmlChar *ret;
4627
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004628 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004629 if (val == NULL)
4630 return(NULL);
4631 ret = xmlDictLookup(ctxt->dict, val, -1);
4632 xmlFree(val);
4633 return(ret);
4634}
4635
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004636/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004637 * *
4638 * Parsing functions *
4639 * *
4640 ************************************************************************/
4641
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004642#define WXS_FIND_GLOBAL_ITEM(slot) \
4643 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4644 ret = xmlHashLookup(schema->slot, name); \
4645 if (ret != NULL) goto exit; \
4646 } \
4647 if (xmlHashSize(schema->schemasImports) > 1) { \
4648 xmlSchemaImportPtr import; \
4649 if (nsName == NULL) \
4650 import = xmlHashLookup(schema->schemasImports, \
4651 XML_SCHEMAS_NO_NAMESPACE); \
4652 else \
4653 import = xmlHashLookup(schema->schemasImports, nsName); \
4654 if (import == NULL) \
4655 goto exit; \
4656 ret = xmlHashLookup(import->schema->slot, name); \
4657 }
4658
Daniel Veillard4255d502002-04-16 15:50:10 +00004659/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004660 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004661 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004662 * @name: the element name
4663 * @ns: the element namespace
4664 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004665 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004666 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004667 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004668 */
4669static xmlSchemaElementPtr
4670xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004671 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004672{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004673 xmlSchemaElementPtr ret = NULL;
4674
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004675 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004676 return(NULL);
4677 if (schema != NULL) {
4678 WXS_FIND_GLOBAL_ITEM(elemDecl)
4679 }
4680exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004681#ifdef DEBUG
4682 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004683 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004684 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004685 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004686 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004687 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004688 }
4689#endif
4690 return (ret);
4691}
4692
4693/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004694 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004695 * @schema: the main schema
4696 * @name: the type's name
4697 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004698 *
4699 * Lookup a type in the schemas or the predefined types
4700 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004701 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004702 */
4703static xmlSchemaTypePtr
4704xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004705 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004706{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004707 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004708
4709 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004710 return (NULL);
4711 /* First try the built-in types. */
4712 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4713 ret = xmlSchemaGetPredefinedType(name, nsName);
4714 if (ret != NULL)
4715 goto exit;
4716 /*
4717 * Note that we try the parsed schemas as well here
4718 * since one might have parsed the S4S, which contain more
4719 * than the built-in types.
4720 * TODO: Can we optimize this?
4721 */
4722 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004723 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004724 WXS_FIND_GLOBAL_ITEM(typeDecl)
4725 }
4726exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004727
Daniel Veillard4255d502002-04-16 15:50:10 +00004728#ifdef DEBUG
4729 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004730 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004731 fprintf(stderr, "Unable to lookup type %s", name);
4732 else
4733 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004734 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004735 }
4736#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004737 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004738}
4739
Daniel Veillard3646d642004-06-02 19:19:14 +00004740/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004741 * xmlSchemaGetAttributeDecl:
4742 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004743 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004744 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004745 *
4746 * Lookup a an attribute in the schema or imported schemas
4747 *
4748 * Returns the attribute declaration or NULL if not found.
4749 */
4750static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004751xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004752 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004753{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004754 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004755
4756 if ((name == NULL) || (schema == NULL))
4757 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004758 if (schema != NULL) {
4759 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004760 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004761exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004762#ifdef DEBUG
4763 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004764 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004765 fprintf(stderr, "Unable to lookup attribute %s", name);
4766 else
4767 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004768 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004769 }
4770#endif
4771 return (ret);
4772}
4773
4774/**
4775 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004776 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004777 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004778 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004779 *
4780 * Lookup a an attribute group in the schema or imported schemas
4781 *
4782 * Returns the attribute group definition or NULL if not found.
4783 */
4784static xmlSchemaAttributeGroupPtr
4785xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004786 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004787{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004788 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004789
4790 if ((name == NULL) || (schema == NULL))
4791 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004792 if (schema != NULL) {
4793 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4794 }
4795exit:
4796 /* TODO:
4797 if ((ret != NULL) && (ret->redef != NULL)) {
4798 * Return the last redefinition. *
4799 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004800 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004801 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004802#ifdef DEBUG
4803 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004804 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004805 fprintf(stderr, "Unable to lookup attribute group %s", name);
4806 else
4807 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004808 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004809 }
4810#endif
4811 return (ret);
4812}
4813
4814/**
4815 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004816 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004817 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004818 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004819 *
4820 * Lookup a group in the schema or imported schemas
4821 *
4822 * Returns the group definition or NULL if not found.
4823 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004824static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004825xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004826 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004827{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004828 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004829
4830 if ((name == NULL) || (schema == NULL))
4831 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004832 if (schema != NULL) {
4833 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004834 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004835exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004836
Daniel Veillard3646d642004-06-02 19:19:14 +00004837#ifdef DEBUG
4838 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004839 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004840 fprintf(stderr, "Unable to lookup group %s", name);
4841 else
4842 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004843 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004844 }
4845#endif
4846 return (ret);
4847}
4848
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004849static xmlSchemaNotationPtr
4850xmlSchemaGetNotation(xmlSchemaPtr schema,
4851 const xmlChar *name,
4852 const xmlChar *nsName)
4853{
4854 xmlSchemaNotationPtr ret = NULL;
4855
4856 if ((name == NULL) || (schema == NULL))
4857 return (NULL);
4858 if (schema != NULL) {
4859 WXS_FIND_GLOBAL_ITEM(notaDecl)
4860 }
4861exit:
4862 return (ret);
4863}
4864
4865static xmlSchemaIDCPtr
4866xmlSchemaGetIDC(xmlSchemaPtr schema,
4867 const xmlChar *name,
4868 const xmlChar *nsName)
4869{
4870 xmlSchemaIDCPtr ret = NULL;
4871
4872 if ((name == NULL) || (schema == NULL))
4873 return (NULL);
4874 if (schema != NULL) {
4875 WXS_FIND_GLOBAL_ITEM(idcDef)
4876 }
4877exit:
4878 return (ret);
4879}
4880
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004881/**
4882 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004883 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004884 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004885 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004886 *
4887 * Lookup a group in the schema or imported schemas
4888 *
4889 * Returns the group definition or NULL if not found.
4890 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004891static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004892xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4893 xmlSchemaTypeType itemType,
4894 const xmlChar *name,
4895 const xmlChar *targetNs)
4896{
4897 switch (itemType) {
4898 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004899 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004900 name, targetNs));
4901 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004902 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004903 name, targetNs));
4904 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004905 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004906 return (NULL);
4907 }
4908}
4909
Daniel Veillard4255d502002-04-16 15:50:10 +00004910/************************************************************************
4911 * *
4912 * Parsing functions *
4913 * *
4914 ************************************************************************/
4915
4916#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004917 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004918
4919/**
4920 * xmlSchemaIsBlank:
4921 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004922 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004923 *
4924 * Check if a string is ignorable
4925 *
4926 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4927 */
4928static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004929xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004930{
Daniel Veillard4255d502002-04-16 15:50:10 +00004931 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004932 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004933 if (len < 0) {
4934 while (*str != 0) {
4935 if (!(IS_BLANK_CH(*str)))
4936 return (0);
4937 str++;
4938 }
4939 } else while ((*str != 0) && (len != 0)) {
4940 if (!(IS_BLANK_CH(*str)))
4941 return (0);
4942 str++;
4943 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004945
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004946 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004947}
4948
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004949#define WXS_COMP_NAME(c, t) ((t) (c))->name
4950#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4951/*
4952* xmlSchemaFindRedefCompInGraph:
4953* ATTENTION TODO: This uses pointer comp. for strings.
4954*/
4955static xmlSchemaBasicItemPtr
4956xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4957 xmlSchemaTypeType type,
4958 const xmlChar *name,
4959 const xmlChar *nsName)
4960{
4961 xmlSchemaBasicItemPtr ret;
4962 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004963
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004964 if ((bucket == NULL) || (name == NULL))
4965 return(NULL);
4966 if ((bucket->globals == NULL) ||
4967 (bucket->globals->nbItems == 0))
4968 goto subschemas;
4969 /*
4970 * Search in global components.
4971 */
4972 for (i = 0; i < bucket->globals->nbItems; i++) {
4973 ret = bucket->globals->items[i];
4974 if (ret->type == type) {
4975 switch (type) {
4976 case XML_SCHEMA_TYPE_COMPLEX:
4977 case XML_SCHEMA_TYPE_SIMPLE:
4978 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
4979 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
4980 nsName))
4981 {
4982 return(ret);
4983 }
4984 break;
4985 case XML_SCHEMA_TYPE_GROUP:
4986 if ((WXS_COMP_NAME(ret,
4987 xmlSchemaModelGroupDefPtr) == name) &&
4988 (WXS_COMP_TNS(ret,
4989 xmlSchemaModelGroupDefPtr) == nsName))
4990 {
4991 return(ret);
4992 }
4993 break;
4994 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4995 if ((WXS_COMP_NAME(ret,
4996 xmlSchemaAttributeGroupPtr) == name) &&
4997 (WXS_COMP_TNS(ret,
4998 xmlSchemaAttributeGroupPtr) == nsName))
4999 {
5000 return(ret);
5001 }
5002 default:
5003 /* Should not be hit. */
5004 return(NULL);
5005 }
5006 }
5007 }
5008subschemas:
5009 /*
5010 * Process imported/included schemas.
5011 */
5012 if (bucket->relations != NULL) {
5013 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5014
5015 /*
5016 * TODO: Marking the bucket will not avoid multiple searches
5017 * in the same schema, but avoids at least circularity.
5018 */
5019 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5020 do {
5021 if ((rel->bucket != NULL) &&
5022 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5023 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5024 type, name, nsName);
5025 if (ret != NULL)
5026 return(ret);
5027 }
5028 rel = rel->next;
5029 } while (rel != NULL);
5030 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5031 }
5032 return(NULL);
5033}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005034
5035/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005036 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005037 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005038 * @schema: the schema being built
5039 * @name: the item name
5040 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005041 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005042 * *WARNING* this interface is highly subject to change
5043 *
5044 * Returns the new struture or NULL in case of error
5045 */
5046static xmlSchemaNotationPtr
5047xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005048 const xmlChar *name, const xmlChar *nsName,
5049 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005050{
5051 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005052
5053 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5054 return (NULL);
5055
Daniel Veillard4255d502002-04-16 15:50:10 +00005056 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5057 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005058 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005059 return (NULL);
5060 }
5061 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005062 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005063 ret->name = name;
5064 ret->targetNamespace = nsName;
5065 /* TODO: do we need the node to be set?
5066 * ret->node = node;*/
5067 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005068 return (ret);
5069}
5070
Daniel Veillard4255d502002-04-16 15:50:10 +00005071/**
5072 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005073 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005074 * @schema: the schema being built
5075 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005076 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005077 *
5078 * Add an XML schema Attrribute declaration
5079 * *WARNING* this interface is highly subject to change
5080 *
5081 * Returns the new struture or NULL in case of error
5082 */
5083static xmlSchemaAttributePtr
5084xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005085 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005086 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005087{
5088 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005089
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005090 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005091 return (NULL);
5092
5093 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5094 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005095 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005096 return (NULL);
5097 }
5098 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005099 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5100 ret->node = node;
5101 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005102 ret->targetNamespace = nsName;
5103
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005104 if (topLevel)
5105 WXS_ADD_GLOBAL(ctxt, ret);
5106 else
5107 WXS_ADD_LOCAL(ctxt, ret);
5108 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 return (ret);
5110}
5111
5112/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005113 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005114 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 * @schema: the schema being built
5116 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005117 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005119 * Add an XML schema Attrribute declaration
5120 * *WARNING* this interface is highly subject to change
5121 *
5122 * Returns the new struture or NULL in case of error
5123 */
5124static xmlSchemaAttributeUsePtr
5125xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5126 xmlNodePtr node)
5127{
5128 xmlSchemaAttributeUsePtr ret = NULL;
5129
5130 if (pctxt == NULL)
5131 return (NULL);
5132
5133 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5134 if (ret == NULL) {
5135 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5136 return (NULL);
5137 }
5138 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5139 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5140 ret->node = node;
5141
5142 WXS_ADD_LOCAL(pctxt, ret);
5143 return (ret);
5144}
5145
5146/*
5147* xmlSchemaAddRedef:
5148*
5149* Adds a redefinition information. This is used at a later stage to:
5150* resolve references to the redefined components and to check constraints.
5151*/
5152static xmlSchemaRedefPtr
5153xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5154 xmlSchemaBucketPtr targetBucket,
5155 void *item,
5156 const xmlChar *refName,
5157 const xmlChar *refTargetNs)
5158{
5159 xmlSchemaRedefPtr ret;
5160
5161 ret = (xmlSchemaRedefPtr)
5162 xmlMalloc(sizeof(xmlSchemaRedef));
5163 if (ret == NULL) {
5164 xmlSchemaPErrMemory(pctxt,
5165 "allocating redefinition info", NULL);
5166 return (NULL);
5167 }
5168 memset(ret, 0, sizeof(xmlSchemaRedef));
5169 ret->item = item;
5170 ret->targetBucket = targetBucket;
5171 ret->refName = refName;
5172 ret->refTargetNs = refTargetNs;
5173 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5174 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5175 else
5176 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5177 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5178
5179 return (ret);
5180}
5181
5182/**
5183 * xmlSchemaAddAttributeGroupDefinition:
5184 * @ctxt: a schema parser context
5185 * @schema: the schema being built
5186 * @name: the item name
5187 * @nsName: the target namespace
5188 * @node: the corresponding node
5189 *
5190 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005191 *
5192 * Returns the new struture or NULL in case of error
5193 */
5194static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005195xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5196 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5197 const xmlChar *name,
5198 const xmlChar *nsName,
5199 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005200{
5201 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005202
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005203 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005204 return (NULL);
5205
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005206 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005207 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005208 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005209 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005210 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005211 }
5212 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005213 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005214 ret->name = name;
5215 ret->targetNamespace = nsName;
5216 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005217
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005218 /* TODO: Remove the flag. */
5219 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5220 if (pctxt->isRedefine) {
5221 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5222 ret, name, nsName);
5223 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005224 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005225 return(NULL);
5226 }
5227 pctxt->redefCounter = 0;
5228 }
5229 WXS_ADD_GLOBAL(pctxt, ret);
5230 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005231 return (ret);
5232}
5233
5234/**
5235 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005236 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005237 * @schema: the schema being built
5238 * @name: the type name
5239 * @namespace: the type namespace
5240 *
5241 * Add an XML schema Element declaration
5242 * *WARNING* this interface is highly subject to change
5243 *
5244 * Returns the new struture or NULL in case of error
5245 */
5246static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005247xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005248 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005249 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005250{
5251 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005253 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005254 return (NULL);
5255
Daniel Veillard4255d502002-04-16 15:50:10 +00005256 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5257 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005258 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005259 return (NULL);
5260 }
5261 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005262 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5263 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005264 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005265 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005266
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005267 if (topLevel)
5268 WXS_ADD_GLOBAL(ctxt, ret);
5269 else
5270 WXS_ADD_LOCAL(ctxt, ret);
5271 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005272 return (ret);
5273}
5274
5275/**
5276 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005277 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005278 * @schema: the schema being built
5279 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005280 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005281 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005282 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005283 * *WARNING* this interface is highly subject to change
5284 *
5285 * Returns the new struture or NULL in case of error
5286 */
5287static xmlSchemaTypePtr
5288xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005289 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005290 const xmlChar * name, const xmlChar * nsName,
5291 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005292{
5293 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005294
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005295 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 return (NULL);
5297
5298 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5299 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005300 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005301 return (NULL);
5302 }
5303 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005304 ret->type = type;
5305 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005306 ret->targetNamespace = nsName;
5307 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005308 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005309 if (ctxt->isRedefine) {
5310 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5311 ret, name, nsName);
5312 if (ctxt->redef == NULL) {
5313 xmlFree(ret);
5314 return(NULL);
5315 }
5316 ctxt->redefCounter = 0;
5317 }
5318 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005319 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005320 WXS_ADD_LOCAL(ctxt, ret);
5321 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005322 return (ret);
5323}
5324
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005325static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005326xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005327 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005328 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005329 const xmlChar *refNs)
5330{
5331 xmlSchemaQNameRefPtr ret;
5332
5333 ret = (xmlSchemaQNameRefPtr)
5334 xmlMalloc(sizeof(xmlSchemaQNameRef));
5335 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005336 xmlSchemaPErrMemory(pctxt,
5337 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005338 return (NULL);
5339 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005340 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005341 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5342 ret->name = refName;
5343 ret->targetNamespace = refNs;
5344 ret->item = NULL;
5345 ret->itemType = refType;
5346 /*
5347 * Store the reference item in the schema.
5348 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005349 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005350 return (ret);
5351}
5352
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005353static xmlSchemaAttributeUseProhibPtr
5354xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5355{
5356 xmlSchemaAttributeUseProhibPtr ret;
5357
5358 ret = (xmlSchemaAttributeUseProhibPtr)
5359 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5360 if (ret == NULL) {
5361 xmlSchemaPErrMemory(pctxt,
5362 "allocating attribute use prohibition", NULL);
5363 return (NULL);
5364 }
5365 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5366 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5367 WXS_ADD_LOCAL(pctxt, ret);
5368 return (ret);
5369}
5370
5371
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005372/**
5373 * xmlSchemaAddModelGroup:
5374 * @ctxt: a schema parser context
5375 * @schema: the schema being built
5376 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005377 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005378 *
5379 * Adds a schema model group
5380 * *WARNING* this interface is highly subject to change
5381 *
5382 * Returns the new struture or NULL in case of error
5383 */
5384static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005385xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5386 xmlSchemaPtr schema,
5387 xmlSchemaTypeType type,
5388 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005389{
5390 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005391
5392 if ((ctxt == NULL) || (schema == NULL))
5393 return (NULL);
5394
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005395 ret = (xmlSchemaModelGroupPtr)
5396 xmlMalloc(sizeof(xmlSchemaModelGroup));
5397 if (ret == NULL) {
5398 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5399 NULL);
5400 return (NULL);
5401 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005402 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005403 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005404 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005405 WXS_ADD_LOCAL(ctxt, ret);
5406 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5407 (type == XML_SCHEMA_TYPE_CHOICE))
5408 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005409 return (ret);
5410}
5411
5412
5413/**
5414 * xmlSchemaAddParticle:
5415 * @ctxt: a schema parser context
5416 * @schema: the schema being built
5417 * @node: the corresponding node in the schema doc
5418 * @min: the minOccurs
5419 * @max: the maxOccurs
5420 *
5421 * Adds an XML schema particle component.
5422 * *WARNING* this interface is highly subject to change
5423 *
5424 * Returns the new struture or NULL in case of error
5425 */
5426static xmlSchemaParticlePtr
5427xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5428 xmlNodePtr node, int min, int max)
5429{
5430 xmlSchemaParticlePtr ret = NULL;
5431 if ((ctxt == NULL) || (schema == NULL))
5432 return (NULL);
5433
5434#ifdef DEBUG
5435 fprintf(stderr, "Adding particle component\n");
5436#endif
5437 ret = (xmlSchemaParticlePtr)
5438 xmlMalloc(sizeof(xmlSchemaParticle));
5439 if (ret == NULL) {
5440 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5441 NULL);
5442 return (NULL);
5443 }
5444 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5445 ret->annot = NULL;
5446 ret->node = node;
5447 ret->minOccurs = min;
5448 ret->maxOccurs = max;
5449 ret->next = NULL;
5450 ret->children = NULL;
5451
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005452 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005453 /*
5454 * Note that addition to pending components will be done locally
5455 * to the specific parsing function, since the most particles
5456 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005457 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005458 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005459 return (ret);
5460}
5461
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005462/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005463 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005464 * @ctxt: a schema validation context
5465 * @schema: the schema being built
5466 * @name: the group name
5467 *
5468 * Add an XML schema Group definition
5469 *
5470 * Returns the new struture or NULL in case of error
5471 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005472static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005473xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5474 xmlSchemaPtr schema,
5475 const xmlChar *name,
5476 const xmlChar *nsName,
5477 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005478{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005479 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005480
5481 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5482 return (NULL);
5483
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005484 ret = (xmlSchemaModelGroupDefPtr)
5485 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005486 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005487 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005488 return (NULL);
5489 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005490 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005491 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005492 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005493 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005494 ret->targetNamespace = nsName;
5495
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005496 if (ctxt->isRedefine) {
5497 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5498 ret, name, nsName);
5499 if (ctxt->redef == NULL) {
5500 xmlFree(ret);
5501 return(NULL);
5502 }
5503 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005504 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005505 WXS_ADD_GLOBAL(ctxt, ret);
5506 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005507 return (ret);
5508}
5509
Daniel Veillard3646d642004-06-02 19:19:14 +00005510/**
5511 * xmlSchemaNewWildcardNs:
5512 * @ctxt: a schema validation context
5513 *
5514 * Creates a new wildcard namespace constraint.
5515 *
5516 * Returns the new struture or NULL in case of error
5517 */
5518static xmlSchemaWildcardNsPtr
5519xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5520{
5521 xmlSchemaWildcardNsPtr ret;
5522
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005523 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005524 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5525 if (ret == NULL) {
5526 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005527 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005528 }
5529 ret->value = NULL;
5530 ret->next = NULL;
5531 return (ret);
5532}
5533
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005534static xmlSchemaIDCPtr
5535xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5536 const xmlChar *name, const xmlChar *nsName,
5537 int category, xmlNodePtr node)
5538{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005539 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005540
5541 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5542 return (NULL);
5543
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005544 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5545 if (ret == NULL) {
5546 xmlSchemaPErrMemory(ctxt,
5547 "allocating an identity-constraint definition", NULL);
5548 return (NULL);
5549 }
5550 memset(ret, 0, sizeof(xmlSchemaIDC));
5551 /* The target namespace of the parent element declaration. */
5552 ret->targetNamespace = nsName;
5553 ret->name = name;
5554 ret->type = category;
5555 ret->node = node;
5556
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005557 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005558 /*
5559 * Only keyrefs need to be fixup up.
5560 */
5561 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005562 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005563 return (ret);
5564}
5565
Daniel Veillard3646d642004-06-02 19:19:14 +00005566/**
5567 * xmlSchemaAddWildcard:
5568 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005569 * @schema: a schema
5570 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005571 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005572 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005573 *
5574 * Returns the new struture or NULL in case of error
5575 */
5576static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005577xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5578 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005579{
5580 xmlSchemaWildcardPtr ret = NULL;
5581
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005582 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005583 return (NULL);
5584
5585 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5586 if (ret == NULL) {
5587 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5588 return (NULL);
5589 }
5590 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005591 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005592 ret->node = node;
5593 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005594 return (ret);
5595}
5596
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005597static void
5598xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5599{
5600 if (group == NULL)
5601 return;
5602 if (group->members != NULL)
5603 xmlSchemaItemListFree(group->members);
5604 xmlFree(group);
5605}
5606
5607static xmlSchemaSubstGroupPtr
5608xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5609 xmlSchemaElementPtr head)
5610{
5611 xmlSchemaSubstGroupPtr ret;
5612
5613 /* Init subst group hash. */
5614 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5615 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5616 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5617 return(NULL);
5618 }
5619 /* Create a new substitution group. */
5620 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5621 if (ret == NULL) {
5622 xmlSchemaPErrMemory(NULL,
5623 "allocating a substitution group container", NULL);
5624 return(NULL);
5625 }
5626 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5627 ret->head = head;
5628 /* Create list of members. */
5629 ret->members = xmlSchemaItemListCreate();
5630 if (ret->members == NULL) {
5631 xmlSchemaSubstGroupFree(ret);
5632 return(NULL);
5633 }
5634 /* Add subst group to hash. */
5635 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5636 head->name, head->targetNamespace, ret) != 0) {
5637 PERROR_INT("xmlSchemaSubstGroupAdd",
5638 "failed to add a new substitution container");
5639 xmlSchemaSubstGroupFree(ret);
5640 return(NULL);
5641 }
5642 return(ret);
5643}
5644
5645static xmlSchemaSubstGroupPtr
5646xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5647 xmlSchemaElementPtr head)
5648{
5649 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5650 return(NULL);
5651 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5652 head->name, head->targetNamespace));
5653
5654}
5655
5656/**
5657 * xmlSchemaAddElementSubstitutionMember:
5658 * @pctxt: a schema parser context
5659 * @head: the head of the substitution group
5660 * @member: the new member of the substitution group
5661 *
5662 * Allocate a new annotation structure.
5663 *
5664 * Returns the newly allocated structure or NULL in case or error
5665 */
5666static int
5667xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5668 xmlSchemaElementPtr head,
5669 xmlSchemaElementPtr member)
5670{
5671 xmlSchemaSubstGroupPtr substGroup = NULL;
5672
5673 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5674 return (-1);
5675
5676 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5677 if (substGroup == NULL)
5678 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5679 if (substGroup == NULL)
5680 return(-1);
5681 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5682 return(-1);
5683 return(0);
5684}
5685
Daniel Veillard4255d502002-04-16 15:50:10 +00005686/************************************************************************
5687 * *
5688 * Utilities for parsing *
5689 * *
5690 ************************************************************************/
5691
Daniel Veillard4255d502002-04-16 15:50:10 +00005692/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005693 * xmlSchemaPValAttrNodeQNameValue:
5694 * @ctxt: a schema parser context
5695 * @schema: the schema context
5696 * @ownerDes: the designation of the parent element
5697 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005698 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005699 * @local: the resulting local part if found, the attribute value otherwise
5700 * @uri: the resulting namespace URI if found
5701 *
5702 * Extracts the local name and the URI of a QName value and validates it.
5703 * This one is intended to be used on attribute values that
5704 * should resolve to schema components.
5705 *
5706 * Returns 0, in case the QName is valid, a positive error code
5707 * if not valid and -1 if an internal error occurs.
5708 */
5709static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005710xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005711 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005712 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005713 xmlAttrPtr attr,
5714 const xmlChar *value,
5715 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005716 const xmlChar **local)
5717{
5718 const xmlChar *pref;
5719 xmlNsPtr ns;
5720 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005721
Daniel Veillardc0826a72004-08-10 14:17:33 +00005722 *uri = NULL;
5723 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005724 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005725 if (ret > 0) {
5726 xmlSchemaPSimpleTypeErr(ctxt,
5727 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5728 ownerItem, (xmlNodePtr) attr,
5729 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5730 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005731 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005732 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005733 } else if (ret < 0)
5734 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005735
5736 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005737 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005738 if (ns)
5739 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5740 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005741 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5742 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005743 /*
5744 * This one takes care of included schemas with no
5745 * target namespace.
5746 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005747 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005748 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005749 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005750 return (0);
5751 }
5752 /*
5753 * At this point xmlSplitQName3 has to return a local name.
5754 */
5755 *local = xmlSplitQName3(value, &len);
5756 *local = xmlDictLookup(ctxt->dict, *local, -1);
5757 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005758 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5759 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005760 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005761 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005762 ownerItem, (xmlNodePtr) attr,
5763 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5764 "The value '%s' of simple type 'xs:QName' has no "
5765 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005766 return (ctxt->err);
5767 } else {
5768 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005769 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005770 return (0);
5771}
5772
5773/**
5774 * xmlSchemaPValAttrNodeQName:
5775 * @ctxt: a schema parser context
5776 * @schema: the schema context
5777 * @ownerDes: the designation of the owner element
5778 * @ownerItem: the owner as a schema object
5779 * @attr: the attribute node
5780 * @local: the resulting local part if found, the attribute value otherwise
5781 * @uri: the resulting namespace URI if found
5782 *
5783 * Extracts and validates the QName of an attribute value.
5784 * This one is intended to be used on attribute values that
5785 * should resolve to schema components.
5786 *
5787 * Returns 0, in case the QName is valid, a positive error code
5788 * if not valid and -1 if an internal error occurs.
5789 */
5790static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005791xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005792 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005793 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005794 xmlAttrPtr attr,
5795 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005796 const xmlChar **local)
5797{
5798 const xmlChar *value;
5799
5800 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005801 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005802 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005803}
5804
5805/**
5806 * xmlSchemaPValAttrQName:
5807 * @ctxt: a schema parser context
5808 * @schema: the schema context
5809 * @ownerDes: the designation of the parent element
5810 * @ownerItem: the owner as a schema object
5811 * @ownerElem: the parent node of the attribute
5812 * @name: the name of the attribute
5813 * @local: the resulting local part if found, the attribute value otherwise
5814 * @uri: the resulting namespace URI if found
5815 *
5816 * Extracts and validates the QName of an attribute value.
5817 *
5818 * Returns 0, in case the QName is valid, a positive error code
5819 * if not valid and -1 if an internal error occurs.
5820 */
5821static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005822xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5823 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005824 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005825 xmlNodePtr ownerElem,
5826 const char *name,
5827 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005828 const xmlChar **local)
5829{
5830 xmlAttrPtr attr;
5831
5832 attr = xmlSchemaGetPropNode(ownerElem, name);
5833 if (attr == NULL) {
5834 *local = NULL;
5835 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005836 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005837 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005838 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005839 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005840}
5841
5842/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005843 * xmlSchemaPValAttrID:
5844 * @ctxt: a schema parser context
5845 * @schema: the schema context
5846 * @ownerDes: the designation of the parent element
5847 * @ownerItem: the owner as a schema object
5848 * @ownerElem: the parent node of the attribute
5849 * @name: the name of the attribute
5850 *
5851 * Extracts and validates the ID of an attribute value.
5852 *
5853 * Returns 0, in case the ID is valid, a positive error code
5854 * if not valid and -1 if an internal error occurs.
5855 */
5856static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005857xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005858{
5859 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005860 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005861
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005862 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005863 return(0);
5864 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5865 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005866 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005867 /*
5868 * NOTE: the IDness might have already be declared in the DTD
5869 */
5870 if (attr->atype != XML_ATTRIBUTE_ID) {
5871 xmlIDPtr res;
5872 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005873
5874 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005875 * TODO: Use xmlSchemaStrip here; it's not exported at this
5876 * moment.
5877 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005878 strip = xmlSchemaCollapseString(value);
5879 if (strip != NULL) {
5880 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005881 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005882 }
5883 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005884 if (res == NULL) {
5885 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005886 xmlSchemaPSimpleTypeErr(ctxt,
5887 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005888 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005889 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5890 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005891 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005892 } else
5893 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005894 }
5895 } else if (ret > 0) {
5896 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005897 xmlSchemaPSimpleTypeErr(ctxt,
5898 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005899 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005900 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5901 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5902 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005903 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005904 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005905 if (value != NULL)
5906 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005907
5908 return (ret);
5909}
5910
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005911static int
5912xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5913 xmlNodePtr ownerElem,
5914 const xmlChar *name)
5915{
5916 xmlAttrPtr attr;
5917
5918 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5919 if (attr == NULL)
5920 return(0);
5921 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5922
5923}
5924
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005925/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005926 * xmlGetMaxOccurs:
5927 * @ctxt: a schema validation context
5928 * @node: a subtree containing XML Schema informations
5929 *
5930 * Get the maxOccurs property
5931 *
5932 * Returns the default if not found, or the value
5933 */
5934static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005935xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5936 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005937{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005938 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005939 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005940 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005941
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005942 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5943 if (attr == NULL)
5944 return (def);
5945 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005946
5947 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005948 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005949 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005950 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5951 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005952 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005953 val, NULL, NULL, NULL);
5954 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005955 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005956 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005957 }
5958
5959 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005960 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005961 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005962 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005963 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005964 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5965 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005966 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005967 val, NULL, NULL, NULL);
5968 return (def);
5969 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005970 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005971 ret = ret * 10 + (*cur - '0');
5972 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005973 }
William M. Brack76e95df2003-10-18 16:20:14 +00005974 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005975 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005976 /*
5977 * TODO: Restrict the maximal value to Integer.
5978 */
5979 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005980 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005981 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5982 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005983 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005984 val, NULL, NULL, NULL);
5985 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005986 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005987 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005988}
5989
5990/**
5991 * xmlGetMinOccurs:
5992 * @ctxt: a schema validation context
5993 * @node: a subtree containing XML Schema informations
5994 *
5995 * Get the minOccurs property
5996 *
5997 * Returns the default if not found, or the value
5998 */
5999static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006000xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006001 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006002{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006003 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006004 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006005 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006006
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006007 attr = xmlSchemaGetPropNode(node, "minOccurs");
6008 if (attr == NULL)
6009 return (def);
6010 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006011 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006012 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006013 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006014 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006015 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006016 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6017 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006018 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006019 val, NULL, NULL, NULL);
6020 return (def);
6021 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006022 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006023 ret = ret * 10 + (*cur - '0');
6024 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006025 }
William M. Brack76e95df2003-10-18 16:20:14 +00006026 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006027 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006028 /*
6029 * TODO: Restrict the maximal value to Integer.
6030 */
6031 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006032 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006033 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6034 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006035 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006036 val, NULL, NULL, NULL);
6037 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006038 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006039 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006040}
6041
6042/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006043 * xmlSchemaPGetBoolNodeValue:
6044 * @ctxt: a schema validation context
6045 * @ownerDes: owner designation
6046 * @ownerItem: the owner as a schema item
6047 * @node: the node holding the value
6048 *
6049 * Converts a boolean string value into 1 or 0.
6050 *
6051 * Returns 0 or 1.
6052 */
6053static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006054xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006055 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006056 xmlNodePtr node)
6057{
6058 xmlChar *value = NULL;
6059 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006060
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006061 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006062 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006063 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006064 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006065 * can have the following legal literals {true, false, 1, 0}.
6066 */
6067 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6068 res = 1;
6069 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6070 res = 0;
6071 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6072 res = 1;
6073 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006074 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006075 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006076 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006077 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006078 ownerItem, node,
6079 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6080 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006081 NULL, NULL, NULL);
6082 }
6083 if (value != NULL)
6084 xmlFree(value);
6085 return (res);
6086}
6087
6088/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006089 * xmlGetBooleanProp:
6090 * @ctxt: a schema validation context
6091 * @node: a subtree containing XML Schema informations
6092 * @name: the attribute name
6093 * @def: the default value
6094 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006095 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006096 *
6097 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006098 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006099 */
6100static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006101xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006102 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006103 const char *name, int def)
6104{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006105 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006106
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006107 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006108 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006109 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006110 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006111 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006112 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006113 * can have the following legal literals {true, false, 1, 0}.
6114 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006115 if (xmlStrEqual(val, BAD_CAST "true"))
6116 def = 1;
6117 else if (xmlStrEqual(val, BAD_CAST "false"))
6118 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006119 else if (xmlStrEqual(val, BAD_CAST "1"))
6120 def = 1;
6121 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006122 def = 0;
6123 else {
6124 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006125 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006126 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006127 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006128 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6129 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006130 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006131 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006132}
6133
6134/************************************************************************
6135 * *
6136 * Shema extraction from an Infoset *
6137 * *
6138 ************************************************************************/
6139static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6140 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006141 xmlNodePtr node,
6142 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006143static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6144 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006145 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006146 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006147 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006148static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6149 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006150 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006151 xmlNodePtr node,
6152 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006153static xmlSchemaBasicItemPtr
6154xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6155 xmlSchemaPtr schema,
6156 xmlNodePtr node,
6157 xmlSchemaItemListPtr uses,
6158 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006159static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6160 xmlSchemaPtr schema,
6161 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006162static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006163xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6164 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006165
6166/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006167 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006168 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006169 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006170 * @ownerDes: the designation of the parent element
6171 * @ownerItem: the schema object owner if existent
6172 * @attr: the schema attribute node being validated
6173 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006174 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006175 *
6176 * Validates a value against the given built-in type.
6177 * This one is intended to be used internally for validation
6178 * of schema attribute values during parsing of the schema.
6179 *
6180 * Returns 0 if the value is valid, a positive error code
6181 * number otherwise and -1 in case of an internal or API error.
6182 */
6183static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006184xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006185 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006186 xmlAttrPtr attr,
6187 const xmlChar *value,
6188 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006189{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006190
6191 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006192
6193 /*
6194 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6195 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006196 */
6197 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006198 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006199 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6200 PERROR_INT("xmlSchemaPValAttrNodeValue",
6201 "the given type is not a built-in type");
6202 return (-1);
6203 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006204 switch (type->builtInType) {
6205 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006206 case XML_SCHEMAS_QNAME:
6207 case XML_SCHEMAS_ANYURI:
6208 case XML_SCHEMAS_TOKEN:
6209 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006210 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6211 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006212 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006213 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006214 PERROR_INT("xmlSchemaPValAttrNodeValue",
6215 "validation using the given type is not supported");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006216 return (-1);
6217 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006218 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006219 /*
6220 * TODO: Should we use the S4S error codes instead?
6221 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006222 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006223 PERROR_INT("xmlSchemaPValAttrNodeValue",
6224 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006225 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006226 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006227 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006228 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6229 else
6230 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6231 xmlSchemaPSimpleTypeErr(pctxt,
6232 ret, ownerItem, (xmlNodePtr) attr,
6233 type, NULL, value, NULL, NULL, NULL);
6234 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006235 return (ret);
6236}
6237
6238/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006239 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006240 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006241 * @ctxt: a schema parser context
6242 * @ownerDes: the designation of the parent element
6243 * @ownerItem: the schema object owner if existent
6244 * @attr: the schema attribute node being validated
6245 * @type: the built-in type to be validated against
6246 * @value: the resulting value if any
6247 *
6248 * Extracts and validates a value against the given built-in type.
6249 * This one is intended to be used internally for validation
6250 * of schema attribute values during parsing of the schema.
6251 *
6252 * Returns 0 if the value is valid, a positive error code
6253 * number otherwise and -1 in case of an internal or API error.
6254 */
6255static int
6256xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006257 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006258 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006259 xmlSchemaTypePtr type,
6260 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006261{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006262 const xmlChar *val;
6263
6264 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006265 return (-1);
6266
Daniel Veillardc0826a72004-08-10 14:17:33 +00006267 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6268 if (value != NULL)
6269 *value = val;
6270
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006271 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006272 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006273}
6274
6275/**
6276 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006277 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006278 * @ctxt: a schema parser context
6279 * @node: the element node of the attribute
6280 * @ownerDes: the designation of the parent element
6281 * @ownerItem: the schema object owner if existent
6282 * @ownerElem: the owner element node
6283 * @name: the name of the schema attribute node
6284 * @type: the built-in type to be validated against
6285 * @value: the resulting value if any
6286 *
6287 * Extracts and validates a value against the given built-in type.
6288 * This one is intended to be used internally for validation
6289 * of schema attribute values during parsing of the schema.
6290 *
6291 * Returns 0 if the value is valid, a positive error code
6292 * number otherwise and -1 in case of an internal or API error.
6293 */
6294static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006295xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6296 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006297 xmlNodePtr ownerElem,
6298 const char *name,
6299 xmlSchemaTypePtr type,
6300 const xmlChar **value)
6301{
6302 xmlAttrPtr attr;
6303
6304 if ((ctxt == NULL) || (type == NULL)) {
6305 if (value != NULL)
6306 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006307 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006308 }
6309 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6310 if (value != NULL)
6311 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006312 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006313 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006314 "Internal error: xmlSchemaPValAttr, the given "
6315 "type '%s' is not a built-in type.\n",
6316 type->name, NULL);
6317 return (-1);
6318 }
6319 attr = xmlSchemaGetPropNode(ownerElem, name);
6320 if (attr == NULL) {
6321 if (value != NULL)
6322 *value = NULL;
6323 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006324 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006325 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006326 type, value));
6327}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006328
6329static int
6330xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006331 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006332 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006333 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006334 const xmlChar *namespaceName)
6335{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006336 /* TODO: Pointer comparison instead? */
6337 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006338 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006339 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006340 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006341 /*
6342 * Check if the referenced namespace was <import>ed.
6343 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006344 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006345 xmlSchemaSchemaRelationPtr rel;
6346
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006347 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006348 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006349 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006350 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006351 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006352 rel = rel->next;
6353 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006354 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006355 /*
6356 * No matching <import>ed namespace found.
6357 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006358 {
6359 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6360
6361 if (namespaceName == NULL)
6362 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6363 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6364 "References from this schema to components in no "
6365 "namespace are not allowed, since not indicated by an "
6366 "import statement", NULL, NULL);
6367 else
6368 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6369 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6370 "References from this schema to components in the "
6371 "namespace '%s' are not allowed, since not indicated by an "
6372 "import statement", namespaceName, NULL);
6373 }
6374 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006375}
6376
Daniel Veillardc0826a72004-08-10 14:17:33 +00006377/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006378 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006379 * @ctxt: a schema validation context
6380 * @schema: the schema being built
6381 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006382 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006383 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006384 * Parses attribute uses and attribute declarations and
6385 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006386 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006387static int
6388xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6389 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6390 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006391{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006392 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006393
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006394 while ((IS_SCHEMA((*child), "attribute")) ||
6395 (IS_SCHEMA((*child), "attributeGroup"))) {
6396 if (IS_SCHEMA((*child), "attribute")) {
6397 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6398 *list, parentType);
6399 } else {
6400 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6401 if ((item != NULL) && (hasRefs != NULL))
6402 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006403 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006404 if (item != NULL) {
6405 if (*list == NULL) {
6406 /* TODO: Customize grow factor. */
6407 *list = xmlSchemaItemListCreate();
6408 if (*list == NULL)
6409 return(-1);
6410 }
6411 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6412 return(-1);
6413 }
6414 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006415 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006416 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006417}
6418
6419/**
6420 * xmlSchemaParseAnnotation:
6421 * @ctxt: a schema validation context
6422 * @schema: the schema being built
6423 * @node: a subtree containing XML Schema informations
6424 *
6425 * parse a XML schema Attrribute declaration
6426 * *WARNING* this interface is highly subject to change
6427 *
William M. Bracke7091952004-05-11 15:09:58 +00006428 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006429 * 1 in case of success.
6430 */
6431static xmlSchemaAnnotPtr
6432xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006433 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006434{
6435 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006436 xmlNodePtr child = NULL;
6437 xmlAttrPtr attr;
6438 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006439
Daniel Veillardc0826a72004-08-10 14:17:33 +00006440 /*
6441 * INFO: S4S completed.
6442 */
6443 /*
6444 * id = ID
6445 * {any attributes with non-schema namespace . . .}>
6446 * Content: (appinfo | documentation)*
6447 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006448 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6449 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006450 if (needed)
6451 ret = xmlSchemaNewAnnot(ctxt, node);
6452 else
6453 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006454 attr = node->properties;
6455 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006456 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006457 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006458 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006459 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006460
6461 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006462 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006463 }
6464 attr = attr->next;
6465 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006466 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006467 /*
6468 * And now for the children...
6469 */
6470 child = node->children;
6471 while (child != NULL) {
6472 if (IS_SCHEMA(child, "appinfo")) {
6473 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006474 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006475 * source = anyURI
6476 * {any attributes with non-schema namespace . . .}>
6477 * Content: ({any})*
6478 */
6479 attr = child->properties;
6480 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006481 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006482 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006483 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006484 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006485
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006486 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006487 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006488 }
6489 attr = attr->next;
6490 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006491 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006492 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006493 child = child->next;
6494 } else if (IS_SCHEMA(child, "documentation")) {
6495 /* TODO: make available the content of "documentation". */
6496 /*
6497 * source = anyURI
6498 * {any attributes with non-schema namespace . . .}>
6499 * Content: ({any})*
6500 */
6501 attr = child->properties;
6502 while (attr != NULL) {
6503 if (attr->ns == NULL) {
6504 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006505 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006506 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006507 }
6508 } else {
6509 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6510 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6511 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006512
6513 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006514 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006515 }
6516 }
6517 attr = attr->next;
6518 }
6519 /*
6520 * Attribute "xml:lang".
6521 */
6522 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6523 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006524 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006525 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006526 child = child->next;
6527 } else {
6528 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006529 xmlSchemaPContentErr(ctxt,
6530 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006531 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006532 barked = 1;
6533 child = child->next;
6534 }
6535 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006536
Daniel Veillard4255d502002-04-16 15:50:10 +00006537 return (ret);
6538}
6539
6540/**
6541 * xmlSchemaParseFacet:
6542 * @ctxt: a schema validation context
6543 * @schema: the schema being built
6544 * @node: a subtree containing XML Schema informations
6545 *
6546 * parse a XML schema Facet declaration
6547 * *WARNING* this interface is highly subject to change
6548 *
6549 * Returns the new type structure or NULL in case of error
6550 */
6551static xmlSchemaFacetPtr
6552xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006553 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006554{
6555 xmlSchemaFacetPtr facet;
6556 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006557 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006558
6559 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6560 return (NULL);
6561
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006562 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006563 if (facet == NULL) {
6564 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6565 return (NULL);
6566 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006567 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006568 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006569 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006570 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6571 "Facet %s has no value\n", node->name, NULL);
6572 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006573 return (NULL);
6574 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006575 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006576 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006577 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006578 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006579 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006580 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006581 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006582 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006583 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006584 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006585 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006586 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006587 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006588 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006589 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006590 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006591 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006592 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006593 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006594 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006595 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006596 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6597 } else if (IS_SCHEMA(node, "minLength")) {
6598 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6599 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006600 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6601 "Unknown facet type %s\n", node->name, NULL);
6602 xmlSchemaFreeFacet(facet);
6603 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006604 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006605 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006606 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006607 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6608 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6609 const xmlChar *fixed;
6610
6611 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6612 if (fixed != NULL) {
6613 if (xmlStrEqual(fixed, BAD_CAST "true"))
6614 facet->fixed = 1;
6615 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006616 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006617 child = node->children;
6618
6619 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006620 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006621 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006622 }
6623 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006624 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6625 "Facet %s has unexpected child content\n",
6626 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006627 }
6628 return (facet);
6629}
6630
6631/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006632 * xmlSchemaParseWildcardNs:
6633 * @ctxt: a schema parser context
6634 * @wildc: the wildcard, already created
6635 * @node: a subtree containing XML Schema informations
6636 *
6637 * Parses the attribute "processContents" and "namespace"
6638 * of a xsd:anyAttribute and xsd:any.
6639 * *WARNING* this interface is highly subject to change
6640 *
6641 * Returns 0 if everything goes fine, a positive error code
6642 * if something is not valid and -1 if an internal error occurs.
6643 */
6644static int
6645xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006646 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006647 xmlSchemaWildcardPtr wildc,
6648 xmlNodePtr node)
6649{
6650 const xmlChar *pc, *ns, *dictnsItem;
6651 int ret = 0;
6652 xmlChar *nsItem;
6653 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6654 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006655
Daniel Veillardc0826a72004-08-10 14:17:33 +00006656 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6657 if ((pc == NULL)
6658 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6659 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6660 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6661 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6662 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6663 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6664 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006665 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006666 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006667 NULL, node,
6668 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006669 NULL, NULL, NULL);
6670 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006671 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006672 }
6673 /*
6674 * Build the namespace constraints.
6675 */
6676 attr = xmlSchemaGetPropNode(node, "namespace");
6677 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006678 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006679 wildc->any = 1;
6680 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6681 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006682 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006683 return (-1);
6684 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006685 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006686 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006687 const xmlChar *end, *cur;
6688
6689 cur = ns;
6690 do {
6691 while (IS_BLANK_CH(*cur))
6692 cur++;
6693 end = cur;
6694 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6695 end++;
6696 if (end == cur)
6697 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006698 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006699 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6700 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006701 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006702 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006703 NULL, (xmlNodePtr) attr,
6704 NULL,
6705 "((##any | ##other) | List of (xs:anyURI | "
6706 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006707 nsItem, NULL, NULL, NULL);
6708 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6709 } else {
6710 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006711 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006712 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6713 dictnsItem = NULL;
6714 } else {
6715 /*
6716 * Validate the item (anyURI).
6717 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006718 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006719 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6720 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6721 }
6722 /*
6723 * Avoid dublicate namespaces.
6724 */
6725 tmp = wildc->nsSet;
6726 while (tmp != NULL) {
6727 if (dictnsItem == tmp->value)
6728 break;
6729 tmp = tmp->next;
6730 }
6731 if (tmp == NULL) {
6732 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6733 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006734 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006735 return (-1);
6736 }
6737 tmp->value = dictnsItem;
6738 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006739 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006740 wildc->nsSet = tmp;
6741 else
6742 lastNs->next = tmp;
6743 lastNs = tmp;
6744 }
6745
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006746 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006747 xmlFree(nsItem);
6748 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006749 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006750 }
6751 return (ret);
6752}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006753
6754static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006755xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6756 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006757 xmlNodePtr node,
6758 int minOccurs,
6759 int maxOccurs) {
6760
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006761 if ((maxOccurs == 0) && ( minOccurs == 0))
6762 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006763 if (maxOccurs != UNBOUNDED) {
6764 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006765 * TODO: Maybe we should better not create the particle,
6766 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006767 * content model.
6768 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006769 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006770 * 3.9.6 Schema Component Constraint: Particle Correct
6771 *
6772 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006773 if (maxOccurs < 1) {
6774 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006775 * 2.2 {max occurs} must be greater than or equal to 1.
6776 */
6777 xmlSchemaPCustomAttrErr(ctxt,
6778 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006779 NULL, NULL,
6780 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006781 "The value must be greater than or equal to 1");
6782 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6783 } else if (minOccurs > maxOccurs) {
6784 /*
6785 * 2.1 {min occurs} must not be greater than {max occurs}.
6786 */
6787 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006788 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006789 NULL, NULL,
6790 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006791 "The value must not be greater than the value of 'maxOccurs'");
6792 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6793 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006794 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006795 return (0);
6796}
6797
Daniel Veillardc0826a72004-08-10 14:17:33 +00006798/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006799 * xmlSchemaParseAny:
6800 * @ctxt: a schema validation context
6801 * @schema: the schema being built
6802 * @node: a subtree containing XML Schema informations
6803 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006804 * Parsea a XML schema <any> element. A particle and wildcard
6805 * will be created (except if minOccurs==maxOccurs==0, in this case
6806 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006807 * *WARNING* this interface is highly subject to change
6808 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006809 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006810 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006811static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006812xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6813 xmlNodePtr node)
6814{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006815 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006816 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006817 xmlSchemaWildcardPtr wild;
6818 int min, max;
6819 xmlAttrPtr attr;
6820 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006821
6822 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6823 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006824 /*
6825 * Check for illegal attributes.
6826 */
6827 attr = node->properties;
6828 while (attr != NULL) {
6829 if (attr->ns == NULL) {
6830 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6831 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6832 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6833 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6834 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006835 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006836 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006837 }
6838 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006839 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006840 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006841 }
6842 attr = attr->next;
6843 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006844 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006845 /*
6846 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006847 */
6848 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6849 "(xs:nonNegativeInteger | unbounded)");
6850 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6851 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006852 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6853 /*
6854 * Create & parse the wildcard.
6855 */
6856 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6857 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006858 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006859 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006860 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006861 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006862 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006863 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006864 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006865 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006866 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006867 }
6868 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006869 xmlSchemaPContentErr(ctxt,
6870 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006871 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006872 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006873 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006874 /*
6875 * No component if minOccurs==maxOccurs==0.
6876 */
6877 if ((min == 0) && (max == 0)) {
6878 /* Don't free the wildcard, since it's already on the list. */
6879 return (NULL);
6880 }
6881 /*
6882 * Create the particle.
6883 */
6884 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6885 if (particle == NULL)
6886 return (NULL);
6887 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006888 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006889
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006890 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006891}
6892
6893/**
6894 * xmlSchemaParseNotation:
6895 * @ctxt: a schema validation context
6896 * @schema: the schema being built
6897 * @node: a subtree containing XML Schema informations
6898 *
6899 * parse a XML schema Notation declaration
6900 *
6901 * Returns the new structure or NULL in case of error
6902 */
6903static xmlSchemaNotationPtr
6904xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006905 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006906{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006907 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006908 xmlSchemaNotationPtr ret;
6909 xmlNodePtr child = NULL;
6910
6911 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6912 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006913 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006914 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006915 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6916 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006917 return (NULL);
6918 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006919 ret = xmlSchemaAddNotation(ctxt, schema, name,
6920 ctxt->targetNamespace, node);
6921 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006922 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006923 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006924
6925 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006926 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006927 child = child->next;
6928 }
6929
Daniel Veillard4255d502002-04-16 15:50:10 +00006930 child = node->children;
6931 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006932 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006933 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006934 }
6935 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006936 xmlSchemaPContentErr(ctxt,
6937 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006938 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006939 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006940 }
6941
6942 return (ret);
6943}
6944
6945/**
6946 * xmlSchemaParseAnyAttribute:
6947 * @ctxt: a schema validation context
6948 * @schema: the schema being built
6949 * @node: a subtree containing XML Schema informations
6950 *
6951 * parse a XML schema AnyAttrribute declaration
6952 * *WARNING* this interface is highly subject to change
6953 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006954 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006955 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006956static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006957xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6958 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006959{
Daniel Veillard3646d642004-06-02 19:19:14 +00006960 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006961 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006962 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006963
6964 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6965 return (NULL);
6966
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006967 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6968 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006969 if (ret == NULL) {
6970 return (NULL);
6971 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006972 /*
6973 * Check for illegal attributes.
6974 */
6975 attr = node->properties;
6976 while (attr != NULL) {
6977 if (attr->ns == NULL) {
6978 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6979 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6980 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006981 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006982 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006983 }
6984 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006985 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006986 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006987 }
6988 attr = attr->next;
6989 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006990 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006991 /*
6992 * Parse the namespace list.
6993 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006994 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006995 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006996 /*
6997 * And now for the children...
6998 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006999 child = node->children;
7000 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007001 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007002 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007003 }
7004 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007005 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007006 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007007 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007008 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007009 }
7010
7011 return (ret);
7012}
7013
7014
7015/**
7016 * xmlSchemaParseAttribute:
7017 * @ctxt: a schema validation context
7018 * @schema: the schema being built
7019 * @node: a subtree containing XML Schema informations
7020 *
7021 * parse a XML schema Attrribute declaration
7022 * *WARNING* this interface is highly subject to change
7023 *
William M. Bracke7091952004-05-11 15:09:58 +00007024 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007025 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007026static xmlSchemaBasicItemPtr
7027xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7028 xmlSchemaPtr schema,
7029 xmlNodePtr node,
7030 xmlSchemaItemListPtr uses,
7031 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007032{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007033 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7034 xmlSchemaAttributeUsePtr use = NULL;
7035 xmlNodePtr child = NULL;
7036 xmlAttrPtr attr;
7037 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7038 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7039 int nberrors, hasForm = 0, defValueType = 0;
7040
7041#define WXS_ATTR_DEF_VAL_DEFAULT 1
7042#define WXS_ATTR_DEF_VAL_FIXED 2
7043
7044 /*
7045 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7046 */
7047
7048 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7049 return (NULL);
7050 attr = xmlSchemaGetPropNode(node, "ref");
7051 if (attr != NULL) {
7052 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7053 NULL, attr, &tmpNs, &tmpName) != 0) {
7054 return (NULL);
7055 }
7056 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7057 return(NULL);
7058 isRef = 1;
7059 }
7060 nberrors = pctxt->nberrors;
7061 /*
7062 * Check for illegal attributes.
7063 */
7064 attr = node->properties;
7065 while (attr != NULL) {
7066 if (attr->ns == NULL) {
7067 if (isRef) {
7068 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7069 xmlSchemaPValAttrNodeID(pctxt, attr);
7070 goto attr_next;
7071 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7072 goto attr_next;
7073 }
7074 } else {
7075 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7076 goto attr_next;
7077 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7078 xmlSchemaPValAttrNodeID(pctxt, attr);
7079 goto attr_next;
7080 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7081 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7082 attr, &tmpNs, &tmpName);
7083 goto attr_next;
7084 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7085 /*
7086 * Evaluate the target namespace
7087 */
7088 hasForm = 1;
7089 attrValue = xmlSchemaGetNodeContent(pctxt,
7090 (xmlNodePtr) attr);
7091 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7092 ns = pctxt->targetNamespace;
7093 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7094 {
7095 xmlSchemaPSimpleTypeErr(pctxt,
7096 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7097 NULL, (xmlNodePtr) attr,
7098 NULL, "(qualified | unqualified)",
7099 attrValue, NULL, NULL, NULL);
7100 }
7101 goto attr_next;
7102 }
7103 }
7104 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7105
7106 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7107 /* TODO: Maybe we need to normalize the value beforehand. */
7108 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7109 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7110 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7111 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7112 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7113 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7114 else {
7115 xmlSchemaPSimpleTypeErr(pctxt,
7116 XML_SCHEMAP_INVALID_ATTR_USE,
7117 NULL, (xmlNodePtr) attr,
7118 NULL, "(optional | prohibited | required)",
7119 attrValue, NULL, NULL, NULL);
7120 }
7121 goto attr_next;
7122 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7123 /*
7124 * 3.2.3 : 1
7125 * default and fixed must not both be present.
7126 */
7127 if (defValue) {
7128 xmlSchemaPMutualExclAttrErr(pctxt,
7129 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7130 NULL, attr, "default", "fixed");
7131 } else {
7132 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7133 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7134 }
7135 goto attr_next;
7136 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7137 /*
7138 * 3.2.3 : 1
7139 * default and fixed must not both be present.
7140 */
7141 if (defValue) {
7142 xmlSchemaPMutualExclAttrErr(pctxt,
7143 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7144 NULL, attr, "default", "fixed");
7145 } else {
7146 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7147 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7148 }
7149 goto attr_next;
7150 }
7151 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7152 goto attr_next;
7153
7154 xmlSchemaPIllegalAttrErr(pctxt,
7155 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7156
7157attr_next:
7158 attr = attr->next;
7159 }
7160 /*
7161 * 3.2.3 : 2
7162 * If default and use are both present, use must have
7163 * the actual value optional.
7164 */
7165 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7166 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7167 xmlSchemaPSimpleTypeErr(pctxt,
7168 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7169 NULL, node, NULL,
7170 "(optional | prohibited | required)", NULL,
7171 "The value of the attribute 'use' must be 'optional' "
7172 "if the attribute 'default' is present",
7173 NULL, NULL);
7174 }
7175 /*
7176 * We want correct attributes.
7177 */
7178 if (nberrors != pctxt->nberrors)
7179 return(NULL);
7180 if (! isRef) {
7181 xmlSchemaAttributePtr attrDecl;
7182
7183 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7184 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7185 ns = pctxt->targetNamespace;
7186 /*
7187 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7188 * TODO: Move this to the component layer.
7189 */
7190 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7191 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7192 XML_SCHEMAP_NO_XSI,
7193 node, NULL,
7194 "The target namespace must not match '%s'",
7195 xmlSchemaInstanceNs, NULL);
7196 }
7197 attr = xmlSchemaGetPropNode(node, "name");
7198 if (attr == NULL) {
7199 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7200 NULL, node, "name", NULL);
7201 return (NULL);
7202 }
7203 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7204 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7205 return (NULL);
7206 }
7207 /*
7208 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7209 * TODO: Move this to the component layer.
7210 */
7211 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7212 xmlSchemaPSimpleTypeErr(pctxt,
7213 XML_SCHEMAP_NO_XMLNS,
7214 NULL, (xmlNodePtr) attr,
7215 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7216 "The value of the attribute must not match 'xmlns'",
7217 NULL, NULL);
7218 return (NULL);
7219 }
7220 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7221 goto check_children;
7222 /*
7223 * Create the attribute use component.
7224 */
7225 use = xmlSchemaAddAttributeUse(pctxt, node);
7226 if (use == NULL)
7227 return(NULL);
7228 use->occurs = occurs;
7229 /*
7230 * Create the attribute declaration.
7231 */
7232 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7233 if (attrDecl == NULL)
7234 return (NULL);
7235 if (tmpName != NULL) {
7236 attrDecl->typeName = tmpName;
7237 attrDecl->typeNs = tmpNs;
7238 }
7239 use->attrDecl = attrDecl;
7240 /*
7241 * Value constraint.
7242 */
7243 if (defValue != NULL) {
7244 attrDecl->defValue = defValue;
7245 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7246 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7247 }
7248 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7249 xmlSchemaQNameRefPtr ref;
7250
7251 /*
7252 * Create the attribute use component.
7253 */
7254 use = xmlSchemaAddAttributeUse(pctxt, node);
7255 if (use == NULL)
7256 return(NULL);
7257 /*
7258 * We need to resolve the reference at later stage.
7259 */
7260 WXS_ADD_PENDING(pctxt, use);
7261 use->occurs = occurs;
7262 /*
7263 * Create a QName reference to the attribute declaration.
7264 */
7265 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7266 tmpName, tmpNs);
7267 if (ref == NULL)
7268 return(NULL);
7269 /*
7270 * Assign the reference. This will be substituted for the
7271 * referenced attribute declaration when the QName is resolved.
7272 */
7273 use->attrDecl = WXS_ATTR_CAST ref;
7274 /*
7275 * Value constraint.
7276 */
7277 if (defValue != NULL)
7278 use->defValue = defValue;
7279 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7280 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7281 }
7282
7283check_children:
7284 /*
7285 * And now for the children...
7286 */
7287 child = node->children;
7288 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7289 xmlSchemaAttributeUseProhibPtr prohib;
7290
7291 if (IS_SCHEMA(child, "annotation")) {
7292 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7293 child = child->next;
7294 }
7295 if (child != NULL) {
7296 xmlSchemaPContentErr(pctxt,
7297 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7298 NULL, node, child, NULL,
7299 "(annotation?)");
7300 }
7301 /*
7302 * Check for pointlessness of attribute prohibitions.
7303 */
7304 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7305 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7306 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7307 node, NULL,
7308 "Skipping attribute use prohibition, since it is "
7309 "pointless inside an <attributeGroup>",
7310 NULL, NULL, NULL);
7311 return(NULL);
7312 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7313 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7314 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7315 node, NULL,
7316 "Skipping attribute use prohibition, since it is "
7317 "pointless when extending a type",
7318 NULL, NULL, NULL);
7319 return(NULL);
7320 }
7321 if (! isRef) {
7322 tmpName = name;
7323 tmpNs = ns;
7324 }
7325 /*
7326 * Check for duplicate attribute prohibitions.
7327 */
7328 if (uses) {
7329 int i;
7330
7331 for (i = 0; i < uses->nbItems; i++) {
7332 use = uses->items[i];
7333 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7334 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7335 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7336 {
7337 xmlChar *str = NULL;
7338
7339 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7340 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7341 node, NULL,
7342 "Skipping duplicate attribute use prohibition '%s'",
7343 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7344 NULL, NULL);
7345 FREE_AND_NULL(str)
7346 return(NULL);
7347 }
7348 }
7349 }
7350 /*
7351 * Create the attribute prohibition helper component.
7352 */
7353 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7354 if (prohib == NULL)
7355 return(NULL);
7356 prohib->node = node;
7357 prohib->name = tmpName;
7358 prohib->targetNamespace = tmpNs;
7359 if (isRef) {
7360 /*
7361 * We need at least to resolve to the attribute declaration.
7362 */
7363 WXS_ADD_PENDING(pctxt, prohib);
7364 }
7365 return(WXS_BASIC_CAST prohib);
7366 } else {
7367 if (IS_SCHEMA(child, "annotation")) {
7368 /*
7369 * TODO: Should this go into the attr decl?
7370 */
7371 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7372 child = child->next;
7373 }
7374 if (isRef) {
7375 if (child != NULL) {
7376 if (IS_SCHEMA(child, "simpleType"))
7377 /*
7378 * 3.2.3 : 3.2
7379 * If ref is present, then all of <simpleType>,
7380 * form and type must be absent.
7381 */
7382 xmlSchemaPContentErr(pctxt,
7383 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7384 NULL, node, child, NULL,
7385 "(annotation?)");
7386 else
7387 xmlSchemaPContentErr(pctxt,
7388 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7389 NULL, node, child, NULL,
7390 "(annotation?)");
7391 }
7392 } else {
7393 if (IS_SCHEMA(child, "simpleType")) {
7394 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7395 /*
7396 * 3.2.3 : 4
7397 * type and <simpleType> must not both be present.
7398 */
7399 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7400 NULL, node, child,
7401 "The attribute 'type' and the <simpleType> child "
7402 "are mutually exclusive", NULL);
7403 } else
7404 WXS_ATTRUSE_TYPEDEF(use) =
7405 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7406 child = child->next;
7407 }
7408 if (child != NULL)
7409 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7410 NULL, node, child, NULL,
7411 "(annotation?, simpleType?)");
7412 }
7413 }
7414 return (WXS_BASIC_CAST use);
7415}
7416
7417
7418static xmlSchemaAttributePtr
7419xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7420 xmlSchemaPtr schema,
7421 xmlNodePtr node)
7422{
7423 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007424 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007425 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007426 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007427
7428 /*
7429 * Note that the w3c spec assumes the schema to be validated with schema
7430 * for schemas beforehand.
7431 *
7432 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007433 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007434 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7435 return (NULL);
7436 /*
7437 * 3.2.3 : 3.1
7438 * One of ref or name must be present, but not both
7439 */
7440 attr = xmlSchemaGetPropNode(node, "name");
7441 if (attr == NULL) {
7442 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7443 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007444 return (NULL);
7445 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007446 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7447 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7448 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007449 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007450 /*
7451 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7452 * TODO: Move this to the component layer.
7453 */
7454 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7455 xmlSchemaPSimpleTypeErr(pctxt,
7456 XML_SCHEMAP_NO_XMLNS,
7457 NULL, (xmlNodePtr) attr,
7458 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7459 "The value of the attribute must not match 'xmlns'",
7460 NULL, NULL);
7461 return (NULL);
7462 }
7463 /*
7464 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7465 * TODO: Move this to the component layer.
7466 * Or better leave it here and add it to the component layer
7467 * if we have a schema construction API.
7468 */
7469 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7470 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7471 XML_SCHEMAP_NO_XSI, node, NULL,
7472 "The target namespace must not match '%s'",
7473 xmlSchemaInstanceNs, NULL);
7474 }
7475
7476 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7477 pctxt->targetNamespace, node, 1);
7478 if (ret == NULL)
7479 return (NULL);
7480 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7481
7482 /*
7483 * Check for illegal attributes.
7484 */
7485 attr = node->properties;
7486 while (attr != NULL) {
7487 if (attr->ns == NULL) {
7488 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7489 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7490 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7491 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7492 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7493 {
7494 xmlSchemaPIllegalAttrErr(pctxt,
7495 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7496 }
7497 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7498 xmlSchemaPIllegalAttrErr(pctxt,
7499 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7500 }
7501 attr = attr->next;
7502 }
7503 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7504 node, "type", &ret->typeNs, &ret->typeName);
7505
7506 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007507 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007508 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007509 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007510 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007511 if (ret->defValue != NULL)
7512 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007513 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007514 * Attribute "default".
7515 */
7516 attr = xmlSchemaGetPropNode(node, "default");
7517 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007518 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007519 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007520 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007521 */
7522 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007523 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7524 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007525 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007526 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007527 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007528 /*
7529 * And now for the children...
7530 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007531 child = node->children;
7532 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007533 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007534 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007535 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007536 if (IS_SCHEMA(child, "simpleType")) {
7537 if (ret->typeName != NULL) {
7538 /*
7539 * 3.2.3 : 4
7540 * type and <simpleType> must not both be present.
7541 */
7542 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7543 NULL, node, child,
7544 "The attribute 'type' and the <simpleType> child "
7545 "are mutually exclusive", NULL);
7546 } else
7547 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7548 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007549 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007550 if (child != NULL)
7551 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7552 NULL, node, child, NULL,
7553 "(annotation?, simpleType?)");
7554
Daniel Veillard4255d502002-04-16 15:50:10 +00007555 return (ret);
7556}
7557
7558/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007559 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007560 * @ctxt: a schema validation context
7561 * @schema: the schema being built
7562 * @node: a subtree containing XML Schema informations
7563 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007564 * Parse an attribute group definition reference.
7565 * Note that a reference to an attribute group does not
7566 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007567 * *WARNING* this interface is highly subject to change
7568 *
7569 * Returns the attribute group or NULL in case of error.
7570 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007571static xmlSchemaQNameRefPtr
7572xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7573 xmlSchemaPtr schema,
7574 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007575{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007576 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007577 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007578 xmlAttrPtr attr;
7579 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007580
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007581 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007582 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007583
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007584 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007585 if (attr == NULL) {
7586 xmlSchemaPMissingAttrErr(pctxt,
7587 XML_SCHEMAP_S4S_ATTR_MISSING,
7588 NULL, node, "ref", NULL);
7589 return (NULL);
7590 }
7591 xmlSchemaPValAttrNodeQName(pctxt, schema,
7592 NULL, attr, &refNs, &ref);
7593 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7594 return(NULL);
7595
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007596 /*
7597 * Check for illegal attributes.
7598 */
7599 attr = node->properties;
7600 while (attr != NULL) {
7601 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007602 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007603 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007604 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007605 xmlSchemaPIllegalAttrErr(pctxt,
7606 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007607 }
7608 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007609 xmlSchemaPIllegalAttrErr(pctxt,
7610 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007611 }
7612 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007613 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007614 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007615 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7616
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007617 /*
7618 * And now for the children...
7619 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007620 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007621 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007622 /*
7623 * TODO: We do not have a place to store the annotation, do we?
7624 */
7625 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007626 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007627 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007628 if (child != NULL) {
7629 xmlSchemaPContentErr(pctxt,
7630 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7631 NULL, node, child, NULL,
7632 "(annotation?)");
7633 }
7634
7635 /*
7636 * Handle attribute group redefinitions.
7637 */
7638 if (pctxt->isRedefine && pctxt->redef &&
7639 (pctxt->redef->item->type ==
7640 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7641 (ref == pctxt->redef->refName) &&
7642 (refNs == pctxt->redef->refTargetNs))
7643 {
7644 /*
7645 * SPEC src-redefine:
7646 * (7.1) "If it has an <attributeGroup> among its contents
7647 * the ·actual value· of whose ref [attribute] is the same
7648 * as the ·actual value· of its own name attribute plus
7649 * target namespace, then it must have exactly one such group."
7650 */
7651 if (pctxt->redefCounter != 0) {
7652 xmlChar *str = NULL;
7653
7654 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7655 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7656 "The redefining attribute group definition "
7657 "'%s' must not contain more than one "
7658 "reference to the redefined definition",
7659 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7660 FREE_AND_NULL(str);
7661 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007662 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007663 pctxt->redefCounter++;
7664 /*
7665 * URGENT TODO: How to ensure that the reference will not be
7666 * handled by the normal component resolution mechanism?
7667 */
7668 ret = xmlSchemaNewQNameRef(pctxt,
7669 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7670 if (ret == NULL)
7671 return(NULL);
7672 ret->node = node;
7673 pctxt->redef->reference = WXS_BASIC_CAST ret;
7674 } else {
7675 /*
7676 * Create a QName-reference helper component. We will substitute this
7677 * component for the attribute uses of the referenced attribute group
7678 * definition.
7679 */
7680 ret = xmlSchemaNewQNameRef(pctxt,
7681 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7682 if (ret == NULL)
7683 return(NULL);
7684 ret->node = node;
7685 /* Add to pending items, to be able to resolve the reference. */
7686 WXS_ADD_PENDING(pctxt, ret);
7687 }
7688 return (ret);
7689}
7690
7691/**
7692 * xmlSchemaParseAttributeGroupDefinition:
7693 * @pctxt: a schema validation context
7694 * @schema: the schema being built
7695 * @node: a subtree containing XML Schema informations
7696 *
7697 * parse a XML schema Attribute Group declaration
7698 * *WARNING* this interface is highly subject to change
7699 *
7700 * Returns the attribute group definition or NULL in case of error.
7701 */
7702static xmlSchemaAttributeGroupPtr
7703xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7704 xmlSchemaPtr schema,
7705 xmlNodePtr node)
7706{
7707 const xmlChar *name;
7708 xmlSchemaAttributeGroupPtr ret;
7709 xmlNodePtr child = NULL;
7710 xmlAttrPtr attr;
7711 int hasRefs = 0;
7712
7713 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7714 return (NULL);
7715
7716 attr = xmlSchemaGetPropNode(node, "name");
7717 if (attr == NULL) {
7718 xmlSchemaPMissingAttrErr(pctxt,
7719 XML_SCHEMAP_S4S_ATTR_MISSING,
7720 NULL, node, "name", NULL);
7721 return (NULL);
7722 }
7723 /*
7724 * The name is crucial, exit if invalid.
7725 */
7726 if (xmlSchemaPValAttrNode(pctxt,
7727 NULL, attr,
7728 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7729 return (NULL);
7730 }
7731 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7732 name, pctxt->targetNamespace, node);
7733 if (ret == NULL)
7734 return (NULL);
7735 /*
7736 * Check for illegal attributes.
7737 */
7738 attr = node->properties;
7739 while (attr != NULL) {
7740 if (attr->ns == NULL) {
7741 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7742 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7743 {
7744 xmlSchemaPIllegalAttrErr(pctxt,
7745 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7746 }
7747 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7748 xmlSchemaPIllegalAttrErr(pctxt,
7749 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7750 }
7751 attr = attr->next;
7752 }
7753 /* Attribute ID */
7754 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7755 /*
7756 * And now for the children...
7757 */
7758 child = node->children;
7759 if (IS_SCHEMA(child, "annotation")) {
7760 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7761 child = child->next;
7762 }
7763 /*
7764 * Parse contained attribute decls/refs.
7765 */
7766 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7767 (xmlSchemaItemListPtr *) &(ret->attrUses),
7768 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7769 return(NULL);
7770 if (hasRefs)
7771 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7772 /*
7773 * Parse the attribute wildcard.
7774 */
7775 if (IS_SCHEMA(child, "anyAttribute")) {
7776 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7777 schema, child);
7778 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007779 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007780 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007781 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007782 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007783 NULL, node, child, NULL,
7784 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007785 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007786 return (ret);
7787}
7788
7789/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007790 * xmlSchemaPValAttrFormDefault:
7791 * @value: the value
7792 * @flags: the flags to be modified
7793 * @flagQualified: the specific flag for "qualified"
7794 *
7795 * Returns 0 if the value is valid, 1 otherwise.
7796 */
7797static int
7798xmlSchemaPValAttrFormDefault(const xmlChar *value,
7799 int *flags,
7800 int flagQualified)
7801{
7802 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7803 if ((*flags & flagQualified) == 0)
7804 *flags |= flagQualified;
7805 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007806 return (1);
7807
William M. Brack2f2a6632004-08-20 23:09:47 +00007808 return (0);
7809}
7810
7811/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007812 * xmlSchemaPValAttrBlockFinal:
7813 * @value: the value
7814 * @flags: the flags to be modified
7815 * @flagAll: the specific flag for "#all"
7816 * @flagExtension: the specific flag for "extension"
7817 * @flagRestriction: the specific flag for "restriction"
7818 * @flagSubstitution: the specific flag for "substitution"
7819 * @flagList: the specific flag for "list"
7820 * @flagUnion: the specific flag for "union"
7821 *
7822 * Validates the value of the attribute "final" and "block". The value
7823 * is converted into the specified flag values and returned in @flags.
7824 *
7825 * Returns 0 if the value is valid, 1 otherwise.
7826 */
7827
7828static int
7829xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007830 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007831 int flagAll,
7832 int flagExtension,
7833 int flagRestriction,
7834 int flagSubstitution,
7835 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007836 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007837{
7838 int ret = 0;
7839
7840 /*
7841 * TODO: This does not check for dublicate entries.
7842 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007843 if ((flags == NULL) || (value == NULL))
7844 return (-1);
7845 if (value[0] == 0)
7846 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007847 if (xmlStrEqual(value, BAD_CAST "#all")) {
7848 if (flagAll != -1)
7849 *flags |= flagAll;
7850 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007851 if (flagExtension != -1)
7852 *flags |= flagExtension;
7853 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007854 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007855 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007856 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007857 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007858 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007859 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007860 *flags |= flagUnion;
7861 }
7862 } else {
7863 const xmlChar *end, *cur = value;
7864 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007865
Daniel Veillardc0826a72004-08-10 14:17:33 +00007866 do {
7867 while (IS_BLANK_CH(*cur))
7868 cur++;
7869 end = cur;
7870 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7871 end++;
7872 if (end == cur)
7873 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007874 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007875 if (xmlStrEqual(item, BAD_CAST "extension")) {
7876 if (flagExtension != -1) {
7877 if ((*flags & flagExtension) == 0)
7878 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007879 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007880 ret = 1;
7881 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7882 if (flagRestriction != -1) {
7883 if ((*flags & flagRestriction) == 0)
7884 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007885 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007886 ret = 1;
7887 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7888 if (flagSubstitution != -1) {
7889 if ((*flags & flagSubstitution) == 0)
7890 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007891 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007892 ret = 1;
7893 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7894 if (flagList != -1) {
7895 if ((*flags & flagList) == 0)
7896 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007897 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007898 ret = 1;
7899 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7900 if (flagUnion != -1) {
7901 if ((*flags & flagUnion) == 0)
7902 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007903 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007904 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007905 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007906 ret = 1;
7907 if (item != NULL)
7908 xmlFree(item);
7909 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007910 } while ((ret == 0) && (*cur != 0));
7911 }
7912
Daniel Veillardc0826a72004-08-10 14:17:33 +00007913 return (ret);
7914}
7915
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007916static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007917xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007918 xmlSchemaIDCPtr idc,
7919 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007920 xmlAttrPtr attr,
7921 int isField)
7922{
7923 xmlNodePtr node;
7924
7925 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007926 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007927 * Schema Component Constraint: Selector Value OK
7928 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007929 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007930 * in [XPath].
7931 */
7932 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007933 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007934 XML_SCHEMAP_INTERNAL,
7935 "Internal error: xmlSchemaCheckCSelectorXPath, "
7936 "the selector is not specified.\n", NULL, NULL);
7937 return (-1);
7938 }
7939 if (attr == NULL)
7940 node = idc->node;
7941 else
7942 node = (xmlNodePtr) attr;
7943 if (selector->xpath == NULL) {
7944 xmlSchemaPCustomErr(ctxt,
7945 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007946 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007947 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007948 "The XPath expression of the selector is not valid", NULL);
7949 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7950 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007951 const xmlChar **nsArray = NULL;
7952 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007953 /*
7954 * Compile the XPath expression.
7955 */
7956 /*
7957 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007958 * TODO: Call xmlPatterncompile with different options for selector/
7959 * field.
7960 */
7961 nsList = xmlGetNsList(attr->doc, attr->parent);
7962 /*
7963 * Build an array of prefixes and namespaces.
7964 */
7965 if (nsList != NULL) {
7966 int i, count = 0;
7967 xmlNsPtr ns;
7968
7969 for (i = 0; nsList[i] != NULL; i++)
7970 count++;
7971
7972 nsArray = (const xmlChar **) xmlMalloc(
7973 (count * 2 + 1) * sizeof(const xmlChar *));
7974 if (nsArray == NULL) {
7975 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
7976 NULL);
7977 return (-1);
7978 }
7979 for (i = 0; i < count; i++) {
7980 ns = nsList[i];
7981 nsArray[2 * i] = nsList[i]->href;
7982 nsArray[2 * i + 1] = nsList[i]->prefix;
7983 }
7984 nsArray[count * 2] = NULL;
7985 xmlFree(nsList);
7986 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007987 /*
7988 * TODO: Differentiate between "selector" and "field".
7989 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007990 if (isField)
7991 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00007992 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007993 else
7994 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00007995 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007996 if (nsArray != NULL)
7997 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007998
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00007999 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008000 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008001 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008002 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008003 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008004 "The XPath expression '%s' could not be "
8005 "compiled", selector->xpath);
8006 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008007 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008008 }
8009 return (0);
8010}
8011
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008012#define ADD_ANNOTATION(annot) \
8013 xmlSchemaAnnotPtr cur = item->annot; \
8014 if (item->annot == NULL) { \
8015 item->annot = annot; \
8016 return (annot); \
8017 } \
8018 cur = item->annot; \
8019 if (cur->next != NULL) { \
8020 cur = cur->next; \
8021 } \
8022 cur->next = annot;
8023
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008024/**
8025 * xmlSchemaAssignAnnotation:
8026 * @item: the schema component
8027 * @annot: the annotation
8028 *
8029 * Adds the annotation to the given schema component.
8030 *
8031 * Returns the given annotaion.
8032 */
8033static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008034xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8035 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008036{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008037 if ((annItem == NULL) || (annot == NULL))
8038 return (NULL);
8039 switch (annItem->type) {
8040 case XML_SCHEMA_TYPE_ELEMENT: {
8041 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8042 ADD_ANNOTATION(annot)
8043 }
8044 break;
8045 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8046 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8047 ADD_ANNOTATION(annot)
8048 }
8049 break;
8050 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8051 case XML_SCHEMA_TYPE_ANY: {
8052 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8053 ADD_ANNOTATION(annot)
8054 }
8055 break;
8056 case XML_SCHEMA_TYPE_PARTICLE:
8057 case XML_SCHEMA_TYPE_IDC_KEY:
8058 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008059 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008060 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8061 ADD_ANNOTATION(annot)
8062 }
8063 break;
8064 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8065 xmlSchemaAttributeGroupPtr item =
8066 (xmlSchemaAttributeGroupPtr) annItem;
8067 ADD_ANNOTATION(annot)
8068 }
8069 break;
8070 case XML_SCHEMA_TYPE_NOTATION: {
8071 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8072 ADD_ANNOTATION(annot)
8073 }
8074 break;
8075 case XML_SCHEMA_FACET_MININCLUSIVE:
8076 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8077 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8078 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8079 case XML_SCHEMA_FACET_TOTALDIGITS:
8080 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8081 case XML_SCHEMA_FACET_PATTERN:
8082 case XML_SCHEMA_FACET_ENUMERATION:
8083 case XML_SCHEMA_FACET_WHITESPACE:
8084 case XML_SCHEMA_FACET_LENGTH:
8085 case XML_SCHEMA_FACET_MAXLENGTH:
8086 case XML_SCHEMA_FACET_MINLENGTH: {
8087 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8088 ADD_ANNOTATION(annot)
8089 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008090 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008091 case XML_SCHEMA_TYPE_SIMPLE:
8092 case XML_SCHEMA_TYPE_COMPLEX: {
8093 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8094 ADD_ANNOTATION(annot)
8095 }
8096 break;
8097 case XML_SCHEMA_TYPE_GROUP: {
8098 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8099 ADD_ANNOTATION(annot)
8100 }
8101 break;
8102 case XML_SCHEMA_TYPE_SEQUENCE:
8103 case XML_SCHEMA_TYPE_CHOICE:
8104 case XML_SCHEMA_TYPE_ALL: {
8105 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8106 ADD_ANNOTATION(annot)
8107 }
8108 break;
8109 default:
8110 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008111 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008112 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008113 "Internal error: xmlSchemaAddAnnotation, "
8114 "The item is not a annotated schema component", NULL);
8115 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008116 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008117 return (annot);
8118}
8119
8120/**
8121 * xmlSchemaParseIDCSelectorAndField:
8122 * @ctxt: a schema validation context
8123 * @schema: the schema being built
8124 * @node: a subtree containing XML Schema informations
8125 *
8126 * Parses a XML Schema identity-contraint definition's
8127 * <selector> and <field> elements.
8128 *
8129 * Returns the parsed identity-constraint definition.
8130 */
8131static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008132xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008133 xmlSchemaPtr schema,
8134 xmlSchemaIDCPtr idc,
8135 xmlNodePtr node,
8136 int isField)
8137{
8138 xmlSchemaIDCSelectPtr item;
8139 xmlNodePtr child = NULL;
8140 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008141
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008142 /*
8143 * Check for illegal attributes.
8144 */
8145 attr = node->properties;
8146 while (attr != NULL) {
8147 if (attr->ns == NULL) {
8148 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8149 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008150 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008151 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008152 }
8153 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008154 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008155 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008156 }
8157 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008158 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008159 /*
8160 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008161 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008162 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8163 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008164 xmlSchemaPErrMemory(ctxt,
8165 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008166 NULL);
8167 return (NULL);
8168 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008169 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008170 /*
8171 * Attribute "xpath" (mandatory).
8172 */
8173 attr = xmlSchemaGetPropNode(node, "xpath");
8174 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008175 xmlSchemaPMissingAttrErr(ctxt,
8176 XML_SCHEMAP_S4S_ATTR_MISSING,
8177 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008178 "name", NULL);
8179 } else {
8180 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8181 /*
8182 * URGENT TODO: "field"s have an other syntax than "selector"s.
8183 */
8184
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008185 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8186 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008187 xmlSchemaPErr(ctxt,
8188 (xmlNodePtr) attr,
8189 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008190 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008191 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008192 NULL, NULL);
8193 }
8194
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008195 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008196 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008197 /*
8198 * And now for the children...
8199 */
8200 child = node->children;
8201 if (IS_SCHEMA(child, "annotation")) {
8202 /*
8203 * Add the annotation to the parent IDC.
8204 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008205 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008206 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008207 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008208 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008209 if (child != NULL) {
8210 xmlSchemaPContentErr(ctxt,
8211 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008212 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008213 NULL, "(annotation?)");
8214 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008215
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008216 return (item);
8217}
8218
8219/**
8220 * xmlSchemaParseIDC:
8221 * @ctxt: a schema validation context
8222 * @schema: the schema being built
8223 * @node: a subtree containing XML Schema informations
8224 *
8225 * Parses a XML Schema identity-contraint definition.
8226 *
8227 * Returns the parsed identity-constraint definition.
8228 */
8229static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008230xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008231 xmlSchemaPtr schema,
8232 xmlNodePtr node,
8233 xmlSchemaTypeType idcCategory,
8234 const xmlChar *targetNamespace)
8235{
8236 xmlSchemaIDCPtr item = NULL;
8237 xmlNodePtr child = NULL;
8238 xmlAttrPtr attr;
8239 const xmlChar *name = NULL;
8240 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008241
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008242 /*
8243 * Check for illegal attributes.
8244 */
8245 attr = node->properties;
8246 while (attr != NULL) {
8247 if (attr->ns == NULL) {
8248 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8249 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8250 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8251 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008252 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008253 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008254 }
8255 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008256 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008257 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008258 }
8259 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008260 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008261 /*
8262 * Attribute "name" (mandatory).
8263 */
8264 attr = xmlSchemaGetPropNode(node, "name");
8265 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008266 xmlSchemaPMissingAttrErr(ctxt,
8267 XML_SCHEMAP_S4S_ATTR_MISSING,
8268 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008269 "name", NULL);
8270 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008271 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008272 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008273 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8274 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008275 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008276 /* Create the component. */
8277 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8278 idcCategory, node);
8279 if (item == NULL)
8280 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008281
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008282 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008283 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8284 /*
8285 * Attribute "refer" (mandatory).
8286 */
8287 attr = xmlSchemaGetPropNode(node, "refer");
8288 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008289 xmlSchemaPMissingAttrErr(ctxt,
8290 XML_SCHEMAP_S4S_ATTR_MISSING,
8291 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008292 "refer", NULL);
8293 } else {
8294 /*
8295 * Create a reference item.
8296 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008297 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008298 NULL, NULL);
8299 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008300 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008301 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008302 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008303 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008304 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008305 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008306 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008307 }
8308 }
8309 /*
8310 * And now for the children...
8311 */
8312 child = node->children;
8313 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008314 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008315 child = child->next;
8316 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008317 if (child == NULL) {
8318 xmlSchemaPContentErr(ctxt,
8319 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008320 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008321 "A child element is missing",
8322 "(annotation?, (selector, field+))");
8323 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008324 /*
8325 * Child element <selector>.
8326 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008327 if (IS_SCHEMA(child, "selector")) {
8328 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008329 item, child, 0);
8330 child = child->next;
8331 /*
8332 * Child elements <field>.
8333 */
8334 if (IS_SCHEMA(child, "field")) {
8335 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008336 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008337 item, child, 1);
8338 if (field != NULL) {
8339 field->index = item->nbFields;
8340 item->nbFields++;
8341 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008342 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008343 else
8344 item->fields = field;
8345 lastField = field;
8346 }
8347 child = child->next;
8348 } while (IS_SCHEMA(child, "field"));
8349 } else {
8350 xmlSchemaPContentErr(ctxt,
8351 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008352 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008353 NULL, "(annotation?, (selector, field+))");
8354 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008355 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008356 if (child != NULL) {
8357 xmlSchemaPContentErr(ctxt,
8358 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008359 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008360 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008361 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008362
8363 return (item);
8364}
8365
Daniel Veillardc0826a72004-08-10 14:17:33 +00008366/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008367 * xmlSchemaParseElement:
8368 * @ctxt: a schema validation context
8369 * @schema: the schema being built
8370 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008371 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008372 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008373 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008374 * *WARNING* this interface is highly subject to change
8375 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008376 * Returns the element declaration or a particle; NULL in case
8377 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008378 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008379static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008380xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008381 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008382{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008383 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008384 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008385 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008386 xmlNodePtr child = NULL;
8387 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008388 int min, max, isRef = 0;
8389 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008390
8391 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8392 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008393
Daniel Veillard4255d502002-04-16 15:50:10 +00008394 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008395 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008396
8397 if (isElemRef != NULL)
8398 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008399 /*
8400 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008401 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008402 * robust.
8403 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008404 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008405 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008406 if ((topLevel) || (attr == NULL)) {
8407 if (nameAttr == NULL) {
8408 xmlSchemaPMissingAttrErr(ctxt,
8409 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008410 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008411 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008412 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008413 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008414 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008415
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008416 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008417 child = node->children;
8418 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008419 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008420 child = child->next;
8421 }
8422 /*
8423 * Skip particle part if a global declaration.
8424 */
8425 if (topLevel)
8426 goto declaration_part;
8427 /*
8428 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008429 */
8430 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8431 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8432 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008433 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8434 if (particle == NULL)
8435 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008436
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008437 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8438
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008439 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008440 const xmlChar *refNs = NULL, *ref = NULL;
8441 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008442 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008443 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008444 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008445 if (isElemRef != NULL)
8446 *isElemRef = 1;
8447
Daniel Veillardc0826a72004-08-10 14:17:33 +00008448 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008449 NULL, attr, &refNs, &ref);
8450 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008451 /*
8452 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008453 */
8454 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008455 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008456 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008457 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008458 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008459 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008460 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008461 attr = node->properties;
8462 while (attr != NULL) {
8463 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008464 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8465 xmlStrEqual(attr->name, BAD_CAST "name") ||
8466 xmlStrEqual(attr->name, BAD_CAST "id") ||
8467 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8468 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8469 {
8470 attr = attr->next;
8471 continue;
8472 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008473 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008474 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008475 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008476 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008477 "Only the attributes 'minOccurs', 'maxOccurs' and "
8478 "'id' are allowed in addition to 'ref'");
8479 break;
8480 }
8481 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8482 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008483 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008484 }
8485 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008486 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008487 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008488 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008489 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008490 if (child != NULL) {
8491 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008492 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008493 }
8494 if ((min == 0) && (max == 0))
8495 goto return_null;
8496 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008497 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008498 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008499 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008500 ref, refNs);
8501 if (refer == NULL)
8502 goto return_null;
8503 particle->children = (xmlSchemaTreeItemPtr) refer;
8504 particle->annot = annot;
8505 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008506 * Add the particle to pending components, since the reference
8507 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008508 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008509 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008510 return ((xmlSchemaBasicItemPtr) particle);
8511 }
8512 /*
8513 * The declaration part ===============================================
8514 */
8515declaration_part:
8516 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008517 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008518 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8519
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008520 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008521 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008522 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008523 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008524 * Evaluate the target namespace.
8525 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008526 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008527 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008528 } else {
8529 attr = xmlSchemaGetPropNode(node, "form");
8530 if (attr != NULL) {
8531 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8532 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008533 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008534 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008535 xmlSchemaPSimpleTypeErr(ctxt,
8536 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8537 NULL, (xmlNodePtr) attr,
8538 NULL, "(qualified | unqualified)",
8539 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008540 }
8541 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008542 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008543 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008544 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008545 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008546 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008547 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008548 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008549 * Check for illegal attributes.
8550 */
William M. Bracke7091952004-05-11 15:09:58 +00008551 attr = node->properties;
8552 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008553 if (attr->ns == NULL) {
8554 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8555 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008556 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008557 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008558 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008559 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008560 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8561 {
8562 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008563 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008564 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008565 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008566 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008567 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008568 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008569 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008570 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8571 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008572 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8573
8574 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008575 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008576 }
8577 }
8578 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008579
Daniel Veillardc0826a72004-08-10 14:17:33 +00008580 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008581 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008582 }
8583 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008584 }
William M. Bracke7091952004-05-11 15:09:58 +00008585 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008586 * Extract/validate attributes.
8587 */
8588 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008589 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008590 * Process top attributes of global element declarations here.
8591 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008592 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8593 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008594 xmlSchemaPValAttrQName(ctxt, schema,
8595 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008596 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008597 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008599 /*
8600 * Attribute "final".
8601 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008602 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008603 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008604 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8605 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8606 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8607 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008608 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008609 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8610 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008611 -1,
8612 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8613 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008614 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008615 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008616 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008617 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008618 attrValue, NULL, NULL, NULL);
8619 }
8620 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008621 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008622 /*
8623 * Attribute "block".
8624 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008625 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008626 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008627 /*
8628 * Apply default "block" values.
8629 */
8630 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8631 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8632 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8633 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8634 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8635 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008636 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008637 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8638 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008639 -1,
8640 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008641 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008642 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8643 xmlSchemaPSimpleTypeErr(ctxt,
8644 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008645 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008646 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008647 "restriction | substitution))", attrValue,
8648 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008649 }
8650 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008651 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008652 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008653
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008654 attr = xmlSchemaGetPropNode(node, "type");
8655 if (attr != NULL) {
8656 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008657 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008658 &(decl->namedTypeNs), &(decl->namedType));
8659 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008660 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008661 }
8662 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8663 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008664 if (attr != NULL) {
8665 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008666 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008667 /*
8668 * 3.3.3 : 1
8669 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008670 */
8671 xmlSchemaPMutualExclAttrErr(ctxt,
8672 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008673 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008674 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008675 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8676 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008677 }
William M. Bracke7091952004-05-11 15:09:58 +00008678 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008679 /*
8680 * And now for the children...
8681 */
William M. Bracke7091952004-05-11 15:09:58 +00008682 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008683 /*
8684 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008685 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008686 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008687 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008688 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008689 xmlSchemaPContentErr(ctxt,
8690 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008691 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008692 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008693 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008694 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008695 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008696 child = child->next;
8697 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008698 /*
8699 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008700 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008701 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008702 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008703 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008704 xmlSchemaPContentErr(ctxt,
8705 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008706 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008707 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008708 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008709 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008710 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008711 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008712 }
William M. Bracke7091952004-05-11 15:09:58 +00008713 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008714 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008715 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008716 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008717 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008718 } else if (IS_SCHEMA(child, "key")) {
8719 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008720 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008721 } else if (IS_SCHEMA(child, "keyref")) {
8722 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008723 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008724 }
8725 if (lastIDC != NULL)
8726 lastIDC->next = curIDC;
8727 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008728 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008729 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008730 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008731 }
8732 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008733 xmlSchemaPContentErr(ctxt,
8734 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008735 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008736 NULL, "(annotation?, ((simpleType | complexType)?, "
8737 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008738 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008739 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008740 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008741 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008742 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008743 * different layer.
8744 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008745 FREE_AND_NULL(des)
8746 if (topLevel)
8747 return ((xmlSchemaBasicItemPtr) decl);
8748 else {
8749 particle->children = (xmlSchemaTreeItemPtr) decl;
8750 return ((xmlSchemaBasicItemPtr) particle);
8751 }
8752
8753return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008754 FREE_AND_NULL(des);
8755 if (annot != NULL) {
8756 if (particle != NULL)
8757 particle->annot = NULL;
8758 if (decl != NULL)
8759 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008760 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008761 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008762 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008763}
8764
8765/**
8766 * xmlSchemaParseUnion:
8767 * @ctxt: a schema validation context
8768 * @schema: the schema being built
8769 * @node: a subtree containing XML Schema informations
8770 *
8771 * parse a XML schema Union definition
8772 * *WARNING* this interface is highly subject to change
8773 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008774 * Returns -1 in case of internal error, 0 in case of success and a positive
8775 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008776 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008777static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008778xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008779 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008780{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008781 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008782 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008783 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008784 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008785
8786 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008787 return (-1);
8788 /* Not a component, don't create it. */
8789 type = ctxt->ctxtType;
8790 /*
8791 * Mark the simple type as being of variety "union".
8792 */
8793 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008794 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008795 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8796 * then the ·simple ur-type definition·."
8797 */
8798 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008799 /*
8800 * Check for illegal attributes.
8801 */
8802 attr = node->properties;
8803 while (attr != NULL) {
8804 if (attr->ns == NULL) {
8805 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8806 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008807 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008808 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008809 }
8810 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008811 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008812 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008813 }
8814 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008815 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008816 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008817 /*
8818 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008819 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008820 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008821 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008822 if (attr != NULL) {
8823 const xmlChar *end;
8824 xmlChar *tmp;
8825 const xmlChar *localName, *nsName;
8826 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8827 xmlSchemaQNameRefPtr ref;
8828
8829 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008830 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008831 do {
8832 while (IS_BLANK_CH(*cur))
8833 cur++;
8834 end = cur;
8835 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8836 end++;
8837 if (end == cur)
8838 break;
8839 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008840 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008841 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008842 /*
8843 * Create the member type link.
8844 */
8845 link = (xmlSchemaTypeLinkPtr)
8846 xmlMalloc(sizeof(xmlSchemaTypeLink));
8847 if (link == NULL) {
8848 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8849 "allocating a type link", NULL);
8850 return (-1);
8851 }
8852 link->type = NULL;
8853 link->next = NULL;
8854 if (lastLink == NULL)
8855 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008856 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008857 lastLink->next = link;
8858 lastLink = link;
8859 /*
8860 * Create a reference item.
8861 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008862 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008863 localName, nsName);
8864 if (ref == NULL) {
8865 FREE_AND_NULL(tmp)
8866 return (-1);
8867 }
8868 /*
8869 * Assign the reference to the link, it will be resolved
8870 * later during fixup of the union simple type.
8871 */
8872 link->type = (xmlSchemaTypePtr) ref;
8873 }
8874 FREE_AND_NULL(tmp)
8875 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008876 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008877
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008878 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008879 /*
8880 * And now for the children...
8881 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008882 child = node->children;
8883 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008884 /*
8885 * Add the annotation to the simple type ancestor.
8886 */
8887 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008888 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008889 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008890 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008891 if (IS_SCHEMA(child, "simpleType")) {
8892 xmlSchemaTypePtr subtype, last = NULL;
8893
8894 /*
8895 * Anchor the member types in the "subtypes" field of the
8896 * simple type.
8897 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008898 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008899 subtype = (xmlSchemaTypePtr)
8900 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8901 if (subtype != NULL) {
8902 if (last == NULL) {
8903 type->subtypes = subtype;
8904 last = subtype;
8905 } else {
8906 last->next = subtype;
8907 last = subtype;
8908 }
8909 last->next = NULL;
8910 }
8911 child = child->next;
8912 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008913 }
8914 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008915 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008916 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008917 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008918 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008919 if ((attr == NULL) && (type->subtypes == NULL)) {
8920 /*
8921 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008922 * Either the memberTypes [attribute] of the <union> element must
8923 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008924 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008925 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008926 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008927 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008928 "Either the attribute 'memberTypes' or "
8929 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008930 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008931 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008932}
8933
8934/**
8935 * xmlSchemaParseList:
8936 * @ctxt: a schema validation context
8937 * @schema: the schema being built
8938 * @node: a subtree containing XML Schema informations
8939 *
8940 * parse a XML schema List definition
8941 * *WARNING* this interface is highly subject to change
8942 *
William M. Bracke7091952004-05-11 15:09:58 +00008943 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008944 * 1 in case of success.
8945 */
8946static xmlSchemaTypePtr
8947xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008948 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008949{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008950 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008951 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008952 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008953
8954 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8955 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008956 /* Not a component, don't create it. */
8957 type = ctxt->ctxtType;
8958 /*
8959 * Mark the type as being of variety "list".
8960 */
8961 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008962 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008963 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8964 * then the ·simple ur-type definition·."
8965 */
8966 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008967 /*
8968 * Check for illegal attributes.
8969 */
8970 attr = node->properties;
8971 while (attr != NULL) {
8972 if (attr->ns == NULL) {
8973 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8974 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008975 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008976 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008977 }
8978 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008979 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008980 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008981 }
8982 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008983 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008984
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008985 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008986
William M. Brack2f2a6632004-08-20 23:09:47 +00008987 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008988 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
8989 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008990 *
8991 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
8992 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00008993 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008994 xmlSchemaPValAttrQName(ctxt, schema, NULL,
8995 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00008996 /*
8997 * And now for the children...
8998 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008999 child = node->children;
9000 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009001 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009002 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009003 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009004 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009005 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009006 /*
9007 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009008 * Either the itemType [attribute] or the <simpleType> [child] of
9009 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009010 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009011 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009012 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009013 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009014 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009015 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009016 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009018 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009019 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009020 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009021 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009022 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009023 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009024 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009025 "Either the attribute 'itemType' or the <simpleType> child "
9026 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009027 }
9028 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009029 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009030 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009031 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009032 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009033 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009034 (type->subtypes == NULL) &&
9035 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009036 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009037 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009038 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009039 "Either the attribute 'itemType' or the <simpleType> child "
9040 "must be present", NULL);
9041 }
9042 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009043}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009044
Daniel Veillard4255d502002-04-16 15:50:10 +00009045/**
9046 * xmlSchemaParseSimpleType:
9047 * @ctxt: a schema validation context
9048 * @schema: the schema being built
9049 * @node: a subtree containing XML Schema informations
9050 *
9051 * parse a XML schema Simple Type definition
9052 * *WARNING* this interface is highly subject to change
9053 *
William M. Bracke7091952004-05-11 15:09:58 +00009054 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009055 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009056 */
9057static xmlSchemaTypePtr
9058xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009059 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009060{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009061 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009062 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009063 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009064 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009065 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009066
9067 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9068 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009069
Daniel Veillardc0826a72004-08-10 14:17:33 +00009070 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009071 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009072 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009073 xmlSchemaPMissingAttrErr(ctxt,
9074 XML_SCHEMAP_S4S_ATTR_MISSING,
9075 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009076 "name", NULL);
9077 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009078 } else {
9079 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009080 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009081 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9082 return (NULL);
9083 /*
9084 * Skip built-in types.
9085 */
9086 if (ctxt->isS4S) {
9087 xmlSchemaTypePtr biType;
9088
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009089 if (ctxt->isRedefine) {
9090 /*
9091 * REDEFINE: Disallow redefinition of built-in-types.
9092 * TODO: It seems that the spec does not say anything
9093 * about this case.
9094 */
9095 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009096 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009097 "Redefinition of built-in simple types is not "
9098 "supported", NULL);
9099 return(NULL);
9100 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009101 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9102 if (biType != NULL)
9103 return (biType);
9104 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009105 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009106 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009107 /*
9108 * TargetNamespace:
9109 * SPEC "The ·actual value· of the targetNamespace [attribute]
9110 * of the <schema> ancestor element information item if present,
9111 * otherwise ·absent·.
9112 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009113 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009114#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009115 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009116#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009117 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009118 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009119 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009120#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009121 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009122 type = xmlSchemaAddType(ctxt, schema,
9123 XML_SCHEMA_TYPE_SIMPLE,
9124 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009125 ctxt->targetNamespace, node, 0);
9126#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009127 type = xmlSchemaAddType(ctxt, schema,
9128 XML_SCHEMA_TYPE_SIMPLE,
9129 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009130#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009131 if (type == NULL)
9132 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009133 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009134 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009135 /*
9136 * Check for illegal attributes.
9137 */
9138 attr = node->properties;
9139 while (attr != NULL) {
9140 if (attr->ns == NULL) {
9141 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009142 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009143 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009144 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009145 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009146 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009147 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009148 }
9149 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009150 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009151 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009152 /*
9153 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009154 *
9155 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009156 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009157 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9158 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009159 if (type == NULL)
9160 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009162 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009163 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9164 /*
9165 * Check for illegal attributes.
9166 */
9167 attr = node->properties;
9168 while (attr != NULL) {
9169 if (attr->ns == NULL) {
9170 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9171 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009172 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009173 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009174 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009175 }
9176 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009177 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009178 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009179 }
9180 attr = attr->next;
9181 }
9182 /*
9183 * Attribute "final".
9184 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009185 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009186 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009187 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9188 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9189 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9190 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9191 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9192 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009193 } else {
9194 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009195 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9196 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009197 XML_SCHEMAS_TYPE_FINAL_LIST,
9198 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9199
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009200 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009201 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009202 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009203 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009204 attrValue, NULL, NULL, NULL);
9205 }
9206 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009207 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009208 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009209 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009210 /*
9211 * And now for the children...
9212 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009213 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009214
Daniel Veillardc0826a72004-08-10 14:17:33 +00009215 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009216
Daniel Veillard4255d502002-04-16 15:50:10 +00009217 child = node->children;
9218 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009219 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009220 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009221 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009222 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009223 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009224 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009225 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009226 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009227 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009228 XML_SCHEMA_TYPE_SIMPLE);
9229 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009230 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009231 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009232 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009233 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009234 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009235 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009236 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009237 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009238 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009239 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009240 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009241 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009242 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009243 /*
9244 * REDEFINE: SPEC src-redefine (5)
9245 * "Within the [children], each <simpleType> must have a
9246 * <restriction> among its [children] ... the ·actual value· of whose
9247 * base [attribute] must be the same as the ·actual value· of its own
9248 * name attribute plus target namespace;"
9249 */
9250 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9251 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009252 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009253 "<simpleType> must have a <restriction> child", NULL);
9254 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009255
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009256 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009257 return (type);
9258}
9259
Daniel Veillard4255d502002-04-16 15:50:10 +00009260/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009261 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009262 * @ctxt: the parser context
9263 * @schema: the schema being built
9264 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009265 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009266 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009267 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009268 * We will return a particle component with a qname-component or
9269 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009270 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009271static xmlSchemaTreeItemPtr
9272xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9273 xmlSchemaPtr schema,
9274 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009275{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009276 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009277 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009278 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009279 const xmlChar *ref = NULL, *refNs = NULL;
9280 int min, max;
9281
9282 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009283 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009284
9285 attr = xmlSchemaGetPropNode(node, "ref");
9286 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009287 xmlSchemaPMissingAttrErr(ctxt,
9288 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009289 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009290 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009291 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009292 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009293 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009294 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009295 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009296 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009297 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009298 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009299 /*
9300 * Check for illegal attributes.
9301 */
9302 attr = node->properties;
9303 while (attr != NULL) {
9304 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009305 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009306 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9307 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9308 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009309 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009310 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009311 }
9312 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009313 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009314 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009315 }
9316 attr = attr->next;
9317 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009318 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009319 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9320 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009321 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009322 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009323 * Create a qname-reference and set as the term; it will be substituted
9324 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009325 */
9326 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009327 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009328 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9329 /*
9330 * And now for the children...
9331 */
9332 child = node->children;
9333 /* TODO: Is annotation even allowed for a model group reference? */
9334 if (IS_SCHEMA(child, "annotation")) {
9335 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009336 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009337 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009338 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009339 child = child->next;
9340 }
9341 if (child != NULL) {
9342 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009343 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009344 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009345 "(annotation?)");
9346 }
9347 /*
9348 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9349 */
9350 if ((min == 0) && (max == 0))
9351 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009352
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009353 return ((xmlSchemaTreeItemPtr) item);
9354}
9355
9356/**
9357 * xmlSchemaParseModelGroupDefinition:
9358 * @ctxt: a schema validation context
9359 * @schema: the schema being built
9360 * @node: a subtree containing XML Schema informations
9361 *
9362 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009363 *
9364 * Note that the contraint src-redefine (6.2) can't be applied until
9365 * references have been resolved. So we will do this at the
9366 * component fixup level.
9367 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009368 * *WARNING* this interface is highly subject to change
9369 *
9370 * Returns -1 in case of error, 0 if the declaration is improper and
9371 * 1 in case of success.
9372 */
9373static xmlSchemaModelGroupDefPtr
9374xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9375 xmlSchemaPtr schema,
9376 xmlNodePtr node)
9377{
9378 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009379 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009380 xmlAttrPtr attr;
9381 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009382
9383 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009384 return (NULL);
9385
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009386 attr = xmlSchemaGetPropNode(node, "name");
9387 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009388 xmlSchemaPMissingAttrErr(ctxt,
9389 XML_SCHEMAP_S4S_ATTR_MISSING,
9390 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009391 "name", NULL);
9392 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009393 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009394 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9395 return (NULL);
9396 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009397 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9398 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009399 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009400 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009401 /*
9402 * Check for illegal attributes.
9403 */
9404 attr = node->properties;
9405 while (attr != NULL) {
9406 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009407 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009408 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009409 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009410 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009411 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009412 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009413 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009414 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009415 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009416 attr = attr->next;
9417 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009418 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009419 /*
9420 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009421 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009422 child = node->children;
9423 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009424 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009425 child = child->next;
9426 }
9427 if (IS_SCHEMA(child, "all")) {
9428 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9429 XML_SCHEMA_TYPE_ALL, 0);
9430 child = child->next;
9431 } else if (IS_SCHEMA(child, "choice")) {
9432 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9433 XML_SCHEMA_TYPE_CHOICE, 0);
9434 child = child->next;
9435 } else if (IS_SCHEMA(child, "sequence")) {
9436 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9437 XML_SCHEMA_TYPE_SEQUENCE, 0);
9438 child = child->next;
9439 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009440
9441
9442
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009443 if (child != NULL) {
9444 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009445 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009446 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009447 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009448 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009449 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009450}
9451
9452/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009453 * xmlSchemaCleanupDoc:
9454 * @ctxt: a schema validation context
9455 * @node: the root of the document.
9456 *
9457 * removes unwanted nodes in a schemas document tree
9458 */
9459static void
9460xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9461{
9462 xmlNodePtr delete, cur;
9463
9464 if ((ctxt == NULL) || (root == NULL)) return;
9465
9466 /*
9467 * Remove all the blank text nodes
9468 */
9469 delete = NULL;
9470 cur = root;
9471 while (cur != NULL) {
9472 if (delete != NULL) {
9473 xmlUnlinkNode(delete);
9474 xmlFreeNode(delete);
9475 delete = NULL;
9476 }
9477 if (cur->type == XML_TEXT_NODE) {
9478 if (IS_BLANK_NODE(cur)) {
9479 if (xmlNodeGetSpacePreserve(cur) != 1) {
9480 delete = cur;
9481 }
9482 }
9483 } else if ((cur->type != XML_ELEMENT_NODE) &&
9484 (cur->type != XML_CDATA_SECTION_NODE)) {
9485 delete = cur;
9486 goto skip_children;
9487 }
9488
9489 /*
9490 * Skip to next node
9491 */
9492 if (cur->children != NULL) {
9493 if ((cur->children->type != XML_ENTITY_DECL) &&
9494 (cur->children->type != XML_ENTITY_REF_NODE) &&
9495 (cur->children->type != XML_ENTITY_NODE)) {
9496 cur = cur->children;
9497 continue;
9498 }
9499 }
9500 skip_children:
9501 if (cur->next != NULL) {
9502 cur = cur->next;
9503 continue;
9504 }
9505
9506 do {
9507 cur = cur->parent;
9508 if (cur == NULL)
9509 break;
9510 if (cur == root) {
9511 cur = NULL;
9512 break;
9513 }
9514 if (cur->next != NULL) {
9515 cur = cur->next;
9516 break;
9517 }
9518 } while (cur != NULL);
9519 }
9520 if (delete != NULL) {
9521 xmlUnlinkNode(delete);
9522 xmlFreeNode(delete);
9523 delete = NULL;
9524 }
9525}
9526
William M. Brack2f2a6632004-08-20 23:09:47 +00009527
William M. Brack2f2a6632004-08-20 23:09:47 +00009528static void
9529xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9530{
9531 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9532 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9533
9534 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9535 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9536
9537 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9538 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9539 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9540 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9541 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9542 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9543 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9544 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9545
9546 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9547 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9548 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9549 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9550 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9551 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9552}
9553
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009554static int
9555xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009556 xmlSchemaPtr schema,
9557 xmlNodePtr node)
9558{
9559 xmlAttrPtr attr;
9560 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009561 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009562
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009563 /*
9564 * Those flags should be moved to the parser context flags,
9565 * since they are not visible at the component level. I.e.
9566 * they are used if processing schema *documents* only.
9567 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009568 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009569 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009570
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009571 /*
9572 * Since the version is of type xs:token, we won't bother to
9573 * check it.
9574 */
9575 /* REMOVED:
9576 attr = xmlSchemaGetPropNode(node, "version");
9577 if (attr != NULL) {
9578 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9579 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9580 HFAILURE;
9581 }
9582 */
9583 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9584 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009585 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009586 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9587 HFAILURE;
9588 if (res != 0) {
9589 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9590 goto exit;
9591 }
9592 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009593 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009594 if (attr != NULL) {
9595 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009596 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9597 XML_SCHEMAS_QUALIF_ELEM);
9598 HFAILURE;
9599 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009600 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009601 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009602 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009603 "(qualified | unqualified)", val, NULL, NULL, NULL);
9604 }
9605 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009606 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009607 if (attr != NULL) {
9608 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009609 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9610 XML_SCHEMAS_QUALIF_ATTR);
9611 HFAILURE;
9612 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009613 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009614 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009615 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009616 "(qualified | unqualified)", val, NULL, NULL, NULL);
9617 }
9618 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009619 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009620 if (attr != NULL) {
9621 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009622 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009623 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9624 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9625 -1,
9626 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009627 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9628 HFAILURE;
9629 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009630 xmlSchemaPSimpleTypeErr(ctxt,
9631 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009632 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009633 "(#all | List of (extension | restriction | list | union))",
9634 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009635 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009636 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009637 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009638 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009639 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9640 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009641 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9642 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009643 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9644 HFAILURE;
9645 if (res != 0) {
9646 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009647 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009648 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 "(#all | List of (extension | restriction | substitution))",
9650 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009651 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009652 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009653
9654exit:
9655 if (oldErrs != ctxt->nberrors)
9656 res = ctxt->err;
9657 return(res);
9658exit_failure:
9659 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009660}
9661
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009662/**
9663 * xmlSchemaParseSchemaTopLevel:
9664 * @ctxt: a schema validation context
9665 * @schema: the schemas
9666 * @nodes: the list of top level nodes
9667 *
9668 * Returns the internal XML Schema structure built from the resource or
9669 * NULL in case of error
9670 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009671static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009672xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9673 xmlSchemaPtr schema, xmlNodePtr nodes)
9674{
9675 xmlNodePtr child;
9676 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009677 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009678
9679 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009680 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009681
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009682 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009683 child = nodes;
9684 while ((IS_SCHEMA(child, "include")) ||
9685 (IS_SCHEMA(child, "import")) ||
9686 (IS_SCHEMA(child, "redefine")) ||
9687 (IS_SCHEMA(child, "annotation"))) {
9688 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009689 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009690 if (schema->annot == NULL)
9691 schema->annot = annot;
9692 else
9693 xmlSchemaFreeAnnot(annot);
9694 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009695 tmpOldErrs = ctxt->nberrors;
9696 res = xmlSchemaParseImport(ctxt, schema, child);
9697 HFAILURE;
9698 HSTOP(ctxt);
9699 if (tmpOldErrs != ctxt->nberrors)
9700 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009701 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009702 tmpOldErrs = ctxt->nberrors;
9703 res = xmlSchemaParseInclude(ctxt, schema, child);
9704 HFAILURE;
9705 HSTOP(ctxt);
9706 if (tmpOldErrs != ctxt->nberrors)
9707 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009708 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009709 tmpOldErrs = ctxt->nberrors;
9710 res = xmlSchemaParseRedefine(ctxt, schema, child);
9711 HFAILURE;
9712 HSTOP(ctxt);
9713 if (tmpOldErrs != ctxt->nberrors)
9714 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009715 }
9716 child = child->next;
9717 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009718 /*
9719 * URGENT TODO: Change the functions to return int results.
9720 * We need especially to catch internal errors.
9721 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009722 while (child != NULL) {
9723 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009724 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009725 child = child->next;
9726 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009727 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009728 child = child->next;
9729 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009730 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009731 child = child->next;
9732 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009733 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009734 child = child->next;
9735 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009736 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009737 child = child->next;
9738 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009739 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009740 child = child->next;
9741 } else if (IS_SCHEMA(child, "notation")) {
9742 xmlSchemaParseNotation(ctxt, schema, child);
9743 child = child->next;
9744 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009745 xmlSchemaPContentErr(ctxt,
9746 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009747 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009748 NULL, "((include | import | redefine | annotation)*, "
9749 "(((simpleType | complexType | group | attributeGroup) "
9750 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009751 child = child->next;
9752 }
9753 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009754 /*
9755 * TODO: We should add all annotations.
9756 */
9757 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009758 if (schema->annot == NULL)
9759 schema->annot = annot;
9760 else
9761 xmlSchemaFreeAnnot(annot);
9762 child = child->next;
9763 }
9764 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009765exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009766 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009767 if (oldErrs != ctxt->nberrors)
9768 res = ctxt->err;
9769 return(res);
9770exit_failure:
9771 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009772}
9773
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009774static xmlSchemaSchemaRelationPtr
9775xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009776{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009777 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009778
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009779 ret = (xmlSchemaSchemaRelationPtr)
9780 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009781 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009782 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009783 return(NULL);
9784 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009785 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009786 return(ret);
9787}
9788
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009789#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009790static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009792{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009793 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009794}
9795#endif
9796
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009797static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009798xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9799{
9800 xmlSchemaRedefPtr prev;
9801
9802 while (redef != NULL) {
9803 prev = redef;
9804 redef = redef->next;
9805 xmlFree(prev);
9806 }
9807}
9808
9809static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009810xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9811{
9812 /*
9813 * After the construction context has been freed, there will be
9814 * no schema graph available any more. Only the schema buckets
9815 * will stay alive, which are put into the "schemasImports" and
9816 * "includes" slots of the xmlSchema.
9817 */
9818 if (con->buckets != NULL)
9819 xmlSchemaItemListFree(con->buckets);
9820 if (con->pending != NULL)
9821 xmlSchemaItemListFree(con->pending);
9822 if (con->substGroups != NULL)
9823 xmlHashFree(con->substGroups,
9824 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009825 if (con->redefs != NULL)
9826 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009827 if (con->dict != NULL)
9828 xmlDictFree(con->dict);
9829 xmlFree(con);
9830}
9831
9832static xmlSchemaConstructionCtxtPtr
9833xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9834{
9835 xmlSchemaConstructionCtxtPtr ret;
9836
9837 ret = (xmlSchemaConstructionCtxtPtr)
9838 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9839 if (ret == NULL) {
9840 xmlSchemaPErrMemory(NULL,
9841 "allocating schema construction context", NULL);
9842 return (NULL);
9843 }
9844 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9845
9846 ret->buckets = xmlSchemaItemListCreate();
9847 if (ret->buckets == NULL) {
9848 xmlSchemaPErrMemory(NULL,
9849 "allocating list of schema buckets", NULL);
9850 xmlFree(ret);
9851 return (NULL);
9852 }
9853 ret->pending = xmlSchemaItemListCreate();
9854 if (ret->pending == NULL) {
9855 xmlSchemaPErrMemory(NULL,
9856 "allocating list of pending global components", NULL);
9857 xmlSchemaConstructionCtxtFree(ret);
9858 return (NULL);
9859 }
9860 ret->dict = dict;
9861 xmlDictReference(dict);
9862 return(ret);
9863}
9864
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009865static xmlSchemaParserCtxtPtr
9866xmlSchemaParserCtxtCreate(void)
9867{
9868 xmlSchemaParserCtxtPtr ret;
9869
9870 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9871 if (ret == NULL) {
9872 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9873 NULL);
9874 return (NULL);
9875 }
9876 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9877 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009878 ret->attrProhibs = xmlSchemaItemListCreate();
9879 if (ret->attrProhibs == NULL) {
9880 xmlFree(ret);
9881 return(NULL);
9882 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009883 return(ret);
9884}
9885
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009886/**
9887 * xmlSchemaNewParserCtxtUseDict:
9888 * @URL: the location of the schema
9889 * @dict: the dictionary to be used
9890 *
9891 * Create an XML Schemas parse context for that file/resource expected
9892 * to contain an XML Schemas file.
9893 *
9894 * Returns the parser context or NULL in case of error
9895 */
9896static xmlSchemaParserCtxtPtr
9897xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9898{
9899 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009900
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009901 ret = xmlSchemaParserCtxtCreate();
9902 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009903 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009904 ret->dict = dict;
9905 xmlDictReference(dict);
9906 if (URL != NULL)
9907 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009908 return (ret);
9909}
9910
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009911static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009912xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9913{
9914 if (vctxt->pctxt == NULL) {
9915 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009916 vctxt->pctxt =
9917 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009918 else
9919 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9920 if (vctxt->pctxt == NULL) {
9921 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9922 "failed to create a temp. parser context");
9923 return (-1);
9924 }
9925 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009926 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
9927 vctxt->warning, vctxt->userData);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009928 }
9929 return (0);
9930}
9931
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009932/**
9933 * xmlSchemaGetSchemaBucket:
9934 * @pctxt: the schema parser context
9935 * @schemaLocation: the URI of the schema document
9936 *
9937 * Returns a schema bucket if it was already parsed.
9938 *
9939 * Returns a schema bucket if it was already parsed from
9940 * @schemaLocation, NULL otherwise.
9941 */
9942static xmlSchemaBucketPtr
9943xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9944 const xmlChar *schemaLocation)
9945{
9946 xmlSchemaBucketPtr cur;
9947 xmlSchemaItemListPtr list;
9948
9949 list = pctxt->constructor->buckets;
9950 if (list->nbItems == 0)
9951 return(NULL);
9952 else {
9953 int i;
9954 for (i = 0; i < list->nbItems; i++) {
9955 cur = (xmlSchemaBucketPtr) list->items[i];
9956 /* Pointer comparison! */
9957 if (cur->schemaLocation == schemaLocation)
9958 return(cur);
9959 }
9960 }
9961 return(NULL);
9962}
9963
9964static xmlSchemaBucketPtr
9965xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9966 const xmlChar *schemaLocation,
9967 const xmlChar *targetNamespace)
9968{
9969 xmlSchemaBucketPtr cur;
9970 xmlSchemaItemListPtr list;
9971
9972 list = pctxt->constructor->buckets;
9973 if (list->nbItems == 0)
9974 return(NULL);
9975 else {
9976 int i;
9977 for (i = 0; i < list->nbItems; i++) {
9978 cur = (xmlSchemaBucketPtr) list->items[i];
9979 /* Pointer comparison! */
9980 if ((cur->origTargetNamespace == NULL) &&
9981 (cur->schemaLocation == schemaLocation) &&
9982 (cur->targetNamespace == targetNamespace))
9983 return(cur);
9984 }
9985 }
9986 return(NULL);
9987}
9988
9989
9990#define IS_BAD_SCHEMA_DOC(b) \
9991 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
9992
9993static xmlSchemaBucketPtr
9994xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
9995 const xmlChar *targetNamespace,
9996 int imported)
9997{
9998 xmlSchemaBucketPtr cur;
9999 xmlSchemaItemListPtr list;
10000
10001 list = pctxt->constructor->buckets;
10002 if (list->nbItems == 0)
10003 return(NULL);
10004 else {
10005 int i;
10006 for (i = 0; i < list->nbItems; i++) {
10007 cur = (xmlSchemaBucketPtr) list->items[i];
10008 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10009 (cur->origTargetNamespace == targetNamespace) &&
10010 ((imported && cur->imported) ||
10011 ((!imported) && (!cur->imported))))
10012 return(cur);
10013 }
10014 }
10015 return(NULL);
10016}
10017
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010018static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010019xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10020 xmlSchemaPtr schema,
10021 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010022{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010023 int oldFlags;
10024 xmlDocPtr oldDoc;
10025 xmlNodePtr node;
10026 int ret, oldErrs;
10027 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10028
10029 /*
10030 * Save old values; reset the *main* schema.
10031 * URGENT TODO: This is not good; move the per-document information
10032 * to the parser.
10033 */
10034 oldFlags = schema->flags;
10035 oldDoc = schema->doc;
10036 if (schema->flags != 0)
10037 xmlSchemaClearSchemaDefaults(schema);
10038 schema->doc = bucket->doc;
10039 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10040 pctxt->schema = schema;
10041 /*
10042 * Keep the current target namespace on the parser *not* on the
10043 * main schema.
10044 */
10045 pctxt->targetNamespace = bucket->targetNamespace;
10046 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010047
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010048 if ((bucket->targetNamespace != NULL) &&
10049 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010050 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010051 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010052 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010053 pctxt->isS4S = 1;
10054 }
10055 /* Mark it as parsed, even if parsing fails. */
10056 bucket->parsed++;
10057 /* Compile the schema doc. */
10058 node = xmlDocGetRootElement(bucket->doc);
10059 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10060 if (ret != 0)
10061 goto exit;
10062 /* An empty schema; just get out. */
10063 if (node->children == NULL)
10064 goto exit;
10065 oldErrs = pctxt->nberrors;
10066 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10067 if (ret != 0)
10068 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010069 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010070 * TODO: Not nice, but I'm not 100% sure we will get always an error
10071 * as a result of the obove functions; so better rely on pctxt->err
10072 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010073 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010074 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10075 ret = pctxt->err;
10076 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010077 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010078
10079exit:
10080 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10081 /* Restore schema values. */
10082 schema->doc = oldDoc;
10083 schema->flags = oldFlags;
10084 return(ret);
10085}
10086
10087static int
10088xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10089 xmlSchemaPtr schema,
10090 xmlSchemaBucketPtr bucket)
10091{
10092 xmlSchemaParserCtxtPtr newpctxt;
10093 int res = 0;
10094
10095 if (bucket == NULL)
10096 return(0);
10097 if (bucket->parsed) {
10098 PERROR_INT("xmlSchemaParseNewDoc",
10099 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010100 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010101 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010102 if (bucket->doc == NULL) {
10103 PERROR_INT("xmlSchemaParseNewDoc",
10104 "parsing a schema doc, but there's no doc");
10105 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010106 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010107 if (pctxt->constructor == NULL) {
10108 PERROR_INT("xmlSchemaParseNewDoc",
10109 "no constructor");
10110 return(-1);
10111 }
10112 /* Create and init the temporary parser context. */
10113 newpctxt = xmlSchemaNewParserCtxtUseDict(
10114 (const char *) bucket->schemaLocation, pctxt->dict);
10115 if (newpctxt == NULL)
10116 return(-1);
10117 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010118 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010119 * TODO: Can we avoid that the parser knows about the main schema?
10120 * It would be better if he knows about the current schema bucket
10121 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010122 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010123 newpctxt->schema = schema;
10124 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
10125 pctxt->userData);
10126 newpctxt->counter = pctxt->counter;
10127
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010128
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010129 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10130
10131 /* Channel back errors and cleanup the temporary parser context. */
10132 if (res != 0)
10133 pctxt->err = res;
10134 pctxt->nberrors += newpctxt->nberrors;
10135 pctxt->counter = newpctxt->counter;
10136 newpctxt->constructor = NULL;
10137 /* Free the parser context. */
10138 xmlSchemaFreeParserCtxt(newpctxt);
10139 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010140}
William M. Brack2f2a6632004-08-20 23:09:47 +000010141
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010142static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010143xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10144 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010145{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010146 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010147
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010148 if (cur == NULL) {
10149 bucket->relations = rel;
10150 return;
10151 }
10152 while (cur->next != NULL)
10153 cur = cur->next;
10154 cur->next = rel;
10155}
10156
10157
10158static const xmlChar *
10159xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10160 xmlNodePtr ctxtNode)
10161{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010162 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010163 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010164 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010165 if (location != NULL) {
10166 if (ctxtNode == NULL)
10167 return(location);
10168 else {
10169 xmlChar *base, *URI;
10170 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010171
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010172 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10173 if (base == NULL) {
10174 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10175 } else {
10176 URI = xmlBuildURI(location, base);
10177 xmlFree(base);
10178 }
10179 if (URI != NULL) {
10180 ret = xmlDictLookup(dict, URI, -1);
10181 xmlFree(URI);
10182 return(ret);
10183 }
10184 }
10185 }
10186 return(NULL);
10187}
10188
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010189
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010190
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010191/**
10192 * xmlSchemaAddSchemaDoc:
10193 * @pctxt: a schema validation context
10194 * @schema: the schema being built
10195 * @node: a subtree containing XML Schema informations
10196 *
10197 * Parse an included (and to-be-redefined) XML schema document.
10198 *
10199 * Returns 0 on success, a positive error code on errors and
10200 * -1 in case of an internal or API error.
10201 */
10202
10203static int
10204xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10205 int type, /* import or include or redefine */
10206 const xmlChar *schemaLocation,
10207 xmlDocPtr schemaDoc,
10208 const char *schemaBuffer,
10209 int schemaBufferLen,
10210 xmlNodePtr invokingNode,
10211 const xmlChar *sourceTargetNamespace,
10212 const xmlChar *importNamespace,
10213 xmlSchemaBucketPtr *bucket)
10214{
10215 const xmlChar *targetNamespace = NULL;
10216 xmlSchemaSchemaRelationPtr relation = NULL;
10217 xmlDocPtr doc = NULL;
10218 int res = 0, err = 0, located = 0, preserveDoc = 0;
10219 xmlSchemaBucketPtr bkt = NULL;
10220
10221 if (bucket != NULL)
10222 *bucket = NULL;
10223
10224 switch (type) {
10225 case XML_SCHEMA_SCHEMA_IMPORT:
10226 case XML_SCHEMA_SCHEMA_MAIN:
10227 err = XML_SCHEMAP_SRC_IMPORT;
10228 break;
10229 case XML_SCHEMA_SCHEMA_INCLUDE:
10230 err = XML_SCHEMAP_SRC_INCLUDE;
10231 break;
10232 case XML_SCHEMA_SCHEMA_REDEFINE:
10233 err = XML_SCHEMAP_SRC_REDEFINE;
10234 break;
10235 }
10236
10237
10238 /* Special handling for the main schema:
10239 * skip the location and relation logic and just parse the doc.
10240 * We need just a bucket to be returned in this case.
10241 */
10242 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10243 goto doc_load;
10244
10245 /* Note that we expect the location to be an absulute URI. */
10246 if (schemaLocation != NULL) {
10247 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10248 if ((bkt != NULL) &&
10249 (pctxt->constructor->bucket == bkt)) {
10250 /* Report self-imports/inclusions/redefinitions. */
10251
10252 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10253 invokingNode, NULL,
10254 "The schema must not import/include/redefine itself",
10255 NULL, NULL);
10256 goto exit;
10257 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010258 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010259 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010260 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010261 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010262 relation = xmlSchemaSchemaRelationCreate();
10263 if (relation == NULL)
10264 return(-1);
10265 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10266 relation);
10267 relation->type = type;
10268
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010269 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010270 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010271 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010272 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010273 relation->importNamespace = importNamespace;
10274 if (schemaLocation == NULL) {
10275 /*
10276 * No location; this is just an import of the namespace.
10277 * Note that we don't assign a bucket to the relation
10278 * in this case.
10279 */
10280 goto exit;
10281 }
10282 targetNamespace = importNamespace;
10283 }
10284
10285 /* Did we already fetch the doc? */
10286 if (bkt != NULL) {
10287 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010288 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010289 /* We included/redefined and then try to import a schema. */
10290 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10291 invokingNode, NULL,
10292 "The schema document '%s' cannot be imported, since "
10293 "it was already included or redefined",
10294 schemaLocation, NULL);
10295 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010296 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010297 /* We imported and then try to include/redefine a schema. */
10298 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10299 invokingNode, NULL,
10300 "The schema document '%s' cannot be included or "
10301 "redefined, since it was already imported",
10302 schemaLocation, NULL);
10303 goto exit;
10304 }
10305 }
10306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010307 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010308 /*
10309 * Given that the schemaLocation [attribute] is only a hint, it is open
10310 * to applications to ignore all but the first <import> for a given
10311 * namespace, regardless of the ·actual value· of schemaLocation, but
10312 * such a strategy risks missing useful information when new
10313 * schemaLocations are offered.
10314 *
10315 * We will use the first <import> that comes with a location.
10316 * Further <import>s *with* a location, will result in an error.
10317 * TODO: Better would be to just report a warning here, but
10318 * we'll try it this way until someone complains.
10319 *
10320 * Schema Document Location Strategy:
10321 * 3 Based on the namespace name, identify an existing schema document,
10322 * either as a resource which is an XML document or a <schema> element
10323 * information item, in some local schema repository;
10324 * 5 Attempt to resolve the namespace name to locate such a resource.
10325 *
10326 * NOTE: (3) and (5) are not supported.
10327 */
10328 if (bkt != NULL) {
10329 relation->bucket = bkt;
10330 goto exit;
10331 }
10332 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10333 importNamespace, 1);
10334
10335 if (bkt != NULL) {
10336 relation->bucket = bkt;
10337 if (bkt->schemaLocation == NULL) {
10338 /* First given location of the schema; load the doc. */
10339 bkt->schemaLocation = schemaLocation;
10340 } else {
10341 if (!xmlStrEqual(schemaLocation,
10342 bkt->schemaLocation)) {
10343 /*
10344 * Additional location given; just skip it.
10345 * URGENT TODO: We should report a warning here.
10346 * res = XML_SCHEMAP_SRC_IMPORT;
10347 */
10348 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10349 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10350 invokingNode, NULL,
10351 "Skipping import of schema located at '%s' for the "
10352 "namespace '%s', since this namespace was already "
10353 "imported with the schema located at '%s'",
10354 schemaLocation, importNamespace, bkt->schemaLocation);
10355 }
10356 goto exit;
10357 }
10358 }
10359 /*
10360 * No bucket + first location: load the doc and create a
10361 * bucket.
10362 */
10363 } else {
10364 /* <include> and <redefine> */
10365 if (bkt != NULL) {
10366
10367 if ((bkt->origTargetNamespace == NULL) &&
10368 (bkt->targetNamespace != sourceTargetNamespace)) {
10369 xmlSchemaBucketPtr chamel;
10370
10371 /*
10372 * Chameleon include/redefine: skip loading only if it was
10373 * aleady build for the targetNamespace of the including
10374 * schema.
10375 */
10376 /*
10377 * URGENT TODO: If the schema is a chameleon-include then copy
10378 * the components into the including schema and modify the
10379 * targetNamespace of those components, do nothing otherwise.
10380 * NOTE: This is currently worked-around by compiling the
10381 * chameleon for every destinct including targetNamespace; thus
10382 * not performant at the moment.
10383 * TODO: Check when the namespace in wildcards for chameleons
10384 * needs to be converted: before we built wildcard intersections
10385 * or after.
10386 * Answer: after!
10387 */
10388 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10389 schemaLocation, sourceTargetNamespace);
10390 if (chamel != NULL) {
10391 /* A fitting chameleon was already parsed; NOP. */
10392 relation->bucket = chamel;
10393 goto exit;
10394 }
10395 /*
10396 * We need to parse the chameleon again for a different
10397 * targetNamespace.
10398 * CHAMELEON TODO: Optimize this by only parsing the
10399 * chameleon once, and then copying the components to
10400 * the new targetNamespace.
10401 */
10402 bkt = NULL;
10403 } else {
10404 relation->bucket = bkt;
10405 goto exit;
10406 }
10407 }
10408 }
10409 if ((bkt != NULL) && (bkt->doc != NULL)) {
10410 PERROR_INT("xmlSchemaAddSchemaDoc",
10411 "trying to load a schema doc, but a doc is already "
10412 "assigned to the schema bucket");
10413 goto exit_failure;
10414 }
10415
10416doc_load:
10417 /*
10418 * Load the document.
10419 */
10420 if (schemaDoc != NULL) {
10421 doc = schemaDoc;
10422 /* Don' free this one, since it was provided by the caller. */
10423 preserveDoc = 1;
10424 /* TODO: Does the context or the doc hold the location? */
10425 if (schemaDoc->URL != NULL)
10426 schemaLocation = xmlDictLookup(pctxt->dict,
10427 schemaDoc->URL, -1);
10428
10429 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10430 xmlParserCtxtPtr parserCtxt;
10431
10432 parserCtxt = xmlNewParserCtxt();
10433 if (parserCtxt == NULL) {
10434 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10435 "allocating a parser context", NULL);
10436 goto exit_failure;
10437 }
10438 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10439 /*
10440 * TODO: Do we have to burden the schema parser dict with all
10441 * the content of the schema doc?
10442 */
10443 xmlDictFree(parserCtxt->dict);
10444 parserCtxt->dict = pctxt->dict;
10445 xmlDictReference(parserCtxt->dict);
10446 }
10447 if (schemaLocation != NULL) {
10448 /* Parse from file. */
10449 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10450 NULL, SCHEMAS_PARSE_OPTIONS);
10451 } else if (schemaBuffer != NULL) {
10452 /* Parse from memory buffer. */
10453 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10454 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10455 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10456 if (doc != NULL)
10457 doc->URL = schemaLocation;
10458 }
10459 /*
10460 * For <import>:
10461 * 2.1 The referent is (a fragment of) a resource which is an
10462 * XML document (see clause 1.1), which in turn corresponds to
10463 * a <schema> element information item in a well-formed information
10464 * set, which in turn corresponds to a valid schema.
10465 * TODO: (2.1) fragments of XML documents are not supported.
10466 *
10467 * 2.2 The referent is a <schema> element information item in
10468 * a well-formed information set, which in turn corresponds
10469 * to a valid schema.
10470 * TODO: (2.2) is not supported.
10471 */
10472 if (doc == NULL) {
10473 xmlErrorPtr lerr;
10474 lerr = xmlGetLastError();
10475 /*
10476 * Check if this a parser error, or if the document could
10477 * just not be located.
10478 * TODO: Try to find specific error codes to react only on
10479 * localisation failures.
10480 */
10481 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10482 /*
10483 * We assume a parser error here.
10484 */
10485 located = 1;
10486 /* TODO: Error code ?? */
10487 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10488 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10489 invokingNode, NULL,
10490 "Failed to parse the XML resource '%s'",
10491 schemaLocation, NULL);
10492 }
10493 }
10494 xmlFreeParserCtxt(parserCtxt);
10495 if ((doc == NULL) && located)
10496 goto exit_error;
10497 } else {
10498 xmlSchemaPErr(pctxt, NULL,
10499 XML_SCHEMAP_NOTHING_TO_PARSE,
10500 "No information for parsing was provided with the "
10501 "given schema parser context.\n",
10502 NULL, NULL);
10503 goto exit_failure;
10504 }
10505 /*
10506 * Preprocess the document.
10507 */
10508 if (doc != NULL) {
10509 xmlNodePtr docElem = NULL;
10510
10511 located = 1;
10512 docElem = xmlDocGetRootElement(doc);
10513 if (docElem == NULL) {
10514 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10515 invokingNode, NULL,
10516 "The document '%s' has no document element",
10517 schemaLocation, NULL);
10518 xmlFreeDoc(doc);
10519 doc = NULL;
10520 goto exit_error;
10521 }
10522 /*
10523 * Remove all the blank text nodes.
10524 */
10525 xmlSchemaCleanupDoc(pctxt, docElem);
10526 /*
10527 * Check the schema's top level element.
10528 */
10529 if (!IS_SCHEMA(docElem, "schema")) {
10530 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10531 invokingNode, NULL,
10532 "The XML document '%s' is not a schema document",
10533 schemaLocation, NULL);
10534 xmlFreeDoc(doc);
10535 doc = NULL;
10536 goto exit_error;
10537 }
10538 /*
10539 * Note that we don't apply a type check for the
10540 * targetNamespace value here.
10541 */
10542 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10543 "targetNamespace");
10544 }
10545
10546/* after_doc_loading: */
10547 if ((bkt == NULL) && located) {
10548 /* Only create a bucket if the schema was located. */
10549 bkt = xmlSchemaBucketCreate(pctxt, type,
10550 targetNamespace);
10551 if (bkt == NULL)
10552 goto exit_failure;
10553 }
10554 if (bkt != NULL) {
10555 bkt->schemaLocation = schemaLocation;
10556 bkt->located = located;
10557 if (doc != NULL) {
10558 bkt->doc = doc;
10559 bkt->targetNamespace = targetNamespace;
10560 bkt->origTargetNamespace = targetNamespace;
10561 if (preserveDoc)
10562 bkt->preserveDoc = 1;
10563 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010564 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010565 bkt->imported++;
10566 /*
10567 * Add it to the graph of schemas.
10568 */
10569 if (relation != NULL)
10570 relation->bucket = bkt;
10571 }
10572
10573exit:
10574 /*
10575 * Return the bucket explicitely; this is needed for the
10576 * main schema.
10577 */
10578 if (bucket != NULL)
10579 *bucket = bkt;
10580 return (0);
10581
10582exit_error:
10583 if ((doc != NULL) && (! preserveDoc)) {
10584 xmlFreeDoc(doc);
10585 if (bkt != NULL)
10586 bkt->doc = NULL;
10587 }
10588 return(pctxt->err);
10589
10590exit_failure:
10591 if ((doc != NULL) && (! preserveDoc)) {
10592 xmlFreeDoc(doc);
10593 if (bkt != NULL)
10594 bkt->doc = NULL;
10595 }
10596 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010597}
10598
William M. Brack2f2a6632004-08-20 23:09:47 +000010599/**
10600 * xmlSchemaParseImport:
10601 * @ctxt: a schema validation context
10602 * @schema: the schema being built
10603 * @node: a subtree containing XML Schema informations
10604 *
10605 * parse a XML schema Import definition
10606 * *WARNING* this interface is highly subject to change
10607 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010608 * Returns 0 in case of success, a positive error code if
10609 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010610 */
10611static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010612xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010613 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010614{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010615 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010616 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10617 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010618 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010619 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010620 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010621
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010622 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010623 return (-1);
10624
10625 /*
10626 * Check for illegal attributes.
10627 */
10628 attr = node->properties;
10629 while (attr != NULL) {
10630 if (attr->ns == NULL) {
10631 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10632 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10633 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010634 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010635 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010636 }
10637 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010638 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010639 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010640 }
10641 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010642 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010643 /*
10644 * Extract and validate attributes.
10645 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010646 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010647 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010648 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010649 xmlSchemaPSimpleTypeErr(pctxt,
10650 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010651 NULL, node,
10652 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010653 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010654 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010655 }
10656
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010657 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010658 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010659 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010660 xmlSchemaPSimpleTypeErr(pctxt,
10661 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010662 NULL, node,
10663 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010664 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010665 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010666 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010667 /*
10668 * And now for the children...
10669 */
10670 child = node->children;
10671 if (IS_SCHEMA(child, "annotation")) {
10672 /*
10673 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010674 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010675 */
10676 child = child->next;
10677 }
10678 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010679 xmlSchemaPContentErr(pctxt,
10680 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010681 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010682 "(annotation?)");
10683 }
10684 /*
10685 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010686 *
10687 * Note that it is important to use the original @targetNamespace
10688 * (or none at all), to rule out imports of schemas _with_ a
10689 * @targetNamespace if the importing schema is a chameleon schema
10690 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010691 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010692 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010693 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010694 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010695 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10696 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010697 * targetNamespace [attribute].
10698 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010699 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010700 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010701 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010702 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010703 "The value of the attribute 'namespace' must not match "
10704 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010705 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010706 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010707 }
10708 } else {
10709 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010710 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010711 * <schema> must have a targetNamespace [attribute].
10712 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010713 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010714 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010715 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010716 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010717 "The attribute 'namespace' must be existent if "
10718 "the importing schema has no target namespace",
10719 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010720 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010721 }
10722 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010723 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010724 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010725 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010726 if (schemaLocation != NULL)
10727 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10728 schemaLocation, node);
10729 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010730 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010731 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010732
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010733 if (ret != 0)
10734 return(ret);
10735
10736 /*
10737 * For <import>: "It is *not* an error for the application
10738 * schema reference strategy to fail."
10739 * So just don't parse if no schema document was found.
10740 * Note that we will get no bucket if the schema could not be
10741 * located or if there was no schemaLocation.
10742 */
10743 if ((bucket == NULL) && (schemaLocation != NULL)) {
10744 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10745 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10746 node, NULL,
10747 "Failed to locate a schema at location '%s'. "
10748 "Skipping the import", schemaLocation, NULL, NULL);
10749 }
10750
10751 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10752 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10753 }
10754
10755 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010756}
10757
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010758static int
10759xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10760 xmlSchemaPtr schema,
10761 xmlNodePtr node,
10762 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010763 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010764{
10765 xmlAttrPtr attr;
10766
10767 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10768 (schemaLocation == NULL))
10769 return (-1);
10770
10771 *schemaLocation = NULL;
10772 /*
10773 * Check for illegal attributes.
10774 * Applies for both <include> and <redefine>.
10775 */
10776 attr = node->properties;
10777 while (attr != NULL) {
10778 if (attr->ns == NULL) {
10779 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10780 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10781 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010782 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010783 }
10784 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10785 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010786 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010787 }
10788 attr = attr->next;
10789 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010790 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010791 /*
10792 * Preliminary step, extract the URI-Reference and make an URI
10793 * from the base.
10794 */
10795 /*
10796 * Attribute "schemaLocation" is mandatory.
10797 */
10798 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10799 if (attr != NULL) {
10800 xmlChar *base = NULL;
10801 xmlChar *uri = NULL;
10802
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010803 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010804 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10805 (const xmlChar **) schemaLocation) != 0)
10806 goto exit_error;
10807 base = xmlNodeGetBase(node->doc, node);
10808 if (base == NULL) {
10809 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10810 } else {
10811 uri = xmlBuildURI(*schemaLocation, base);
10812 xmlFree(base);
10813 }
10814 if (uri == NULL) {
10815 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10816 "could not build an URI from the schemaLocation")
10817 goto exit_failure;
10818 }
10819 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10820 xmlFree(uri);
10821 } else {
10822 xmlSchemaPMissingAttrErr(pctxt,
10823 XML_SCHEMAP_S4S_ATTR_MISSING,
10824 NULL, node, "schemaLocation", NULL);
10825 goto exit_error;
10826 }
10827 /*
10828 * Report self-inclusion and self-redefinition.
10829 */
10830 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010831 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010832 xmlSchemaPCustomErr(pctxt,
10833 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010834 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010835 "The schema document '%s' cannot redefine itself.",
10836 *schemaLocation);
10837 } else {
10838 xmlSchemaPCustomErr(pctxt,
10839 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010840 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010841 "The schema document '%s' cannot include itself.",
10842 *schemaLocation);
10843 }
10844 goto exit_error;
10845 }
10846
10847 return(0);
10848exit_error:
10849 return(pctxt->err);
10850exit_failure:
10851 return(-1);
10852}
10853
10854static int
10855xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10856 xmlSchemaPtr schema,
10857 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010858 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010859{
10860 xmlNodePtr child = NULL;
10861 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010862 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010863 int isChameleon = 0, wasChameleon = 0;
10864 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010865
10866 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10867 return (-1);
10868
10869 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010870 * Parse attributes. Note that the returned schemaLocation will
10871 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010872 */
10873 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010874 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010875 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010876 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010877 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010878 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010879 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010880 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10881 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010882 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010883 return(res);
10884 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010885 * If we get no schema bucket back, then this means that the schema
10886 * document could not be located or was broken XML or was not
10887 * a schema document.
10888 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010889 if ((bucket == NULL) || (bucket->doc == NULL)) {
10890 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10891 /*
10892 * WARNING for <include>:
10893 * We will raise an error if the schema cannot be located
10894 * for inclusions, since the that was the feedback from the
10895 * schema people. I.e. the following spec piece will *not* be
10896 * satisfied:
10897 * SPEC src-include: "It is not an error for the ·actual value· of the
10898 * schemaLocation [attribute] to fail to resolve it all, in which
10899 * case no corresponding inclusion is performed.
10900 * So do we need a warning report here?"
10901 */
10902 res = XML_SCHEMAP_SRC_INCLUDE;
10903 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10904 node, NULL,
10905 "Failed to load the document '%s' for inclusion",
10906 schemaLocation, NULL);
10907 } else {
10908 /*
10909 * NOTE: This was changed to raise an error even if no redefinitions
10910 * are specified.
10911 *
10912 * SPEC src-redefine (1)
10913 * "If there are any element information items among the [children]
10914 * other than <annotation> then the ·actual value· of the
10915 * schemaLocation [attribute] must successfully resolve."
10916 * TODO: Ask the WG if a the location has always to resolve
10917 * here as well!
10918 */
10919 res = XML_SCHEMAP_SRC_REDEFINE;
10920 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10921 node, NULL,
10922 "Failed to load the document '%s' for redefinition",
10923 schemaLocation, NULL);
10924 }
10925 } else {
10926 /*
10927 * Check targetNamespace sanity before parsing the new schema.
10928 * TODO: Note that we won't check further content if the
10929 * targetNamespace was bad.
10930 */
10931 if (bucket->origTargetNamespace != NULL) {
10932 /*
10933 * SPEC src-include (2.1)
10934 * "SII has a targetNamespace [attribute], and its ·actual
10935 * value· is identical to the ·actual value· of the targetNamespace
10936 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10937 */
10938 if (pctxt->targetNamespace == NULL) {
10939 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10940 XML_SCHEMAP_SRC_INCLUDE,
10941 node, NULL,
10942 "The target namespace of the included/redefined schema "
10943 "'%s' has to be absent, since the including/redefining "
10944 "schema has no target namespace",
10945 schemaLocation, NULL);
10946 goto exit_error;
10947 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10948 pctxt->targetNamespace)) {
10949 /* TODO: Change error function. */
10950 xmlSchemaPCustomErrExt(pctxt,
10951 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010952 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010953 "The target namespace '%s' of the included/redefined "
10954 "schema '%s' differs from '%s' of the "
10955 "including/redefining schema",
10956 bucket->origTargetNamespace, schemaLocation,
10957 pctxt->targetNamespace);
10958 goto exit_error;
10959 }
10960 } else if (pctxt->targetNamespace != NULL) {
10961 /*
10962 * Chameleons: the original target namespace will
10963 * differ from the resulting namespace.
10964 */
10965 isChameleon = 1;
10966 if (bucket->parsed &&
10967 (bucket->targetNamespace != pctxt->targetNamespace)) {
10968 /*
10969 * This is a sanity check, I dunno yet if this can happen.
10970 */
10971 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10972 "trying to use an already parsed schema for a "
10973 "different targetNamespace");
10974 return(-1);
10975 }
10976 bucket->targetNamespace = pctxt->targetNamespace;
10977 }
10978 }
10979 /*
10980 * Parse the schema.
10981 */
10982 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
10983 if (isChameleon) {
10984 /* TODO: Get rid of this flag on the schema itself. */
10985 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
10986 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
10987 } else
10988 wasChameleon = 1;
10989 }
10990 xmlSchemaParseNewDoc(pctxt, schema, bucket);
10991 /* Restore chameleon flag. */
10992 if (isChameleon && (!wasChameleon))
10993 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
10994 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010995 /*
10996 * And now for the children...
10997 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010998 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010999 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011000 /*
11001 * Parse (simpleType | complexType | group | attributeGroup))*
11002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011003 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011004 /*
11005 * How to proceed if the redefined schema was not located?
11006 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011007 pctxt->isRedefine = 1;
11008 while (IS_SCHEMA(child, "annotation") ||
11009 IS_SCHEMA(child, "simpleType") ||
11010 IS_SCHEMA(child, "complexType") ||
11011 IS_SCHEMA(child, "group") ||
11012 IS_SCHEMA(child, "attributeGroup")) {
11013 if (IS_SCHEMA(child, "annotation")) {
11014 /*
11015 * TODO: discard or not?
11016 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011017 } else if (IS_SCHEMA(child, "simpleType")) {
11018 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11019 } else if (IS_SCHEMA(child, "complexType")) {
11020 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011021 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011022 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011023 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011024 xmlSchemaParseModelGroupDefinition(pctxt,
11025 schema, child);
11026 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011027 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011028 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11029 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011030 }
11031 child = child->next;
11032 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011033 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011034 pctxt->isRedefine = 0;
11035 } else {
11036 if (IS_SCHEMA(child, "annotation")) {
11037 /*
11038 * TODO: discard or not?
11039 */
11040 child = child->next;
11041 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011042 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011043 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011044 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011045 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11046 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011047 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011048 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11049 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011050 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011051 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011052 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011053 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011054 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011055 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011056
11057exit_error:
11058 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011059}
11060
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011061static int
11062xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11063 xmlNodePtr node)
11064{
11065 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011066#ifndef ENABLE_REDEFINE
11067 TODO
11068 return(0);
11069#endif
11070 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11071 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011072 if (res != 0)
11073 return(res);
11074 return(0);
11075}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011076
11077static int
11078xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11079 xmlNodePtr node)
11080{
11081 int res;
11082
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011083 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11084 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011085 if (res != 0)
11086 return(res);
11087 return(0);
11088}
11089
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011090/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011091 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011092 * @ctxt: a schema validation context
11093 * @schema: the schema being built
11094 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011095 * @type: the "compositor" type
11096 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011097 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011098 * parse a XML schema Sequence definition.
11099 * Applies parts of:
11100 * Schema Representation Constraint:
11101 * Redefinition Constraints and Semantics (src-redefine)
11102 * (6.1), (6.1.1), (6.1.2)
11103 *
11104 * Schema Component Constraint:
11105 * All Group Limited (cos-all-limited) (2)
11106 * TODO: Actually this should go to component-level checks,
11107 * but is done here due to performance. Move it to an other layer
11108 * is schema construction via an API is implemented.
11109 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011110 * *WARNING* this interface is highly subject to change
11111 *
William M. Bracke7091952004-05-11 15:09:58 +000011112 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011113 * 1 in case of success.
11114 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011115static xmlSchemaTreeItemPtr
11116xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11117 xmlNodePtr node, xmlSchemaTypeType type,
11118 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011119{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011120 xmlSchemaModelGroupPtr item;
11121 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011122 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011123 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011124 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011125
11126 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011127 return (NULL);
11128 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011129 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011130 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011131 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011132 if (item == NULL)
11133 return (NULL);
11134
11135 if (withParticle) {
11136 if (type == XML_SCHEMA_TYPE_ALL) {
11137 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011138 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011139 } else {
11140 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011141 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11142 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11143 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011144 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011145 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11146 /*
11147 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011148 */
11149 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11150 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011151 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011152 particle->children = (xmlSchemaTreeItemPtr) item;
11153 /*
11154 * Check for illegal attributes.
11155 */
11156 attr = node->properties;
11157 while (attr != NULL) {
11158 if (attr->ns == NULL) {
11159 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11160 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11161 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011162 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011163 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011164 }
11165 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011166 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011167 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011168 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011169 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011170 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011171 } else {
11172 /*
11173 * Check for illegal attributes.
11174 */
11175 attr = node->properties;
11176 while (attr != NULL) {
11177 if (attr->ns == NULL) {
11178 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011179 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011180 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011181 }
11182 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011183 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011184 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011185 }
11186 attr = attr->next;
11187 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011188 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011189
William M. Brack2f2a6632004-08-20 23:09:47 +000011190 /*
11191 * Extract and validate attributes.
11192 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011193 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011194 /*
11195 * And now for the children...
11196 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011197 child = node->children;
11198 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011199 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011200 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011201 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011202 if (type == XML_SCHEMA_TYPE_ALL) {
11203 xmlSchemaParticlePtr part, last = NULL;
11204
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011205 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011206 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011207 schema, child, &isElemRef, 0);
11208 /*
11209 * SPEC cos-all-limited (2)
11210 * "The {max occurs} of all the particles in the {particles}
11211 * of the ('all') group must be 0 or 1.
11212 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011213 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011214 if (isElemRef)
11215 hasRefs++;
11216 if (part->minOccurs > 1) {
11217 xmlSchemaPCustomErr(ctxt,
11218 XML_SCHEMAP_COS_ALL_LIMITED,
11219 NULL, child,
11220 "Invalid value for minOccurs (must be 0 or 1)",
11221 NULL);
11222 /* Reset to 1. */
11223 part->minOccurs = 1;
11224 }
11225 if (part->maxOccurs > 1) {
11226 xmlSchemaPCustomErr(ctxt,
11227 XML_SCHEMAP_COS_ALL_LIMITED,
11228 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011229 "Invalid value for maxOccurs (must be 0 or 1)",
11230 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011231 /* Reset to 1. */
11232 part->maxOccurs = 1;
11233 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011234 if (last == NULL)
11235 item->children = (xmlSchemaTreeItemPtr) part;
11236 else
11237 last->next = (xmlSchemaTreeItemPtr) part;
11238 last = part;
11239 }
11240 child = child->next;
11241 }
11242 if (child != NULL) {
11243 xmlSchemaPContentErr(ctxt,
11244 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011245 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011246 "(annotation?, (annotation?, element*)");
11247 }
11248 } else {
11249 /* choice + sequence */
11250 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11251
11252 while ((IS_SCHEMA(child, "element")) ||
11253 (IS_SCHEMA(child, "group")) ||
11254 (IS_SCHEMA(child, "any")) ||
11255 (IS_SCHEMA(child, "choice")) ||
11256 (IS_SCHEMA(child, "sequence"))) {
11257
11258 if (IS_SCHEMA(child, "element")) {
11259 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011260 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11261 if (part && isElemRef)
11262 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011263 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011264 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011265 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011266 if (part != NULL)
11267 hasRefs++;
11268 /*
11269 * Handle redefinitions.
11270 */
11271 if (ctxt->isRedefine && ctxt->redef &&
11272 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11273 part && part->children)
11274 {
11275 if ((xmlSchemaGetQNameRefName(part->children) ==
11276 ctxt->redef->refName) &&
11277 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11278 ctxt->redef->refTargetNs))
11279 {
11280 /*
11281 * SPEC src-redefine:
11282 * (6.1) "If it has a <group> among its contents at
11283 * some level the ·actual value· of whose ref
11284 * [attribute] is the same as the ·actual value· of
11285 * its own name attribute plus target namespace, then
11286 * all of the following must be true:"
11287 * (6.1.1) "It must have exactly one such group."
11288 */
11289 if (ctxt->redefCounter != 0) {
11290 xmlChar *str = NULL;
11291
11292 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11293 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11294 "The redefining model group definition "
11295 "'%s' must not contain more than one "
11296 "reference to the redefined definition",
11297 xmlSchemaFormatQName(&str,
11298 ctxt->redef->refTargetNs,
11299 ctxt->redef->refName),
11300 NULL);
11301 FREE_AND_NULL(str)
11302 part = NULL;
11303 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11304 ((WXS_PARTICLE(part))->maxOccurs != 1))
11305 {
11306 xmlChar *str = NULL;
11307 /*
11308 * SPEC src-redefine:
11309 * (6.1.2) "The ·actual value· of both that
11310 * group's minOccurs and maxOccurs [attribute]
11311 * must be 1 (or ·absent·).
11312 */
11313 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11314 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11315 "The redefining model group definition "
11316 "'%s' must not contain a reference to the "
11317 "redefined definition with a "
11318 "maxOccurs/minOccurs other than 1",
11319 xmlSchemaFormatQName(&str,
11320 ctxt->redef->refTargetNs,
11321 ctxt->redef->refName),
11322 NULL);
11323 FREE_AND_NULL(str)
11324 part = NULL;
11325 }
11326 ctxt->redef->reference = WXS_BASIC_CAST part;
11327 ctxt->redefCounter++;
11328 }
11329 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011330 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011331 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011332 xmlSchemaParseAny(ctxt, schema, child);
11333 } else if (IS_SCHEMA(child, "choice")) {
11334 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11335 XML_SCHEMA_TYPE_CHOICE, 1);
11336 } else if (IS_SCHEMA(child, "sequence")) {
11337 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11338 XML_SCHEMA_TYPE_SEQUENCE, 1);
11339 }
11340 if (part != NULL) {
11341 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011342 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011343 else
11344 last->next = part;
11345 last = part;
11346 }
11347 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011348 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011349 if (child != NULL) {
11350 xmlSchemaPContentErr(ctxt,
11351 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011352 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011353 "(annotation?, (element | group | choice | sequence | any)*)");
11354 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011355 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011356 if ((max == 0) && (min == 0))
11357 return (NULL);
11358 if (hasRefs) {
11359 /*
11360 * We need to resolve references.
11361 */
11362 WXS_ADD_PENDING(ctxt, item);
11363 }
11364 if (withParticle)
11365 return ((xmlSchemaTreeItemPtr) particle);
11366 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011367 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011368}
11369
11370/**
11371 * xmlSchemaParseRestriction:
11372 * @ctxt: a schema validation context
11373 * @schema: the schema being built
11374 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011375 *
11376 * parse a XML schema Restriction definition
11377 * *WARNING* this interface is highly subject to change
11378 *
11379 * Returns the type definition or NULL in case of error
11380 */
11381static xmlSchemaTypePtr
11382xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011383 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011384{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011385 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011386 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011387 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011388
11389 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11390 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011391 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011392 type = ctxt->ctxtType;
11393 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011394
11395 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011396 * Check for illegal attributes.
11397 */
11398 attr = node->properties;
11399 while (attr != NULL) {
11400 if (attr->ns == NULL) {
11401 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11402 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011403 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011404 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011405 }
11406 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011407 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011408 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011409 }
11410 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011411 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011412 /*
11413 * Extract and validate attributes.
11414 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011415 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011416 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011417 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011418 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011419 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011420 * Extract the base type. The "base" attribute is mandatory if inside
11421 * a complex type or if redefining.
11422 *
11423 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011424 * among its [children]), the simple type definition which is
11425 * the {content type} of the type definition ·resolved· to by
11426 * the ·actual value· of the base [attribute]"
11427 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011428 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011429 &(type->baseNs), &(type->base)) == 0)
11430 {
11431 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11432 xmlSchemaPMissingAttrErr(ctxt,
11433 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011434 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011435 } else if ((ctxt->isRedefine) &&
11436 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11437 {
11438 if (type->base == NULL) {
11439 xmlSchemaPMissingAttrErr(ctxt,
11440 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011441 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011442 } else if ((! xmlStrEqual(type->base, type->name)) ||
11443 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11444 {
11445 xmlChar *str1 = NULL, *str2 = NULL;
11446 /*
11447 * REDEFINE: SPEC src-redefine (5)
11448 * "Within the [children], each <simpleType> must have a
11449 * <restriction> among its [children] ... the ·actual value· of
11450 * whose base [attribute] must be the same as the ·actual value·
11451 * of its own name attribute plus target namespace;"
11452 */
11453 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011454 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011455 "value '%s' of the 'base' attribute does not match the "
11456 "type's designation '%s'",
11457 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11458 xmlSchemaFormatQName(&str1, type->targetNamespace,
11459 type->name), NULL);
11460 FREE_AND_NULL(str1);
11461 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011462 /* Avoid confusion and erase the values. */
11463 type->base = NULL;
11464 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011465 }
11466 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011467 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011468 /*
11469 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011470 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011471 child = node->children;
11472 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011473 /*
11474 * Add the annotation to the simple type ancestor.
11475 */
11476 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011477 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011478 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011479 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011480 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11481 /*
11482 * Corresponds to <simpleType><restriction><simpleType>.
11483 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011484 if (IS_SCHEMA(child, "simpleType")) {
11485 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011486 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011487 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011488 * Either the base [attribute] or the simpleType [child] of the
11489 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011490 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011491 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011492 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011493 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011494 "The attribute 'base' and the <simpleType> child are "
11495 "mutually exclusive", NULL);
11496 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011497 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011498 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011499 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011500 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011501 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011502 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011503 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011504 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011505 "Either the attribute 'base' or a <simpleType> child "
11506 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011507 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011508 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11509 /*
11510 * Corresponds to <complexType><complexContent><restriction>...
11511 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011512 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011513 * Model groups <all>, <choice> and <sequence>.
11514 */
11515 if (IS_SCHEMA(child, "all")) {
11516 type->subtypes = (xmlSchemaTypePtr)
11517 xmlSchemaParseModelGroup(ctxt, schema, child,
11518 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011519 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011520 } else if (IS_SCHEMA(child, "choice")) {
11521 type->subtypes = (xmlSchemaTypePtr)
11522 xmlSchemaParseModelGroup(ctxt,
11523 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11524 child = child->next;
11525 } else if (IS_SCHEMA(child, "sequence")) {
11526 type->subtypes = (xmlSchemaTypePtr)
11527 xmlSchemaParseModelGroup(ctxt, schema, child,
11528 XML_SCHEMA_TYPE_SEQUENCE, 1);
11529 child = child->next;
11530 /*
11531 * Model group reference <group>.
11532 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011533 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011534 type->subtypes = (xmlSchemaTypePtr)
11535 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011536 /*
11537 * Note that the reference will be resolved in
11538 * xmlSchemaResolveTypeReferences();
11539 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011540 child = child->next;
11541 }
11542 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011543 /*
11544 * Corresponds to <complexType><simpleContent><restriction>...
11545 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011546 * "1.1 the simple type definition corresponding to the <simpleType>
11547 * among the [children] of <restriction> if there is one;"
11548 */
11549 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011550 /*
11551 * We will store the to-be-restricted simple type in
11552 * type->contentTypeDef *temporarily*.
11553 */
11554 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011555 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011556 if ( type->contentTypeDef == NULL)
11557 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011558 child = child->next;
11559 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011560 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011561
11562 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011563 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011564 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011565 /*
11566 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011567 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011568 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011569
Daniel Veillard01fa6152004-06-29 17:04:39 +000011570 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011571 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011572 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011573 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011574 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11575 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011576 * *Single Facet Value*
11577 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011578 while ((IS_SCHEMA(child, "minInclusive")) ||
11579 (IS_SCHEMA(child, "minExclusive")) ||
11580 (IS_SCHEMA(child, "maxInclusive")) ||
11581 (IS_SCHEMA(child, "maxExclusive")) ||
11582 (IS_SCHEMA(child, "totalDigits")) ||
11583 (IS_SCHEMA(child, "fractionDigits")) ||
11584 (IS_SCHEMA(child, "pattern")) ||
11585 (IS_SCHEMA(child, "enumeration")) ||
11586 (IS_SCHEMA(child, "whiteSpace")) ||
11587 (IS_SCHEMA(child, "length")) ||
11588 (IS_SCHEMA(child, "maxLength")) ||
11589 (IS_SCHEMA(child, "minLength"))) {
11590 facet = xmlSchemaParseFacet(ctxt, schema, child);
11591 if (facet != NULL) {
11592 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011593 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011594 else
11595 lastfacet->next = facet;
11596 lastfacet = facet;
11597 lastfacet->next = NULL;
11598 }
11599 child = child->next;
11600 }
11601 /*
11602 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011603 */
11604 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011605 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11606
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011607 facet = type->facets;
11608 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011609 facetLink = (xmlSchemaFacetLinkPtr)
11610 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011611 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011612 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011613 xmlFree(facetLink);
11614 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011615 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011616 facetLink->facet = facet;
11617 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011618 if (lastFacetLink == NULL)
11619 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011620 else
11621 lastFacetLink->next = facetLink;
11622 lastFacetLink = facetLink;
11623 facet = facet->next;
11624 } while (facet != NULL);
11625 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011626 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011627 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11628 /*
11629 * Attribute uses/declarations.
11630 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011631 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11632 (xmlSchemaItemListPtr *) &(type->attrUses),
11633 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11634 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011635 /*
11636 * Attribute wildcard.
11637 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011638 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011639 type->attributeWildcard =
11640 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011641 child = child->next;
11642 }
11643 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011644 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011645 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11646 xmlSchemaPContentErr(ctxt,
11647 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011648 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011649 "annotation?, (group | all | choice | sequence)?, "
11650 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011651 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011652 xmlSchemaPContentErr(ctxt,
11653 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011654 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011655 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11656 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11657 "length | minLength | maxLength | enumeration | whiteSpace | "
11658 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11659 } else {
11660 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011661 xmlSchemaPContentErr(ctxt,
11662 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011663 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011664 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11665 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11666 "length | minLength | maxLength | enumeration | whiteSpace | "
11667 "pattern)*))");
11668 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011669 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011670 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011671}
11672
11673/**
11674 * xmlSchemaParseExtension:
11675 * @ctxt: a schema validation context
11676 * @schema: the schema being built
11677 * @node: a subtree containing XML Schema informations
11678 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011679 * Parses an <extension>, which is found inside a
11680 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011681 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011682 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011683 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011684 */
11685static xmlSchemaTypePtr
11686xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011687 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011688{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011689 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011690 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011691 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011692
11693 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11694 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011695 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011696 type = ctxt->ctxtType;
11697 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011698
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011699 /*
11700 * Check for illegal attributes.
11701 */
11702 attr = node->properties;
11703 while (attr != NULL) {
11704 if (attr->ns == NULL) {
11705 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11706 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011707 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011708 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011709 }
11710 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011711 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011712 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011713 }
11714 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011715 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011716
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011717 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011718
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011719 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011720 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011721 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011722 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11723 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011724 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011725 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011726 XML_SCHEMAP_S4S_ATTR_MISSING,
11727 NULL, node, "base", NULL);
11728 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011729 /*
11730 * And now for the children...
11731 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011732 child = node->children;
11733 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011734 /*
11735 * Add the annotation to the type ancestor.
11736 */
11737 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011738 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011739 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011740 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011741 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11742 /*
11743 * Corresponds to <complexType><complexContent><extension>... and:
11744 *
11745 * Model groups <all>, <choice>, <sequence> and <group>.
11746 */
11747 if (IS_SCHEMA(child, "all")) {
11748 type->subtypes = (xmlSchemaTypePtr)
11749 xmlSchemaParseModelGroup(ctxt, schema,
11750 child, XML_SCHEMA_TYPE_ALL, 1);
11751 child = child->next;
11752 } else if (IS_SCHEMA(child, "choice")) {
11753 type->subtypes = (xmlSchemaTypePtr)
11754 xmlSchemaParseModelGroup(ctxt, schema,
11755 child, XML_SCHEMA_TYPE_CHOICE, 1);
11756 child = child->next;
11757 } else if (IS_SCHEMA(child, "sequence")) {
11758 type->subtypes = (xmlSchemaTypePtr)
11759 xmlSchemaParseModelGroup(ctxt, schema,
11760 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11761 child = child->next;
11762 } else if (IS_SCHEMA(child, "group")) {
11763 type->subtypes = (xmlSchemaTypePtr)
11764 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011765 /*
11766 * Note that the reference will be resolved in
11767 * xmlSchemaResolveTypeReferences();
11768 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011769 child = child->next;
11770 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011771 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011772 if (child != NULL) {
11773 /*
11774 * Attribute uses/declarations.
11775 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011776 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11777 (xmlSchemaItemListPtr *) &(type->attrUses),
11778 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11779 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011780 /*
11781 * Attribute wildcard.
11782 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011783 if (IS_SCHEMA(child, "anyAttribute")) {
11784 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011785 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11786 child = child->next;
11787 }
11788 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011789 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011790 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11791 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011792 xmlSchemaPContentErr(ctxt,
11793 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011794 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011795 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011796 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011797 } else {
11798 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011799 xmlSchemaPContentErr(ctxt,
11800 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011801 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011802 "(annotation?, ((attribute | attributeGroup)*, "
11803 "anyAttribute?))");
11804 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011805 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011806 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011807}
11808
11809/**
11810 * xmlSchemaParseSimpleContent:
11811 * @ctxt: a schema validation context
11812 * @schema: the schema being built
11813 * @node: a subtree containing XML Schema informations
11814 *
11815 * parse a XML schema SimpleContent definition
11816 * *WARNING* this interface is highly subject to change
11817 *
11818 * Returns the type definition or NULL in case of error
11819 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011820static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011821xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011822 xmlSchemaPtr schema, xmlNodePtr node,
11823 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011824{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011825 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011826 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011827 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011828
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011829 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11830 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011831 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011832 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011833 /* Not a component, don't create it. */
11834 type = ctxt->ctxtType;
11835 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11836 /*
11837 * Check for illegal attributes.
11838 */
11839 attr = node->properties;
11840 while (attr != NULL) {
11841 if (attr->ns == NULL) {
11842 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011843 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011844 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011845 }
11846 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011847 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011848 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011849 }
11850 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011851 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011852
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011853 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011854
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011855 /*
11856 * And now for the children...
11857 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011858 child = node->children;
11859 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011860 /*
11861 * Add the annotation to the complex type ancestor.
11862 */
11863 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011864 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011865 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011866 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011867 if (child == NULL) {
11868 xmlSchemaPContentErr(ctxt,
11869 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011870 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011871 "(annotation?, (restriction | extension))");
11872 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011873 if (child == NULL) {
11874 xmlSchemaPContentErr(ctxt,
11875 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011876 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011877 "(annotation?, (restriction | extension))");
11878 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011879 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011880 xmlSchemaParseRestriction(ctxt, schema, child,
11881 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011882 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011883 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011884 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011885 xmlSchemaParseExtension(ctxt, schema, child,
11886 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011887 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011888 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011889 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011890 if (child != NULL) {
11891 xmlSchemaPContentErr(ctxt,
11892 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011893 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011894 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011895 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011896 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011897}
11898
11899/**
11900 * xmlSchemaParseComplexContent:
11901 * @ctxt: a schema validation context
11902 * @schema: the schema being built
11903 * @node: a subtree containing XML Schema informations
11904 *
11905 * parse a XML schema ComplexContent definition
11906 * *WARNING* this interface is highly subject to change
11907 *
11908 * Returns the type definition or NULL in case of error
11909 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011910static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011911xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011912 xmlSchemaPtr schema, xmlNodePtr node,
11913 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011914{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011915 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011916 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011917 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011918
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011919 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11920 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011921 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011922 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011923 /* Not a component, don't create it. */
11924 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011925 /*
11926 * Check for illegal attributes.
11927 */
11928 attr = node->properties;
11929 while (attr != NULL) {
11930 if (attr->ns == NULL) {
11931 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011932 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011933 {
11934 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011935 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011936 }
11937 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11938 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011939 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011940 }
11941 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011942 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011943
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011944 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011945
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011946 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011947 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011948 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011949 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011950 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11951 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011952 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011953 child = node->children;
11954 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011955 /*
11956 * Add the annotation to the complex type ancestor.
11957 */
11958 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011959 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011960 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011961 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011962 if (child == NULL) {
11963 xmlSchemaPContentErr(ctxt,
11964 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011965 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011966 NULL, "(annotation?, (restriction | extension))");
11967 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011968 if (child == NULL) {
11969 xmlSchemaPContentErr(ctxt,
11970 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011971 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011972 NULL, "(annotation?, (restriction | extension))");
11973 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011974 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011975 xmlSchemaParseRestriction(ctxt, schema, child,
11976 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011977 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011978 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011979 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011980 xmlSchemaParseExtension(ctxt, schema, child,
11981 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011982 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011983 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011984 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011985 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011986 xmlSchemaPContentErr(ctxt,
11987 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011988 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011989 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011990 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011991 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011992}
11993
11994/**
11995 * xmlSchemaParseComplexType:
11996 * @ctxt: a schema validation context
11997 * @schema: the schema being built
11998 * @node: a subtree containing XML Schema informations
11999 *
12000 * parse a XML schema Complex Type definition
12001 * *WARNING* this interface is highly subject to change
12002 *
12003 * Returns the type definition or NULL in case of error
12004 */
12005static xmlSchemaTypePtr
12006xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012007 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012008{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012009 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012010 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012011 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012012 xmlAttrPtr attr;
12013 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012014#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012015 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012016#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012017 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012018
Daniel Veillard4255d502002-04-16 15:50:10 +000012019
12020 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12021 return (NULL);
12022
Daniel Veillard01fa6152004-06-29 17:04:39 +000012023 ctxtType = ctxt->ctxtType;
12024
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012025 if (topLevel) {
12026 attr = xmlSchemaGetPropNode(node, "name");
12027 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012028 xmlSchemaPMissingAttrErr(ctxt,
12029 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012030 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012031 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012032 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12033 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012034 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012035 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012036
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012037 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012038 /*
12039 * Parse as local complex type definition.
12040 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012041#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012042 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012043 type = xmlSchemaAddType(ctxt, schema,
12044 XML_SCHEMA_TYPE_COMPLEX,
12045 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012046 ctxt->targetNamespace, node, 0);
12047#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012048 type = xmlSchemaAddType(ctxt, schema,
12049 XML_SCHEMA_TYPE_COMPLEX,
12050 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012051#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012052 if (type == NULL)
12053 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012054 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012055 type->node = node;
12056 type->type = XML_SCHEMA_TYPE_COMPLEX;
12057 /*
12058 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012059 */
12060 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012061 /*
12062 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012063 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012064 type = xmlSchemaAddType(ctxt, schema,
12065 XML_SCHEMA_TYPE_COMPLEX,
12066 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012067 if (type == NULL)
12068 return (NULL);
12069 type->node = node;
12070 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012071 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012072 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012073 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012074 /*
12075 * Handle attributes.
12076 */
12077 attr = node->properties;
12078 while (attr != NULL) {
12079 if (attr->ns == NULL) {
12080 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12081 /*
12082 * Attribute "id".
12083 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012084 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012085 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12086 /*
12087 * Attribute "mixed".
12088 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012089 if (xmlSchemaPGetBoolNodeValue(ctxt,
12090 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012091 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12092 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012093 /*
12094 * Attributes of global complex type definitions.
12095 */
12096 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12097 /* Pass. */
12098 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12099 /*
12100 * Attribute "abstract".
12101 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012102 if (xmlSchemaPGetBoolNodeValue(ctxt,
12103 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012104 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12105 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12106 /*
12107 * Attribute "final".
12108 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012109 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012110 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012111 if (xmlSchemaPValAttrBlockFinal(attrValue,
12112 &(type->flags),
12113 -1,
12114 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12115 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12116 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012117 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012118 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012119 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012120 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012121 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012122 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012123 } else
12124 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012125 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12126 /*
12127 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012128 */
12129 attrValue = xmlSchemaGetNodeContent(ctxt,
12130 (xmlNodePtr) attr);
12131 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012132 -1,
12133 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012134 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012135 -1, -1, -1) != 0) {
12136 xmlSchemaPSimpleTypeErr(ctxt,
12137 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012138 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012139 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012140 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012141 } else
12142 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012143 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012144 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012145 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012146 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012147 } else {
12148 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012149 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012150 }
12151 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012152 xmlSchemaPIllegalAttrErr(ctxt,
12153 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012154 }
12155 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012156 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012157 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012158 /*
12159 * Apply default "block" values.
12160 */
12161 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12162 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12163 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12164 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12165 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012166 if (! final) {
12167 /*
12168 * Apply default "block" values.
12169 */
12170 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12171 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12172 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12173 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12174 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012175 /*
12176 * And now for the children...
12177 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012178 child = node->children;
12179 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012180 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012181 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012182 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012183 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012184 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012185 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012186 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012187 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012188 * Specifying mixed='true' when the <simpleContent>
12189 * alternative is chosen has no effect
12190 */
William M. Bracke7091952004-05-11 15:09:58 +000012191 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12192 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012193 xmlSchemaParseSimpleContent(ctxt, schema, child,
12194 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012195 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012196 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012197 /*
12198 * <complexType><complexContent>...
12199 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012200 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012201 xmlSchemaParseComplexContent(ctxt, schema, child,
12202 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012203 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012204 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012205 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012206 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12207 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012208 * SPEC
12209 * "...the third alternative (neither <simpleContent> nor
12210 * <complexContent>) is chosen. This case is understood as shorthand
12211 * for complex content restricting the ·ur-type definition·, and the
12212 * details of the mappings should be modified as necessary.
12213 */
12214 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12215 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012216 /*
12217 * Parse model groups.
12218 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012219 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012220 type->subtypes = (xmlSchemaTypePtr)
12221 xmlSchemaParseModelGroup(ctxt, schema, child,
12222 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012223 child = child->next;
12224 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012225 type->subtypes = (xmlSchemaTypePtr)
12226 xmlSchemaParseModelGroup(ctxt, schema, child,
12227 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012228 child = child->next;
12229 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012230 type->subtypes = (xmlSchemaTypePtr)
12231 xmlSchemaParseModelGroup(ctxt, schema, child,
12232 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012233 child = child->next;
12234 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012235 type->subtypes = (xmlSchemaTypePtr)
12236 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012237 /*
12238 * Note that the reference will be resolved in
12239 * xmlSchemaResolveTypeReferences();
12240 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012241 child = child->next;
12242 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012243 /*
12244 * Parse attribute decls/refs.
12245 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012246 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12247 (xmlSchemaItemListPtr *) &(type->attrUses),
12248 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12249 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012250 /*
12251 * Parse attribute wildcard.
12252 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012253 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012254 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12255 child = child->next;
12256 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012257 }
12258 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012259 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012260 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012261 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012262 NULL, "(annotation?, (simpleContent | complexContent | "
12263 "((group | all | choice | sequence)?, ((attribute | "
12264 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012265 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012266 /*
12267 * REDEFINE: SPEC src-redefine (5)
12268 */
12269 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12270 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012271 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012272 "<complexType> must have a <restriction> or <extension> "
12273 "grand-child", NULL);
12274 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012275 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012276 return (type);
12277}
12278
Daniel Veillard4255d502002-04-16 15:50:10 +000012279/************************************************************************
12280 * *
12281 * Validating using Schemas *
12282 * *
12283 ************************************************************************/
12284
12285/************************************************************************
12286 * *
12287 * Reading/Writing Schemas *
12288 * *
12289 ************************************************************************/
12290
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012291#if 0 /* Will be enabled if it is clear what options are needed. */
12292/**
12293 * xmlSchemaParserCtxtSetOptions:
12294 * @ctxt: a schema parser context
12295 * @options: a combination of xmlSchemaParserOption
12296 *
12297 * Sets the options to be used during the parse.
12298 *
12299 * Returns 0 in case of success, -1 in case of an
12300 * API error.
12301 */
12302static int
12303xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12304 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012305
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012306{
12307 int i;
12308
12309 if (ctxt == NULL)
12310 return (-1);
12311 /*
12312 * WARNING: Change the start value if adding to the
12313 * xmlSchemaParseOption.
12314 */
12315 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12316 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012317 return (-1);
12318 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012319 }
12320 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012321 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012322}
12323
12324/**
12325 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012326 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012327 *
12328 * Returns the option combination of the parser context.
12329 */
12330static int
12331xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012332
12333{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012334 if (ctxt == NULL)
12335 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012336 else
12337 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012338}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012339#endif
12340
Daniel Veillard4255d502002-04-16 15:50:10 +000012341/**
12342 * xmlSchemaNewParserCtxt:
12343 * @URL: the location of the schema
12344 *
12345 * Create an XML Schemas parse context for that file/resource expected
12346 * to contain an XML Schemas file.
12347 *
12348 * Returns the parser context or NULL in case of error
12349 */
12350xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012351xmlSchemaNewParserCtxt(const char *URL)
12352{
Daniel Veillard4255d502002-04-16 15:50:10 +000012353 xmlSchemaParserCtxtPtr ret;
12354
12355 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012356 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012357
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012358 ret = xmlSchemaParserCtxtCreate();
12359 if (ret == NULL)
12360 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012361 ret->dict = xmlDictCreate();
12362 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012363 return (ret);
12364}
12365
12366/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012367 * xmlSchemaNewMemParserCtxt:
12368 * @buffer: a pointer to a char array containing the schemas
12369 * @size: the size of the array
12370 *
12371 * Create an XML Schemas parse context for that memory buffer expected
12372 * to contain an XML Schemas file.
12373 *
12374 * Returns the parser context or NULL in case of error
12375 */
12376xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012377xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12378{
Daniel Veillard6045c902002-10-09 21:13:59 +000012379 xmlSchemaParserCtxtPtr ret;
12380
12381 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012382 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012383 ret = xmlSchemaParserCtxtCreate();
12384 if (ret == NULL)
12385 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012386 ret->buffer = buffer;
12387 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012388 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012389 return (ret);
12390}
12391
12392/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012393 * xmlSchemaNewDocParserCtxt:
12394 * @doc: a preparsed document tree
12395 *
12396 * Create an XML Schemas parse context for that document.
12397 * NB. The document may be modified during the parsing process.
12398 *
12399 * Returns the parser context or NULL in case of error
12400 */
12401xmlSchemaParserCtxtPtr
12402xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12403{
12404 xmlSchemaParserCtxtPtr ret;
12405
12406 if (doc == NULL)
12407 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012408 ret = xmlSchemaParserCtxtCreate();
12409 if (ret == NULL)
12410 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012411 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012412 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012413 /* The application has responsibility for the document */
12414 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012415
12416 return (ret);
12417}
12418
12419/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012420 * xmlSchemaFreeParserCtxt:
12421 * @ctxt: the schema parser context
12422 *
12423 * Free the resources associated to the schema parser context
12424 */
12425void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012426xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12427{
Daniel Veillard4255d502002-04-16 15:50:10 +000012428 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012429 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012430 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012431 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012432 if (ctxt->vctxt != NULL) {
12433 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12434 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012435 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12436 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12437 ctxt->constructor = NULL;
12438 ctxt->ownsConstructor = 0;
12439 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012440 if (ctxt->attrProhibs != NULL)
12441 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012442 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012443 xmlFree(ctxt);
12444}
12445
12446/************************************************************************
12447 * *
12448 * Building the content models *
12449 * *
12450 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012451
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012452static void
12453xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012454 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012455{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012456 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012457 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012458 xmlSchemaSubstGroupPtr substGroup;
12459 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012460
12461 elemDecl = (xmlSchemaElementPtr) particle->children;
12462 /*
12463 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012464 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012465 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012466 if (end == NULL)
12467 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012468 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012469 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012470 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012471 XML_SCHEMAP_INTERNAL,
12472 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12473 "declaration is marked having a subst. group but none "
12474 "available.\n", elemDecl->name, NULL);
12475 return;
12476 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012477 if (counter >= 0) {
12478 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012479 * NOTE that we put the declaration in, even if it's abstract.
12480 * However, an error will be raised during *validation* if an element
12481 * information item shall be validated against an abstract element
12482 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012483 */
12484 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12485 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12486 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12487 /*
12488 * Add subst. group members.
12489 */
12490 for (i = 0; i < substGroup->members->nbItems; i++) {
12491 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12492 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12493 member->name, member->targetNamespace, member);
12494 }
12495 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012496 /*
12497 * NOTE that we put the declaration in, even if it's abstract,
12498 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012499 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012500 xmlAutomataNewTransition2(pctxt->am,
12501 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012502 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12503 /*
12504 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012505 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012506 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012507 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012508 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12509 member->name, member->targetNamespace,
12510 1, 1, member);
12511 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012512 }
12513 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012514 xmlAutomataStatePtr hop;
12515 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12516 UNBOUNDED : particle->maxOccurs - 1;
12517 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12518
12519 counter =
12520 xmlAutomataNewCounter(pctxt->am, minOccurs,
12521 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012522 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012523
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012524 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012525 xmlAutomataNewTransition2(pctxt->am,
12526 start, NULL,
12527 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012528 hop);
12529 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012530 * Add subst. group members.
12531 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012532 for (i = 0; i < substGroup->members->nbItems; i++) {
12533 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12534 xmlAutomataNewEpsilon(pctxt->am,
12535 xmlAutomataNewTransition2(pctxt->am,
12536 start, NULL,
12537 member->name, member->targetNamespace, member),
12538 hop);
12539 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012540 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12541 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12542 }
12543 if (particle->minOccurs == 0)
12544 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012545 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012546}
12547
12548static void
12549xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12550 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012551{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012552 if (((xmlSchemaElementPtr) particle->children)->flags &
12553 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012554 /*
12555 * Substitution groups.
12556 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012557 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012558 } else {
12559 xmlSchemaElementPtr elemDecl;
12560 xmlAutomataStatePtr start;
12561
12562 elemDecl = (xmlSchemaElementPtr) particle->children;
12563
12564 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012565 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012566 if (particle->maxOccurs == 1) {
12567 start = ctxt->state;
12568 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012569 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12570 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12571 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012572 /* Special case. */
12573 start = ctxt->state;
12574 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12575 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012576 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012577 } else {
12578 int counter;
12579 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12580 UNBOUNDED : particle->maxOccurs - 1;
12581 int minOccurs = particle->minOccurs < 1 ?
12582 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012583
12584 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012585 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12586 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12587 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12588 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12589 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12590 NULL, counter);
12591 }
12592 if (particle->minOccurs == 0)
12593 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12594 }
12595}
12596
Daniel Veillard4255d502002-04-16 15:50:10 +000012597/**
12598 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012599 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012600 * @particle: the particle component
12601 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012602 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012603 * Create the automaton for the {content type} of a complex type.
12604 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012605 */
12606static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012607xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012608 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012609{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012610 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012611 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012612 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012613 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012614 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012615 /*
12616 * Just return in this case. A missing "term" of the particle
12617 * might arise due to an invalid "term" component.
12618 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012619 return;
12620 }
12621
12622 switch (particle->children->type) {
12623 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012624 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012625 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012626 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012627
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012628 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012629
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012630 start = pctxt->state;
12631 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012632
12633 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012634 if (wild->any == 1) {
12635 /*
12636 * We need to add both transitions:
12637 *
12638 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012639 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012640 pctxt->state =
12641 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012642 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012643 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012644 /*
12645 * 2. the {"*"} for elements in no namespace.
12646 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012647 pctxt->state =
12648 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012649 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012650 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012651
12652 } else if (wild->nsSet != NULL) {
12653 ns = wild->nsSet;
12654 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012655 pctxt->state = start;
12656 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12657 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12658 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012659 ns = ns->next;
12660 } while (ns != NULL);
12661
12662 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012663 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12664 start, end, BAD_CAST "*", wild->negNsSet->value,
12665 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012666 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012667 } else {
12668 int counter;
12669 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012670 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012671 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012672 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012673 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012674
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012675 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12676 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012677 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012678 pctxt->state =
12679 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012680 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012681 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12682 pctxt->state =
12683 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012684 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012685 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012686 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012687 ns = wild->nsSet;
12688 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012689 pctxt->state =
12690 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012691 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012692 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012693 ns = ns->next;
12694 } while (ns != NULL);
12695
12696 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012697 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012698 start, hop, BAD_CAST "*", wild->negNsSet->value,
12699 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012700 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012701 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12702 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012703 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012704 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012705 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012706 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012707 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012708 break;
12709 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012710 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012711 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012712 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012713 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012714 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012715
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012716 /*
12717 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012718 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012719 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012720 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12721 sub = particle->children->children;
12722 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012723 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012724 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012725 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012726 }
12727 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012728 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012729
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012730 if (particle->maxOccurs >= UNBOUNDED) {
12731 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012732 xmlAutomataStatePtr tmp;
12733 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012734
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012735 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012736 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012737 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012738
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012739 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012740 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012741
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012742 sub = particle->children->children;
12743 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012744 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012745 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012746 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012747 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012748 tmp = pctxt->state;
12749 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012750 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012751 pctxt->state =
12752 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012753 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012754
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012755 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012756 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012757 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012758 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012759
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012760 sub = particle->children->children;
12761 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012762 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012763 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012764 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012765 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012766 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012767 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012768 /*
12769 * epsilon needed to block previous trans from
12770 * being allowed to enter back from another
12771 * construct
12772 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012773 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12774 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012775 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012776 xmlAutomataNewEpsilon(pctxt->am,
12777 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012778 }
12779 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012780 } else if ((particle->maxOccurs > 1)
12781 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012782 xmlAutomataStatePtr tmp;
12783 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012784
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012785 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012786 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012787 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012788
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012789 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012790 particle->minOccurs - 1,
12791 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012792
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012793 sub = particle->children->children;
12794 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012795 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012796 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012797 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012798 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012799 tmp = pctxt->state;
12800 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012801 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012802 pctxt->state =
12803 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012804 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012805 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012806 xmlAutomataNewEpsilon(pctxt->am,
12807 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012808 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012809 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012810 sub = particle->children->children;
12811 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012812 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012813 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012814 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012815 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012816 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012817 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12818 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012819 }
12820 }
12821 }
12822 break;
12823 }
12824 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012825 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012826 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012827
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012828 start = pctxt->state;
12829 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012830
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012831 /*
12832 * iterate over the subtypes and remerge the end with an
12833 * epsilon transition
12834 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012835 if (particle->maxOccurs == 1) {
12836 sub = particle->children->children;
12837 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012838 pctxt->state = start;
12839 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012840 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012841 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012842 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012843 }
12844 } else {
12845 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012846 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012847 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12848 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012849 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012850 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012851
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012852 /*
12853 * use a counter to keep track of the number of transtions
12854 * which went through the choice.
12855 */
12856 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012857 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12858 hop = xmlAutomataNewState(pctxt->am);
12859 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012860
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012861 sub = particle->children->children;
12862 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012863 pctxt->state = base;
12864 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012865 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012866 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012867 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012868 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012869 xmlAutomataNewEpsilon(pctxt->am, start, base);
12870 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12871 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012872 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012873 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012874 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012875 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012876 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012877 break;
12878 }
12879 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012880 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012881 xmlSchemaParticlePtr sub;
12882 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012883 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012884
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012885 sub = (xmlSchemaParticlePtr) particle->children->children;
12886 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012887 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012889 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012890 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012891
12892 elemDecl = (xmlSchemaElementPtr) sub->children;
12893 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012894 PERROR_INT("xmlSchemaBuildAContentModel",
12895 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012896 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012897 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012898 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012899 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012900 * {particles} of the group must be 0 or 1; this is
12901 * already ensured during the parse of the content of
12902 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012903 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012904 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12905 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012906
Daniel Veillarda980bef2005-07-18 21:34:03 +000012907 /*
12908 * This is an abstract group, we need to share
12909 * the same counter for all the element transitions
12910 * derived from the group
12911 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012912 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012913 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012914 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12915 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012916 } else {
12917 if ((sub->minOccurs == 1) &&
12918 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012919 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12920 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012921 elemDecl->name,
12922 elemDecl->targetNamespace,
12923 1, 1, elemDecl);
12924 } else if ((sub->minOccurs == 0) &&
12925 (sub->maxOccurs == 1)) {
12926
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012927 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12928 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012929 elemDecl->name,
12930 elemDecl->targetNamespace,
12931 0,
12932 1,
12933 elemDecl);
12934 }
12935 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012936 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012937 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012938 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012939 pctxt->state =
12940 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012941 break;
12942 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012943 case XML_SCHEMA_TYPE_GROUP:
12944 /*
12945 * If we hit a model group definition, then this means that
12946 * it was empty, thus was not substituted for the containing
12947 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012948 * TODO: But the group should be substituted and not occur at
12949 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012950 */
12951 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012952 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012953 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12954 "xmlSchemaBuildAContentModel",
12955 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012956 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012957 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012958 }
12959}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012960
Daniel Veillard4255d502002-04-16 15:50:10 +000012961/**
12962 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012963 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012964 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012965 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012966 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012967 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012968 */
12969static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012970xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012971 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012972{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012973 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
12974 (type->contModel != NULL) ||
12975 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
12976 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012977 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012978
12979#ifdef DEBUG_CONTENT
12980 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012981 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012982#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012983 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000012984 ctxt->am = xmlNewAutomata();
12985 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012986 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012987 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012988 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012989 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000012990 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012991 /*
12992 * Build the automaton.
12993 */
12994 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000012995 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012996 type->contModel = xmlAutomataCompile(ctxt->am);
12997 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012998 xmlSchemaPCustomErr(ctxt,
12999 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013000 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013001 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013002 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013003 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013004 XML_SCHEMAP_NOT_DETERMINISTIC,
13005 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013006 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013007 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013008 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013009#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013010 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013011 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013012 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013013#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013014 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013015 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013016 xmlFreeAutomata(ctxt->am);
13017 ctxt->am = NULL;
13018}
13019
13020/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013021 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013022 * @elem: the schema element context
13023 * @ctxt: the schema parser context
13024 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013025 * Resolves the references of an element declaration
13026 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013027 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013028 */
13029static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013030xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13031 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013032{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013033 if ((ctxt == NULL) || (elemDecl == NULL) ||
13034 ((elemDecl != NULL) &&
13035 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013036 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013037 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013038
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013039 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013040 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013041
13042 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013043 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013044 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013045 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013046 elemDecl->namedTypeNs);
13047 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013048 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013049 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013050 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013051 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013052 XML_SCHEMA_TYPE_BASIC, "type definition");
13053 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013054 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013055 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013056 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013057 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013058
Daniel Veillardc0826a72004-08-10 14:17:33 +000013059 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013060 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013061 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013062 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013063 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13064 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013065 if (substHead == NULL) {
13066 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013067 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013068 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013069 "substitutionGroup", elemDecl->substGroup,
13070 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013071 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013072 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013073 /*
13074 * Set the "substitution group affiliation".
13075 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013076 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013077 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013078 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013079 * The type definitions is set to:
13080 * SPEC "...the {type definition} of the element
13081 * declaration ·resolved· to by the ·actual value·
13082 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013083 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013084 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013085 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013086 }
13087 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013088 /*
13089 * SPEC "The definition of anyType serves as the default type definition
13090 * for element declarations whose XML representation does not specify one."
13091 */
13092 if ((elemDecl->subtypes == NULL) &&
13093 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013094 (elemDecl->substGroup == NULL))
13095 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013096}
13097
13098/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013099 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013100 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013101 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013102 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013103 * Checks and builds the "member type definitions" property of the union
13104 * simple type. This handles part (1), part (2) is done in
13105 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13106 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013107 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013108 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013109static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013110xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13111 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013112{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013113
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013114 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013115 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013116
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013117 /*
13118 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13119 * define the explicit members as the type definitions ·resolved·
13120 * to by the items in the ·actual value· of the memberTypes [attribute],
13121 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013122 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013123 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013124 /*
13125 * Resolve references.
13126 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013127 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013128 lastLink = NULL;
13129 while (link != NULL) {
13130 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013131
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013132 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13133 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13134
13135 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013136 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013137 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013138 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013139 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13140 /*
13141 * Remove the member type link.
13142 */
13143 if (lastLink == NULL)
13144 type->memberTypes = link->next;
13145 else
13146 lastLink->next = link->next;
13147 newLink = link;
13148 link = link->next;
13149 xmlFree(newLink);
13150 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013151 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013152 lastLink = link;
13153 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013154 }
13155 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013156 /*
13157 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013158 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013159 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013160 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013161 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13162 if (link == NULL) {
13163 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13164 return (-1);
13165 }
13166 link->type = memberType;
13167 link->next = NULL;
13168 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013169 type->memberTypes = link;
13170 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013171 lastLink->next = link;
13172 lastLink = link;
13173 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013174 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013175 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013176}
13177
Daniel Veillard4255d502002-04-16 15:50:10 +000013178/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013179 * xmlSchemaIsDerivedFromBuiltInType:
13180 * @ctxt: the schema parser context
13181 * @type: the type definition
13182 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013183 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013184 *
13185 * Returns 1 if the type has the given value type, or
13186 * is derived from such a type.
13187 */
William M. Brack803812b2004-06-03 02:11:24 +000013188static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013189xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013190{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013191 if (type == NULL)
13192 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013193 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013194 return (0);
13195 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13196 if (type->builtInType == valType)
13197 return(1);
13198 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13199 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13200 return (0);
13201 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13202 } else
13203 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013204
13205 return (0);
13206}
13207
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013208#if 0
13209/**
13210 * xmlSchemaIsDerivedFromBuiltInType:
13211 * @ctxt: the schema parser context
13212 * @type: the type definition
13213 * @valType: the value type
13214 *
13215 *
13216 * Returns 1 if the type has the given value type, or
13217 * is derived from such a type.
13218 */
13219static int
13220xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13221{
13222 if (type == NULL)
13223 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013224 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013225 return (0);
13226 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13227 if (type->builtInType == valType)
13228 return(1);
13229 return (0);
13230 } else
13231 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13232
13233 return (0);
13234}
13235#endif
13236
13237static xmlSchemaTypePtr
13238xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13239{
13240 if (type == NULL)
13241 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013242 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013243 return (NULL);
13244 if (type->type == XML_SCHEMA_TYPE_BASIC)
13245 return(type);
13246 else
13247 return(xmlSchemaQueryBuiltInType(type->subtypes));
13248
13249 return (NULL);
13250}
13251
Daniel Veillard3646d642004-06-02 19:19:14 +000013252/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013253 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013254 * @type: the simpleType definition
13255 *
13256 * Returns the primitive type of the given type or
13257 * NULL in case of error.
13258 */
13259static xmlSchemaTypePtr
13260xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13261{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013262
Daniel Veillard01fa6152004-06-29 17:04:39 +000013263 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013264 /*
13265 * Note that anySimpleType is actually not a primitive type
13266 * but we need that here.
13267 */
13268 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13269 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013270 return (type);
13271 type = type->baseType;
13272 }
13273
13274 return (NULL);
13275}
13276
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013277#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013278/**
13279 * xmlSchemaGetBuiltInTypeAncestor:
13280 * @type: the simpleType definition
13281 *
13282 * Returns the primitive type of the given type or
13283 * NULL in case of error.
13284 */
13285static xmlSchemaTypePtr
13286xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13287{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013288 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013289 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013290 while (type != NULL) {
13291 if (type->type == XML_SCHEMA_TYPE_BASIC)
13292 return (type);
13293 type = type->baseType;
13294 }
13295
13296 return (NULL);
13297}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013298#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013299
Daniel Veillard01fa6152004-06-29 17:04:39 +000013300/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013301 * xmlSchemaCloneWildcardNsConstraints:
13302 * @ctxt: the schema parser context
13303 * @dest: the destination wildcard
13304 * @source: the source wildcard
13305 *
13306 * Clones the namespace constraints of source
13307 * and assignes them to dest.
13308 * Returns -1 on internal error, 0 otherwise.
13309 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013310static int
13311xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013312 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013313 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013314{
13315 xmlSchemaWildcardNsPtr cur, tmp, last;
13316
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013317 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013318 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013319 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013320 cur = source->nsSet;
13321 last = NULL;
13322 while (cur != NULL) {
13323 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13324 if (tmp == NULL)
13325 return(-1);
13326 tmp->value = cur->value;
13327 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013328 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013329 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013330 last->next = tmp;
13331 last = tmp;
13332 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013333 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013334 if (dest->negNsSet != NULL)
13335 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013336 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013337 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13338 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013339 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013340 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013341 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013342 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013343 return(0);
13344}
13345
Daniel Veillard50355f02004-06-08 17:52:16 +000013346/**
13347 * xmlSchemaUnionWildcards:
13348 * @ctxt: the schema parser context
13349 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013350 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013351 *
13352 * Unions the namespace constraints of the given wildcards.
13353 * @completeWild will hold the resulting union.
13354 * Returns a positive error code on failure, -1 in case of an
13355 * internal error, 0 otherwise.
13356 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013357static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013358xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013359 xmlSchemaWildcardPtr completeWild,
13360 xmlSchemaWildcardPtr curWild)
13361{
13362 xmlSchemaWildcardNsPtr cur, curB, tmp;
13363
13364 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013365 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013366 * value.
13367 */
13368 if ((completeWild->any == curWild->any) &&
13369 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13370 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013371
Daniel Veillard3646d642004-06-02 19:19:14 +000013372 if ((completeWild->negNsSet == NULL) ||
13373 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013374
Daniel Veillard3646d642004-06-02 19:19:14 +000013375 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013376 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013377
13378 /*
13379 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013380 */
13381 cur = completeWild->nsSet;
13382 while (cur != NULL) {
13383 found = 0;
13384 curB = curWild->nsSet;
13385 while (curB != NULL) {
13386 if (cur->value == curB->value) {
13387 found = 1;
13388 break;
13389 }
13390 curB = curB->next;
13391 }
13392 if (!found)
13393 break;
13394 cur = cur->next;
13395 }
13396 if (found)
13397 return(0);
13398 } else
13399 return(0);
13400 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013401 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013402 /*
13403 * 2 If either O1 or O2 is any, then any must be the value
13404 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013405 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013406 if (completeWild->any == 0) {
13407 completeWild->any = 1;
13408 if (completeWild->nsSet != NULL) {
13409 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13410 completeWild->nsSet = NULL;
13411 }
13412 if (completeWild->negNsSet != NULL) {
13413 xmlFree(completeWild->negNsSet);
13414 completeWild->negNsSet = NULL;
13415 }
13416 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013417 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013418 }
13419 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013420 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013421 * then the union of those sets must be the value.
13422 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013423 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013424 int found;
13425 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013426
Daniel Veillard3646d642004-06-02 19:19:14 +000013427 cur = curWild->nsSet;
13428 start = completeWild->nsSet;
13429 while (cur != NULL) {
13430 found = 0;
13431 curB = start;
13432 while (curB != NULL) {
13433 if (cur->value == curB->value) {
13434 found = 1;
13435 break;
13436 }
13437 curB = curB->next;
13438 }
13439 if (!found) {
13440 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013441 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013442 return (-1);
13443 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013444 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013445 completeWild->nsSet = tmp;
13446 }
13447 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013448 }
13449
Daniel Veillard3646d642004-06-02 19:19:14 +000013450 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013451 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013452 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013453 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013454 * or ·absent·), then a pair of not and ·absent· must be the value.
13455 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013456 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013457 (curWild->negNsSet != NULL) &&
13458 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13459 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013460
13461 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013462 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013463 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013464 * 5.
13465 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013466 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013467 (completeWild->negNsSet->value != NULL) &&
13468 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013469 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013470 (curWild->negNsSet->value != NULL) &&
13471 (completeWild->nsSet != NULL))) {
13472
13473 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013474
Daniel Veillard3646d642004-06-02 19:19:14 +000013475 if (completeWild->nsSet != NULL) {
13476 cur = completeWild->nsSet;
13477 curB = curWild->negNsSet;
13478 } else {
13479 cur = curWild->nsSet;
13480 curB = completeWild->negNsSet;
13481 }
13482 nsFound = 0;
13483 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013484 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013485 absentFound = 1;
13486 else if (cur->value == curB->value)
13487 nsFound = 1;
13488 if (nsFound && absentFound)
13489 break;
13490 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013491 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013492
13493 if (nsFound && absentFound) {
13494 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013495 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013496 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013497 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013498 completeWild->any = 1;
13499 if (completeWild->nsSet != NULL) {
13500 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13501 completeWild->nsSet = NULL;
13502 }
13503 if (completeWild->negNsSet != NULL) {
13504 xmlFree(completeWild->negNsSet);
13505 completeWild->negNsSet = NULL;
13506 }
13507 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013508 /*
13509 * 5.2 If the set S includes the negated namespace name
13510 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013511 * be the value.
13512 */
13513 if (completeWild->nsSet != NULL) {
13514 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13515 completeWild->nsSet = NULL;
13516 }
13517 if (completeWild->negNsSet == NULL) {
13518 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13519 if (completeWild->negNsSet == NULL)
13520 return (-1);
13521 }
13522 completeWild->negNsSet->value = NULL;
13523 } else if ((!nsFound) && absentFound) {
13524 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013525 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013526 * namespace name, then the union is not expressible.
13527 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013528 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013529 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013530 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013531 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013532 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013533 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013534 /*
13535 * 5.4 If the set S does not include either the negated namespace
13536 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013537 * and a namespace name must be the value.
13538 */
13539 if (completeWild->negNsSet == NULL) {
13540 if (completeWild->nsSet != NULL) {
13541 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13542 completeWild->nsSet = NULL;
13543 }
13544 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13545 if (completeWild->negNsSet == NULL)
13546 return (-1);
13547 completeWild->negNsSet->value = curWild->negNsSet->value;
13548 }
13549 }
13550 return (0);
13551 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013552 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013553 * 6.
13554 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013555 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013556 (completeWild->negNsSet->value == NULL) &&
13557 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013558 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013559 (curWild->negNsSet->value == NULL) &&
13560 (completeWild->nsSet != NULL))) {
13561
13562 if (completeWild->nsSet != NULL) {
13563 cur = completeWild->nsSet;
13564 } else {
13565 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013566 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013567 while (cur != NULL) {
13568 if (cur->value == NULL) {
13569 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013570 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013571 * value.
13572 */
13573 completeWild->any = 1;
13574 if (completeWild->nsSet != NULL) {
13575 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13576 completeWild->nsSet = NULL;
13577 }
13578 if (completeWild->negNsSet != NULL) {
13579 xmlFree(completeWild->negNsSet);
13580 completeWild->negNsSet = NULL;
13581 }
13582 return (0);
13583 }
13584 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013585 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013586 if (completeWild->negNsSet == NULL) {
13587 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013588 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013589 * and ·absent· must be the value.
13590 */
13591 if (completeWild->nsSet != NULL) {
13592 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13593 completeWild->nsSet = NULL;
13594 }
13595 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13596 if (completeWild->negNsSet == NULL)
13597 return (-1);
13598 completeWild->negNsSet->value = NULL;
13599 }
13600 return (0);
13601 }
13602 return (0);
13603
13604}
13605
Daniel Veillard50355f02004-06-08 17:52:16 +000013606/**
13607 * xmlSchemaIntersectWildcards:
13608 * @ctxt: the schema parser context
13609 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013610 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013611 *
13612 * Intersects the namespace constraints of the given wildcards.
13613 * @completeWild will hold the resulting intersection.
13614 * Returns a positive error code on failure, -1 in case of an
13615 * internal error, 0 otherwise.
13616 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013617static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013618xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013619 xmlSchemaWildcardPtr completeWild,
13620 xmlSchemaWildcardPtr curWild)
13621{
William M. Brack803812b2004-06-03 02:11:24 +000013622 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013623
13624 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013625 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013626 * value.
13627 */
13628 if ((completeWild->any == curWild->any) &&
13629 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13630 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013631
Daniel Veillard3646d642004-06-02 19:19:14 +000013632 if ((completeWild->negNsSet == NULL) ||
13633 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013634
Daniel Veillard3646d642004-06-02 19:19:14 +000013635 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013636 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013637
13638 /*
13639 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013640 */
13641 cur = completeWild->nsSet;
13642 while (cur != NULL) {
13643 found = 0;
13644 curB = curWild->nsSet;
13645 while (curB != NULL) {
13646 if (cur->value == curB->value) {
13647 found = 1;
13648 break;
13649 }
13650 curB = curB->next;
13651 }
13652 if (!found)
13653 break;
13654 cur = cur->next;
13655 }
13656 if (found)
13657 return(0);
13658 } else
13659 return(0);
13660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013661 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013662 /*
13663 * 2 If either O1 or O2 is any, then the other must be the value.
13664 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013665 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013666 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013667 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013668 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013669 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013670 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013671 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13672 * name or ·absent·) and the other is a set of (namespace names or
13673 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013674 * the set, minus ·absent· if it was in the set, must be the value.
13675 */
13676 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13677 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13678 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013679
Daniel Veillard3646d642004-06-02 19:19:14 +000013680 if (completeWild->nsSet == NULL) {
13681 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013682 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013683 return(-1);
13684 } else
13685 neg = curWild->negNsSet->value;
13686 /*
13687 * Remove absent and negated.
13688 */
13689 prev = NULL;
13690 cur = completeWild->nsSet;
13691 while (cur != NULL) {
13692 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013693 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013694 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013695 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013696 prev->next = cur->next;
13697 xmlFree(cur);
13698 break;
13699 }
13700 prev = cur;
13701 cur = cur->next;
13702 }
13703 if (neg != NULL) {
13704 prev = NULL;
13705 cur = completeWild->nsSet;
13706 while (cur != NULL) {
13707 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013708 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013709 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013710 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013711 prev->next = cur->next;
13712 xmlFree(cur);
13713 break;
13714 }
13715 prev = cur;
13716 cur = cur->next;
13717 }
13718 }
13719
13720 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013721 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013722 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013723 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013724 * then the intersection of those sets must be the value.
13725 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013726 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013727 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013728
Daniel Veillard3646d642004-06-02 19:19:14 +000013729 cur = completeWild->nsSet;
13730 prev = NULL;
13731 while (cur != NULL) {
13732 found = 0;
13733 curB = curWild->nsSet;
13734 while (curB != NULL) {
13735 if (cur->value == curB->value) {
13736 found = 1;
13737 break;
13738 }
13739 curB = curB->next;
13740 }
13741 if (!found) {
13742 if (prev == NULL)
13743 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013744 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013745 prev->next = cur->next;
13746 tmp = cur->next;
13747 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013748 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013749 continue;
13750 }
13751 prev = cur;
13752 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013753 }
13754
Daniel Veillard3646d642004-06-02 19:19:14 +000013755 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013756 }
13757 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013758 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013759 */
13760 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013761 (curWild->negNsSet != NULL) &&
13762 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013763 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013764 (curWild->negNsSet->value != NULL)) {
13765
13766 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013767 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013768 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013769 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013770 }
13771 /*
13772 * 6 If the one is a negation of a namespace name and the other
13773 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013774 * of a namespace name must be the value.
13775 */
13776 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13777 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013778 (completeWild->negNsSet->value == NULL)) {
13779 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013780 }
13781 return(0);
13782}
13783
Daniel Veillard50355f02004-06-08 17:52:16 +000013784/**
13785 * xmlSchemaIsWildcardNsConstraintSubset:
13786 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013787 * @sub: the first wildcard
13788 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013789 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013790 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13791 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013792 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013793 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013794 */
13795static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013796xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13797 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013798{
Daniel Veillard50355f02004-06-08 17:52:16 +000013799 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013800 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013801 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013802 if (super->any)
13803 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013804 /*
13805 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13806 * 2.2 super must be a pair of not and the same value.
13807 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013808 if ((sub->negNsSet != NULL) &&
13809 (super->negNsSet != NULL) &&
13810 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013811 return (0);
13812 /*
13813 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013814 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013815 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013816 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013817 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013818 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013819 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013820 xmlSchemaWildcardNsPtr cur, curB;
13821 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013822
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013823 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013824 while (cur != NULL) {
13825 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013826 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013827 while (curB != NULL) {
13828 if (cur->value == curB->value) {
13829 found = 1;
13830 break;
13831 }
13832 curB = curB->next;
13833 }
13834 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013835 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013836 cur = cur->next;
13837 }
13838 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013839 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013840 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013841 xmlSchemaWildcardNsPtr cur;
13842 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013843 * 3.2.2 super must be a pair of not and a namespace name or
13844 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013845 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013846 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013847 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013848 if (cur->value == super->negNsSet->value)
13849 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013850 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013851 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013852 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013853 }
13854 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013855 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013856}
13857
Daniel Veillard50355f02004-06-08 17:52:16 +000013858static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013859xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013860 int *fixed,
13861 const xmlChar **value,
13862 xmlSchemaValPtr *val)
13863{
13864 *fixed = 0;
13865 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013866 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013867 *val = NULL;
13868
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013869 if (attruse->defValue != NULL) {
13870 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013871 if (val != NULL)
13872 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013873 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013874 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013875 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013876 } else if ((attruse->attrDecl != NULL) &&
13877 (attruse->attrDecl->defValue != NULL)) {
13878 *value = attruse->attrDecl->defValue;
13879 if (val != NULL)
13880 *val = attruse->attrDecl->defVal;
13881 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013882 *fixed = 1;
13883 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013884 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013885 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013886}
Daniel Veillard3646d642004-06-02 19:19:14 +000013887/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013888 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013889 * @wild: the wildcard
13890 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013891 *
13892 * Validation Rule: Wildcard allows Namespace Name
13893 * (cvc-wildcard-namespace)
13894 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013895 * Returns 0 if the given namespace matches the wildcard,
13896 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013897 */
13898static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013899xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13900 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013901{
13902 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013903 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013904
13905 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013906 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013907 else if (wild->nsSet != NULL) {
13908 xmlSchemaWildcardNsPtr cur;
13909
13910 cur = wild->nsSet;
13911 while (cur != NULL) {
13912 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013913 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013914 cur = cur->next;
13915 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013916 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013917 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013918 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013919
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013920 return(1);
13921}
13922
13923#define XML_SCHEMA_ACTION_DERIVE 0
13924#define XML_SCHEMA_ACTION_REDEFINE 1
13925
13926#define WXS_ACTION_STR(a) \
13927((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13928
13929/*
13930* Schema Component Constraint:
13931* Derivation Valid (Restriction, Complex)
13932* derivation-ok-restriction (2) - (4)
13933*
13934* ATTENTION:
13935* In XML Schema 1.1 this will be:
13936* Validation Rule:
13937* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13938*
13939*/
13940static int
13941xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13942 int action,
13943 xmlSchemaBasicItemPtr item,
13944 xmlSchemaBasicItemPtr baseItem,
13945 xmlSchemaItemListPtr uses,
13946 xmlSchemaItemListPtr baseUses,
13947 xmlSchemaWildcardPtr wild,
13948 xmlSchemaWildcardPtr baseWild)
13949{
13950 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013951 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013952 const xmlChar *bEffValue;
13953 int effFixed;
13954
13955 if (uses != NULL) {
13956 for (i = 0; i < uses->nbItems; i++) {
13957 cur = uses->items[i];
13958 found = 0;
13959 if (baseUses == NULL)
13960 goto not_found;
13961 for (j = 0; j < baseUses->nbItems; j++) {
13962 bcur = baseUses->items[j];
13963 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13964 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13965 (WXS_ATTRUSE_DECL_TNS(cur) ==
13966 WXS_ATTRUSE_DECL_TNS(bcur)))
13967 {
13968 /*
13969 * (2.1) "If there is an attribute use in the {attribute
13970 * uses} of the {base type definition} (call this B) whose
13971 * {attribute declaration} has the same {name} and {target
13972 * namespace}, then all of the following must be true:"
13973 */
13974 found = 1;
13975
13976 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
13977 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
13978 {
13979 xmlChar *str = NULL;
13980 /*
13981 * (2.1.1) "one of the following must be true:"
13982 * (2.1.1.1) "B's {required} is false."
13983 * (2.1.1.2) "R's {required} is true."
13984 */
13985 xmlSchemaPAttrUseErr4(pctxt,
13986 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
13987 WXS_ITEM_NODE(item), item, cur,
13988 "The 'optional' attribute use is inconsistent "
13989 "with the corresponding 'required' attribute use of "
13990 "the %s %s",
13991 WXS_ACTION_STR(action),
13992 xmlSchemaGetComponentDesignation(&str, baseItem),
13993 NULL, NULL);
13994 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013995 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013996 } else if (xmlSchemaCheckCOSSTDerivedOK(pctxt,
13997 WXS_ATTRUSE_TYPEDEF(cur),
13998 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
13999 {
14000 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14001
14002 /*
14003 * SPEC (2.1.2) "R's {attribute declaration}'s
14004 * {type definition} must be validly derived from
14005 * B's {type definition} given the empty set as
14006 * defined in Type Derivation OK (Simple) (§3.14.6)."
14007 */
14008 xmlSchemaPAttrUseErr4(pctxt,
14009 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14010 WXS_ITEM_NODE(item), item, cur,
14011 "The attribute declaration's %s "
14012 "is not validly derived from "
14013 "the corresponding %s of the "
14014 "attribute declaration in the %s %s",
14015 xmlSchemaGetComponentDesignation(&strA,
14016 WXS_ATTRUSE_TYPEDEF(cur)),
14017 xmlSchemaGetComponentDesignation(&strB,
14018 WXS_ATTRUSE_TYPEDEF(bcur)),
14019 WXS_ACTION_STR(action),
14020 xmlSchemaGetComponentDesignation(&strC, baseItem));
14021 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14022 FREE_AND_NULL(strA);
14023 FREE_AND_NULL(strB);
14024 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014025 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014026 } else {
14027 /*
14028 * 2.1.3 [Definition:] Let the effective value
14029 * constraint of an attribute use be its {value
14030 * constraint}, if present, otherwise its {attribute
14031 * declaration}'s {value constraint} .
14032 */
14033 xmlSchemaGetEffectiveValueConstraint(bcur,
14034 &effFixed, &bEffValue, NULL);
14035 /*
14036 * 2.1.3 ... one of the following must be true
14037 *
14038 * 2.1.3.1 B's ·effective value constraint· is
14039 * ·absent· or default.
14040 */
14041 if ((bEffValue != NULL) &&
14042 (effFixed == 1)) {
14043 const xmlChar *rEffValue = NULL;
14044
14045 xmlSchemaGetEffectiveValueConstraint(bcur,
14046 &effFixed, &rEffValue, NULL);
14047 /*
14048 * 2.1.3.2 R's ·effective value constraint· is
14049 * fixed with the same string as B's.
14050 * MAYBE TODO: Compare the computed values.
14051 * Hmm, it says "same string" so
14052 * string-equality might really be sufficient.
14053 */
14054 if ((effFixed == 0) ||
14055 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14056 {
14057 xmlChar *str = NULL;
14058
14059 xmlSchemaPAttrUseErr4(pctxt,
14060 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14061 WXS_ITEM_NODE(item), item, cur,
14062 "The effective value constraint of the "
14063 "attribute use is inconsistent with "
14064 "its correspondent in the %s %s",
14065 WXS_ACTION_STR(action),
14066 xmlSchemaGetComponentDesignation(&str,
14067 baseItem),
14068 NULL, NULL);
14069 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014070 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014071 }
14072 }
14073 }
14074 break;
14075 }
14076 }
14077not_found:
14078 if (!found) {
14079 /*
14080 * (2.2) "otherwise the {base type definition} must have an
14081 * {attribute wildcard} and the {target namespace} of the
14082 * R's {attribute declaration} must be ·valid· with respect
14083 * to that wildcard, as defined in Wildcard allows Namespace
14084 * Name (§3.10.4)."
14085 */
14086 if ((baseWild == NULL) ||
14087 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14088 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14089 {
14090 xmlChar *str = NULL;
14091
14092 xmlSchemaPAttrUseErr4(pctxt,
14093 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14094 WXS_ITEM_NODE(item), item, cur,
14095 "Neither a matching attribute use, "
14096 "nor a matching wildcard exists in the %s %s",
14097 WXS_ACTION_STR(action),
14098 xmlSchemaGetComponentDesignation(&str, baseItem),
14099 NULL, NULL);
14100 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014101 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014102 }
14103 }
14104 }
14105 }
14106 /*
14107 * SPEC derivation-ok-restriction (3):
14108 * (3) "For each attribute use in the {attribute uses} of the {base type
14109 * definition} whose {required} is true, there must be an attribute
14110 * use with an {attribute declaration} with the same {name} and
14111 * {target namespace} as its {attribute declaration} in the {attribute
14112 * uses} of the complex type definition itself whose {required} is true.
14113 */
14114 if (baseUses != NULL) {
14115 for (j = 0; j < baseUses->nbItems; j++) {
14116 bcur = baseUses->items[j];
14117 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14118 continue;
14119 found = 0;
14120 if (uses != NULL) {
14121 for (i = 0; i < uses->nbItems; i++) {
14122 cur = uses->items[i];
14123 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14124 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14125 (WXS_ATTRUSE_DECL_TNS(cur) ==
14126 WXS_ATTRUSE_DECL_TNS(bcur))) {
14127 found = 1;
14128 break;
14129 }
14130 }
14131 }
14132 if (!found) {
14133 xmlChar *strA = NULL, *strB = NULL;
14134
14135 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14136 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14137 NULL, item,
14138 "A matching attribute use for the "
14139 "'required' %s of the %s %s is missing",
14140 xmlSchemaGetComponentDesignation(&strA, bcur),
14141 WXS_ACTION_STR(action),
14142 xmlSchemaGetComponentDesignation(&strB, baseItem),
14143 NULL);
14144 FREE_AND_NULL(strA);
14145 FREE_AND_NULL(strB);
14146 }
14147 }
14148 }
14149 /*
14150 * derivation-ok-restriction (4)
14151 */
14152 if (wild != NULL) {
14153 /*
14154 * (4) "If there is an {attribute wildcard}, all of the
14155 * following must be true:"
14156 */
14157 if (baseWild == NULL) {
14158 xmlChar *str = NULL;
14159
14160 /*
14161 * (4.1) "The {base type definition} must also have one."
14162 */
14163 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14164 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14165 NULL, item,
14166 "The %s has an attribute wildcard, "
14167 "but the %s %s '%s' does not have one",
14168 WXS_ITEM_TYPE_NAME(item),
14169 WXS_ACTION_STR(action),
14170 WXS_ITEM_TYPE_NAME(baseItem),
14171 xmlSchemaGetComponentQName(&str, baseItem));
14172 FREE_AND_NULL(str);
14173 return(pctxt->err);
14174 } else if ((baseWild->any == 0) &&
14175 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14176 {
14177 xmlChar *str = NULL;
14178 /*
14179 * (4.2) "The complex type definition's {attribute wildcard}'s
14180 * {namespace constraint} must be a subset of the {base type
14181 * definition}'s {attribute wildcard}'s {namespace constraint},
14182 * as defined by Wildcard Subset (§3.10.6)."
14183 */
14184 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14185 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14186 NULL, item,
14187 "The attribute wildcard is not a valid "
14188 "subset of the wildcard in the %s %s '%s'",
14189 WXS_ACTION_STR(action),
14190 WXS_ITEM_TYPE_NAME(baseItem),
14191 xmlSchemaGetComponentQName(&str, baseItem),
14192 NULL);
14193 FREE_AND_NULL(str);
14194 return(pctxt->err);
14195 }
14196 /* 4.3 Unless the {base type definition} is the ·ur-type
14197 * definition·, the complex type definition's {attribute
14198 * wildcard}'s {process contents} must be identical to or
14199 * stronger than the {base type definition}'s {attribute
14200 * wildcard}'s {process contents}, where strict is stronger
14201 * than lax is stronger than skip.
14202 */
14203 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14204 (wild->processContents < baseWild->processContents)) {
14205 xmlChar *str = NULL;
14206 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14207 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14208 NULL, baseItem,
14209 "The {process contents} of the attribute wildcard is "
14210 "weaker than the one in the %s %s '%s'",
14211 WXS_ACTION_STR(action),
14212 WXS_ITEM_TYPE_NAME(baseItem),
14213 xmlSchemaGetComponentQName(&str, baseItem),
14214 NULL);
14215 FREE_AND_NULL(str)
14216 return(pctxt->err);
14217 }
14218 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014219 return(0);
14220}
14221
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014222
14223static int
14224xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14225 xmlSchemaBasicItemPtr item,
14226 xmlSchemaWildcardPtr *completeWild,
14227 xmlSchemaItemListPtr list,
14228 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014229/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014230 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014231 * @ctxt: the schema parser context
14232 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014233 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014234 *
14235 * Builds the wildcard and the attribute uses on the given complex type.
14236 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014237 *
14238 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14239 * strings, so recheck this if we start to hardcode some schemata, since
14240 * they might not be in the same dict.
14241 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014242 */
14243static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014244xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014245 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014246{
14247 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014248 xmlSchemaAttributeUsePtr use;
14249 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014250
Daniel Veillard01fa6152004-06-29 17:04:39 +000014251 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014252 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014253 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014254 return (-1);
14255 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014256 baseType = type->baseType;
14257 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14258 if (xmlSchemaTypeFixup(baseType, pctxt) == -1)
14259 return(-1);
14260
14261 uses = type->attrUses;
14262 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014263 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014264 * Expand attribute group references. And build the 'complete'
14265 * wildcard, i.e. intersect multiple wildcards.
14266 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014267 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014268 if (uses != NULL) {
14269 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014270 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014271 * This one will transfer all attr. prohibitions
14272 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014273 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014274 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14275 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14276 pctxt->attrProhibs) == -1)
14277 {
14278 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14279 "failed to expand attributes");
14280 }
14281 if (pctxt->attrProhibs->nbItems != 0)
14282 prohibs = pctxt->attrProhibs;
14283 } else {
14284 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14285 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14286 NULL) == -1)
14287 {
14288 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14289 "failed to expand attributes");
14290 }
14291 }
14292 }
14293 /*
14294 * Inherit the attribute uses of the base type.
14295 */
14296 if (baseUses != NULL) {
14297 int i, j;
14298 xmlSchemaAttributeUseProhibPtr pro;
14299
14300 if (WXS_IS_RESTRICTION(type)) {
14301 int usesCount;
14302 xmlSchemaAttributeUsePtr tmp;
14303
14304 if (uses != NULL)
14305 usesCount = uses->nbItems;
14306 else
14307 usesCount = 0;
14308
14309 /* Restriction. */
14310 for (i = 0; i < baseUses->nbItems; i++) {
14311 use = baseUses->items[i];
14312 if (prohibs) {
14313 /*
14314 * Filter out prohibited uses.
14315 */
14316 for (j = 0; j < prohibs->nbItems; j++) {
14317 pro = prohibs->items[j];
14318 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14319 (WXS_ATTRUSE_DECL_TNS(use) ==
14320 pro->targetNamespace))
14321 {
14322 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014323 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014324 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014325 }
14326 if (usesCount) {
14327 /*
14328 * Filter out existing uses.
14329 */
14330 for (j = 0; j < usesCount; j++) {
14331 tmp = uses->items[j];
14332 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14333 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14334 (WXS_ATTRUSE_DECL_TNS(use) ==
14335 WXS_ATTRUSE_DECL_TNS(tmp)))
14336 {
14337 goto inherit_next;
14338 }
14339 }
14340 }
14341 if (uses == NULL) {
14342 type->attrUses = xmlSchemaItemListCreate();
14343 if (type->attrUses == NULL)
14344 goto exit_failure;
14345 uses = type->attrUses;
14346 }
14347 xmlSchemaItemListAddSize(uses, 2, use);
14348inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014350 } else {
14351 /* Extension. */
14352 for (i = 0; i < baseUses->nbItems; i++) {
14353 use = baseUses->items[i];
14354 if (uses == NULL) {
14355 type->attrUses = xmlSchemaItemListCreate();
14356 if (type->attrUses == NULL)
14357 goto exit_failure;
14358 uses = type->attrUses;
14359 }
14360 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14361 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014362 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014363 }
14364 /*
14365 * Shrink attr. uses.
14366 */
14367 if (uses) {
14368 if (uses->nbItems == 0) {
14369 xmlSchemaItemListFree(uses);
14370 type->attrUses = NULL;
14371 }
14372 /*
14373 * TODO: We could shrink the size of the array
14374 * to fit the actual number of items.
14375 */
14376 }
14377 /*
14378 * Compute the complete wildcard.
14379 */
14380 if (WXS_IS_EXTENSION(type)) {
14381 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014382 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014383 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14384 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014385 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014386 if (type->attributeWildcard != NULL) {
14387 /*
14388 * Union the complete wildcard with the base wildcard.
14389 * SPEC {attribute wildcard}
14390 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14391 * and {annotation} are those of the ·complete wildcard·,
14392 * and whose {namespace constraint} is the intensional union
14393 * of the {namespace constraint} of the ·complete wildcard·
14394 * and of the ·base wildcard·, as defined in Attribute
14395 * Wildcard Union (§3.10.6)."
14396 */
14397 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14398 baseType->attributeWildcard) == -1)
14399 goto exit_failure;
14400 } else {
14401 /*
14402 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14403 * then the ·base wildcard·."
14404 */
14405 type->attributeWildcard = baseType->attributeWildcard;
14406 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014407 } else {
14408 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014409 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14410 * ·complete wildcard"
14411 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014412 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014413 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014414 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014415 /*
14416 * SPEC {attribute wildcard}
14417 * (3.1) "If the <restriction> alternative is chosen, then the
14418 * ·complete wildcard·;"
14419 * NOOP
14420 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014421 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014422
Daniel Veillard3646d642004-06-02 19:19:14 +000014423 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014424
14425exit_failure:
14426 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014427}
14428
14429/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014430 * xmlSchemaTypeFinalContains:
14431 * @schema: the schema
14432 * @type: the type definition
14433 * @final: the final
14434 *
14435 * Evaluates if a type definition contains the given "final".
14436 * This does take "finalDefault" into account as well.
14437 *
14438 * Returns 1 if the type does containt the given "final",
14439 * 0 otherwise.
14440 */
14441static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014442xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014443{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014444 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014445 return (0);
14446 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014447 return (1);
14448 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014449 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014450}
14451
14452/**
14453 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14454 * @type: the Union Simple Type
14455 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014456 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014457 * returns NULL otherwise.
14458 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014459static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014460xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14461{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014462 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014463 if (type->memberTypes != NULL)
14464 return (type->memberTypes);
14465 else
14466 type = type->baseType;
14467 }
14468 return (NULL);
14469}
14470
14471/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014472 * xmlSchemaGetParticleTotalRangeMin:
14473 * @particle: the particle
14474 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014475 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014476 * (all and sequence) + (choice)
14477 *
14478 * Returns the minimun Effective Total Range.
14479 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014480static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014481xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014482{
14483 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014484 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014485 return (0);
14486 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014487 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014488 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014489 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014490
14491 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014492 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014493 while (part != NULL) {
14494 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14495 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014496 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014497 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014498 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014499 if (cur == 0)
14500 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014501 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014502 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014503 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014504 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014505 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014506 } else {
14507 /* <all> and <sequence> */
14508 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014509 xmlSchemaParticlePtr part =
14510 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014511
14512 if (part == NULL)
14513 return (0);
14514 do {
14515 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14516 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014517 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014518 else
14519 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014520 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014521 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014522 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014523 }
14524}
14525
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014526/**
14527 * xmlSchemaGetParticleTotalRangeMax:
14528 * @particle: the particle
14529 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014530 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014531 * (all and sequence) + (choice)
14532 *
14533 * Returns the maximum Effective Total Range.
14534 */
14535static int
14536xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14537{
14538 if ((particle->children == NULL) ||
14539 (particle->children->children == NULL))
14540 return (0);
14541 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14542 int max = -1, cur;
14543 xmlSchemaParticlePtr part =
14544 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014545
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014546 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14547 if (part->children == NULL)
14548 continue;
14549 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14550 (part->children->type == XML_SCHEMA_TYPE_ANY))
14551 cur = part->maxOccurs;
14552 else
14553 cur = xmlSchemaGetParticleTotalRangeMax(part);
14554 if (cur == UNBOUNDED)
14555 return (UNBOUNDED);
14556 if ((max < cur) || (max == -1))
14557 max = cur;
14558 }
14559 /* TODO: Handle overflows? */
14560 return (particle->maxOccurs * max);
14561 } else {
14562 /* <all> and <sequence> */
14563 int sum = 0, cur;
14564 xmlSchemaParticlePtr part =
14565 (xmlSchemaParticlePtr) particle->children->children;
14566
14567 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14568 if (part->children == NULL)
14569 continue;
14570 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14571 (part->children->type == XML_SCHEMA_TYPE_ANY))
14572 cur = part->maxOccurs;
14573 else
14574 cur = xmlSchemaGetParticleTotalRangeMax(part);
14575 if (cur == UNBOUNDED)
14576 return (UNBOUNDED);
14577 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14578 return (UNBOUNDED);
14579 sum += cur;
14580 }
14581 /* TODO: Handle overflows? */
14582 return (particle->maxOccurs * sum);
14583 }
14584}
14585
14586/**
14587 * xmlSchemaIsParticleEmptiable:
14588 * @particle: the particle
14589 *
14590 * Schema Component Constraint: Particle Emptiable
14591 * Checks whether the given particle is emptiable.
14592 *
14593 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014594 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014595static int
14596xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14597{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014598 /*
14599 * SPEC (1) "Its {min occurs} is 0."
14600 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014601 if ((particle == NULL) || (particle->minOccurs == 0) ||
14602 (particle->children == NULL))
14603 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014604 /*
14605 * SPEC (2) "Its {term} is a group and the minimum part of the
14606 * effective total range of that group, [...] is 0."
14607 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014608 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014609 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014610 return (1);
14611 }
14612 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014613}
14614
14615/**
14616 * xmlSchemaCheckCOSSTDerivedOK:
14617 * @type: the derived simple type definition
14618 * @baseType: the base type definition
14619 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014620 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014621 * Type Derivation OK (Simple) (cos-st-derived-OK)
14622 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014623 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014624 * derived from @baseType.
14625 *
14626 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014627 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014628static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014629xmlSchemaCheckCOSSTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
14630 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014631 xmlSchemaTypePtr baseType,
14632 int subset)
14633{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014634 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014635 * 1 They are the same type definition.
14636 * TODO: The identy check might have to be more complex than this.
14637 */
14638 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014639 return (0);
14640 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014641 * 2.1 restriction is not in the subset, or in the {final}
14642 * of its own {base type definition};
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014643 * TODO: Revise this, it looks strange.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014644 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014645 if (WXS_IS_TYPE_NOT_FIXED(type))
14646 if (xmlSchemaTypeFixup(type, ctxt) == -1)
14647 return(-1);
14648 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14649 if (xmlSchemaTypeFixup(baseType, ctxt) == -1)
14650 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014651 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014652 (xmlSchemaTypeFinalContains(type->baseType,
14653 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14654 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014655 }
14656 /* 2.2 */
14657 if (type->baseType == baseType) {
14658 /*
14659 * 2.2.1 D's ·base type definition· is B.
14660 */
14661 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014662 }
14663 /*
14664 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14665 * and is validly derived from B given the subset, as defined by this
14666 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014667 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014668 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
14669 (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014670 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014671 return (0);
14672 }
14673 /*
14674 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014675 * definition·.
14676 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014677 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14678 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014679 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014680 }
14681 /*
14682 * 2.2.4 B's {variety} is union and D is validly derived from a type
14683 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014684 * defined by this constraint.
14685 *
14686 * NOTE: This seems not to involve built-in types, since there is no
14687 * built-in Union Simple Type.
14688 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014689 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014690 xmlSchemaTypeLinkPtr cur;
14691
14692 cur = baseType->memberTypes;
14693 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014694 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
14695 if (xmlSchemaTypeFixup(cur->type, ctxt) == -1)
14696 return(-1);
14697 if (xmlSchemaCheckCOSSTDerivedOK(ctxt,
14698 type, cur->type, subset) == 0)
14699 {
14700 /*
14701 * It just has to be validly derived from at least one
14702 * member-type.
14703 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014704 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014705 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014706 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014707 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014708 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014709 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14710}
14711
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014712/**
14713 * xmlSchemaCheckTypeDefCircularInternal:
14714 * @pctxt: the schema parser context
14715 * @ctxtType: the type definition
14716 * @ancestor: an ancestor of @ctxtType
14717 *
14718 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014719 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014720 *
14721 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14722 * circular, 0 otherwise.
14723 */
14724static int
14725xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14726 xmlSchemaTypePtr ctxtType,
14727 xmlSchemaTypePtr ancestor)
14728{
14729 int ret;
14730
14731 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14732 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014733
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014734 if (ctxtType == ancestor) {
14735 xmlSchemaPCustomErr(pctxt,
14736 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014737 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014738 "The definition is circular", NULL);
14739 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14740 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014741 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14742 /*
14743 * Avoid inifinite recursion on circular types not yet checked.
14744 */
14745 return (0);
14746 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014747 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14748 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14749 ancestor->baseType);
14750 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14751 return (ret);
14752}
14753
14754/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014755 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014756 * @item: the complex/simple type definition
14757 * @ctxt: the parser context
14758 * @name: the name
14759 *
14760 * Checks for circular type definitions.
14761 */
14762static void
14763xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014764 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014765{
14766 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014767 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14768 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014769 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014770 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14771 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014772}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014773
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014774/*
14775* Simple Type Definition Representation OK (src-simple-type) 4
14776*
14777* "4 Circular union type definition is disallowed. That is, if the
14778* <union> alternative is chosen, there must not be any entries in the
14779* memberTypes [attribute] at any depth which resolve to the component
14780* corresponding to the <simpleType>."
14781*
14782* Note that this should work on the *representation* of a component,
14783* thus assumes any union types in the member types not being yet
14784* substituted. At this stage we need the variety of the types
14785* to be already computed.
14786*/
14787static int
14788xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14789 xmlSchemaTypePtr ctxType,
14790 xmlSchemaTypeLinkPtr members)
14791{
14792 xmlSchemaTypeLinkPtr member;
14793 xmlSchemaTypePtr memberType;
14794
14795 member = members;
14796 while (member != NULL) {
14797 memberType = member->type;
14798 while ((memberType != NULL) &&
14799 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14800 if (memberType == ctxType) {
14801 xmlSchemaPCustomErr(pctxt,
14802 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014803 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014804 "The union type definition is circular", NULL);
14805 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14806 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014807 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014808 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14809 {
14810 int res;
14811 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14812 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14813 ctxType,
14814 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14815 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14816 if (res != 0)
14817 return(res);
14818 }
14819 memberType = memberType->baseType;
14820 }
14821 member = member->next;
14822 }
14823 return(0);
14824}
14825
14826static int
14827xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14828 xmlSchemaTypePtr type)
14829{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014830 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014831 return(0);
14832 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14833 type->memberTypes));
14834}
14835
Daniel Veillard01fa6152004-06-29 17:04:39 +000014836/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014837 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014838 * @item: the complex/simple type definition
14839 * @ctxt: the parser context
14840 * @name: the name
14841 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014842 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014843 */
14844static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014845xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014846 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014847{
14848 if (typeDef == NULL)
14849 return;
14850
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014851 /*
14852 * Resolve the base type.
14853 */
14854 if (typeDef->baseType == NULL) {
14855 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14856 typeDef->base, typeDef->baseNs);
14857 if (typeDef->baseType == NULL) {
14858 xmlSchemaPResCompAttrErr(ctxt,
14859 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014860 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014861 "base", typeDef->base, typeDef->baseNs,
14862 XML_SCHEMA_TYPE_SIMPLE, NULL);
14863 return;
14864 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014865 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014866 if (WXS_IS_SIMPLE(typeDef)) {
14867 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014868 /*
14869 * Resolve the memberTypes.
14870 */
14871 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14872 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014873 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014874 /*
14875 * Resolve the itemType.
14876 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014877 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14878
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014879 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014880 typeDef->base, typeDef->baseNs);
14881
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014882 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014883 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14884 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014885 typeDef->subtypes = NULL;
14886 xmlSchemaPResCompAttrErr(ctxt,
14887 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014888 WXS_BASIC_CAST typeDef, typeDef->node,
14889 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014890 XML_SCHEMA_TYPE_SIMPLE, NULL);
14891 }
14892 }
14893 return;
14894 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014895 }
14896 /*
14897 * The ball of letters below means, that if we have a particle
14898 * which has a QName-helper component as its {term}, we want
14899 * to resolve it...
14900 */
14901 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14902 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14903 XML_SCHEMA_TYPE_PARTICLE) &&
14904 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14905 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14906 XML_SCHEMA_EXTRA_QNAMEREF))
14907 {
14908 xmlSchemaQNameRefPtr ref =
14909 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14910 xmlSchemaModelGroupDefPtr groupDef;
14911
14912 /*
14913 * URGENT TODO: Test this.
14914 */
14915 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14916 /*
14917 * Resolve the MG definition reference.
14918 */
14919 groupDef =
14920 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14921 ref->itemType, ref->name, ref->targetNamespace);
14922 if (groupDef == NULL) {
14923 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14924 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14925 "ref", ref->name, ref->targetNamespace, ref->itemType,
14926 NULL);
14927 /* Remove the particle. */
14928 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14929 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14930 /* Remove the particle. */
14931 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14932 else {
14933 /*
14934 * Assign the MG definition's {model group} to the
14935 * particle's {term}.
14936 */
14937 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14938
14939 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14940 /*
14941 * SPEC cos-all-limited (1.2)
14942 * "1.2 the {term} property of a particle with
14943 * {max occurs}=1 which is part of a pair which constitutes
14944 * the {content type} of a complex type definition."
14945 */
14946 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14947 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14948 /* TODO: error code */
14949 XML_SCHEMAP_COS_ALL_LIMITED,
14950 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14951 "The particle's {max occurs} must be 1, since the "
14952 "reference resolves to an 'all' model group",
14953 NULL, NULL);
14954 }
14955 }
14956 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014957 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014958}
14959
14960
14961
14962/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014963 * xmlSchemaCheckSTPropsCorrect:
14964 * @ctxt: the schema parser context
14965 * @type: the simple type definition
14966 *
14967 * Checks st-props-correct.
14968 *
14969 * Returns 0 if the properties are correct,
14970 * if not, a positive error code and -1 on internal
14971 * errors.
14972 */
14973static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014974xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014975 xmlSchemaTypePtr type)
14976{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014977 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000014978 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000014979
Daniel Veillardc0826a72004-08-10 14:17:33 +000014980 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014981 /*
14982 * Schema Component Constraint: Simple Type Definition Properties Correct
14983 *
14984 * NOTE: This is somehow redundant, since we actually built a simple type
14985 * to have all the needed information; this acts as an self test.
14986 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014987 /* Base type: If the datatype has been ·derived· by ·restriction·
14988 * then the Simple Type Definition component from which it is ·derived·,
14989 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000014990 */
14991 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014992 /*
14993 * TODO: Think about: "modulo the impact of Missing
14994 * Sub-components (§5.3)."
14995 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000014996 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014997 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014998 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000014999 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015000 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015001
Daniel Veillard01fa6152004-06-29 17:04:39 +000015002 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015003 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015004 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015005 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015006 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015007 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015008 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015009 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015010 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15011 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015012 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015013 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015014 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015015 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015016 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015017 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015018 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015019 "the simple ur-type definition as base type, not '%s'",
15020 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015021 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015022 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15023 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015024 /*
15025 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015026 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015027 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15028 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015029 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015030 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015031 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015032 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015033 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15034 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015035 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015036
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015037 /*
15038 * 3 The {final} of the {base type definition} must not contain restriction.
15039 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015040 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015041 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15042 xmlSchemaPCustomErr(ctxt,
15043 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015044 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015045 "The 'final' of its base type '%s' must not contain "
15046 "'restriction'",
15047 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015048 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015049 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015050 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015051
15052 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015053 * 2 All simple type definitions must be derived ultimately from the ·simple
15054 * ur-type definition (so· circular definitions are disallowed). That is, it
15055 * must be possible to reach a built-in primitive datatype or the ·simple
15056 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015057 *
15058 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015059 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015060 return (0);
15061}
15062
15063/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015064 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015065 * @ctxt: the schema parser context
15066 * @type: the simple type definition
15067 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015068 * Schema Component Constraint:
15069 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15070
15071 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015072 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015073 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015074 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015075 * a positive error code otherwise.
15076 */
15077static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015078xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015079 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015080{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015081 xmlChar *str = NULL;
15082
Daniel Veillard01fa6152004-06-29 17:04:39 +000015083 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015084 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15085 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015086 return (-1);
15087 }
15088
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015089 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015090 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015091 /*
15092 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015093 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015094 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015095 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015096 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015097 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015098 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015099 "The base type '%s' is not an atomic simple type",
15100 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015101 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015102 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15103 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015104 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015105 * restriction.
15106 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015107 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015108 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015109 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015110 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015111 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015112 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015113 "The final of its base type '%s' must not contain 'restriction'",
15114 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015115 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015116 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15117 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015118
15119 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015120 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015121 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015122 * Primitive datatypes.
15123 */
15124 if (type->facets != NULL) {
15125 xmlSchemaFacetPtr facet;
15126 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015127
Daniel Veillard01fa6152004-06-29 17:04:39 +000015128 primitive = xmlSchemaGetPrimitiveType(type);
15129 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015130 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15131 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015132 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015133 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015134 facet = type->facets;
15135 do {
15136 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015137 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015138 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015139 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015140 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015141 }
15142 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015143 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015144 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015145 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015146 }
15147 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015148 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15149 * of the {base type definition} (call this BF),then the DF's {value}
15150 * must be a valid restriction of BF's {value} as defined in
15151 * [XML Schemas: Datatypes]."
15152 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015153 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015154 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015155 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015156 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015157 xmlSchemaTypePtr itemType = NULL;
15158
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015159 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015160 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015161 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15162 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015163 return (-1);
15164 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015165 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015166 xmlSchemaTypeFixup(itemType, pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015167 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015168 * 2.1 The {item type definition} must have a {variety} of atomic or
15169 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015170 * must be atomic).
15171 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015172 if ((! WXS_IS_ATOMIC(itemType)) &&
15173 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015174 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015175 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015176 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015177 "The item type '%s' does not have a variety of atomic or union",
15178 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015179 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015180 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015181 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 xmlSchemaTypeLinkPtr member;
15183
15184 member = itemType->memberTypes;
15185 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015186 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015187 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015188 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015189 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015190 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015191 "member type '%s' of this item type is not atomic",
15192 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015193 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015194 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15195 }
15196 member = member->next;
15197 }
15198 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015199
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015200 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015201 xmlSchemaFacetPtr facet;
15202 /*
15203 * This is the case if we have: <simpleType><list ..
15204 */
15205 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015206 * 2.3.1
15207 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015208 * contain list.
15209 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015210 if (xmlSchemaTypeFinalContains(itemType,
15211 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15212 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015213 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015214 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015215 "The final of its item type '%s' must not contain 'list'",
15216 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015217 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015218 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15219 }
15220 /*
15221 * 2.3.1.2 The {facets} must only contain the whiteSpace
15222 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015223 * OPTIMIZE TODO: the S4S already disallows any facet
15224 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015225 */
15226 if (type->facets != NULL) {
15227 facet = type->facets;
15228 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015229 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015230 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015231 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015232 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015233 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15234 }
15235 facet = facet->next;
15236 } while (facet != NULL);
15237 }
15238 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015239 * MAYBE TODO: (Hmm, not really) Datatypes states:
15240 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15241 * whose ·lexical space· allows space (such as string or anyURI)or
15242 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015243 * ·lexical space· allows space.
15244 */
15245 } else {
15246 /*
15247 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015248 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015249 */
15250 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015251 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015252 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15253 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015254 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015255 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015256 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015257 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015258 "The base type '%s' must be a list type",
15259 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015260 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015261 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15262 }
15263 /*
15264 * 2.3.2.2 The {final} of the {base type definition} must not
15265 * contain restriction.
15266 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015267 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015268 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015269 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015270 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015271 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015272 "The 'final' of the base type '%s' must not contain 'restriction'",
15273 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015274 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015275 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15276 }
15277 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015278 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015279 * from the {base type definition}'s {item type definition} given
15280 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15281 */
15282 {
15283 xmlSchemaTypePtr baseItemType;
15284
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015285 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015286 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15288 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015289 return (-1);
15290 }
15291 if ((itemType != baseItemType) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015292 (xmlSchemaCheckCOSSTDerivedOK(pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015293 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015294 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015295 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015296 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015297 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015298 "The item type '%s' is not validly derived from "
15299 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015300 xmlSchemaGetComponentQName(&str, itemType),
15301 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15302 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015303
15304 FREE_AND_NULL(str)
15305 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015306 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015307 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15308 }
15309 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015310
Daniel Veillard01fa6152004-06-29 17:04:39 +000015311 if (type->facets != NULL) {
15312 xmlSchemaFacetPtr facet;
15313 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015314 /*
15315 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015316 * and enumeration facet components are allowed among the {facets}.
15317 */
15318 facet = type->facets;
15319 do {
15320 switch (facet->type) {
15321 case XML_SCHEMA_FACET_LENGTH:
15322 case XML_SCHEMA_FACET_MINLENGTH:
15323 case XML_SCHEMA_FACET_MAXLENGTH:
15324 case XML_SCHEMA_FACET_WHITESPACE:
15325 /*
15326 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015327 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015328 */
15329 case XML_SCHEMA_FACET_PATTERN:
15330 case XML_SCHEMA_FACET_ENUMERATION:
15331 break;
15332 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015333 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015334 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015335 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015336 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015337 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015338 * invalid facets.
15339 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015340 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015341 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015342 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015343 facet = facet->next;
15344 } while (facet != NULL);
15345 if (ok == 0)
15346 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15347 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015348 * SPEC (2.3.2.5) (same as 1.3.2)
15349 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015350 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015351 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015352 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015353 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015354 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015355 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015356 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015357 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015358 * atomic or list.
15359 */
15360 xmlSchemaTypeLinkPtr member;
15361
15362 member = type->memberTypes;
15363 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015364 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015365 xmlSchemaTypeFixup(member->type, pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015366
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015367 if ((! WXS_IS_ATOMIC(member->type)) &&
15368 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015369 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015370 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015371 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015372 "The member type '%s' is neither an atomic, nor a list type",
15373 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015374 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015375 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15376 }
15377 member = member->next;
15378 }
15379 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015380 * 3.3.1 If the {base type definition} is the ·simple ur-type
15381 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015382 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015383 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015384 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015385 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015386 * {final} which does not contain union.
15387 */
15388 member = type->memberTypes;
15389 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015390 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015391 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015392 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015393 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015394 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015395 "The 'final' of member type '%s' contains 'union'",
15396 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015397 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015398 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15399 }
15400 member = member->next;
15401 }
15402 /*
15403 * 3.3.1.2 The {facets} must be empty.
15404 */
15405 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015406 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015407 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015408 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015409 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015410 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15411 }
15412 } else {
15413 /*
15414 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015415 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015416 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015417 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015418 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015419 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015420 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015421 "The base type '%s' is not a union type",
15422 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015423 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015424 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15425 }
15426 /*
15427 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15428 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015429 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015431 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015432 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015433 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015434 "The 'final' of its base type '%s' must not contain 'restriction'",
15435 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015436 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015437 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15438 }
15439 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015440 * 3.3.2.3 The {member type definitions}, in order, must be validly
15441 * derived from the corresponding type definitions in the {base
15442 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015443 * as defined in Type Derivation OK (Simple) (§3.14.6).
15444 */
15445 {
15446 xmlSchemaTypeLinkPtr baseMember;
15447
15448 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015449 * OPTIMIZE: if the type is restricting, it has no local defined
15450 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015451 * thus a check for equality can be skipped.
15452 */
15453 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015454 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015455 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015456 * types of it's base type. This check seems not necessary with
15457 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015458 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015459 */
15460 if (type->memberTypes != NULL) {
15461 member = type->memberTypes;
15462 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015463 if ((member == NULL) && (baseMember != NULL)) {
15464 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15465 "different number of member types in base");
15466 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015467 while (member != NULL) {
15468 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015469 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15470 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015471 }
15472 if ((member->type != baseMember->type) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015473 (xmlSchemaCheckCOSSTDerivedOK(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015474 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015475 xmlChar *strBMT = NULL, *strBT = NULL;
15476
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015477 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015478 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015479 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015480 "The member type %s is not validly "
15481 "derived from its corresponding member "
15482 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015483 xmlSchemaGetComponentQName(&str, member->type),
15484 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15485 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015486 FREE_AND_NULL(str)
15487 FREE_AND_NULL(strBMT)
15488 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015489 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015490 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015491 member = member->next;
15492 baseMember = baseMember->next;
15493 }
15494 }
15495 }
15496 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015497 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015498 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015499 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015500 if (type->facets != NULL) {
15501 xmlSchemaFacetPtr facet;
15502 int ok = 1;
15503
15504 facet = type->facets;
15505 do {
15506 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15507 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015508 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015509 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015510 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015511 ok = 0;
15512 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015513 facet = facet->next;
15514 } while (facet != NULL);
15515 if (ok == 0)
15516 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517
Daniel Veillard01fa6152004-06-29 17:04:39 +000015518 }
15519 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015520 * SPEC (3.3.2.5) (same as 1.3.2)
15521 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015522 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015523 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015524 */
15525 }
15526 }
15527
15528 return (0);
15529}
15530
15531/**
15532 * xmlSchemaCheckSRCSimpleType:
15533 * @ctxt: the schema parser context
15534 * @type: the simple type definition
15535 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015536 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015537 *
15538 * Returns 0 if the constraints are satisfied,
15539 * if not a positive error code and -1 on internal
15540 * errors.
15541 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015542#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015543static int
15544xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15545 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015546{
15547 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015548 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015549 * must satisfy the conditions set out in Constraints on Simple Type
15550 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015551 */
15552 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015553 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015554 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015555 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015556 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015557 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015558 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015559 /*
15560 *
15561 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015562 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015563 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015564 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015565 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015566 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015567 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015568 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015569 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015570 /*
15571 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015572 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015573 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015574 return (0);
15575}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015576#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015577
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015578static int
15579xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15580{
15581 if (ctxt->vctxt == NULL) {
15582 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15583 if (ctxt->vctxt == NULL) {
15584 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015585 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015586 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015587 "failed to create a temp. validation context.\n",
15588 NULL, NULL);
15589 return (-1);
15590 }
15591 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015592 xmlSchemaSetValidErrors(ctxt->vctxt,
15593 ctxt->error, ctxt->warning, ctxt->userData);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015594 }
15595 return (0);
15596}
15597
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015598static int
15599xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15600 xmlNodePtr node,
15601 xmlSchemaTypePtr type,
15602 const xmlChar *value,
15603 xmlSchemaValPtr *retVal,
15604 int fireErrors,
15605 int normalize,
15606 int isNormalized);
15607
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015608/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015609 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015610 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015611 * @type: the simple type definition
15612 * @value: the default value
15613 * @node: an optional node (the holder of the value)
15614 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015615 * Schema Component Constraint: Element Default Valid (Immediate)
15616 * (cos-valid-default)
15617 * This will be used by the parser only. For the validator there's
15618 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015619 *
15620 * Returns 0 if the constraints are satisfied,
15621 * if not, a positive error code and -1 on internal
15622 * errors.
15623 */
15624static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015625xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15626 xmlNodePtr node,
15627 xmlSchemaTypePtr type,
15628 const xmlChar *value,
15629 xmlSchemaValPtr *val)
15630{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015631 int ret = 0;
15632
15633 /*
15634 * cos-valid-default:
15635 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015636 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015637 * definition the appropriate case among the following must be true:
15638 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015639 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015640 /*
15641 * Complex type.
15642 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015643 * SPEC (2.1) "its {content type} must be a simple type definition
15644 * or mixed."
15645 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015646 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015647 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015648 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015649 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15650 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015651 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015652 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015653 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015654 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015655 "For a string to be a valid default, the type definition "
15656 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015657 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015658 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15659 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015660 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015661 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015662 * 1 If the type definition is a simple type definition, then the string
15663 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015664 * Valid (§3.14.4).
15665 *
15666 * AND
15667 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015668 * 2.2.1 If the {content type} is a simple type definition, then the
15669 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015670 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015671 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015672 if (WXS_IS_SIMPLE(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, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015675 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015676 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015677 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015678 else
15679 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015680
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015681 if (ret < 0) {
15682 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15683 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015684 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015685
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015686 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015687}
15688
15689/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015690 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015691 * @ctxt: the schema parser context
15692 * @type: the complex type definition
15693 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015694 *.(4.6) Constraints on Complex Type Definition Schema Components
15695 * Schema Component Constraint:
15696 * Complex Type Definition Properties Correct (ct-props-correct)
15697 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015698 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015699 * Returns 0 if the constraints are satisfied, a positive
15700 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015701 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015702static int
15703xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15704 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015705{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015706 /*
15707 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15708 *
15709 * SPEC (1) "The values of the properties of a complex type definition must
15710 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015711 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015712 * Sub-components (§5.3)."
15713 */
15714 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015715 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015716 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015717 /*
15718 * SPEC (2) "If the {base type definition} is a simple type definition,
15719 * the {derivation method} must be extension."
15720 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015721 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015722 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015723 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015724 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015725 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015726 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015727 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015728 /*
15729 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15730 * definition·. That is, it must be possible to reach the ·ur-type
15731 * definition by repeatedly following the {base type definition}."
15732 *
15733 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015734 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015735 /*
15736 * NOTE that (4) and (5) need the following:
15737 * - attribute uses need to be already inherited (apply attr. prohibitions)
15738 * - attribute group references need to be expanded already
15739 * - simple types need to be typefixed already
15740 */
15741 if (type->attrUses &&
15742 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15743 {
15744 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15745 xmlSchemaAttributeUsePtr use, tmp;
15746 int i, j, hasId = 0;
15747
15748 for (i = uses->nbItems -1; i >= 0; i--) {
15749 use = uses->items[i];
15750
15751 /*
15752 * SPEC ct-props-correct
15753 * (4) "Two distinct attribute declarations in the
15754 * {attribute uses} must not have identical {name}s and
15755 * {target namespace}s."
15756 */
15757 if (i > 0) {
15758 for (j = i -1; j >= 0; j--) {
15759 tmp = uses->items[j];
15760 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15761 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15762 (WXS_ATTRUSE_DECL_TNS(use) ==
15763 WXS_ATTRUSE_DECL_TNS(tmp)))
15764 {
15765 xmlChar *str = NULL;
15766
15767 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15768 XML_SCHEMAP_AG_PROPS_CORRECT,
15769 NULL, WXS_BASIC_CAST type,
15770 "Duplicate %s",
15771 xmlSchemaGetComponentDesignation(&str, use),
15772 NULL);
15773 FREE_AND_NULL(str);
15774 /*
15775 * Remove the duplicate.
15776 */
15777 if (xmlSchemaItemListRemove(uses, i) == -1)
15778 goto exit_failure;
15779 goto next_use;
15780 }
15781 }
15782 }
15783 /*
15784 * SPEC ct-props-correct
15785 * (5) "Two distinct attribute declarations in the
15786 * {attribute uses} must not have {type definition}s which
15787 * are or are derived from ID."
15788 */
15789 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15790 if (xmlSchemaIsDerivedFromBuiltInType(
15791 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15792 {
15793 if (hasId) {
15794 xmlChar *str = NULL;
15795
15796 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15797 XML_SCHEMAP_AG_PROPS_CORRECT,
15798 NULL, WXS_BASIC_CAST type,
15799 "There must not exist more than one attribute "
15800 "declaration of type 'xs:ID' "
15801 "(or derived from 'xs:ID'). The %s violates this "
15802 "constraint",
15803 xmlSchemaGetComponentDesignation(&str, use),
15804 NULL);
15805 FREE_AND_NULL(str);
15806 if (xmlSchemaItemListRemove(uses, i) == -1)
15807 goto exit_failure;
15808 }
15809
15810 hasId = 1;
15811 }
15812 }
15813next_use: {}
15814 }
15815 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015816 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015817exit_failure:
15818 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015819}
15820
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015821static int
15822xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15823 xmlSchemaTypePtr typeB)
15824{
15825 /*
15826 * TODO: This should implement component-identity
15827 * in the future.
15828 */
15829 if ((typeA == NULL) || (typeB == NULL))
15830 return (0);
15831 return (typeA == typeB);
15832}
15833
15834/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015835 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015836 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015837 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015838 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015839 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015840 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015841 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015842 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15843 *
15844 * STATUS: completed
15845 *
15846 * Returns 0 if the constraints are satisfied, or 1
15847 * if not.
15848 */
15849static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015850xmlSchemaCheckCOSCTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15851 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015852 xmlSchemaTypePtr baseType,
15853 int set)
15854{
15855 int equal = xmlSchemaAreEqualTypes(type, baseType);
15856 /* TODO: Error codes. */
15857 /*
15858 * SPEC "For a complex type definition (call it D, for derived)
15859 * to be validly derived from a type definition (call this
15860 * B, for base) given a subset of {extension, restriction}
15861 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015862 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015863 if (! equal) {
15864 /*
15865 * SPEC (1) "If B and D are not the same type definition, then the
15866 * {derivation method} of D must not be in the subset."
15867 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015868 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15869 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015870 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015871 } else {
15872 /*
15873 * SPEC (2.1) "B and D must be the same type definition."
15874 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015875 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015876 }
15877 /*
15878 * SPEC (2.2) "B must be D's {base type definition}."
15879 */
15880 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015881 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015882 /*
15883 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15884 * definition·."
15885 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015886 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015887 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015888
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015889 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015890 /*
15891 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15892 * must be validly derived from B given the subset as defined by this
15893 * constraint."
15894 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015895 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015896 baseType, set));
15897 } else {
15898 /*
15899 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15900 * must be validly derived from B given the subset as defined in Type
15901 * Derivation OK (Simple) (§3.14.6).
15902 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015903 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
15904 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015905 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015906}
15907
15908/**
15909 * xmlSchemaCheckCOSDerivedOK:
15910 * @type: the derived simple type definition
15911 * @baseType: the base type definition
15912 *
15913 * Calls:
15914 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015915 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015916 * Checks wheter @type can be validly derived from @baseType.
15917 *
15918 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015919 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015920static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015921xmlSchemaCheckCOSDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15922 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015923 xmlSchemaTypePtr baseType,
15924 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015925{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015926 if (WXS_IS_SIMPLE(type))
15927 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015928 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015929 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015930}
15931
William M. Brack2f2a6632004-08-20 23:09:47 +000015932/**
15933 * xmlSchemaCheckCOSCTExtends:
15934 * @ctxt: the schema parser context
15935 * @type: the complex type definition
15936 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015937 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015938 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015939 * Derivation Valid (Extension) (cos-ct-extends)
15940 *
15941 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015942 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015943 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015944 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015945 *
15946 * Returns 0 if the constraints are satisfied, a positive
15947 * error code if not and -1 if an internal error occured.
15948 */
15949static int
15950xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15951 xmlSchemaTypePtr type)
15952{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015953 xmlSchemaTypePtr base = type->baseType;
15954 /*
15955 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15956 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015957 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015958 /*
15959 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015960 * then all of the following must be true:"
15961 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015962 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015963 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015964 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015965 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000015966 */
15967 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
15968 xmlSchemaPCustomErr(ctxt,
15969 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015970 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000015971 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015972 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000015973 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
15974 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015975
15976 /*
15977 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
15978 * since they are automatically satisfied through the
15979 * inheriting mechanism.
15980 * Note that even if redefining components, the inheriting mechanism
15981 * is used.
15982 */
15983#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000015984 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015985 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015986 * uses}
15987 * of the complex type definition itself, that is, for every attribute
15988 * use in the {attribute uses} of the {base type definition}, there
15989 * must be an attribute use in the {attribute uses} of the complex
15990 * type definition itself whose {attribute declaration} has the same
15991 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015992 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000015993 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015994 if (base->attrUses != NULL) {
15995 int i, j, found;
15996 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000015997
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015998 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
15999 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16000 found = 0;
16001 if (type->attrUses != NULL) {
16002 use = (WXS_LIST_CAST type->attrUses)->items[j];
16003 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16004 {
16005 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16006 WXS_ATTRUSE_DECL_NAME(buse)) &&
16007 (WXS_ATTRUSE_DECL_TNS(use) ==
16008 WXS_ATTRUSE_DECL_TNS(buse)) &&
16009 (WXS_ATTRUSE_TYPEDEF(use) ==
16010 WXS_ATTRUSE_TYPEDEF(buse))
16011 {
16012 found = 1;
16013 break;
16014 }
16015 }
16016 }
16017 if (! found) {
16018 xmlChar *str = NULL;
16019
16020 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16021 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16022 NULL, WXS_BASIC_CAST type,
16023 /*
16024 * TODO: The report does not indicate that also the
16025 * type needs to be the same.
16026 */
16027 "This type is missing a matching correspondent "
16028 "for its {base type}'s %s in its {attribute uses}",
16029 xmlSchemaGetComponentDesignation(&str,
16030 buse->children),
16031 NULL);
16032 FREE_AND_NULL(str)
16033 }
16034 }
16035 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016036 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016037 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16038 * definition must also have one, and the base type definition's
16039 * {attribute wildcard}'s {namespace constraint} must be a subset
16040 * of the complex type definition's {attribute wildcard}'s {namespace
16041 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016042 */
16043
16044 /*
16045 * MAYBE TODO: Enable if ever needed. But this will be needed only
16046 * if created the type via a schema construction API.
16047 */
16048 if (base->attributeWildcard != NULL) {
16049 if (type->attributeWilcard == NULL) {
16050 xmlChar *str = NULL;
16051
16052 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16053 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16054 NULL, type,
16055 "The base %s has an attribute wildcard, "
16056 "but this type is missing an attribute wildcard",
16057 xmlSchemaGetComponentDesignation(&str, base));
16058 FREE_AND_NULL(str)
16059
16060 } else if (xmlSchemaCheckCOSNSSubset(
16061 base->attributeWildcard, type->attributeWildcard))
16062 {
16063 xmlChar *str = NULL;
16064
16065 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16066 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16067 NULL, type,
16068 "The attribute wildcard is not a valid "
16069 "superset of the one in the base %s",
16070 xmlSchemaGetComponentDesignation(&str, base));
16071 FREE_AND_NULL(str)
16072 }
16073 }
16074#endif
16075 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016076 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016077 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016078 if ((type->contentTypeDef != NULL) &&
16079 (type->contentTypeDef == base->contentTypeDef)) {
16080 /*
16081 * SPEC (1.4.1) "The {content type} of the {base type definition}
16082 * and the {content type} of the complex type definition itself
16083 * must be the same simple type definition"
16084 * PASS
16085 */
16086 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16087 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16088 /*
16089 * SPEC (1.4.2) "The {content type} of both the {base type
16090 * definition} and the complex type definition itself must
16091 * be empty."
16092 * PASS
16093 */
16094 } else {
16095 /*
16096 * SPEC (1.4.3) "All of the following must be true:"
16097 */
16098 if (type->subtypes == NULL) {
16099 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016100 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016101 * definition itself must specify a particle.
16102 */
16103 xmlSchemaPCustomErr(ctxt,
16104 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016105 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016106 "The content type must specify a particle", NULL);
16107 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16108 }
16109 /*
16110 * SPEC (1.4.3.2) "One of the following must be true:"
16111 */
16112 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16113 /*
16114 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16115 * definition} must be empty.
16116 * PASS
16117 */
16118 } else {
16119 /*
16120 * SPEC (1.4.3.2.2) "All of the following must be true:"
16121 */
16122 if ((type->contentType != base->contentType) ||
16123 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16124 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16125 /*
16126 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16127 * or both must be element-only."
16128 */
16129 xmlSchemaPCustomErr(ctxt,
16130 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016131 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016132 "The content type of both, the type and its base "
16133 "type, must either 'mixed' or 'element-only'", NULL);
16134 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016135 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016136 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016137 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016138 * complex type definition must be a ·valid extension·
16139 * of the {base type definition}'s particle, as defined
16140 * in Particle Valid (Extension) (§3.9.6)."
16141 *
16142 * NOTE that we won't check "Particle Valid (Extension)",
16143 * since it is ensured by the derivation process in
16144 * xmlSchemaTypeFixup(). We need to implement this when heading
16145 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016146 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016147 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016148 }
16149 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016150 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016151 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016152 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016153 } else {
16154 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016155 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016156 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016157 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016158 if (type->contentTypeDef != base) {
16159 /*
16160 * SPEC (2.1) "The {content type} must be the same simple type
16161 * definition."
16162 */
16163 xmlSchemaPCustomErr(ctxt,
16164 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016165 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016166 "The content type must be the simple base type", NULL);
16167 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16168 }
16169 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16170 /*
16171 * SPEC (2.2) "The {final} of the {base type definition} must not
16172 * contain extension"
16173 * NOTE that this is the same as (1.1).
16174 */
16175 xmlSchemaPCustomErr(ctxt,
16176 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016177 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016178 "The 'final' of the base type definition "
16179 "contains 'extension'", NULL);
16180 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016181 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016182 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016183 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016184}
16185
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016186/**
16187 * xmlSchemaCheckDerivationOKRestriction:
16188 * @ctxt: the schema parser context
16189 * @type: the complex type definition
16190 *
16191 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016192 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016193 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16194 *
16195 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016196 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016197 * (5.4.2) ???
16198 *
16199 * ATTENTION:
16200 * In XML Schema 1.1 this will be:
16201 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016202 *
16203 * Returns 0 if the constraints are satisfied, a positive
16204 * error code if not and -1 if an internal error occured.
16205 */
16206static int
16207xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16208 xmlSchemaTypePtr type)
16209{
16210 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016211
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016212 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016213 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016214 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016215 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016216 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016217 if (! WXS_IS_COMPLEX(base)) {
16218 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16219 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16220 type->node, WXS_BASIC_CAST type,
16221 "The base type must be a complex type", NULL, NULL);
16222 return(ctxt->err);
16223 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016224 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16225 /*
16226 * SPEC (1) "The {base type definition} must be a complex type
16227 * definition whose {final} does not contain restriction."
16228 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016229 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16230 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16231 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016232 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016233 "contains 'restriction'", NULL, NULL);
16234 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016235 }
16236 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016237 * SPEC (2), (3) and (4)
16238 * Those are handled in a separate function, since the
16239 * same constraints are needed for redefinition of
16240 * attribute groups as well.
16241 */
16242 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16243 XML_SCHEMA_ACTION_DERIVE,
16244 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16245 type->attrUses, base->attrUses,
16246 type->attributeWildcard,
16247 base->attributeWildcard) == -1)
16248 {
16249 return(-1);
16250 }
16251 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016252 * SPEC (5) "One of the following must be true:"
16253 */
16254 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16255 /*
16256 * SPEC (5.1) "The {base type definition} must be the
16257 * ·ur-type definition·."
16258 * PASS
16259 */
16260 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16261 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16262 /*
16263 * SPEC (5.2.1) "The {content type} of the complex type definition
16264 * must be a simple type definition"
16265 *
16266 * SPEC (5.2.2) "One of the following must be true:"
16267 */
16268 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016269 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16270 {
16271 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016272 /*
16273 * SPEC (5.2.2.1) "The {content type} of the {base type
16274 * definition} must be a simple type definition from which
16275 * the {content type} is validly derived given the empty
16276 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016277 *
16278 * ATTENTION TODO: This seems not needed if the type implicitely
16279 * derived from the base type.
16280 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016281 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016282 err = xmlSchemaCheckCOSSTDerivedOK(ctxt, type->contentTypeDef,
16283 base->contentTypeDef, 0);
16284 if (err != 0) {
16285 xmlChar *strA = NULL, *strB = NULL;
16286
16287 if (err == -1)
16288 return(-1);
16289 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16290 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16291 NULL, WXS_BASIC_CAST type,
16292 "The {content type} %s is not validly derived from the "
16293 "base type's {content type} %s",
16294 xmlSchemaGetComponentDesignation(&strA,
16295 type->contentTypeDef),
16296 xmlSchemaGetComponentDesignation(&strB,
16297 base->contentTypeDef));
16298 FREE_AND_NULL(strA);
16299 FREE_AND_NULL(strB);
16300 return(ctxt->err);
16301 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016302 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16303 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016304 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016305 /*
16306 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16307 * and have a particle which is ·emptiable· as defined in
16308 * Particle Emptiable (§3.9.6)."
16309 * PASS
16310 */
16311 } else {
16312 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016313 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16314 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016315 "The content type of the base type must be either "
16316 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016317 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016318 }
16319 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16320 /*
16321 * SPEC (5.3.1) "The {content type} of the complex type itself must
16322 * be empty"
16323 */
16324 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16325 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016326 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016327 * definition} must also be empty."
16328 * PASS
16329 */
16330 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16331 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16332 xmlSchemaIsParticleEmptiable(
16333 (xmlSchemaParticlePtr) base->subtypes)) {
16334 /*
16335 * SPEC (5.3.2.2) "The {content type} of the {base type
16336 * definition} must be elementOnly or mixed and have a particle
16337 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16338 * PASS
16339 */
16340 } else {
16341 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016342 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16343 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016344 "The content type of the base type must be either "
16345 "empty or 'mixed' (or 'elements-only') and an emptiable "
16346 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016347 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016348 }
16349 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016350 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016351 /*
16352 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16353 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016354 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016355 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016356 /*
16357 * SPEC (5.4.1.2) "The {content type} of the complex type
16358 * definition itself and of the {base type definition} must be
16359 * mixed"
16360 */
16361 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016362 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16363 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016364 "If the content type is 'mixed', then the content type of the "
16365 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016366 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016367 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016368 /*
16369 * SPEC (5.4.2) "The particle of the complex type definition itself
16370 * must be a ·valid restriction· of the particle of the {content
16371 * type} of the {base type definition} as defined in Particle Valid
16372 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016373 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016374 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016375 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016376 } else {
16377 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016378 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16379 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016380 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016381 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016382 }
16383 return (0);
16384}
16385
16386/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016387 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016388 * @ctxt: the schema parser context
16389 * @type: the complex type definition
16390 *
16391 * (3.4.6) Constraints on Complex Type Definition Schema Components
16392 *
16393 * Returns 0 if the constraints are satisfied, a positive
16394 * error code if not and -1 if an internal error occured.
16395 */
16396static int
16397xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16398 xmlSchemaTypePtr type)
16399{
16400 int ret;
16401 /*
16402 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016403 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016404 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16405 if (ret != 0)
16406 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016407 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016408 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16409 else
16410 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16411 return (ret);
16412}
16413
16414/**
16415 * xmlSchemaCheckSRCCT:
16416 * @ctxt: the schema parser context
16417 * @type: the complex type definition
16418 *
16419 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016420 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016421 * Complex Type Definition Representation OK (src-ct)
16422 *
16423 * Returns 0 if the constraints are satisfied, a positive
16424 * error code if not and -1 if an internal error occured.
16425 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016426static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016427xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016428 xmlSchemaTypePtr type)
16429{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016430 xmlSchemaTypePtr base;
16431 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016432
16433 /*
16434 * TODO: Adjust the error codes here, as I used
16435 * XML_SCHEMAP_SRC_CT_1 only yet.
16436 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016437 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016438 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016439 /*
16440 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016441 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016442 * must be a complex type definition;
16443 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016444 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016445 xmlChar *str = NULL;
16446 xmlSchemaPCustomErr(ctxt,
16447 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016448 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016449 "If using <complexContent>, the base type is expected to be "
16450 "a complex type. The base type '%s' is a simple type",
16451 xmlSchemaFormatQName(&str, base->targetNamespace,
16452 base->name));
16453 FREE_AND_NULL(str)
16454 return (XML_SCHEMAP_SRC_CT_1);
16455 }
16456 } else {
16457 /*
16458 * SPEC
16459 * 2 If the <simpleContent> alternative is chosen, all of the
16460 * following must be true:
16461 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16462 * base [attribute] must be one of the following:
16463 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016464 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016465 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016466 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016467 /*
16468 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016469 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016470 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016471 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016472 xmlSchemaPCustomErr(ctxt,
16473 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016474 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016475 "If using <simpleContent> and <restriction>, the base "
16476 "type must be a complex type. The base type '%s' is "
16477 "a simple type",
16478 xmlSchemaFormatQName(&str, base->targetNamespace,
16479 base->name));
16480 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016481 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016482 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016483 } else {
16484 /* Base type is a complex type. */
16485 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16486 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16487 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016488 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016489 * simple type definition;
16490 * PASS
16491 */
16492 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016493 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016494 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016495 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016496 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016497 type->name);
16498 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016499 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016500 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016501 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016502
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016503 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016504 * 2.1.2 only if the <restriction> alternative is also
16505 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016506 * is mixed and a particle emptiable.
16507 */
16508 if (! xmlSchemaIsParticleEmptiable(
16509 (xmlSchemaParticlePtr) base->subtypes)) {
16510 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016511 } else
16512 /*
16513 * Attention: at this point the <simpleType> child is in
16514 * ->contentTypeDef (put there during parsing).
16515 */
16516 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016517 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016518 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016519 * 2.2 If clause 2.1.2 above is satisfied, then there
16520 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016521 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016522 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016523 /* TODO: Change error code to ..._SRC_CT_2_2. */
16524 xmlSchemaPCustomErr(ctxt,
16525 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016526 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016527 "A <simpleType> is expected among the children "
16528 "of <restriction>, if <simpleContent> is used and "
16529 "the base type '%s' is a complex type",
16530 xmlSchemaFormatQName(&str, base->targetNamespace,
16531 base->name));
16532 FREE_AND_NULL(str)
16533 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016534 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016535 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016536 ret = XML_SCHEMAP_SRC_CT_1;
16537 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016538 }
16539 if (ret > 0) {
16540 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016541 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016542 xmlSchemaPCustomErr(ctxt,
16543 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016544 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016545 "If <simpleContent> and <restriction> is used, the "
16546 "base type must be a simple type or a complex type with "
16547 "mixed content and particle emptiable. The base type "
16548 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016549 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016550 base->name));
16551 } else {
16552 xmlSchemaPCustomErr(ctxt,
16553 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016554 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016555 "If <simpleContent> and <extension> is used, the "
16556 "base type must be a simple type. The base type '%s' "
16557 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016558 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016559 base->name));
16560 }
16561 FREE_AND_NULL(str)
16562 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016563 }
16564 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016565 * SPEC (3) "The corresponding complex type definition component must
16566 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016567 * Definition Schema Components (§3.4.6);"
16568 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016569 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016570 /*
16571 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016572 * above for {attribute wildcard} is satisfied, the intensional
16573 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016574 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016575 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016576 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016577 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016578}
William M. Brack2f2a6632004-08-20 23:09:47 +000016579
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016580#ifdef ENABLE_PARTICLE_RESTRICTION
16581/**
16582 * xmlSchemaCheckParticleRangeOK:
16583 * @ctxt: the schema parser context
16584 * @type: the complex type definition
16585 *
16586 * (3.9.6) Constraints on Particle Schema Components
16587 * Schema Component Constraint:
16588 * Occurrence Range OK (range-ok)
16589 *
16590 * STATUS: complete
16591 *
16592 * Returns 0 if the constraints are satisfied, a positive
16593 * error code if not and -1 if an internal error occured.
16594 */
16595static int
16596xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16597 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016598{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016599 if (rmin < bmin)
16600 return (1);
16601 if ((bmax != UNBOUNDED) &&
16602 (rmax > bmax))
16603 return (1);
16604 return (0);
16605}
16606
16607/**
16608 * xmlSchemaCheckRCaseNameAndTypeOK:
16609 * @ctxt: the schema parser context
16610 * @r: the restricting element declaration particle
16611 * @b: the base element declaration particle
16612 *
16613 * (3.9.6) Constraints on Particle Schema Components
16614 * Schema Component Constraint:
16615 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16616 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016617 *
16618 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016619 * MISSING (3.2.3)
16620 * CLARIFY: (3.2.2)
16621 *
16622 * Returns 0 if the constraints are satisfied, a positive
16623 * error code if not and -1 if an internal error occured.
16624 */
16625static int
16626xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16627 xmlSchemaParticlePtr r,
16628 xmlSchemaParticlePtr b)
16629{
16630 xmlSchemaElementPtr elemR, elemB;
16631
16632 /* TODO: Error codes (rcase-NameAndTypeOK). */
16633 elemR = (xmlSchemaElementPtr) r->children;
16634 elemB = (xmlSchemaElementPtr) b->children;
16635 /*
16636 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16637 * the same."
16638 */
16639 if ((elemR != elemB) &&
16640 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16641 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16642 return (1);
16643 /*
16644 * SPEC (2) "R's occurrence range is a valid restriction of B's
16645 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16646 */
16647 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16648 b->minOccurs, b->maxOccurs) != 0)
16649 return (1);
16650 /*
16651 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16652 * {scope} are global."
16653 */
16654 if (elemR == elemB)
16655 return (0);
16656 /*
16657 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16658 */
16659 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16660 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16661 return (1);
16662 /*
16663 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16664 * or is not fixed, or R's declaration's {value constraint} is fixed
16665 * with the same value."
16666 */
16667 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16668 ((elemR->value == NULL) ||
16669 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16670 /* TODO: Equality of the initial value or normalized or canonical? */
16671 (! xmlStrEqual(elemR->value, elemB->value))))
16672 return (1);
16673 /*
16674 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16675 * definitions} is a subset of B's declaration's {identity-constraint
16676 * definitions}, if any."
16677 */
16678 if (elemB->idcs != NULL) {
16679 /* TODO */
16680 }
16681 /*
16682 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16683 * superset of B's declaration's {disallowed substitutions}."
16684 */
16685 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16686 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16687 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16688 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16689 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16690 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16691 return (1);
16692 /*
16693 * SPEC (3.2.5) "R's {type definition} is validly derived given
16694 * {extension, list, union} from B's {type definition}"
16695 *
16696 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16697 * set, if the corresponding constraints handle "restriction" and
16698 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016699 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016700 */
16701 {
16702 int set = 0;
16703
16704 set |= SUBSET_EXTENSION;
16705 set |= SUBSET_LIST;
16706 set |= SUBSET_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016707 if (xmlSchemaCheckCOSDerivedOK(elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016708 elemB->subtypes, set) != 0)
16709 return (1);
16710 }
16711 return (0);
16712}
16713
16714/**
16715 * xmlSchemaCheckRCaseNSCompat:
16716 * @ctxt: the schema parser context
16717 * @r: the restricting element declaration particle
16718 * @b: the base wildcard particle
16719 *
16720 * (3.9.6) Constraints on Particle Schema Components
16721 * Schema Component Constraint:
16722 * Particle Derivation OK (Elt:Any -- NSCompat)
16723 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016724 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016725 * STATUS: complete
16726 *
16727 * Returns 0 if the constraints are satisfied, a positive
16728 * error code if not and -1 if an internal error occured.
16729 */
16730static int
16731xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16732 xmlSchemaParticlePtr r,
16733 xmlSchemaParticlePtr b)
16734{
16735 /* TODO:Error codes (rcase-NSCompat). */
16736 /*
16737 * SPEC "For an element declaration particle to be a ·valid restriction·
16738 * of a wildcard particle all of the following must be true:"
16739 *
16740 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16741 * with respect to the wildcard's {namespace constraint} as defined by
16742 * Wildcard allows Namespace Name (§3.10.4)."
16743 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016744 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016745 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16746 return (1);
16747 /*
16748 * SPEC (2) "R's occurrence range is a valid restriction of B's
16749 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16750 */
16751 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16752 b->minOccurs, b->maxOccurs) != 0)
16753 return (1);
16754
16755 return (0);
16756}
16757
16758/**
16759 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16760 * @ctxt: the schema parser context
16761 * @r: the restricting element declaration particle
16762 * @b: the base model group particle
16763 *
16764 * (3.9.6) Constraints on Particle Schema Components
16765 * Schema Component Constraint:
16766 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16767 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016768 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016769 * STATUS: TODO
16770 *
16771 * Returns 0 if the constraints are satisfied, a positive
16772 * error code if not and -1 if an internal error occured.
16773 */
16774static int
16775xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16776 xmlSchemaParticlePtr r,
16777 xmlSchemaParticlePtr b)
16778{
16779 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16780 TODO
16781 return (0);
16782}
16783
16784/**
16785 * xmlSchemaCheckRCaseNSSubset:
16786 * @ctxt: the schema parser context
16787 * @r: the restricting wildcard particle
16788 * @b: the base wildcard particle
16789 *
16790 * (3.9.6) Constraints on Particle Schema Components
16791 * Schema Component Constraint:
16792 * Particle Derivation OK (Any:Any -- NSSubset)
16793 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016794 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016795 * STATUS: complete
16796 *
16797 * Returns 0 if the constraints are satisfied, a positive
16798 * error code if not and -1 if an internal error occured.
16799 */
16800static int
16801xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16802 xmlSchemaParticlePtr r,
16803 xmlSchemaParticlePtr b,
16804 int isAnyTypeBase)
16805{
16806 /* TODO: Error codes (rcase-NSSubset). */
16807 /*
16808 * SPEC (1) "R's occurrence range is a valid restriction of B's
16809 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16810 */
16811 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16812 b->minOccurs, b->maxOccurs))
16813 return (1);
16814 /*
16815 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16816 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16817 */
16818 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16819 (xmlSchemaWildcardPtr) b->children))
16820 return (1);
16821 /*
16822 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16823 * definition·, R's {process contents} must be identical to or stronger
16824 * than B's {process contents}, where strict is stronger than lax is
16825 * stronger than skip."
16826 */
16827 if (! isAnyTypeBase) {
16828 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16829 ((xmlSchemaWildcardPtr) b->children)->processContents)
16830 return (1);
16831 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016832
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016833 return (0);
16834}
16835
16836/**
16837 * xmlSchemaCheckCOSParticleRestrict:
16838 * @ctxt: the schema parser context
16839 * @type: the complex type definition
16840 *
16841 * (3.9.6) Constraints on Particle Schema Components
16842 * Schema Component Constraint:
16843 * Particle Valid (Restriction) (cos-particle-restrict)
16844 *
16845 * STATUS: TODO
16846 *
16847 * Returns 0 if the constraints are satisfied, a positive
16848 * error code if not and -1 if an internal error occured.
16849 */
16850static int
16851xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16852 xmlSchemaParticlePtr r,
16853 xmlSchemaParticlePtr b)
16854{
16855 int ret = 0;
16856
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016857 /*part = WXS_TYPE_PARTICLE(type);
16858 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016859 */
16860
16861 TODO
16862
16863 /*
16864 * SPEC (1) "They are the same particle."
16865 */
16866 if (r == b)
16867 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016868
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016869
16870 return (0);
16871}
16872
16873/**
16874 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16875 * @ctxt: the schema parser context
16876 * @r: the model group particle
16877 * @b: the base wildcard particle
16878 *
16879 * (3.9.6) Constraints on Particle Schema Components
16880 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016881 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016882 * NSRecurseCheckCardinality)
16883 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016884 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016885 * STATUS: TODO: subst-groups
16886 *
16887 * Returns 0 if the constraints are satisfied, a positive
16888 * error code if not and -1 if an internal error occured.
16889 */
16890static int
16891xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16892 xmlSchemaParticlePtr r,
16893 xmlSchemaParticlePtr b)
16894{
16895 xmlSchemaParticlePtr part;
16896 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16897 if ((r->children == NULL) || (r->children->children == NULL))
16898 return (-1);
16899 /*
16900 * SPEC "For a group particle to be a ·valid restriction· of a
16901 * wildcard particle..."
16902 *
16903 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016904 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016905 * Particle Valid (Restriction) (§3.9.6)."
16906 */
16907 part = (xmlSchemaParticlePtr) r->children->children;
16908 do {
16909 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16910 return (1);
16911 part = (xmlSchemaParticlePtr) part->next;
16912 } while (part != NULL);
16913 /*
16914 * SPEC (2) "The effective total range of the group [...] is a
16915 * valid restriction of B's occurrence range as defined by
16916 * Occurrence Range OK (§3.9.6)."
16917 */
16918 if (xmlSchemaCheckParticleRangeOK(
16919 xmlSchemaGetParticleTotalRangeMin(r),
16920 xmlSchemaGetParticleTotalRangeMax(r),
16921 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016922 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016923 return (0);
16924}
16925
16926/**
16927 * xmlSchemaCheckRCaseRecurse:
16928 * @ctxt: the schema parser context
16929 * @r: the <all> or <sequence> model group particle
16930 * @b: the base <all> or <sequence> model group particle
16931 *
16932 * (3.9.6) Constraints on Particle Schema Components
16933 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016934 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016935 Recurse)
16936 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016937 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016938 * STATUS: ?
16939 * TODO: subst-groups
16940 *
16941 * Returns 0 if the constraints are satisfied, a positive
16942 * error code if not and -1 if an internal error occured.
16943 */
16944static int
16945xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16946 xmlSchemaParticlePtr r,
16947 xmlSchemaParticlePtr b)
16948{
16949 /* xmlSchemaParticlePtr part; */
16950 /* TODO: Error codes (rcase-Recurse). */
16951 if ((r->children == NULL) || (b->children == NULL) ||
16952 (r->children->type != b->children->type))
16953 return (-1);
16954 /*
16955 * SPEC "For an all or sequence group particle to be a ·valid
16956 * restriction· of another group particle with the same {compositor}..."
16957 *
16958 * SPEC (1) "R's occurrence range is a valid restriction of B's
16959 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16960 */
16961 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16962 b->minOccurs, b->maxOccurs))
16963 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016964
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016965
16966 return (0);
16967}
16968
16969#endif
16970
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016971#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
16972 xmlSchemaPCustomErrExt(pctxt, \
16973 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016974 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016975 "It is an error for both '%s' and '%s' to be specified on the "\
16976 "same type definition", \
16977 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
16978 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
16979
16980#define FACET_RESTR_ERR(fac1, msg) \
16981 xmlSchemaPCustomErr(pctxt, \
16982 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016983 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016984 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016985
16986#define FACET_RESTR_FIXED_ERR(fac) \
16987 xmlSchemaPCustomErr(pctxt, \
16988 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016989 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016990 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016991 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016992
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000016993static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016994xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
16995 xmlSchemaFacetPtr facet1,
16996 xmlSchemaFacetPtr facet2,
16997 int lessGreater,
16998 int orEqual,
16999 int ofBase)
17000{
17001 xmlChar *msg = NULL;
17002
17003 msg = xmlStrdup(BAD_CAST "'");
17004 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17005 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17006 if (lessGreater == 0)
17007 msg = xmlStrcat(msg, BAD_CAST " equal to");
17008 if (lessGreater == 1)
17009 msg = xmlStrcat(msg, BAD_CAST " greater than");
17010 else
17011 msg = xmlStrcat(msg, BAD_CAST " less than");
17012
17013 if (orEqual)
17014 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17015 msg = xmlStrcat(msg, BAD_CAST " '");
17016 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17017 if (ofBase)
17018 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17019 else
17020 msg = xmlStrcat(msg, BAD_CAST "'");
17021
17022 xmlSchemaPCustomErr(pctxt,
17023 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017024 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017025 (const char *) msg, NULL);
17026
17027 if (msg != NULL)
17028 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017029}
17030
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017031/*
17032* xmlSchemaDeriveAndValidateFacets:
17033*
17034* Schema Component Constraint: Simple Type Restriction (Facets)
17035* (st-restrict-facets)
17036*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017037static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017038xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17039 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017040{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017041 xmlSchemaTypePtr base = type->baseType;
17042 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017043 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017044 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17045 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17046 fmininc = NULL, fmaxinc = NULL,
17047 fminexc = NULL, fmaxexc = NULL,
17048 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17049 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17050 bfmininc = NULL, bfmaxinc = NULL,
17051 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017052 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017053
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017054 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017055 * SPEC st-restrict-facets 1:
17056 * "The {variety} of R is the same as that of B."
17057 */
17058 /*
17059 * SPEC st-restrict-facets 2:
17060 * "If {variety} is atomic, the {primitive type definition}
17061 * of R is the same as that of B."
17062 *
17063 * NOTE: we leave 1 & 2 out for now, since this will be
17064 * satisfied by the derivation process.
17065 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17066 */
17067 /*
17068 * SPEC st-restrict-facets 3:
17069 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017070 * of B, eliminating duplicates. To eliminate duplicates,
17071 * when a facet of the same kind occurs in both S and the
17072 * {facets} of B, the one in the {facets} of B is not
17073 * included, with the exception of enumeration and pattern
17074 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017075 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017076 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017077
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017078 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17079 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017080
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017081 last = type->facetSet;
17082 if (last != NULL)
17083 while (last->next != NULL)
17084 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017085
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017086 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17087 facet = cur->facet;
17088 switch (facet->type) {
17089 case XML_SCHEMA_FACET_LENGTH:
17090 flength = facet; break;
17091 case XML_SCHEMA_FACET_MINLENGTH:
17092 fminlen = facet; break;
17093 case XML_SCHEMA_FACET_MININCLUSIVE:
17094 fmininc = facet; break;
17095 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17096 fminexc = facet; break;
17097 case XML_SCHEMA_FACET_MAXLENGTH:
17098 fmaxlen = facet; break;
17099 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17100 fmaxinc = facet; break;
17101 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17102 fmaxexc = facet; break;
17103 case XML_SCHEMA_FACET_TOTALDIGITS:
17104 ftotdig = facet; break;
17105 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17106 ffracdig = facet; break;
17107 default:
17108 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017109 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017110 }
17111 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17112 facet = cur->facet;
17113 switch (facet->type) {
17114 case XML_SCHEMA_FACET_LENGTH:
17115 bflength = facet; break;
17116 case XML_SCHEMA_FACET_MINLENGTH:
17117 bfminlen = facet; break;
17118 case XML_SCHEMA_FACET_MININCLUSIVE:
17119 bfmininc = facet; break;
17120 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17121 bfminexc = facet; break;
17122 case XML_SCHEMA_FACET_MAXLENGTH:
17123 bfmaxlen = facet; break;
17124 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17125 bfmaxinc = facet; break;
17126 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17127 bfmaxexc = facet; break;
17128 case XML_SCHEMA_FACET_TOTALDIGITS:
17129 bftotdig = facet; break;
17130 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17131 bffracdig = facet; break;
17132 default:
17133 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017134 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017135 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017136 /*
17137 * length and minLength or maxLength (2.2) + (3.2)
17138 */
17139 if (flength && (fminlen || fmaxlen)) {
17140 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17141 "either of 'minLength' or 'maxLength' to be specified on "
17142 "the same type definition")
17143 }
17144 /*
17145 * Mutual exclusions in the same derivation step.
17146 */
17147 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017148 /*
17149 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017150 */
17151 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17152 }
17153 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017154 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017155 * SCC "minInclusive and minExclusive"
17156 */
17157 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017158 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017159
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017160 if (flength && bflength) {
17161 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017162 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017163 * The values have to be equal.
17164 */
17165 res = xmlSchemaCompareValues(flength->val, bflength->val);
17166 if (res == -2)
17167 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017168 if (res != 0)
17169 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17170 if ((res != 0) && (bflength->fixed)) {
17171 FACET_RESTR_FIXED_ERR(flength)
17172 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017173
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017174 }
17175 if (fminlen && bfminlen) {
17176 /*
17177 * SCC "minLength valid restriction"
17178 * minLength >= BASE minLength
17179 */
17180 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17181 if (res == -2)
17182 goto internal_error;
17183 if (res == -1)
17184 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17185 if ((res != 0) && (bfminlen->fixed)) {
17186 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017187 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017188 }
17189 if (fmaxlen && bfmaxlen) {
17190 /*
17191 * SCC "maxLength valid restriction"
17192 * maxLength <= BASE minLength
17193 */
17194 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17195 if (res == -2)
17196 goto internal_error;
17197 if (res == 1)
17198 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17199 if ((res != 0) && (bfmaxlen->fixed)) {
17200 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017201 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017202 }
17203 /*
17204 * SCC "length and minLength or maxLength"
17205 */
17206 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017207 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017208 if (flength) {
17209 if (! fminlen)
17210 flength = bflength;
17211 if (fminlen) {
17212 /* (1.1) length >= minLength */
17213 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17214 if (res == -2)
17215 goto internal_error;
17216 if (res == -1)
17217 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17218 }
17219 if (! fmaxlen)
17220 fmaxlen = bfmaxlen;
17221 if (fmaxlen) {
17222 /* (2.1) length <= maxLength */
17223 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17224 if (res == -2)
17225 goto internal_error;
17226 if (res == 1)
17227 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17228 }
17229 }
17230 if (fmaxinc) {
17231 /*
17232 * "maxInclusive"
17233 */
17234 if (fmininc) {
17235 /* SCC "maxInclusive >= minInclusive" */
17236 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17237 if (res == -2)
17238 goto internal_error;
17239 if (res == -1) {
17240 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17241 }
17242 }
17243 /*
17244 * SCC "maxInclusive valid restriction"
17245 */
17246 if (bfmaxinc) {
17247 /* maxInclusive <= BASE maxInclusive */
17248 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17249 if (res == -2)
17250 goto internal_error;
17251 if (res == 1)
17252 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17253 if ((res != 0) && (bfmaxinc->fixed)) {
17254 FACET_RESTR_FIXED_ERR(fmaxinc)
17255 }
17256 }
17257 if (bfmaxexc) {
17258 /* maxInclusive < BASE maxExclusive */
17259 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17260 if (res == -2)
17261 goto internal_error;
17262 if (res != -1) {
17263 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17264 }
17265 }
17266 if (bfmininc) {
17267 /* maxInclusive >= BASE minInclusive */
17268 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17269 if (res == -2)
17270 goto internal_error;
17271 if (res == -1) {
17272 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17273 }
17274 }
17275 if (bfminexc) {
17276 /* maxInclusive > BASE minExclusive */
17277 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17278 if (res == -2)
17279 goto internal_error;
17280 if (res != 1) {
17281 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17282 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017283 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017284 }
17285 if (fmaxexc) {
17286 /*
17287 * "maxExclusive >= minExclusive"
17288 */
17289 if (fminexc) {
17290 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17291 if (res == -2)
17292 goto internal_error;
17293 if (res == -1) {
17294 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17295 }
17296 }
17297 /*
17298 * "maxExclusive valid restriction"
17299 */
17300 if (bfmaxexc) {
17301 /* maxExclusive <= BASE maxExclusive */
17302 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17303 if (res == -2)
17304 goto internal_error;
17305 if (res == 1) {
17306 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17307 }
17308 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017309 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017310 }
17311 }
17312 if (bfmaxinc) {
17313 /* maxExclusive <= BASE maxInclusive */
17314 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17315 if (res == -2)
17316 goto internal_error;
17317 if (res == 1) {
17318 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17319 }
17320 }
17321 if (bfmininc) {
17322 /* maxExclusive > BASE minInclusive */
17323 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17324 if (res == -2)
17325 goto internal_error;
17326 if (res != 1) {
17327 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17328 }
17329 }
17330 if (bfminexc) {
17331 /* maxExclusive > BASE minExclusive */
17332 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17333 if (res == -2)
17334 goto internal_error;
17335 if (res != 1) {
17336 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17337 }
17338 }
17339 }
17340 if (fminexc) {
17341 /*
17342 * "minExclusive < maxInclusive"
17343 */
17344 if (fmaxinc) {
17345 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17346 if (res == -2)
17347 goto internal_error;
17348 if (res != -1) {
17349 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17350 }
17351 }
17352 /*
17353 * "minExclusive valid restriction"
17354 */
17355 if (bfminexc) {
17356 /* minExclusive >= BASE minExclusive */
17357 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17358 if (res == -2)
17359 goto internal_error;
17360 if (res == -1) {
17361 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17362 }
17363 if ((res != 0) && (bfminexc->fixed)) {
17364 FACET_RESTR_FIXED_ERR(fminexc)
17365 }
17366 }
17367 if (bfmaxinc) {
17368 /* minExclusive <= BASE maxInclusive */
17369 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17370 if (res == -2)
17371 goto internal_error;
17372 if (res == 1) {
17373 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17374 }
17375 }
17376 if (bfmininc) {
17377 /* minExclusive >= BASE minInclusive */
17378 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17379 if (res == -2)
17380 goto internal_error;
17381 if (res == -1) {
17382 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17383 }
17384 }
17385 if (bfmaxexc) {
17386 /* minExclusive < BASE maxExclusive */
17387 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17388 if (res == -2)
17389 goto internal_error;
17390 if (res != -1) {
17391 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17392 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017393 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017394 }
17395 if (fmininc) {
17396 /*
17397 * "minInclusive < maxExclusive"
17398 */
17399 if (fmaxexc) {
17400 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17401 if (res == -2)
17402 goto internal_error;
17403 if (res != -1) {
17404 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17405 }
17406 }
17407 /*
17408 * "minExclusive valid restriction"
17409 */
17410 if (bfmininc) {
17411 /* minInclusive >= BASE minInclusive */
17412 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17413 if (res == -2)
17414 goto internal_error;
17415 if (res == -1) {
17416 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17417 }
17418 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017419 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017420 }
17421 }
17422 if (bfmaxinc) {
17423 /* minInclusive <= BASE maxInclusive */
17424 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17425 if (res == -2)
17426 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017427 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017428 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17429 }
17430 }
17431 if (bfminexc) {
17432 /* minInclusive > BASE minExclusive */
17433 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17434 if (res == -2)
17435 goto internal_error;
17436 if (res != 1)
17437 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17438 }
17439 if (bfmaxexc) {
17440 /* minInclusive < BASE maxExclusive */
17441 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17442 if (res == -2)
17443 goto internal_error;
17444 if (res != -1)
17445 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17446 }
17447 }
17448 if (ftotdig && bftotdig) {
17449 /*
17450 * SCC " totalDigits valid restriction"
17451 * totalDigits <= BASE totalDigits
17452 */
17453 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17454 if (res == -2)
17455 goto internal_error;
17456 if (res == 1)
17457 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17458 -1, 1, 1);
17459 if ((res != 0) && (bftotdig->fixed)) {
17460 FACET_RESTR_FIXED_ERR(ftotdig)
17461 }
17462 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017463 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017464 /*
17465 * SCC "fractionDigits valid restriction"
17466 * fractionDigits <= BASE fractionDigits
17467 */
17468 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17469 if (res == -2)
17470 goto internal_error;
17471 if (res == 1)
17472 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17473 -1, 1, 1);
17474 if ((res != 0) && (bffracdig->fixed)) {
17475 FACET_RESTR_FIXED_ERR(ffracdig)
17476 }
17477 }
17478 /*
17479 * SCC "fractionDigits less than or equal to totalDigits"
17480 */
17481 if (! ftotdig)
17482 ftotdig = bftotdig;
17483 if (! ffracdig)
17484 ffracdig = bffracdig;
17485 if (ftotdig && ffracdig) {
17486 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17487 if (res == -2)
17488 goto internal_error;
17489 if (res == 1)
17490 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17491 -1, 1, 0);
17492 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017493 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017494 * *Enumerations* won' be added here, since only the first set
17495 * of enumerations in the ancestor-or-self axis is used
17496 * for validation, plus we need to use the base type of those
17497 * enumerations for whitespace.
17498 *
17499 * *Patterns*: won't be add here, since they are ORed at
17500 * type level and ANDed at ancestor level. This will
17501 * happed during validation by walking the base axis
17502 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017503 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017504 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17505 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017506 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017507 * Special handling of enumerations and patterns.
17508 * TODO: hmm, they should not appear in the set, so remove this.
17509 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017510 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017511 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017512 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017513 /*
17514 * Search for a duplicate facet in the current type.
17515 */
17516 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017517 /* err = 0; */
17518 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017519 while (link != NULL) {
17520 facet = link->facet;
17521 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017522 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017523 case XML_SCHEMA_FACET_WHITESPACE:
17524 /*
17525 * The whitespace must be stronger.
17526 */
17527 if (facet->whitespace < bfacet->whitespace) {
17528 FACET_RESTR_ERR(flength,
17529 "The 'whitespace' value has to be equal to "
17530 "or stronger than the 'whitespace' value of "
17531 "the base type")
17532 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017533 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017534 (facet->whitespace != bfacet->whitespace)) {
17535 FACET_RESTR_FIXED_ERR(facet)
17536 }
17537 break;
17538 default:
17539 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017540 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017541 /* Duplicate found. */
17542 break;
17543 }
17544 link = link->next;
17545 }
17546 /*
17547 * If no duplicate was found: add the base types's facet
17548 * to the set.
17549 */
17550 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017551 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017552 xmlMalloc(sizeof(xmlSchemaFacetLink));
17553 if (link == NULL) {
17554 xmlSchemaPErrMemory(pctxt,
17555 "deriving facets, creating a facet link", NULL);
17556 return (-1);
17557 }
17558 link->facet = cur->facet;
17559 link->next = NULL;
17560 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017561 type->facetSet = link;
17562 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017563 last->next = link;
17564 last = link;
17565 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017566
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017567 }
17568
17569 return (0);
17570internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017571 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17572 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017573 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017574}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017575
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017576static int
17577xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17578 xmlSchemaTypePtr type)
17579{
17580 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17581 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017582 * The actual value is then formed by replacing any union type
17583 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017584 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017585 *
17586 * TODO: There's a bug entry at
17587 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17588 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017589 */
17590 link = type->memberTypes;
17591 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017592
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017593 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017594 xmlSchemaTypeFixup(link->type, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017595
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017596 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017597 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017598 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017599 link->type = subLink->type;
17600 if (subLink->next != NULL) {
17601 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017602 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017603 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017604 while (subLink != NULL) {
17605 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017606 xmlMalloc(sizeof(xmlSchemaTypeLink));
17607 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017608 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017609 NULL);
17610 return (-1);
17611 }
17612 newLink->type = subLink->type;
17613 prevLink->next = newLink;
17614 prevLink = newLink;
17615 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017616
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017617 subLink = subLink->next;
17618 }
17619 }
17620 }
17621 }
17622 link = link->next;
17623 }
17624 return (0);
17625}
17626
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017627static void
17628xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17629{
17630 int has = 0, needVal = 0, normVal = 0;
17631
17632 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17633 if (has) {
17634 needVal = (type->baseType->flags &
17635 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17636 normVal = (type->baseType->flags &
17637 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17638 }
17639 if (type->facets != NULL) {
17640 xmlSchemaFacetPtr fac;
17641
17642 for (fac = type->facets; fac != NULL; fac = fac->next) {
17643 switch (fac->type) {
17644 case XML_SCHEMA_FACET_WHITESPACE:
17645 break;
17646 case XML_SCHEMA_FACET_PATTERN:
17647 normVal = 1;
17648 has = 1;
17649 break;
17650 case XML_SCHEMA_FACET_ENUMERATION:
17651 needVal = 1;
17652 normVal = 1;
17653 has = 1;
17654 break;
17655 default:
17656 has = 1;
17657 break;
17658 }
17659 }
17660 }
17661 if (normVal)
17662 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17663 if (needVal)
17664 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17665 if (has)
17666 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17667
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017668 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017669 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17670 /*
17671 * OPTIMIZE VAL TODO: Some facets need a computed value.
17672 */
17673 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17674 (prim->builtInType != XML_SCHEMAS_STRING)) {
17675 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17676 }
17677 }
17678}
17679
17680static int
17681xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17682{
17683
17684
17685 /*
17686 * Evaluate the whitespace-facet value.
17687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017688 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017689 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17690 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017691 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017692 return (0);
17693
17694 if (type->facetSet != NULL) {
17695 xmlSchemaFacetLinkPtr lin;
17696
17697 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17698 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17699 switch (lin->facet->whitespace) {
17700 case XML_SCHEMAS_FACET_PRESERVE:
17701 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17702 break;
17703 case XML_SCHEMAS_FACET_REPLACE:
17704 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17705 break;
17706 case XML_SCHEMAS_FACET_COLLAPSE:
17707 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17708 break;
17709 default:
17710 return (-1);
17711 }
17712 return (0);
17713 }
17714 }
17715 }
17716 /*
17717 * For all ·atomic· datatypes other than string (and types ·derived·
17718 * by ·restriction· from it) the value of whiteSpace is fixed to
17719 * collapse
17720 */
17721 {
17722 xmlSchemaTypePtr anc;
17723
17724 for (anc = type->baseType; anc != NULL &&
17725 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17726 anc = anc->baseType) {
17727
17728 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17729 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17730 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17731
17732 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17733 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17734 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17735
17736 } else
17737 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17738 break;
17739 }
17740 }
17741 return (0);
17742 }
17743 return (0);
17744}
17745
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017746static int
17747xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17748 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017749{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017750 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17751 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017752 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017753 return(0);
17754 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017755
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017756 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017757 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017758 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017759 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017760 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017761 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017762 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017763 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017764 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017765 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017766 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017767 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017768 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017769 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017770 * Corresponds to <simpleType><union>...
17771 */
17772 if (type->memberTypes == NULL) {
17773 /*
17774 * This one is really needed, so get out.
17775 */
17776 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17777 "union type has no member-types assigned");
17778 return(-1);
17779 }
17780 } else {
17781 /*
17782 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017783 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017784 if (type->baseType == NULL) {
17785 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17786 "type has no base-type assigned");
17787 return(-1);
17788 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017789 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017790 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17791 return(-1);
17792 /*
17793 * Variety
17794 * If the <restriction> alternative is chosen, then the
17795 * {variety} of the {base type definition}.
17796 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017797 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017798 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017799 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017800 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017801 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017802 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017803 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017804 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017805 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017806 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017807 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017808 * NOTE that we won't assign the memberTypes of the base,
17809 * since this will make trouble when freeing them; we will
17810 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017811 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017812 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017813 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017814 return(0);
17815}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017816
Daniel Veillard8651f532002-04-17 09:06:27 +000017817#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017818xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17819 xmlSchemaTypePtr type)
17820{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017821 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017822 xmlGenericError(xmlGenericErrorContext,
17823 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017824 type->node->doc->URL,
17825 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017826 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017827 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017828 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017829 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017830 switch (type->contentType) {
17831 case XML_SCHEMA_CONTENT_SIMPLE:
17832 xmlGenericError(xmlGenericErrorContext, "simple\n");
17833 break;
17834 case XML_SCHEMA_CONTENT_ELEMENTS:
17835 xmlGenericError(xmlGenericErrorContext, "elements\n");
17836 break;
17837 case XML_SCHEMA_CONTENT_UNKNOWN:
17838 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17839 break;
17840 case XML_SCHEMA_CONTENT_EMPTY:
17841 xmlGenericError(xmlGenericErrorContext, "empty\n");
17842 break;
17843 case XML_SCHEMA_CONTENT_MIXED:
17844 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017845 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017846 xmlGenericError(xmlGenericErrorContext,
17847 "mixed as emptiable particle\n");
17848 else
17849 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17850 break;
17851 /* Removed, since not used. */
17852 /*
17853 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17854 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17855 break;
17856 */
17857 case XML_SCHEMA_CONTENT_BASIC:
17858 xmlGenericError(xmlGenericErrorContext, "basic\n");
17859 break;
17860 default:
17861 xmlGenericError(xmlGenericErrorContext,
17862 "not registered !!!\n");
17863 break;
17864 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017865 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017866}
Daniel Veillard8651f532002-04-17 09:06:27 +000017867#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017868
17869/*
17870* 3.14.6 Constraints on Simple Type Definition Schema Components
17871*/
17872static int
17873xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17874 xmlSchemaTypePtr type)
17875{
17876 int res, olderrs = pctxt->nberrors;
17877
17878 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17879 return(-1);
17880
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017881 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017882 return(0);
17883
17884 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17885 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17886
17887 if (type->baseType == NULL) {
17888 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17889 "missing baseType");
17890 goto exit_failure;
17891 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017892 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017893 xmlSchemaTypeFixup(type->baseType, pctxt);
17894 /*
17895 * If a member type of a union is a union itself, we need to substitute
17896 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017897 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17898 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017899 */
17900 if ((type->memberTypes != NULL) &&
17901 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17902 return(-1);
17903 /*
17904 * SPEC src-simple-type 1
17905 * "The corresponding simple type definition, if any, must satisfy
17906 * the conditions set out in Constraints on Simple Type Definition
17907 * Schema Components (§3.14.6)."
17908 */
17909 /*
17910 * Schema Component Constraint: Simple Type Definition Properties Correct
17911 * (st-props-correct)
17912 */
17913 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17914 HFAILURE HERROR
17915 /*
17916 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17917 * (cos-st-restricts)
17918 */
17919 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17920 HFAILURE HERROR
17921 /*
17922 * TODO: Removed the error report, since it got annoying to get an
17923 * extra error report, if anything failed until now.
17924 * Enable this if needed.
17925 *
17926 * xmlSchemaPErr(ctxt, type->node,
17927 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17928 * "Simple type '%s' does not satisfy the constraints "
17929 * "on simple type definitions.\n",
17930 * type->name, NULL);
17931 */
17932 /*
17933 * Schema Component Constraint: Simple Type Restriction (Facets)
17934 * (st-restrict-facets)
17935 */
17936 res = xmlSchemaCheckFacetValues(type, pctxt);
17937 HFAILURE HERROR
17938 if ((type->facetSet != NULL) ||
17939 (type->baseType->facetSet != NULL)) {
17940 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17941 HFAILURE HERROR
17942 }
17943 /*
17944 * Whitespace value.
17945 */
17946 res = xmlSchemaTypeFixupWhitespace(type);
17947 HFAILURE HERROR
17948 xmlSchemaTypeFixupOptimFacets(type);
17949
17950exit_error:
17951#ifdef DEBUG_TYPE
17952 xmlSchemaDebugFixedType(pctxt, type);
17953#endif
17954 if (olderrs != pctxt->nberrors)
17955 return(pctxt->err);
17956 return(0);
17957
17958exit_failure:
17959#ifdef DEBUG_TYPE
17960 xmlSchemaDebugFixedType(pctxt, type);
17961#endif
17962 return(-1);
17963}
17964
17965static int
17966xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
17967 xmlSchemaTypePtr type)
17968{
17969 int res = 0, olderrs = pctxt->nberrors;
17970 xmlSchemaTypePtr baseType = type->baseType;
17971
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017972 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017973 return(0);
17974 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17975 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017976 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017977 "missing baseType");
17978 goto exit_failure;
17979 }
17980 /*
17981 * Fixup the base type.
17982 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017983 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017984 xmlSchemaTypeFixup(baseType, pctxt);
17985 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
17986 /*
17987 * Skip fixup if the base type is invalid.
17988 * TODO: Generate a warning!
17989 */
17990 return(0);
17991 }
17992 /*
17993 * This basically checks if the base type can be derived.
17994 */
17995 res = xmlSchemaCheckSRCCT(pctxt, type);
17996 HFAILURE HERROR
17997 /*
17998 * Fixup the content type.
17999 */
18000 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18001 /*
18002 * Corresponds to <complexType><simpleContent>...
18003 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018004 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018005 (baseType->contentTypeDef != NULL) &&
18006 (WXS_IS_RESTRICTION(type))) {
18007 xmlSchemaTypePtr contentBase, content;
18008#ifdef ENABLE_NAMED_LOCALS
18009 char buf[30];
18010 const xmlChar *tmpname;
18011#endif
18012 /*
18013 * SPEC (1) If <restriction> + base type is <complexType>,
18014 * "whose own {content type} is a simple type..."
18015 */
18016 if (type->contentTypeDef != NULL) {
18017 /*
18018 * SPEC (1.1) "the simple type definition corresponding to the
18019 * <simpleType> among the [children] of <restriction> if there
18020 * is one;"
18021 * Note that this "<simpleType> among the [children]" was put
18022 * into ->contentTypeDef during parsing.
18023 */
18024 contentBase = type->contentTypeDef;
18025 type->contentTypeDef = NULL;
18026 } else {
18027 /*
18028 * (1.2) "...otherwise (<restriction> has no <simpleType>
18029 * among its [children]), the simple type definition which
18030 * is the {content type} of the ... base type."
18031 */
18032 contentBase = baseType->contentTypeDef;
18033 }
18034 /*
18035 * SPEC
18036 * "... a simple type definition which restricts the simple
18037 * type definition identified in clause 1.1 or clause 1.2
18038 * with a set of facet components"
18039 *
18040 * Create the anonymous simple type, which will be the content
18041 * type of the complex type.
18042 */
18043#ifdef ENABLE_NAMED_LOCALS
18044 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18045 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018046 content = xmlSchemaAddType(pctxt, pctxt->schema,
18047 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018048 type->node, 0);
18049#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018050 content = xmlSchemaAddType(pctxt, pctxt->schema,
18051 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018052 type->node, 0);
18053#endif
18054 if (content == NULL)
18055 goto exit_failure;
18056 /*
18057 * We will use the same node as for the <complexType>
18058 * to have it somehow anchored in the schema doc.
18059 */
18060 content->type = XML_SCHEMA_TYPE_SIMPLE;
18061 content->baseType = contentBase;
18062 /*
18063 * Move the facets, previously anchored on the
18064 * complexType during parsing.
18065 */
18066 content->facets = type->facets;
18067 type->facets = NULL;
18068 content->facetSet = type->facetSet;
18069 type->facetSet = NULL;
18070
18071 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018072 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018073 xmlSchemaTypeFixup(contentBase, pctxt);
18074 /*
18075 * Fixup the newly created type. We don't need to check
18076 * for circularity here.
18077 */
18078 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18079 HFAILURE HERROR
18080 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18081 HFAILURE HERROR
18082
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018083 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018084 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18085 (WXS_IS_RESTRICTION(type))) {
18086 /*
18087 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18088 * an emptiable particle, then a simple type definition which
18089 * restricts the <restriction>'s <simpleType> child.
18090 */
18091 if ((type->contentTypeDef == NULL) ||
18092 (type->contentTypeDef->baseType == NULL)) {
18093 /*
18094 * TODO: Check if this ever happens.
18095 */
18096 xmlSchemaPCustomErr(pctxt,
18097 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018098 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018099 "Internal error: xmlSchemaTypeFixup, "
18100 "complex type '%s': the <simpleContent><restriction> "
18101 "is missing a <simpleType> child, but was not catched "
18102 "by xmlSchemaCheckSRCCT()", type->name);
18103 goto exit_failure;
18104 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018105 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018106 /*
18107 * SPEC (3) If <extension> + base is <complexType> with
18108 * <simpleType> content, "...then the {content type} of that
18109 * complex type definition"
18110 */
18111 if (baseType->contentTypeDef == NULL) {
18112 /*
18113 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18114 * should have catched this already.
18115 */
18116 xmlSchemaPCustomErr(pctxt,
18117 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018118 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018119 "Internal error: xmlSchemaTypeFixup, "
18120 "complex type '%s': the <extension>ed base type is "
18121 "a complex type with no simple content type",
18122 type->name);
18123 goto exit_failure;
18124 }
18125 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018126 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018127 /*
18128 * SPEC (4) <extension> + base is <simpleType>
18129 * "... then that simple type definition"
18130 */
18131 type->contentTypeDef = baseType;
18132 } else {
18133 /*
18134 * TODO: Check if this ever happens.
18135 */
18136 xmlSchemaPCustomErr(pctxt,
18137 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018138 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018139 "Internal error: xmlSchemaTypeFixup, "
18140 "complex type '%s' with <simpleContent>: unhandled "
18141 "derivation case", type->name);
18142 goto exit_failure;
18143 }
18144 } else {
18145 int dummySequence = 0;
18146 xmlSchemaParticlePtr particle =
18147 (xmlSchemaParticlePtr) type->subtypes;
18148 /*
18149 * Corresponds to <complexType><complexContent>...
18150 *
18151 * NOTE that the effective mixed was already set during parsing of
18152 * <complexType> and <complexContent>; its flag value is
18153 * XML_SCHEMAS_TYPE_MIXED.
18154 *
18155 * Compute the "effective content":
18156 * (2.1.1) + (2.1.2) + (2.1.3)
18157 */
18158 if ((particle == NULL) ||
18159 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18160 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18161 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18162 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18163 (particle->minOccurs == 0))) &&
18164 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18165 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18166 /*
18167 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18168 * a particle whose properties are as follows:..."
18169 *
18170 * Empty sequence model group with
18171 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18172 * NOTE that we sill assign it the <complexType> node to
18173 * somehow anchor it in the doc.
18174 */
18175 if ((particle == NULL) ||
18176 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18177 /*
18178 * Create the particle.
18179 */
18180 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18181 type->node, 1, 1);
18182 if (particle == NULL)
18183 goto exit_failure;
18184 /*
18185 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018186 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018187 particle->children = (xmlSchemaTreeItemPtr)
18188 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18189 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18190 if (particle->children == NULL)
18191 goto exit_failure;
18192
18193 type->subtypes = (xmlSchemaTypePtr) particle;
18194 }
18195 dummySequence = 1;
18196 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18197 } else {
18198 /*
18199 * SPEC (2.1.5) "otherwise empty"
18200 */
18201 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18202 }
18203 } else {
18204 /*
18205 * SPEC (2.2) "otherwise the particle corresponding to the
18206 * <all>, <choice>, <group> or <sequence> among the
18207 * [children]."
18208 */
18209 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18210 }
18211 /*
18212 * Compute the "content type".
18213 */
18214 if (WXS_IS_RESTRICTION(type)) {
18215 /*
18216 * SPEC (3.1) "If <restriction>..."
18217 * (3.1.1) + (3.1.2) */
18218 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18219 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18220 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18221 }
18222 } else {
18223 /*
18224 * SPEC (3.2) "If <extension>..."
18225 */
18226 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18227 /*
18228 * SPEC (3.2.1)
18229 */
18230 type->contentType = baseType->contentType;
18231 type->subtypes = baseType->subtypes;
18232 /*
18233 * NOTE that the effective mixed is ignored here.
18234 */
18235 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18236 /*
18237 * SPEC (3.2.2)
18238 */
18239 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18240 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18241 } else {
18242 /*
18243 * SPEC (3.2.3)
18244 */
18245 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18246 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18247 /*
18248 * "A model group whose {compositor} is sequence and whose
18249 * {particles} are..."
18250 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018251 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18252 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18253 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18254 XML_SCHEMA_TYPE_ALL))
18255 {
18256 /*
18257 * SPEC cos-all-limited (1)
18258 */
18259 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18260 /* TODO: error code */
18261 XML_SCHEMAP_COS_ALL_LIMITED,
18262 WXS_ITEM_NODE(type), NULL,
18263 "The type has an 'all' model group in its "
18264 "{content type} and thus cannot be derived from "
18265 "a non-empty type, since this would produce a "
18266 "'sequence' model group containing the 'all' "
18267 "model group; 'all' model groups are not "
18268 "allowed to appear inside other model groups",
18269 NULL, NULL);
18270
18271 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18272 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18273 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18274 XML_SCHEMA_TYPE_ALL))
18275 {
18276 /*
18277 * SPEC cos-all-limited (1)
18278 */
18279 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18280 /* TODO: error code */
18281 XML_SCHEMAP_COS_ALL_LIMITED,
18282 WXS_ITEM_NODE(type), NULL,
18283 "A type cannot be derived by extension from a type "
18284 "which has an 'all' model group in its "
18285 "{content type}, since this would produce a "
18286 "'sequence' model group containing the 'all' "
18287 "model group; 'all' model groups are not "
18288 "allowed to appear inside other model groups",
18289 NULL, NULL);
18290
18291 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018292 xmlSchemaTreeItemPtr effectiveContent =
18293 (xmlSchemaTreeItemPtr) type->subtypes;
18294 /*
18295 * Create the particle.
18296 */
18297 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18298 type->node, 1, 1);
18299 if (particle == NULL)
18300 goto exit_failure;
18301 /*
18302 * Create the "sequence" model group.
18303 */
18304 particle->children = (xmlSchemaTreeItemPtr)
18305 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18306 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18307 if (particle->children == NULL)
18308 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018309 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018310 /*
18311 * SPEC "the particle of the {content type} of
18312 * the ... base ..."
18313 * Create a duplicate of the base type's particle
18314 * and assign its "term" to it.
18315 */
18316 particle->children->children =
18317 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18318 pctxt->schema, type->node,
18319 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18320 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18321 if (particle->children->children == NULL)
18322 goto exit_failure;
18323 particle = (xmlSchemaParticlePtr)
18324 particle->children->children;
18325 particle->children =
18326 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18327 /*
18328 * SPEC "followed by the ·effective content·."
18329 */
18330 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018331 /*
18332 * This all will result in:
18333 * new-particle
18334 * --> new-sequence(
18335 * new-particle
18336 * --> base-model,
18337 * this-particle
18338 * --> this-model
18339 * )
18340 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018341 } else {
18342 /*
18343 * This is the case when there is already an empty
18344 * <sequence> with minOccurs==maxOccurs==1.
18345 * Just add the base types's content type.
18346 * NOTE that, although we miss to add an intermediate
18347 * <sequence>, this should produce no difference to
18348 * neither the regex compilation of the content model,
18349 * nor to the complex type contraints.
18350 */
18351 particle->children->children =
18352 (xmlSchemaTreeItemPtr) baseType->subtypes;
18353 }
18354 }
18355 }
18356 }
18357 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018358 * Now fixup attribute uses:
18359 * - expand attr. group references
18360 * - intersect attribute wildcards
18361 * - inherit attribute uses of the base type
18362 * - inherit or union attr. wildcards if extending
18363 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018364 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018365 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018366 HFAILURE HERROR
18367 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018368 * Apply the complex type component constraints; this will not
18369 * check attributes, since this is done in
18370 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018371 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018372 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018373 HFAILURE HERROR
18374
18375#ifdef DEBUG_TYPE
18376 xmlSchemaDebugFixedType(pctxt, type);
18377#endif
18378 if (olderrs != pctxt->nberrors)
18379 return(pctxt->err);
18380 else
18381 return(0);
18382
18383exit_error:
18384 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18385#ifdef DEBUG_TYPE
18386 xmlSchemaDebugFixedType(pctxt, type);
18387#endif
18388 return(pctxt->err);
18389
18390exit_failure:
18391 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18392#ifdef DEBUG_TYPE
18393 xmlSchemaDebugFixedType(pctxt, type);
18394#endif
18395 return(-1);
18396}
18397
18398
18399/**
18400 * xmlSchemaTypeFixup:
18401 * @typeDecl: the schema type definition
18402 * @ctxt: the schema parser context
18403 *
18404 * Fixes the content model of the type.
18405 * URGENT TODO: We need an int result!
18406 */
18407static int
18408xmlSchemaTypeFixup(xmlSchemaTypePtr type,
18409 xmlSchemaParserCtxtPtr pctxt)
18410{
18411 if (type == NULL)
18412 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018413 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018414 return(0);
18415 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
18416 return(xmlSchemaFixupComplexType(pctxt, type));
18417 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
18418 return(xmlSchemaFixupSimpleTypeStageTwo(pctxt, type));
18419 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018420}
18421
18422/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018423 * xmlSchemaCheckFacet:
18424 * @facet: the facet
18425 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018426 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018427 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018428 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018429 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018430 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018431 * Returns 0 if valid, a positive error code if not valid and
18432 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018433 */
18434int
18435xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018436 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018437 xmlSchemaParserCtxtPtr pctxt,
18438 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018439{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018440 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018441
Daniel Veillardce682bc2004-11-05 17:22:25 +000018442 if ((facet == NULL) || (typeDecl == NULL))
18443 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018444 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018445 * TODO: will the parser context be given if used from
18446 * the relaxNG module?
18447 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018448 if (pctxt == NULL)
18449 ctxtGiven = 0;
18450 else
18451 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018452
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018453 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018454 case XML_SCHEMA_FACET_MININCLUSIVE:
18455 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18456 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018457 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18458 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018459 /*
18460 * Okay we need to validate the value
18461 * at that point.
18462 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018463 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018464
18465 /* 4.3.5.5 Constraints on enumeration Schema Components
18466 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018467 * It is an ·error· if any member of {value} is not in the
18468 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018469 *
18470 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018471 * The value ·must· be in the
18472 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018473 */
18474 /*
18475 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018476 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018477 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018478 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018479 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018480 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018481 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018482 */
18483 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18484 base = typeDecl->baseType;
18485 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018486 PERROR_INT("xmlSchemaCheckFacet",
18487 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018488 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018489 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018490 } else
18491 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018492
18493 if (! ctxtGiven) {
18494 /*
18495 * A context is needed if called from RelaxNG.
18496 */
18497 pctxt = xmlSchemaNewParserCtxt("*");
18498 if (pctxt == NULL)
18499 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018500 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018501 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018502 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018503 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018504 * facet->node is just the node holding the facet
18505 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018506 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018507 */
18508 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018509 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018510 facet->value, &(facet->val), 1, 1, 0);
18511 if (ret != 0) {
18512 if (ret < 0) {
18513 /* No error message for RelaxNG. */
18514 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018515 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018516 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18517 "Internal error: xmlSchemaCheckFacet, "
18518 "failed to validate the value '%s' of the "
18519 "facet '%s' against the base type",
18520 facet->value, xmlSchemaFacetTypeToString(facet->type));
18521 }
18522 goto internal_error;
18523 }
18524 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18525 /* No error message for RelaxNG. */
18526 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018527 xmlChar *str = NULL;
18528
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018529 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018530 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018531 "The value '%s' of the facet does not validate "
18532 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018533 facet->value,
18534 xmlSchemaFormatQName(&str,
18535 base->targetNamespace, base->name));
18536 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018537 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018538 goto exit;
18539 } else if (facet->val == NULL) {
18540 if (ctxtGiven) {
18541 PERROR_INT("xmlSchemaCheckFacet",
18542 "value was not computed");
18543 }
18544 TODO
18545 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018546 break;
18547 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018548 case XML_SCHEMA_FACET_PATTERN:
18549 facet->regexp = xmlRegexpCompile(facet->value);
18550 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018551 ret = XML_SCHEMAP_REGEXP_INVALID;
18552 /* No error message for RelaxNG. */
18553 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018554 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018555 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018556 "The value '%s' of the facet 'pattern' is not a "
18557 "valid regular expression",
18558 facet->value, NULL);
18559 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018560 }
18561 break;
18562 case XML_SCHEMA_FACET_TOTALDIGITS:
18563 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18564 case XML_SCHEMA_FACET_LENGTH:
18565 case XML_SCHEMA_FACET_MAXLENGTH:
18566 case XML_SCHEMA_FACET_MINLENGTH:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018567 ret = xmlSchemaValidatePredefinedType(
18568 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18569 facet->value, &(facet->val));
18570 if (ret != 0) {
18571 if (ret < 0) {
18572 /* No error message for RelaxNG. */
18573 if (ctxtGiven) {
18574 PERROR_INT("xmlSchemaCheckFacet",
18575 "validating facet value");
18576 }
18577 goto internal_error;
18578 }
18579 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18580 /* No error message for RelaxNG. */
18581 if (ctxtGiven) {
18582 /* error code */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018583 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018584 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018585 "The value '%s' of the facet '%s' is not a valid "
18586 "'nonNegativeInteger'",
18587 facet->value,
18588 xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018589 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018590 }
18591 break;
18592 }
18593 case XML_SCHEMA_FACET_WHITESPACE:{
18594 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18595 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18596 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18597 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18598 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18599 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18600 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018601 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18602 /* No error message for RelaxNG. */
18603 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018604 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018605 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018606 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018607 "The value '%s' of the facet 'whitespace' is not "
18608 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018609 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018610 }
18611 }
18612 default:
18613 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018614 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018615exit:
18616 if ((! ctxtGiven) && (pctxt != NULL))
18617 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018618 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018619internal_error:
18620 if ((! ctxtGiven) && (pctxt != NULL))
18621 xmlSchemaFreeParserCtxt(pctxt);
18622 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018623}
18624
18625/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018626 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018627 * @typeDecl: the schema type definition
18628 * @ctxt: the schema parser context
18629 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018630 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018631 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018632static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018633xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018634 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018635{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018636 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018637 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018638 /*
18639 * NOTE: It is intended to use the facets list, instead
18640 * of facetSet.
18641 */
18642 if (typeDecl->facets != NULL) {
18643 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018644
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018645 /*
18646 * Temporarily assign the "schema" to the validation context
18647 * of the parser context. This is needed for NOTATION validation.
18648 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018649 if (pctxt->vctxt == NULL) {
18650 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18651 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018652 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018653 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018654 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018655 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18656 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018657 facet = facet->next;
18658 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018659 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018660 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018661 if (olderrs != pctxt->nberrors)
18662 return(pctxt->err);
18663 return(0);
18664exit_failure:
18665 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018666}
18667
18668/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018669 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018670 * @ctxtMGroup: the searched model group
18671 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018672 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018673 *
18674 * This one is intended to be used by
18675 * xmlSchemaCheckGroupDefCircular only.
18676 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018677 * Returns the particle with the circular model group definition reference,
18678 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018679 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018680static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018681xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018682 xmlSchemaTreeItemPtr particle)
18683{
18684 xmlSchemaTreeItemPtr circ = NULL;
18685 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018686 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018687
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018688 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018689 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018690 if (term == NULL)
18691 continue;
18692 switch (term->type) {
18693 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018694 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018695 if (gdef == groupDef)
18696 return (particle);
18697 /*
18698 * Mark this model group definition to avoid infinite
18699 * recursion on circular references not yet examined.
18700 */
18701 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18702 continue;
18703 if (gdef->children != NULL) {
18704 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18705 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18706 gdef->children->children);
18707 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18708 if (circ != NULL)
18709 return (circ);
18710 }
18711 break;
18712 case XML_SCHEMA_TYPE_SEQUENCE:
18713 case XML_SCHEMA_TYPE_CHOICE:
18714 case XML_SCHEMA_TYPE_ALL:
18715 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18716 if (circ != NULL)
18717 return (circ);
18718 break;
18719 default:
18720 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018721 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018722 }
18723 return (NULL);
18724}
18725
18726/**
18727 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018728 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018729 * @ctxt: the parser context
18730 * @name: the name
18731 *
18732 * Checks for circular references to model group definitions.
18733 */
18734static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018735xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018736 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018737{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018738 /*
18739 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018740 * 2 Circular groups are disallowed. That is, within the {particles}
18741 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018742 * is the group itself.
18743 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018744 if ((item == NULL) ||
18745 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18746 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018747 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018748 {
18749 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018750
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018751 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018752 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018753 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018754 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018755 * TODO: The error report is not adequate: this constraint
18756 * is defined for model groups but not definitions, but since
18757 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018758 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018759 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018760 */
18761 xmlSchemaPCustomErr(ctxt,
18762 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018763 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018764 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018765 "defined", xmlSchemaFormatQName(&str,
18766 item->targetNamespace, item->name));
18767 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018768 /*
18769 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018770 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018771 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018772 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018773 }
18774 }
18775}
18776
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018777/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018778 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018779 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018780 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018781 *
18782 * Assigns the model group of model group definitions to the "term"
18783 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018784 * In xmlSchemaResolveModelGroupParticleReferences the model group
18785 * definitions were assigned to the "term", since needed for the
18786 * circularity check.
18787 *
18788 * Schema Component Constraint:
18789 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018790 */
18791static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018792xmlSchemaModelGroupToModelGroupDefFixup(
18793 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18794 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018795{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018796 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18797
18798 while (particle != NULL) {
18799 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18800 ((WXS_PARTICLE_TERM(particle))->type !=
18801 XML_SCHEMA_TYPE_GROUP))
18802 {
18803 particle = WXS_PTC_CAST particle->next;
18804 continue;
18805 }
18806 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18807 /*
18808 * TODO: Remove the particle.
18809 */
18810 WXS_PARTICLE_TERM(particle) = NULL;
18811 particle = WXS_PTC_CAST particle->next;
18812 continue;
18813 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018814 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018815 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018816 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018817 WXS_PARTICLE_TERM(particle) =
18818 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18819
18820 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018821 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018822}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018823
18824/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018825 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018826 * @ctxtGr: the searched attribute group
18827 * @attr: the current attribute list to be processed
18828 *
18829 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018830 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018831 *
18832 * Returns the circular attribute grou reference, otherwise NULL.
18833 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018834static xmlSchemaQNameRefPtr
18835xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18836 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018837{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018838 xmlSchemaAttributeGroupPtr gr;
18839 xmlSchemaQNameRefPtr ref, circ;
18840 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018841 /*
18842 * We will search for an attribute group reference which
18843 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018844 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018845 for (i = 0; i < list->nbItems; i++) {
18846 ref = list->items[i];
18847 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18848 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18849 (ref->item != NULL))
18850 {
18851 gr = WXS_ATTR_GROUP_CAST ref->item;
18852 if (gr == ctxtGr)
18853 return(ref);
18854 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18855 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018856 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018857 * Mark as visited to avoid infinite recursion on
18858 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018859 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018860 if ((gr->attrUses) &&
18861 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18862 {
18863 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18864 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18865 (xmlSchemaItemListPtr) gr->attrUses);
18866 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18867 if (circ != NULL)
18868 return (circ);
18869 }
18870
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018871 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018872 }
18873 return (NULL);
18874}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018875
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018876/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018877 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018878 * attrGr: the attribute group definition
18879 * @ctxt: the parser context
18880 * @name: the name
18881 *
18882 * Checks for circular references of attribute groups.
18883 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018884static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018885xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018886 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018887{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018888 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018889 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018890 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018891 * 3 Circular group reference is disallowed outside <redefine>.
18892 * That is, unless this element information item's parent is
18893 * <redefine>, then among the [children], if any, there must
18894 * not be an <attributeGroup> with ref [attribute] which resolves
18895 * to the component corresponding to this <attributeGroup>. Indirect
18896 * circularity is also ruled out. That is, when QName resolution
18897 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18898 * any <attributeGroup>s with a ref [attribute] among the [children],
18899 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018900 * which resolves to the component corresponding to this <attributeGroup>.
18901 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018902 if (attrGr->attrUses == NULL)
18903 return(0);
18904 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18905 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018906 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018907 xmlSchemaQNameRefPtr circ;
18908
18909 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18910 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018911 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018912 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018913 /*
18914 * TODO: Report the referenced attr group as QName.
18915 */
18916 xmlSchemaPCustomErr(ctxt,
18917 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018918 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018919 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018920 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18921 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018922 /*
18923 * NOTE: We will cut the reference to avoid further
18924 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018925 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018926 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018927 circ->item = NULL;
18928 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018929 }
18930 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018931 return(0);
18932}
18933
18934static int
18935xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18936 xmlSchemaAttributeGroupPtr attrGr);
18937
18938/**
18939 * xmlSchemaExpandAttributeGroupRefs:
18940 * @pctxt: the parser context
18941 * @node: the node of the component holding the attribute uses
18942 * @completeWild: the intersected wildcard to be returned
18943 * @list: the attribute uses
18944 *
18945 * Substitutes contained attribute group references
18946 * for their attribute uses. Wilcards are intersected.
18947 * Attribute use prohibitions are removed from the list
18948 * and returned via the @prohibs list.
18949 * Pointlessness of attr. prohibs, if a matching attr. decl
18950 * is existent a well, are checked.
18951 */
18952static int
18953xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
18954 xmlSchemaBasicItemPtr item,
18955 xmlSchemaWildcardPtr *completeWild,
18956 xmlSchemaItemListPtr list,
18957 xmlSchemaItemListPtr prohibs)
18958{
18959 xmlSchemaAttributeGroupPtr gr;
18960 xmlSchemaAttributeUsePtr use;
18961 xmlSchemaItemListPtr sublist;
18962 int i, j;
18963 int created = (*completeWild == NULL) ? 0 : 1;
18964
18965 if (prohibs)
18966 prohibs->nbItems = 0;
18967
18968 for (i = 0; i < list->nbItems; i++) {
18969 use = list->items[i];
18970
18971 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
18972 if (prohibs == NULL) {
18973 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
18974 "unexpected attr prohibition found");
18975 return(-1);
18976 }
18977 /*
18978 * Remove from attribute uses.
18979 */
18980 if (xmlSchemaItemListRemove(list, i) == -1)
18981 return(-1);
18982 i--;
18983 /*
18984 * Note that duplicate prohibitions were already
18985 * handled at parsing time.
18986 */
18987 /*
18988 * Add to list of prohibitions.
18989 */
18990 xmlSchemaItemListAddSize(prohibs, 2, use);
18991 continue;
18992 }
18993 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18994 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
18995 {
18996 if ((WXS_QNAME_CAST use)->item == NULL)
18997 return(-1);
18998 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
18999 /*
19000 * Expand the referenced attr. group.
19001 * TODO: remove this, this is done in a previous step, so
19002 * already done here.
19003 */
19004 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19005 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19006 return(-1);
19007 }
19008 /*
19009 * Build the 'complete' wildcard; i.e. intersect multiple
19010 * wildcards.
19011 */
19012 if (gr->attributeWildcard != NULL) {
19013 if (*completeWild == NULL) {
19014 *completeWild = gr->attributeWildcard;
19015 } else {
19016 if (! created) {
19017 xmlSchemaWildcardPtr tmpWild;
19018
19019 /*
19020 * Copy the first encountered wildcard as context,
19021 * except for the annotation.
19022 *
19023 * Although the complete wildcard might not correspond
19024 * to any node in the schema, we will anchor it on
19025 * the node of the owner component.
19026 */
19027 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19028 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19029 WXS_ITEM_NODE(item));
19030 if (tmpWild == NULL)
19031 return(-1);
19032 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19033 tmpWild, *completeWild) == -1)
19034 return (-1);
19035 tmpWild->processContents = (*completeWild)->processContents;
19036 *completeWild = tmpWild;
19037 created = 1;
19038 }
19039
19040 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19041 gr->attributeWildcard) == -1)
19042 return(-1);
19043 }
19044 }
19045 /*
19046 * Just remove the reference if the referenced group does not
19047 * contain any attribute uses.
19048 */
19049 if (gr->attrUses == NULL) {
19050 if (xmlSchemaItemListRemove(list, i) == -1)
19051 return(-1);
19052 i--;
19053 continue;
19054 }
19055 /*
19056 * Add the attribute uses.
19057 */
19058 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19059 if (sublist->nbItems != 0) {
19060 list->items[i] = sublist->items[0];
19061 if (sublist->nbItems != 1) {
19062 for (j = 1; j < sublist->nbItems; j++) {
19063 i++;
19064 if (xmlSchemaItemListInsert(list,
19065 sublist->items[j], i) == -1)
19066 return(-1);
19067 }
19068 }
19069 }
19070 }
19071
19072 }
19073 /*
19074 * Handle pointless prohibitions of declared attributes.
19075 */
19076 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19077 xmlSchemaAttributeUseProhibPtr prohib;
19078
19079 for (i = prohibs->nbItems -1; i >= 0; i--) {
19080 prohib = prohibs->items[i];
19081 for (j = 0; j < list->nbItems; j++) {
19082 use = list->items[j];
19083
19084 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19085 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19086 {
19087 xmlChar *str = NULL;
19088
19089 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19090 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19091 prohib->node, NULL,
19092 "Skipping pointless attribute use prohibition "
19093 "'%s', since a corresponding attribute use "
19094 "exists already in the type definition",
19095 xmlSchemaFormatQName(&str,
19096 prohib->targetNamespace, prohib->name),
19097 NULL, NULL);
19098 FREE_AND_NULL(str);
19099 /*
19100 * Remove the prohibition.
19101 */
19102 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19103 return(-1);
19104 break;
19105 }
19106 }
19107 }
19108 }
19109 return(0);
19110}
19111
19112/**
19113 * xmlSchemaAttributeGroupExpandRefs:
19114 * @pctxt: the parser context
19115 * @attrGr: the attribute group definition
19116 *
19117 * Computation of:
19118 * {attribute uses} property
19119 * {attribute wildcard} property
19120 *
19121 * Substitutes contained attribute group references
19122 * for their attribute uses. Wilcards are intersected.
19123 */
19124static int
19125xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19126 xmlSchemaAttributeGroupPtr attrGr)
19127{
19128 if ((attrGr->attrUses == NULL) ||
19129 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19130 return(0);
19131
19132 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19133 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19134 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19135 return(-1);
19136 return(0);
19137}
19138
19139/**
19140 * xmlSchemaAttributeGroupExpandRefs:
19141 * @pctxt: the parser context
19142 * @attrGr: the attribute group definition
19143 *
19144 * Substitutes contained attribute group references
19145 * for their attribute uses. Wilcards are intersected.
19146 *
19147 * Schema Component Constraint:
19148 * Attribute Group Definition Properties Correct (ag-props-correct)
19149 */
19150static int
19151xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19152 xmlSchemaAttributeGroupPtr attrGr)
19153{
19154 /*
19155 * SPEC ag-props-correct
19156 * (1) "The values of the properties of an attribute group definition
19157 * must be as described in the property tableau in The Attribute
19158 * Group Definition Schema Component (§3.6.1), modulo the impact of
19159 * Missing Sub-components (§5.3);"
19160 */
19161
19162 if ((attrGr->attrUses != NULL) &&
19163 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19164 {
19165 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19166 xmlSchemaAttributeUsePtr use, tmp;
19167 int i, j, hasId = 0;
19168
19169 for (i = uses->nbItems -1; i >= 0; i--) {
19170 use = uses->items[i];
19171 /*
19172 * SPEC ag-props-correct
19173 * (2) "Two distinct members of the {attribute uses} must not have
19174 * {attribute declaration}s both of whose {name}s match and whose
19175 * {target namespace}s are identical."
19176 */
19177 if (i > 0) {
19178 for (j = i -1; j >= 0; j--) {
19179 tmp = uses->items[j];
19180 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19181 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19182 (WXS_ATTRUSE_DECL_TNS(use) ==
19183 WXS_ATTRUSE_DECL_TNS(tmp)))
19184 {
19185 xmlChar *str = NULL;
19186
19187 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19188 XML_SCHEMAP_AG_PROPS_CORRECT,
19189 attrGr->node, WXS_BASIC_CAST attrGr,
19190 "Duplicate %s",
19191 xmlSchemaGetComponentDesignation(&str, use),
19192 NULL);
19193 FREE_AND_NULL(str);
19194 /*
19195 * Remove the duplicate.
19196 */
19197 if (xmlSchemaItemListRemove(uses, i) == -1)
19198 return(-1);
19199 goto next_use;
19200 }
19201 }
19202 }
19203 /*
19204 * SPEC ag-props-correct
19205 * (3) "Two distinct members of the {attribute uses} must not have
19206 * {attribute declaration}s both of whose {type definition}s are or
19207 * are derived from ID."
19208 * TODO: Does 'derived' include member-types of unions?
19209 */
19210 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19211 if (xmlSchemaIsDerivedFromBuiltInType(
19212 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19213 {
19214 if (hasId) {
19215 xmlChar *str = NULL;
19216
19217 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19218 XML_SCHEMAP_AG_PROPS_CORRECT,
19219 attrGr->node, WXS_BASIC_CAST attrGr,
19220 "There must not exist more than one attribute "
19221 "declaration of type 'xs:ID' "
19222 "(or derived from 'xs:ID'). The %s violates this "
19223 "constraint",
19224 xmlSchemaGetComponentDesignation(&str, use),
19225 NULL);
19226 FREE_AND_NULL(str);
19227 if (xmlSchemaItemListRemove(uses, i) == -1)
19228 return(-1);
19229 }
19230 hasId = 1;
19231 }
19232 }
19233next_use: {}
19234 }
19235 }
19236 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019237}
19238
19239/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019240 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019241 * @attrgrpDecl: the schema attribute definition
19242 * @ctxt: the schema parser context
19243 * @name: the attribute name
19244 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019245 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019246 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019247static int
19248xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19249 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019250{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019251 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019253 if (ref->item != NULL)
19254 return(0);
19255 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19256 ref->name,
19257 ref->targetNamespace);
19258 if (group == NULL) {
19259 xmlSchemaPResCompAttrErr(ctxt,
19260 XML_SCHEMAP_SRC_RESOLVE,
19261 NULL, ref->node,
19262 "ref", ref->name, ref->targetNamespace,
19263 ref->itemType, NULL);
19264 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019265 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019266 ref->item = WXS_BASIC_CAST group;
19267 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019268}
19269
19270/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019271 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019272 * @item: an schema attribute declaration/use
19273 * @ctxt: a schema parser context
19274 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019275 *
19276 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019277 * Schema Component Constraint:
19278 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019279 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019280 * Validates the value constraints of an attribute declaration/use.
19281 * NOTE that this needs the simle type definitions to be already
19282 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019283 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019284static int
19285xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19286 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019287{
19288
19289 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019290 * SPEC a-props-correct (1)
19291 * "The values of the properties of an attribute declaration must
19292 * be as described in the property tableau in The Attribute
19293 * Declaration Schema Component (§3.2.1), modulo the impact of
19294 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019295 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019296
19297 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19298 return(0);
19299
19300 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019301 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019302
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019303 /*
19304 * SPEC a-props-correct (3)
19305 * "If the {type definition} is or is derived from ID then there
19306 * must not be a {value constraint}."
19307 */
19308 if (xmlSchemaIsDerivedFromBuiltInType(
19309 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19310 {
19311 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19312 XML_SCHEMAP_A_PROPS_CORRECT_3,
19313 NULL, WXS_BASIC_CAST attr,
19314 "Value constraints are not allowed if the type definition "
19315 "is or is derived from xs:ID",
19316 NULL, NULL);
19317 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019318 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019319 /*
19320 * SPEC a-props-correct (2)
19321 * "if there is a {value constraint}, the canonical lexical
19322 * representation of its value must be ·valid· with respect
19323 * to the {type definition} as defined in String Valid (§3.14.4)."
19324 * TODO: Don't care about the *cononical* stuff here, this requirement
19325 * will be removed in WXS 1.1 anyway.
19326 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019327 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019328 attr->node, WXS_ATTR_TYPEDEF(attr),
19329 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019330 1, 1, 0);
19331 if (ret != 0) {
19332 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019333 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019334 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019335 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019336 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019337 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019338 XML_SCHEMAP_A_PROPS_CORRECT_2,
19339 NULL, WXS_BASIC_CAST attr,
19340 "The value of the value constraint is not valid",
19341 NULL, NULL);
19342 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019343 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019344 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019345
19346 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019347}
19348
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019349static xmlSchemaElementPtr
19350xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19351 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019352{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019353 xmlSchemaElementPtr ret;
19354
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019355 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019356 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019357 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019358 return (ancestor);
19359
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019360 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019361 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019362 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019363 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019364 WXS_SUBST_HEAD(ancestor));
19365 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019366
19367 return (ret);
19368}
19369
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019370/**
19371 * xmlSchemaCheckElemPropsCorrect:
19372 * @ctxt: a schema parser context
19373 * @decl: the element declaration
19374 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019375 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019376 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019377 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019378 *
19379 * STATUS:
19380 * missing: (6)
19381 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019382static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019383xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19384 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019385{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019386 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019387 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019388 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019389 * SPEC (1) "The values of the properties of an element declaration
19390 * must be as described in the property tableau in The Element
19391 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19392 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019393 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019394 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19395 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019396
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019397 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019398 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019399 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019400 * affiliation}, then {scope} must be global."
19401 */
19402 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19403 xmlSchemaPCustomErr(pctxt,
19404 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019405 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019406 "Only global element declarations can have a "
19407 "substitution group affiliation", NULL);
19408 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019409 }
19410 /*
19411 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19412 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019413 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019414 * property."
19415 */
19416 if (head == elemDecl)
19417 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019418 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019419 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19420 else
19421 circ = NULL;
19422 if (circ != NULL) {
19423 xmlChar *strA = NULL, *strB = NULL;
19424
19425 xmlSchemaPCustomErrExt(pctxt,
19426 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019427 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019428 "The element declaration '%s' defines a circular "
19429 "substitution group to element declaration '%s'",
19430 xmlSchemaGetComponentQName(&strA, circ),
19431 xmlSchemaGetComponentQName(&strB, head),
19432 NULL);
19433 FREE_AND_NULL(strA)
19434 FREE_AND_NULL(strB)
19435 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19436 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019437 /*
19438 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019439 * the {type definition}
19440 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019441 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019442 * of the {substitution group exclusions} of the {substitution group
19443 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19444 * (if the {type definition} is complex) or as defined in
19445 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019446 * simple)."
19447 *
19448 * NOTE: {substitution group exclusions} means the values of the
19449 * attribute "final".
19450 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019451
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019452 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019453 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019454
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019455 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19456 set |= SUBSET_EXTENSION;
19457 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19458 set |= SUBSET_RESTRICTION;
19459
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019460 if (xmlSchemaCheckCOSDerivedOK(pctxt, typeDef,
19461 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019462 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19463
19464 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019465 xmlSchemaPCustomErrExt(pctxt,
19466 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019467 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019468 "The type definition '%s' was "
19469 "either rejected by the substitution group "
19470 "affiliation '%s', or not validly derived from its type "
19471 "definition '%s'",
19472 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019473 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019474 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019475 FREE_AND_NULL(strA)
19476 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019477 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019478 }
19479 }
19480 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019481 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019482 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019483 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019484 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019485 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019486 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019487 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019488 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019489 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019490 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019491 (WXS_IS_COMPLEX(typeDef) &&
19492 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019493 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19494 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019495
19496 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19497 xmlSchemaPCustomErr(pctxt,
19498 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019499 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019500 "The type definition (or type definition's content type) is or "
19501 "is derived from ID; value constraints are not allowed in "
19502 "conjunction with such a type definition", NULL);
19503 } else if (elemDecl->value != NULL) {
19504 int vcret;
19505 xmlNodePtr node = NULL;
19506
19507 /*
19508 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19509 * representation of its value must be ·valid· with respect to the
19510 * {type definition} as defined in Element Default Valid (Immediate)
19511 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019512 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019513 if (typeDef == NULL) {
19514 xmlSchemaPErr(pctxt, elemDecl->node,
19515 XML_SCHEMAP_INTERNAL,
19516 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19517 "type is missing... skipping validation of "
19518 "the value constraint", NULL, NULL);
19519 return (-1);
19520 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019521 if (elemDecl->node != NULL) {
19522 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19523 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19524 BAD_CAST "fixed");
19525 else
19526 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19527 BAD_CAST "default");
19528 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019529 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19530 typeDef, elemDecl->value, &(elemDecl->defVal));
19531 if (vcret != 0) {
19532 if (vcret < 0) {
19533 PERROR_INT("xmlSchemaElemCheckValConstr",
19534 "failed to validate the value constraint of an "
19535 "element declaration");
19536 return (-1);
19537 }
19538 return (vcret);
19539 }
19540 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019541
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019542 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019543}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019544
19545/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019546 * xmlSchemaCheckElemSubstGroup:
19547 * @ctxt: a schema parser context
19548 * @decl: the element declaration
19549 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019550 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019551 * Schema Component Constraint:
19552 * Substitution Group (cos-equiv-class)
19553 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019554 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019555 * a list will be built for each subst. group head, holding all direct
19556 * referents to this head.
19557 * NOTE that this function needs:
19558 * 1. circular subst. groups to be checked beforehand
19559 * 2. the declaration's type to be derived from the head's type
19560 *
19561 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019562 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019563 */
19564static void
19565xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19566 xmlSchemaElementPtr elemDecl)
19567{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019568 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019569 /* SPEC (1) "Its {abstract} is false." */
19570 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19571 return;
19572 {
19573 xmlSchemaElementPtr head;
19574 xmlSchemaTypePtr headType, type;
19575 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019576 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019577 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19578 * {disallowed substitutions} as the blocking constraint, as defined in
19579 * Substitution Group OK (Transitive) (§3.3.6)."
19580 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019581 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19582 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019583 set = 0;
19584 methSet = 0;
19585 /*
19586 * The blocking constraints.
19587 */
19588 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19589 continue;
19590 headType = head->subtypes;
19591 type = elemDecl->subtypes;
19592 if (headType == type)
19593 goto add_member;
19594 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19595 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19596 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19597 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19598 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019599 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019600 * "The set of all {derivation method}s involved in the
19601 * derivation of D's {type definition} from C's {type definition}
19602 * does not intersect with the union of the blocking constraint,
19603 * C's {prohibited substitutions} (if C is complex, otherwise the
19604 * empty set) and the {prohibited substitutions} (respectively the
19605 * empty set) of any intermediate {type definition}s in the
19606 * derivation of D's {type definition} from C's {type definition}."
19607 */
19608 /*
19609 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19610 * subst.head axis, the methSet does not need to be computed for
19611 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019612 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019613 /*
19614 * The set of all {derivation method}s involved in the derivation
19615 */
19616 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019617 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019618 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19619 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019620
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019621 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019622 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19623 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19624
19625 type = type->baseType;
19626 }
19627 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019628 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019629 * the head's type.
19630 */
19631 type = elemDecl->subtypes->baseType;
19632 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019633 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019634 if ((type->flags &
19635 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19636 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19637 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19638 if ((type->flags &
19639 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19640 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19641 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19642 } else
19643 break;
19644 if (type == headType)
19645 break;
19646 type = type->baseType;
19647 }
19648 if ((set != 0) &&
19649 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19650 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19651 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19652 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19653 continue;
19654 }
19655add_member:
19656 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19657 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19658 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19659 }
19660 }
19661}
19662
19663/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019664 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019665 * @item: an schema element declaration/particle
19666 * @ctxt: a schema parser context
19667 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019668 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019669 * Validates the value constraints of an element declaration.
19670 *
19671 * Fixes finish doing the computations on the element declarations.
19672 */
19673static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019674xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019675 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019676{
19677 if (elemDecl == NULL)
19678 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019679 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19680 return;
19681 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019682 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19683 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019684}
19685
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019686/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019687 * xmlSchemaResolveModelGroupParticleReferences:
19688 * @particle: a particle component
19689 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019690 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019691 * Resolves references of a model group's {particles} to
19692 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019693 */
19694static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019695xmlSchemaResolveModelGroupParticleReferences(
19696 xmlSchemaParserCtxtPtr ctxt,
19697 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019698{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019699 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19700 xmlSchemaQNameRefPtr ref;
19701 xmlSchemaBasicItemPtr refItem;
19702
19703 /*
19704 * URGENT TODO: Test this.
19705 */
19706 while (particle != NULL) {
19707 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19708 ((WXS_PARTICLE_TERM(particle))->type !=
19709 XML_SCHEMA_EXTRA_QNAMEREF))
19710 {
19711 goto next_particle;
19712 }
19713 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019714 /*
19715 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019716 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019717 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019718 particle->children = NULL;
19719
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019720 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19721 ref->itemType, ref->name, ref->targetNamespace);
19722 if (refItem == NULL) {
19723 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019724 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019725 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019726 /* TODO: remove the particle. */
19727 goto next_particle;
19728 }
19729 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19730 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19731 /* TODO: remove the particle. */
19732 goto next_particle;
19733 /*
19734 * NOTE that we will assign the model group definition
19735 * itself to the "term" of the particle. This will ease
19736 * the check for circular model group definitions. After
19737 * that the "term" will be assigned the model group of the
19738 * model group definition.
19739 */
19740 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19741 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019742 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019743 * SPEC cos-all-limited (1)
19744 * SPEC cos-all-limited (1.2)
19745 * "It appears only as the value of one or both of the
19746 * following properties:"
19747 * (1.1) "the {model group} property of a model group
19748 * definition."
19749 * (1.2) "the {term} property of a particle [... of] the "
19750 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019751 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019752 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19753 /* TODO: error code */
19754 XML_SCHEMAP_COS_ALL_LIMITED,
19755 WXS_ITEM_NODE(particle), NULL,
19756 "A model group definition is referenced, but "
19757 "it contains an 'all' model group, which "
19758 "cannot be contained by model groups",
19759 NULL, NULL);
19760 /* TODO: remove the particle. */
19761 goto next_particle;
19762 }
19763 particle->children = (xmlSchemaTreeItemPtr) refItem;
19764 } else {
19765 /*
19766 * TODO: Are referenced element declarations the only
19767 * other components we expect here?
19768 */
19769 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019770 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019771next_particle:
19772 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019773 }
19774}
19775
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019776static int
19777xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19778 xmlSchemaValPtr y)
19779{
19780 xmlSchemaTypePtr tx, ty, ptx, pty;
19781 int ret;
19782
19783 while (x != NULL) {
19784 /* Same types. */
19785 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19786 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19787 ptx = xmlSchemaGetPrimitiveType(tx);
19788 pty = xmlSchemaGetPrimitiveType(ty);
19789 /*
19790 * (1) if a datatype T' is ·derived· by ·restriction· from an
19791 * atomic datatype T then the ·value space· of T' is a subset of
19792 * the ·value space· of T. */
19793 /*
19794 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19795 * from a common atomic ancestor T then the ·value space·s of T'
19796 * and T'' may overlap.
19797 */
19798 if (ptx != pty)
19799 return(0);
19800 /*
19801 * We assume computed values to be normalized, so do a fast
19802 * string comparison for string based types.
19803 */
19804 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019805 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019806 if (! xmlStrEqual(
19807 xmlSchemaValueGetAsString(x),
19808 xmlSchemaValueGetAsString(y)))
19809 return (0);
19810 } else {
19811 ret = xmlSchemaCompareValuesWhtsp(
19812 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19813 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19814 if (ret == -2)
19815 return(-1);
19816 if (ret != 0)
19817 return(0);
19818 }
19819 /*
19820 * Lists.
19821 */
19822 x = xmlSchemaValueGetNext(x);
19823 if (x != NULL) {
19824 y = xmlSchemaValueGetNext(y);
19825 if (y == NULL)
19826 return (0);
19827 } else if (xmlSchemaValueGetNext(y) != NULL)
19828 return (0);
19829 else
19830 return (1);
19831 }
19832 return (0);
19833}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019834
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019835/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019836 * xmlSchemaResolveAttrUseReferences:
19837 * @item: an attribute use
19838 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019839 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019840 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019841 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019842static int
19843xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19844 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019845{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019846 if ((ctxt == NULL) || (ause == NULL))
19847 return(-1);
19848 if ((ause->attrDecl == NULL) ||
19849 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19850 return(0);
19851
19852 {
19853 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19854
19855 /*
19856 * TODO: Evaluate, what errors could occur if the declaration is not
19857 * found.
19858 */
19859 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19860 ref->name, ref->targetNamespace);
19861 if (ause->attrDecl == NULL) {
19862 xmlSchemaPResCompAttrErr(ctxt,
19863 XML_SCHEMAP_SRC_RESOLVE,
19864 WXS_BASIC_CAST ause, ause->node,
19865 "ref", ref->name, ref->targetNamespace,
19866 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19867 return(ctxt->err);;
19868 }
19869 }
19870 return(0);
19871}
19872
19873/**
19874 * xmlSchemaCheckAttrUsePropsCorrect:
19875 * @ctxt: a parser context
19876 * @use: an attribute use
19877 *
19878 * Schema Component Constraint:
19879 * Attribute Use Correct (au-props-correct)
19880 *
19881 */
19882static int
19883xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19884 xmlSchemaAttributeUsePtr use)
19885{
19886 if ((ctxt == NULL) || (use == NULL))
19887 return(-1);
19888 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19889 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19890 return(0);
19891
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019892 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019893 * SPEC au-props-correct (1)
19894 * "The values of the properties of an attribute use must be as
19895 * described in the property tableau in The Attribute Use Schema
19896 * Component (§3.5.1), modulo the impact of Missing
19897 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019898 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019899
19900 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19901 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19902 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19903 {
19904 xmlSchemaPCustomErr(ctxt,
19905 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19906 WXS_BASIC_CAST use, NULL,
19907 "The attribute declaration has a 'fixed' value constraint "
19908 ", thus the attribute use must also have a 'fixed' value "
19909 "constraint",
19910 NULL);
19911 return(ctxt->err);
19912 }
19913 /*
19914 * Compute and check the value constraint's value.
19915 */
19916 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19917 int ret;
19918 /*
19919 * TODO: The spec seems to be missing a check of the
19920 * value constraint of the attribute use. We will do it here.
19921 */
19922 /*
19923 * SPEC a-props-correct (3)
19924 */
19925 if (xmlSchemaIsDerivedFromBuiltInType(
19926 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19927 {
19928 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19929 XML_SCHEMAP_AU_PROPS_CORRECT,
19930 NULL, WXS_BASIC_CAST use,
19931 "Value constraints are not allowed if the type definition "
19932 "is or is derived from xs:ID",
19933 NULL, NULL);
19934 return(ctxt->err);
19935 }
19936
19937 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19938 use->node, WXS_ATTRUSE_TYPEDEF(use),
19939 use->defValue, &(use->defVal),
19940 1, 1, 0);
19941 if (ret != 0) {
19942 if (ret < 0) {
19943 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19944 "calling xmlSchemaVCheckCVCSimpleType()");
19945 return(-1);
19946 }
19947 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19948 XML_SCHEMAP_AU_PROPS_CORRECT,
19949 NULL, WXS_BASIC_CAST use,
19950 "The value of the value constraint is not valid",
19951 NULL, NULL);
19952 return(ctxt->err);
19953 }
19954 }
19955 /*
19956 * SPEC au-props-correct (2)
19957 * "If the {attribute declaration} has a fixed
19958 * {value constraint}, then if the attribute use itself has a
19959 * {value constraint}, it must also be fixed and its value must match
19960 * that of the {attribute declaration}'s {value constraint}."
19961 */
19962 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
19963 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19964 {
19965 if (! xmlSchemaAreValuesEqual(use->defVal,
19966 (WXS_ATTRUSE_DECL(use))->defVal))
19967 {
19968 xmlSchemaPCustomErr(ctxt,
19969 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19970 WXS_BASIC_CAST use, NULL,
19971 "The 'fixed' value constraint of the attribute use "
19972 "must match the attribute declaration's value "
19973 "constraint '%s'",
19974 (WXS_ATTRUSE_DECL(use))->defValue);
19975 }
19976 return(ctxt->err);
19977 }
19978 return(0);
19979}
19980
19981
19982
19983
19984/**
19985 * xmlSchemaResolveAttrTypeReferences:
19986 * @item: an attribute declaration
19987 * @ctxt: a parser context
19988 *
19989 * Resolves the referenced type definition component.
19990 */
19991static int
19992xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
19993 xmlSchemaParserCtxtPtr ctxt)
19994{
Daniel Veillard01fa6152004-06-29 17:04:39 +000019995 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019996 * The simple type definition corresponding to the <simpleType> element
19997 * information item in the [children], if present, otherwise the simple
19998 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000019999 * [attribute], if present, otherwise the ·simple ur-type definition·.
20000 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020001 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020002 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020003 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20004 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020005 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020006 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020007 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020008
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020009 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20010 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020011 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020012 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020013 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020014 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020015 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020016 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020017 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020018 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020019 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020020
Daniel Veillard3646d642004-06-02 19:19:14 +000020021 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020022 /*
20023 * The type defaults to the xs:anySimpleType.
20024 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020025 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20026 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020027 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020028}
20029
20030/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020031 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020032 * @idc: the identity-constraint definition
20033 * @ctxt: the schema parser context
20034 * @name: the attribute name
20035 *
20036 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020037 * Schema Component Constraint:
20038 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020039 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020040static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020041xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020042 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020043{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020044 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020045 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020046 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020047 idc->ref->item = (xmlSchemaBasicItemPtr)
20048 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20049 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020050 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020051 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020052 * TODO: It is actually not an error to fail to resolve
20053 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020054 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020055 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020056 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020057 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020058 "refer", idc->ref->name,
20059 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020060 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020061 return(pctxt->err);
20062 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20063 /*
20064 * SPEC c-props-correct (1)
20065 */
20066 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20067 XML_SCHEMAP_C_PROPS_CORRECT,
20068 NULL, WXS_BASIC_CAST idc,
20069 "The keyref references a keyref",
20070 NULL, NULL);
20071 idc->ref->item = NULL;
20072 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020073 } else {
20074 if (idc->nbFields !=
20075 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20076 xmlChar *str = NULL;
20077 xmlSchemaIDCPtr refer;
20078
20079 refer = (xmlSchemaIDCPtr) idc->ref->item;
20080 /*
20081 * SPEC c-props-correct(2)
20082 * "If the {identity-constraint category} is keyref,
20083 * the cardinality of the {fields} must equal that of
20084 * the {fields} of the {referenced key}.
20085 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020086 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020087 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020088 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020089 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020090 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020091 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020092 refer->name),
20093 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020094 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020095 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020096 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020097 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020098 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020099 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020100}
20101
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020102static int
20103xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20104 xmlSchemaParserCtxtPtr pctxt)
20105{
20106 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20107 prohib->targetNamespace) == NULL) {
20108
20109 xmlSchemaPResCompAttrErr(pctxt,
20110 XML_SCHEMAP_SRC_RESOLVE,
20111 NULL, prohib->node,
20112 "ref", prohib->name, prohib->targetNamespace,
20113 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20114 return(XML_SCHEMAP_SRC_RESOLVE);
20115 }
20116 return(0);
20117}
20118
20119#define WXS_REDEFINED_TYPE(c) \
20120(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20121
20122#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20123(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20124
20125#define WXS_REDEFINED_ATTR_GROUP(c) \
20126(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20127
20128static int
20129xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20130{
20131 int err = 0;
20132 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20133 xmlSchemaBasicItemPtr prev, item;
20134 int wasRedefined;
20135
20136 if (redef == NULL)
20137 return(0);
20138
20139 do {
20140 item = redef->item;
20141 /*
20142 * First try to locate the redefined component in the
20143 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020144 * NOTE: According to this schema bug entry:
20145 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20146 * it's not clear if the referenced component needs to originate
20147 * from the <redefine>d schema _document_ or the schema; the latter
20148 * would include all imported and included sub-schemas of the
20149 * <redefine>d schema. Currenlty we latter approach is used.
20150 * SUPPLEMENT: It seems that the WG moves towards the latter
20151 * approach, so we are doing it right.
20152 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020153 */
20154 prev = xmlSchemaFindRedefCompInGraph(
20155 redef->targetBucket, item->type,
20156 redef->refName, redef->refTargetNs);
20157 if (prev == NULL) {
20158 xmlChar *str = NULL;
20159 xmlNodePtr node;
20160
20161 /*
20162 * SPEC src-redefine:
20163 * (6.2.1) "The ·actual value· of its own name attribute plus
20164 * target namespace must successfully ·resolve· to a model
20165 * group definition in I."
20166 * (7.2.1) "The ·actual value· of its own name attribute plus
20167 * target namespace must successfully ·resolve· to an attribute
20168 * group definition in I."
20169
20170 *
20171 * Note that, if we are redefining with the use of references
20172 * to components, the spec assumes the src-resolve to be used;
20173 * but this won't assure that we search only *inside* the
20174 * redefined schema.
20175 */
20176 if (redef->reference)
20177 node = WXS_ITEM_NODE(redef->reference);
20178 else
20179 node = WXS_ITEM_NODE(item);
20180 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20181 /*
20182 * TODO: error code.
20183 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20184 * reference kind.
20185 */
20186 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
20187 "The %s to be redefined could not be found in "
20188 "the redefined schema",
20189 WXS_ITEM_TYPE_NAME(item),
20190 xmlSchemaFormatQName(&str, redef->refTargetNs,
20191 redef->refName));
20192 FREE_AND_NULL(str);
20193 err = pctxt->err;
20194 redef = redef->next;
20195 continue;
20196 }
20197 /*
20198 * TODO: Obtaining and setting the redefinition state is really
20199 * clumsy.
20200 */
20201 wasRedefined = 0;
20202 switch (item->type) {
20203 case XML_SCHEMA_TYPE_COMPLEX:
20204 case XML_SCHEMA_TYPE_SIMPLE:
20205 if ((WXS_TYPE_CAST prev)->flags &
20206 XML_SCHEMAS_TYPE_REDEFINED)
20207 {
20208 wasRedefined = 1;
20209 break;
20210 }
20211 /* Mark it as redefined. */
20212 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20213 /*
20214 * Assign the redefined type to the
20215 * base type of the redefining type.
20216 * TODO: How
20217 */
20218 ((xmlSchemaTypePtr) item)->baseType =
20219 (xmlSchemaTypePtr) prev;
20220 break;
20221 case XML_SCHEMA_TYPE_GROUP:
20222 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20223 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20224 {
20225 wasRedefined = 1;
20226 break;
20227 }
20228 /* Mark it as redefined. */
20229 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20230 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20231 if (redef->reference != NULL) {
20232 /*
20233 * Overwrite the QName-reference with the
20234 * referenced model group def.
20235 */
20236 (WXS_PTC_CAST redef->reference)->children =
20237 WXS_TREE_CAST prev;
20238 }
20239 redef->target = prev;
20240 break;
20241 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20242 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20243 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20244 {
20245 wasRedefined = 1;
20246 break;
20247 }
20248 (WXS_ATTR_GROUP_CAST prev)->flags |=
20249 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20250 if (redef->reference != NULL) {
20251 /*
20252 * Assign the redefined attribute group to the
20253 * QName-reference component.
20254 * This is the easy case, since we will just
20255 * expand the redefined group.
20256 */
20257 (WXS_QNAME_CAST redef->reference)->item = prev;
20258 redef->target = NULL;
20259 } else {
20260 /*
20261 * This is the complicated case: we need
20262 * to apply src-redefine (7.2.2) at a later
20263 * stage, i.e. when attribute group references
20264 * have beed expanded and simple types have
20265 * beed fixed.
20266 */
20267 redef->target = prev;
20268 }
20269 break;
20270 default:
20271 PERROR_INT("xmlSchemaResolveRedefReferences",
20272 "Unexpected redefined component type");
20273 return(-1);
20274 }
20275 if (wasRedefined) {
20276 xmlChar *str = NULL;
20277 xmlNodePtr node;
20278
20279 if (redef->reference)
20280 node = WXS_ITEM_NODE(redef->reference);
20281 else
20282 node = WXS_ITEM_NODE(redef->item);
20283
20284 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20285 /* TODO: error code. */
20286 XML_SCHEMAP_SRC_REDEFINE,
20287 node, NULL,
20288 "The referenced %s was already redefined. Multiple "
20289 "redefinition of the same component is not supported",
20290 xmlSchemaGetComponentDesignation(&str, prev),
20291 NULL);
20292 FREE_AND_NULL(str)
20293 err = pctxt->err;
20294 redef = redef->next;
20295 continue;
20296 }
20297 redef = redef->next;
20298 } while (redef != NULL);
20299
20300 return(err);
20301}
20302
20303static int
20304xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20305{
20306 int err = 0;
20307 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20308 xmlSchemaBasicItemPtr item;
20309
20310 if (redef == NULL)
20311 return(0);
20312
20313 do {
20314 if (redef->target == NULL) {
20315 redef = redef->next;
20316 continue;
20317 }
20318 item = redef->item;
20319
20320 switch (item->type) {
20321 case XML_SCHEMA_TYPE_SIMPLE:
20322 case XML_SCHEMA_TYPE_COMPLEX:
20323 /*
20324 * Since the spec wants the {name} of the redefined
20325 * type to be 'absent', we'll NULL it.
20326 */
20327 (WXS_TYPE_CAST redef->target)->name = NULL;
20328
20329 /*
20330 * TODO: Seems like there's nothing more to do. The normal
20331 * inheritance mechanism is used. But not 100% sure.
20332 */
20333 break;
20334 case XML_SCHEMA_TYPE_GROUP:
20335 /*
20336 * URGENT TODO:
20337 * SPEC src-redefine:
20338 * (6.2.2) "The {model group} of the model group definition
20339 * which corresponds to it per XML Representation of Model
20340 * Group Definition Schema Components (§3.7.2) must be a
20341 * ·valid restriction· of the {model group} of that model
20342 * group definition in I, as defined in Particle Valid
20343 * (Restriction) (§3.9.6)."
20344 */
20345 break;
20346 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20347 /*
20348 * SPEC src-redefine:
20349 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20350 * the attribute group definition which corresponds to it
20351 * per XML Representation of Attribute Group Definition Schema
20352 * Components (§3.6.2) must be ·valid restrictions· of the
20353 * {attribute uses} and {attribute wildcard} of that attribute
20354 * group definition in I, as defined in clause 2, clause 3 and
20355 * clause 4 of Derivation Valid (Restriction, Complex)
20356 * (§3.4.6) (where references to the base type definition are
20357 * understood as references to the attribute group definition
20358 * in I)."
20359 */
20360 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20361 XML_SCHEMA_ACTION_REDEFINE,
20362 item, redef->target,
20363 (WXS_ATTR_GROUP_CAST item)->attrUses,
20364 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20365 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20366 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20367 if (err == -1)
20368 return(-1);
20369 break;
20370 default:
20371 break;
20372 }
20373 redef = redef->next;
20374 } while (redef != NULL);
20375 return(0);
20376}
20377
20378
20379static int
20380xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20381 xmlSchemaBucketPtr bucket)
20382{
20383 xmlSchemaBasicItemPtr item;
20384 int err;
20385 xmlHashTablePtr *table;
20386 const xmlChar *name;
20387 int i;
20388
20389#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20390 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20391 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20392 else \
20393 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20394
20395 /*
20396 * Add global components to the schema's hash tables.
20397 * This is the place where duplicate components will be
20398 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020399 * TODO: I think normally we should support imports of the
20400 * same namespace from multiple locations. We don't do currently,
20401 * but if we do then according to:
20402 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20403 * we would need, if imported directly, to import redefined
20404 * components as well to be able to catch clashing components.
20405 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020406 */
20407 if (bucket == NULL)
20408 return(-1);
20409 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20410 return(0);
20411 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20412
20413 for (i = 0; i < bucket->globals->nbItems; i++) {
20414 item = bucket->globals->items[i];
20415 table = NULL;
20416 switch (item->type) {
20417 case XML_SCHEMA_TYPE_COMPLEX:
20418 case XML_SCHEMA_TYPE_SIMPLE:
20419 if (WXS_REDEFINED_TYPE(item))
20420 continue;
20421 name = (WXS_TYPE_CAST item)->name;
20422 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20423 break;
20424 case XML_SCHEMA_TYPE_ELEMENT:
20425 name = (WXS_ELEM_CAST item)->name;
20426 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20427 break;
20428 case XML_SCHEMA_TYPE_ATTRIBUTE:
20429 name = (WXS_ATTR_CAST item)->name;
20430 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20431 break;
20432 case XML_SCHEMA_TYPE_GROUP:
20433 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20434 continue;
20435 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20436 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20437 break;
20438 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20439 if (WXS_REDEFINED_ATTR_GROUP(item))
20440 continue;
20441 name = (WXS_ATTR_GROUP_CAST item)->name;
20442 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20443 break;
20444 case XML_SCHEMA_TYPE_IDC_KEY:
20445 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20446 case XML_SCHEMA_TYPE_IDC_KEYREF:
20447 name = (WXS_IDC_CAST item)->name;
20448 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20449 break;
20450 case XML_SCHEMA_TYPE_NOTATION:
20451 name = ((xmlSchemaNotationPtr) item)->name;
20452 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20453 break;
20454 default:
20455 PERROR_INT("xmlSchemaAddComponents",
20456 "Unexpected global component type");
20457 continue;
20458 }
20459 if (*table == NULL) {
20460 *table = xmlHashCreateDict(10, pctxt->dict);
20461 if (*table == NULL) {
20462 PERROR_INT("xmlSchemaAddComponents",
20463 "failed to create a component hash table");
20464 return(-1);
20465 }
20466 }
20467 err = xmlHashAddEntry(*table, name, item);
20468 if (err != 0) {
20469 xmlChar *str = NULL;
20470
20471 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20472 XML_SCHEMAP_REDEFINED_TYPE,
20473 WXS_ITEM_NODE(item),
20474 WXS_BASIC_CAST item,
20475 "A global %s '%s' does already exist",
20476 WXS_ITEM_TYPE_NAME(item),
20477 xmlSchemaGetComponentQName(&str, item));
20478 FREE_AND_NULL(str);
20479 }
20480 }
20481 /*
20482 * Process imported/included schemas.
20483 */
20484 if (bucket->relations != NULL) {
20485 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20486 do {
20487 if ((rel->bucket != NULL) &&
20488 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20489 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20490 return(-1);
20491 }
20492 rel = rel->next;
20493 } while (rel != NULL);
20494 }
20495 return(0);
20496}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020497
20498static int
20499xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20500{
20501 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20502 xmlSchemaTreeItemPtr item, *items;
20503 int nbItems, i;
20504
20505#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20506
20507 if ((con->pending == NULL) ||
20508 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020509 return(0);
20510
20511 /* TODO:
20512 * SPEC (src-redefine):
20513 * (6.2) "If it has no such self-reference, then all of the
20514 * following must be true:"
20515
20516 * (6.2.2) The {model group} of the model group definition which
20517 * corresponds to it per XML Representation of Model Group
20518 * Definition Schema Components (§3.7.2) must be a ·valid
20519 * restriction· of the {model group} of that model group definition
20520 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20521 */
20522 xmlSchemaCheckSRCRedefineFirst(pctxt);
20523
20524 /*
20525 * Add global components to the schemata's hash tables.
20526 */
20527 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020528
20529 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020530 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20531 nbItems = con->pending->nbItems;
20532 /*
20533 * Now that we have parsed *all* the schema document(s) and converted
20534 * them to schema components, we can resolve references, apply component
20535 * constraints, create the FSA from the content model, etc.
20536 */
20537 /*
20538 * Resolve references of..
20539 *
20540 * 1. element declarations:
20541 * - the type definition
20542 * - the substitution group affiliation
20543 * 2. simple/complex types:
20544 * - the base type definition
20545 * - the memberTypes of union types
20546 * - the itemType of list types
20547 * 3. attributes declarations and attribute uses:
20548 * - the type definition
20549 * - if an attribute use, then the attribute declaration
20550 * 4. attribute group references:
20551 * - the attribute group definition
20552 * 5. particles:
20553 * - the term of the particle (e.g. a model group)
20554 * 6. IDC key-references:
20555 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020556 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020557 */
20558 for (i = 0; i < nbItems; i++) {
20559 item = items[i];
20560 switch (item->type) {
20561 case XML_SCHEMA_TYPE_ELEMENT:
20562 xmlSchemaResolveElementReferences(
20563 (xmlSchemaElementPtr) item, pctxt);
20564 FIXHFAILURE;
20565 break;
20566 case XML_SCHEMA_TYPE_COMPLEX:
20567 case XML_SCHEMA_TYPE_SIMPLE:
20568 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020569 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020570 FIXHFAILURE;
20571 break;
20572 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020573 xmlSchemaResolveAttrTypeReferences(
20574 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020575 FIXHFAILURE;
20576 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020577 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20578 xmlSchemaResolveAttrUseReferences(
20579 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020580 FIXHFAILURE;
20581 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020582 case XML_SCHEMA_EXTRA_QNAMEREF:
20583 if ((WXS_QNAME_CAST item)->itemType ==
20584 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20585 {
20586 xmlSchemaResolveAttrGroupReferences(
20587 WXS_QNAME_CAST item, pctxt);
20588 }
20589 FIXHFAILURE;
20590 break;
20591 case XML_SCHEMA_TYPE_SEQUENCE:
20592 case XML_SCHEMA_TYPE_CHOICE:
20593 case XML_SCHEMA_TYPE_ALL:
20594 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20595 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020596 FIXHFAILURE;
20597 break;
20598 case XML_SCHEMA_TYPE_IDC_KEY:
20599 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20600 case XML_SCHEMA_TYPE_IDC_KEYREF:
20601 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020602 (xmlSchemaIDCPtr) item, pctxt);
20603 FIXHFAILURE;
20604 break;
20605 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20606 /*
20607 * Handle attribue prohibition which had a
20608 * "ref" attribute.
20609 */
20610 xmlSchemaResolveAttrUseProhibReferences(
20611 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020612 FIXHFAILURE;
20613 break;
20614 default:
20615 break;
20616 }
20617 }
20618 if (pctxt->nberrors != 0)
20619 goto exit_error;
20620
20621 /*
20622 * Now that all references are resolved we
20623 * can check for circularity of...
20624 * 1. the base axis of type definitions
20625 * 2. nested model group definitions
20626 * 3. nested attribute group definitions
20627 * TODO: check for circual substitution groups.
20628 */
20629 for (i = 0; i < nbItems; i++) {
20630 item = items[i];
20631 /*
20632 * Let's better stop on the first error here.
20633 */
20634 switch (item->type) {
20635 case XML_SCHEMA_TYPE_COMPLEX:
20636 case XML_SCHEMA_TYPE_SIMPLE:
20637 xmlSchemaCheckTypeDefCircular(
20638 (xmlSchemaTypePtr) item, pctxt);
20639 FIXHFAILURE;
20640 if (pctxt->nberrors != 0)
20641 goto exit_error;
20642 break;
20643 case XML_SCHEMA_TYPE_GROUP:
20644 xmlSchemaCheckGroupDefCircular(
20645 (xmlSchemaModelGroupDefPtr) item, pctxt);
20646 FIXHFAILURE;
20647 if (pctxt->nberrors != 0)
20648 goto exit_error;
20649 break;
20650 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20651 xmlSchemaCheckAttrGroupCircular(
20652 (xmlSchemaAttributeGroupPtr) item, pctxt);
20653 FIXHFAILURE;
20654 if (pctxt->nberrors != 0)
20655 goto exit_error;
20656 break;
20657 default:
20658 break;
20659 }
20660 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020661 if (pctxt->nberrors != 0)
20662 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020663 /*
20664 * Model group definition references:
20665 * Such a reference is reflected by a particle at the component
20666 * level. Until now the 'term' of such particles pointed
20667 * to the model group definition; this was done, in order to
20668 * ease circularity checks. Now we need to set the 'term' of
20669 * such particles to the model group of the model group definition.
20670 */
20671 for (i = 0; i < nbItems; i++) {
20672 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020673 switch (item->type) {
20674 case XML_SCHEMA_TYPE_SEQUENCE:
20675 case XML_SCHEMA_TYPE_CHOICE:
20676 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20677 WXS_MODEL_GROUP_CAST item);
20678 break;
20679 default:
20680 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020681 }
20682 }
20683 if (pctxt->nberrors != 0)
20684 goto exit_error;
20685 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020686 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020688 for (i = 0; i < nbItems; i++) {
20689 item = items[i];
20690 switch (item->type) {
20691 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20692 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20693 WXS_ATTR_GROUP_HAS_REFS(item))
20694 {
20695 xmlSchemaAttributeGroupExpandRefs(pctxt,
20696 WXS_ATTR_GROUP_CAST item);
20697 FIXHFAILURE;
20698 }
20699 break;
20700 default:
20701 break;
20702 }
20703 }
20704 if (pctxt->nberrors != 0)
20705 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020706 /*
20707 * First compute the variety of simple types. This is needed as
20708 * a seperate step, since otherwise we won't be able to detect
20709 * circular union types in all cases.
20710 */
20711 for (i = 0; i < nbItems; i++) {
20712 item = items[i];
20713 switch (item->type) {
20714 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020715 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020716 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20717 (xmlSchemaTypePtr) item);
20718 FIXHFAILURE;
20719 }
20720 break;
20721 default:
20722 break;
20723 }
20724 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020725 if (pctxt->nberrors != 0)
20726 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020727 /*
20728 * Detect circular union types. Note that this needs the variety to
20729 * be already computed.
20730 */
20731 for (i = 0; i < nbItems; i++) {
20732 item = items[i];
20733 switch (item->type) {
20734 case XML_SCHEMA_TYPE_SIMPLE:
20735 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20736 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20737 (xmlSchemaTypePtr) item);
20738 FIXHFAILURE;
20739 }
20740 break;
20741 default:
20742 break;
20743 }
20744 }
20745 if (pctxt->nberrors != 0)
20746 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020747
20748 /*
20749 * Do the complete type fixup for simple types.
20750 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020751 for (i = 0; i < nbItems; i++) {
20752 item = items[i];
20753 switch (item->type) {
20754 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020755 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20756 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20757 FIXHFAILURE;
20758 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020759 break;
20760 default:
20761 break;
20762 }
20763 }
20764 if (pctxt->nberrors != 0)
20765 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020766 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020767 * At this point we need all simple types to be builded and checked.
20768 */
20769 /*
20770 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020771 */
20772 for (i = 0; i < nbItems; i++) {
20773 item = items[i];
20774 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020775 case XML_SCHEMA_TYPE_ATTRIBUTE:
20776 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20777 FIXHFAILURE;
20778 break;
20779 default:
20780 break;
20781 }
20782 }
20783 if (pctxt->nberrors != 0)
20784 goto exit_error;
20785 /*
20786 * Apply constraints for attribute uses.
20787 */
20788 for (i = 0; i < nbItems; i++) {
20789 item = items[i];
20790 switch (item->type) {
20791 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20792 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20793 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20794 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020795 FIXHFAILURE;
20796 }
20797 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020798 default:
20799 break;
20800 }
20801 }
20802 if (pctxt->nberrors != 0)
20803 goto exit_error;
20804
20805 /*
20806 * Apply constraints for attribute group definitions.
20807 */
20808 for (i = 0; i < nbItems; i++) {
20809 item = items[i];
20810 switch (item->type) {
20811 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20812 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20813 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20814 {
20815 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20816 FIXHFAILURE;
20817 }
20818 break;
20819 default:
20820 break;
20821 }
20822 }
20823 if (pctxt->nberrors != 0)
20824 goto exit_error;
20825
20826 /*
20827 * Apply constraints for redefinitions.
20828 */
20829 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20830 xmlSchemaCheckSRCRedefineSecond(pctxt);
20831 if (pctxt->nberrors != 0)
20832 goto exit_error;
20833
20834 /*
20835 * Fixup complex types.
20836 */
20837 for (i = 0; i < nbItems; i++) {
20838 item = con->pending->items[i];
20839 switch (item->type) {
20840 case XML_SCHEMA_TYPE_COMPLEX:
20841 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20842 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20843 FIXHFAILURE;
20844 }
20845 break;
20846 default:
20847 break;
20848 }
20849 }
20850 if (pctxt->nberrors != 0)
20851 goto exit_error;
20852
20853 /*
20854 * The list could have changed, since xmlSchemaFixupComplexType()
20855 * will create particles and model groups in some cases.
20856 */
20857 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20858 nbItems = con->pending->nbItems;
20859
20860 /*
20861 * At this point all complex types need to be builded and checked.
20862 */
20863 /*
20864 * Apply some constraints for element declarations.
20865 */
20866 for (i = 0; i < nbItems; i++) {
20867 item = items[i];
20868 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020869 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020870
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020871 if ((((xmlSchemaElementPtr) item)->flags &
20872 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20873 xmlSchemaCheckElementDeclComponent(
20874 (xmlSchemaElementPtr) item, pctxt);
20875 FIXHFAILURE;
20876 }
20877 break;
20878 default:
20879 break;
20880 }
20881 }
20882 if (pctxt->nberrors != 0)
20883 goto exit_error;
20884 /*
20885 * Finally we can build the automaton from the content model of
20886 * complex types.
20887 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020888
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020889 for (i = 0; i < nbItems; i++) {
20890 item = items[i];
20891 switch (item->type) {
20892 case XML_SCHEMA_TYPE_COMPLEX:
20893 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020894 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020895 break;
20896 default:
20897 break;
20898 }
20899 }
20900 if (pctxt->nberrors != 0)
20901 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020902 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020903 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020904 */
20905 con->pending->nbItems = 0;
20906 return(0);
20907exit_error:
20908 con->pending->nbItems = 0;
20909 return(pctxt->err);
20910exit_failure:
20911 con->pending->nbItems = 0;
20912 return(-1);
20913}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020914/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020915 * xmlSchemaParse:
20916 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020917 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020918 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020919 * XML Shema struture which can be used to validate instances.
20920 * *WARNING* this interface is highly subject to change
20921 *
20922 * Returns the internal XML Schema structure built from the resource or
20923 * NULL in case of error
20924 */
20925xmlSchemaPtr
20926xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20927{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020928 xmlSchemaPtr schema = NULL;
20929 xmlSchemaBucketPtr bucket = NULL;
20930 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020931
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020932 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020933 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020934 * the API; i.e. not automatically by the validated instance document.
20935 */
20936
Daniel Veillard4255d502002-04-16 15:50:10 +000020937 xmlSchemaInitTypes();
20938
Daniel Veillard6045c902002-10-09 21:13:59 +000020939 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020940 return (NULL);
20941
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020942 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020943 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020944 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020945 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020946
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020947 /* Create the *main* schema. */
20948 schema = xmlSchemaNewSchema(ctxt);
20949 if (schema == NULL)
20950 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000020951 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020952 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020953 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020954 if (ctxt->constructor == NULL) {
20955 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
20956 if (ctxt->constructor == NULL)
20957 return(NULL);
20958 /* Take ownership of the constructor to be able to free it. */
20959 ctxt->ownsConstructor = 1;
20960 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020961 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020962 /*
20963 * Locate and add the schema document.
20964 */
20965 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
20966 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
20967 NULL, NULL, &bucket);
20968 if (res == -1)
20969 goto exit_failure;
20970 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000020971 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020972
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020973 if (bucket == NULL) {
20974 /* TODO: Error code, actually we failed to *locate* the schema. */
20975 if (ctxt->URL)
20976 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20977 NULL, NULL,
20978 "Failed to locate the main schema resource at '%s'",
20979 ctxt->URL, NULL);
20980 else
20981 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20982 NULL, NULL,
20983 "Failed to locate the main schema resource",
20984 NULL, NULL);
20985 goto exit;
20986 }
20987 /* Set the main schema bucket. */
20988 ctxt->constructor->bucket = bucket;
20989 ctxt->targetNamespace = bucket->targetNamespace;
20990 schema->targetNamespace = bucket->targetNamespace;
20991
20992 /* Then do the parsing for good. */
20993 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
20994 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020995 if (ctxt->nberrors != 0)
20996 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020997
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020998 schema->doc = bucket->doc;
20999 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021000
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021001 ctxt->schema = schema;
21002
21003 if (xmlSchemaFixupComponents(ctxt) == -1)
21004 goto exit_failure;
21005
21006 /*
21007 * TODO: This is not nice, since we cannot distinguish from the
21008 * result if there was an internal error or not.
21009 */
21010exit:
21011 if (ctxt->nberrors != 0) {
21012 if (schema) {
21013 xmlSchemaFree(schema);
21014 schema = NULL;
21015 }
21016 if (ctxt->constructor) {
21017 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21018 ctxt->constructor = NULL;
21019 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021020 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021021 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021022 ctxt->schema = NULL;
21023 return(schema);
21024exit_failure:
21025 /*
21026 * Quite verbose, but should catch internal errors, which were
21027 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021028 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021029 if (schema) {
21030 xmlSchemaFree(schema);
21031 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021032 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021033 if (ctxt->constructor) {
21034 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21035 ctxt->constructor = NULL;
21036 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021037 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021038 PERROR_INT2("xmlSchemaParse",
21039 "An internal error occured");
21040 ctxt->schema = NULL;
21041 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021042}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021043
Daniel Veillard4255d502002-04-16 15:50:10 +000021044/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021045 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021046 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021047 * @err: the error callback
21048 * @warn: the warning callback
21049 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021050 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021051 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021052 */
21053void
21054xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021055 xmlSchemaValidityErrorFunc err,
21056 xmlSchemaValidityWarningFunc warn, void *ctx)
21057{
Daniel Veillard4255d502002-04-16 15:50:10 +000021058 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021059 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021060 ctxt->error = err;
21061 ctxt->warning = warn;
21062 ctxt->userData = ctx;
21063}
21064
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021065/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021066 * xmlSchemaGetParserErrors:
21067 * @ctxt: a XMl-Schema parser context
21068 * @err: the error callback result
21069 * @warn: the warning callback result
21070 * @ctx: contextual data for the callbacks result
21071 *
21072 * Get the callback information used to handle errors for a parser context
21073 *
21074 * Returns -1 in case of failure, 0 otherwise
21075 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021076int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021077xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
21078 xmlSchemaValidityErrorFunc * err,
21079 xmlSchemaValidityWarningFunc * warn, void **ctx)
21080{
21081 if (ctxt == NULL)
21082 return(-1);
21083 if (err != NULL)
21084 *err = ctxt->error;
21085 if (warn != NULL)
21086 *warn = ctxt->warning;
21087 if (ctx != NULL)
21088 *ctx = ctxt->userData;
21089 return(0);
21090}
21091
21092/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021093 * xmlSchemaFacetTypeToString:
21094 * @type: the facet type
21095 *
21096 * Convert the xmlSchemaTypeType to a char string.
21097 *
21098 * Returns the char string representation of the facet type if the
21099 * type is a facet and an "Internal Error" string otherwise.
21100 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021101static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021102xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21103{
21104 switch (type) {
21105 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021106 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021107 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021108 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021109 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021110 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021111 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021112 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021113 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021114 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021115 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021116 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021117 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021118 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021119 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021120 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021121 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021122 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021123 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021124 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021125 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021126 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021127 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021128 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021129 default:
21130 break;
21131 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021132 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021133}
21134
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021135static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021136xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21137{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021138 /*
21139 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021140 * from xsd:string.
21141 */
21142 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021143 /*
21144 * Note that we assume a whitespace of preserve for anySimpleType.
21145 */
21146 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21147 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21148 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021149 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021150 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021151 else {
21152 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021153 * For all ·atomic· datatypes other than string (and types ·derived·
21154 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021155 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021156 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021157 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021158 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021159 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021160 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021161 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021162 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021163 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021164 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021165 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021166 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021167 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021168 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21169 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21170 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21171 return (XML_SCHEMA_WHITESPACE_REPLACE);
21172 else
21173 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021174 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021175 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021176}
21177
Daniel Veillard4255d502002-04-16 15:50:10 +000021178/************************************************************************
21179 * *
21180 * Simple type validation *
21181 * *
21182 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021183
Daniel Veillard4255d502002-04-16 15:50:10 +000021184
21185/************************************************************************
21186 * *
21187 * DOM Validation code *
21188 * *
21189 ************************************************************************/
21190
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021191/**
21192 * xmlSchemaAssembleByLocation:
21193 * @pctxt: a schema parser context
21194 * @vctxt: a schema validation context
21195 * @schema: the existing schema
21196 * @node: the node that fired the assembling
21197 * @nsName: the namespace name of the new schema
21198 * @location: the location of the schema
21199 *
21200 * Expands an existing schema by an additional schema.
21201 *
21202 * Returns 0 if the new schema is correct, a positive error code
21203 * number otherwise and -1 in case of an internal or API error.
21204 */
21205static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021206xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021207 xmlSchemaPtr schema,
21208 xmlNodePtr node,
21209 const xmlChar *nsName,
21210 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021211{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021212 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021213 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021214 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021215
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021216 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021217 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021218
21219 if (vctxt->pctxt == NULL) {
21220 VERROR_INT("xmlSchemaAssembleByLocation",
21221 "no parser context available");
21222 return(-1);
21223 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021224 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021225 if (pctxt->constructor == NULL) {
21226 PERROR_INT("xmlSchemaAssembleByLocation",
21227 "no constructor");
21228 return(-1);
21229 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021230 /*
21231 * Acquire the schema document.
21232 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021233 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21234 location, node);
21235 /*
21236 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21237 * the process will automatically change this to
21238 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21239 */
21240 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21241 location, NULL, NULL, 0, node, NULL, nsName,
21242 &bucket);
21243 if (ret != 0)
21244 return(ret);
21245 if (bucket == NULL) {
21246 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021247 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021248 return(-1);
21249 }
21250 /*
21251 * The first located schema will be handled as if all other
21252 * schemas imported by XSI were imported by this first schema.
21253 */
21254 if ((bucket != NULL) &&
21255 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21256 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21257 /*
21258 * TODO: Is this handled like an import? I.e. is it not an error
21259 * if the schema cannot be located?
21260 */
21261 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21262 return(0);
21263 /*
21264 * We will reuse the parser context for every schema imported
21265 * directly via XSI. So reset the context.
21266 */
21267 pctxt->nberrors = 0;
21268 pctxt->err = 0;
21269 pctxt->doc = bucket->doc;
21270
21271 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21272 if (ret == -1) {
21273 pctxt->doc = NULL;
21274 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021275 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021276 /* Paranoid error channelling. */
21277 if ((ret == 0) && (pctxt->nberrors != 0))
21278 ret = pctxt->err;
21279 if (pctxt->nberrors == 0) {
21280 /*
21281 * Only bother to fixup pending components, if there was
21282 * no error yet.
21283 */
21284 xmlSchemaFixupComponents(pctxt);
21285 vctxt->nberrors += pctxt->nberrors;
21286 } else {
21287 /* Add to validation error sum. */
21288 vctxt->nberrors += pctxt->nberrors;
21289 }
21290 pctxt->doc = NULL;
21291 return(ret);
21292exit_failure:
21293 pctxt->doc = NULL;
21294 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021295}
21296
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021297static xmlSchemaAttrInfoPtr
21298xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21299 int metaType)
21300{
21301 if (vctxt->nbAttrInfos == 0)
21302 return (NULL);
21303 {
21304 int i;
21305 xmlSchemaAttrInfoPtr iattr;
21306
21307 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21308 iattr = vctxt->attrInfos[i];
21309 if (iattr->metaType == metaType)
21310 return (iattr);
21311 }
21312
21313 }
21314 return (NULL);
21315}
21316
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021317/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021318 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021319 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021320 *
21321 * Expands an existing schema by an additional schema using
21322 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21323 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21324 * must be set to 1.
21325 *
21326 * Returns 0 if the new schema is correct, a positive error code
21327 * number otherwise and -1 in case of an internal or API error.
21328 */
21329static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021330xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021331{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021332 const xmlChar *cur, *end;
21333 const xmlChar *nsname = NULL, *location;
21334 int count = 0;
21335 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021336 xmlSchemaAttrInfoPtr iattr;
21337
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021338 /*
21339 * Parse the value; we will assume an even number of values
21340 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021341 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021342 * URGENT TODO: !! This needs to work for both
21343 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21344 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021345 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021346 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21347 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21348 if (iattr == NULL)
21349 xmlSchemaGetMetaAttrInfo(vctxt,
21350 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21351 if (iattr == NULL)
21352 return (0);
21353 cur = iattr->value;
21354 do {
21355 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021356 /*
21357 * Get the namespace name.
21358 */
21359 while (IS_BLANK_CH(*cur))
21360 cur++;
21361 end = cur;
21362 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21363 end++;
21364 if (end == cur)
21365 break;
21366 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021367 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021368 cur = end;
21369 }
21370 /*
21371 * Get the URI.
21372 */
21373 while (IS_BLANK_CH(*cur))
21374 cur++;
21375 end = cur;
21376 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21377 end++;
21378 if (end == cur)
21379 break;
21380 count++;
21381 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021382 cur = end;
21383 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21384 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021385 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021386 VERROR_INT("xmlSchemaAssembleByXSI",
21387 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021388 return (-1);
21389 }
21390 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021391 return (ret);
21392}
21393
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021394static const xmlChar *
21395xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21396 const xmlChar *prefix)
21397{
21398 if (vctxt->sax != NULL) {
21399 int i, j;
21400 xmlSchemaNodeInfoPtr inode;
21401
21402 for (i = vctxt->depth; i >= 0; i--) {
21403 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21404 inode = vctxt->elemInfos[i];
21405 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21406 if (((prefix == NULL) &&
21407 (inode->nsBindings[j] == NULL)) ||
21408 ((prefix != NULL) && xmlStrEqual(prefix,
21409 inode->nsBindings[j]))) {
21410
21411 /*
21412 * Note that the namespace bindings are already
21413 * in a string dict.
21414 */
21415 return (inode->nsBindings[j+1]);
21416 }
21417 }
21418 }
21419 }
21420 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021421#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021422 } else if (vctxt->reader != NULL) {
21423 xmlChar *nsName;
21424
21425 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21426 if (nsName != NULL) {
21427 const xmlChar *ret;
21428
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021429 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21430 xmlFree(nsName);
21431 return (ret);
21432 } else
21433 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021434#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021435 } else {
21436 xmlNsPtr ns;
21437
21438 if ((vctxt->inode->node == NULL) ||
21439 (vctxt->inode->node->doc == NULL)) {
21440 VERROR_INT("xmlSchemaLookupNamespace",
21441 "no node or node's doc avaliable");
21442 return (NULL);
21443 }
21444 ns = xmlSearchNs(vctxt->inode->node->doc,
21445 vctxt->inode->node, prefix);
21446 if (ns != NULL)
21447 return (ns->href);
21448 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021449 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021450}
21451
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021452/*
21453* This one works on the schema of the validation context.
21454*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021455static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021456xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21457 xmlSchemaPtr schema,
21458 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021459 const xmlChar *value,
21460 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021461 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021462{
21463 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021464
21465 if (vctxt && (vctxt->schema == NULL)) {
21466 VERROR_INT("xmlSchemaValidateNotation",
21467 "a schema is needed on the validation context");
21468 return (-1);
21469 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021470 ret = xmlValidateQName(value, 1);
21471 if (ret != 0)
21472 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021473 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021474 xmlChar *localName = NULL;
21475 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021476
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021477 localName = xmlSplitQName2(value, &prefix);
21478 if (prefix != NULL) {
21479 const xmlChar *nsName = NULL;
21480
21481 if (vctxt != NULL)
21482 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21483 else if (node != NULL) {
21484 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21485 if (ns != NULL)
21486 nsName = ns->href;
21487 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021488 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021489 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021490 return (1);
21491 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021492 if (nsName == NULL) {
21493 xmlFree(prefix);
21494 xmlFree(localName);
21495 return (1);
21496 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021497 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021498 if (valNeeded && (val != NULL)) {
21499 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21500 BAD_CAST xmlStrdup(nsName));
21501 if (*val == NULL)
21502 ret = -1;
21503 }
21504 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021505 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021506 xmlFree(prefix);
21507 xmlFree(localName);
21508 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021509 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021510 if (valNeeded && (val != NULL)) {
21511 (*val) = xmlSchemaNewNOTATIONValue(
21512 BAD_CAST xmlStrdup(value), NULL);
21513 if (*val == NULL)
21514 ret = -1;
21515 }
21516 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021517 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021518 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021519 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021520 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021521}
21522
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021523static int
21524xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21525 const xmlChar* lname,
21526 const xmlChar* nsname)
21527{
21528 int i;
21529
21530 lname = xmlDictLookup(vctxt->dict, lname, -1);
21531 if (lname == NULL)
21532 return(-1);
21533 if (nsname != NULL) {
21534 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21535 if (nsname == NULL)
21536 return(-1);
21537 }
21538 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21539 if ((vctxt->nodeQNames->items [i] == lname) &&
21540 (vctxt->nodeQNames->items[i +1] == nsname))
21541 /* Already there */
21542 return(i);
21543 }
21544 /* Add new entry. */
21545 i = vctxt->nodeQNames->nbItems;
21546 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21547 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21548 return(i);
21549}
21550
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021551/************************************************************************
21552 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021553 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021554 * *
21555 ************************************************************************/
21556
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021557/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021558 * xmlSchemaAugmentIDC:
21559 * @idcDef: the IDC definition
21560 *
21561 * Creates an augmented IDC definition item.
21562 *
21563 * Returns the item, or NULL on internal errors.
21564 */
21565static void
21566xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21567 xmlSchemaValidCtxtPtr vctxt)
21568{
21569 xmlSchemaIDCAugPtr aidc;
21570
21571 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21572 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021573 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021574 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21575 NULL);
21576 return;
21577 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021578 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021579 aidc->def = idcDef;
21580 aidc->next = NULL;
21581 if (vctxt->aidcs == NULL)
21582 vctxt->aidcs = aidc;
21583 else {
21584 aidc->next = vctxt->aidcs;
21585 vctxt->aidcs = aidc;
21586 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021587 /*
21588 * Save if we have keyrefs at all.
21589 */
21590 if ((vctxt->hasKeyrefs == 0) &&
21591 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21592 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021593}
21594
21595/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021596 * xmlSchemaIDCNewBinding:
21597 * @idcDef: the IDC definition of this binding
21598 *
21599 * Creates a new IDC binding.
21600 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021601 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021602 */
21603static xmlSchemaPSVIIDCBindingPtr
21604xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21605{
21606 xmlSchemaPSVIIDCBindingPtr ret;
21607
21608 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21609 sizeof(xmlSchemaPSVIIDCBinding));
21610 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021611 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021612 "allocating a PSVI IDC binding item", NULL);
21613 return (NULL);
21614 }
21615 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21616 ret->definition = idcDef;
21617 return (ret);
21618}
21619
21620/**
21621 * xmlSchemaIDCStoreNodeTableItem:
21622 * @vctxt: the WXS validation context
21623 * @item: the IDC node table item
21624 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021625 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021626 * They are stored to avoid copying them if IDC node-tables are merged
21627 * with corresponding parent IDC node-tables (bubbling).
21628 *
21629 * Returns 0 if succeeded, -1 on internal errors.
21630 */
21631static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021632xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021633 xmlSchemaPSVIIDCNodePtr item)
21634{
21635 /*
21636 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021637 */
21638 if (vctxt->idcNodes == NULL) {
21639 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021640 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21641 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021642 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021643 "allocating the IDC node table item list", NULL);
21644 return (-1);
21645 }
21646 vctxt->sizeIdcNodes = 20;
21647 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21648 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021649 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21650 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021651 sizeof(xmlSchemaPSVIIDCNodePtr));
21652 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021653 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021654 "re-allocating the IDC node table item list", NULL);
21655 return (-1);
21656 }
21657 }
21658 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021659
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021660 return (0);
21661}
21662
21663/**
21664 * xmlSchemaIDCStoreKey:
21665 * @vctxt: the WXS validation context
21666 * @item: the IDC key
21667 *
21668 * The validation context is used to store an IDC key.
21669 *
21670 * Returns 0 if succeeded, -1 on internal errors.
21671 */
21672static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021673xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021674 xmlSchemaPSVIIDCKeyPtr key)
21675{
21676 /*
21677 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021678 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021679 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021680 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021681 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21682 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021683 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021684 "allocating the IDC key storage list", NULL);
21685 return (-1);
21686 }
21687 vctxt->sizeIdcKeys = 40;
21688 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21689 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021690 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21691 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021692 sizeof(xmlSchemaPSVIIDCKeyPtr));
21693 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021694 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021695 "re-allocating the IDC key storage list", NULL);
21696 return (-1);
21697 }
21698 }
21699 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021700
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021701 return (0);
21702}
21703
21704/**
21705 * xmlSchemaIDCAppendNodeTableItem:
21706 * @bind: the IDC binding
21707 * @ntItem: the node-table item
21708 *
21709 * Appends the IDC node-table item to the binding.
21710 *
21711 * Returns 0 on success and -1 on internal errors.
21712 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021713static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021714xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21715 xmlSchemaPSVIIDCNodePtr ntItem)
21716{
21717 if (bind->nodeTable == NULL) {
21718 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021719 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021720 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21721 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021722 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021723 "allocating an array of IDC node-table items", NULL);
21724 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021725 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021726 } else if (bind->sizeNodes <= bind->nbNodes) {
21727 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021728 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21729 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021730 sizeof(xmlSchemaPSVIIDCNodePtr));
21731 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021732 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021733 "re-allocating an array of IDC node-table items", NULL);
21734 return(-1);
21735 }
21736 }
21737 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021738 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021739}
21740
21741/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021742 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021743 * @vctxt: the WXS validation context
21744 * @matcher: the IDC matcher
21745 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021746 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021747 * of the given matcher. If none found, a new one is created
21748 * and added to the IDC table.
21749 *
21750 * Returns an IDC binding or NULL on internal errors.
21751 */
21752static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021753xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021754 xmlSchemaIDCMatcherPtr matcher)
21755{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021756 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021757
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021758 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021759
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021760 if (ielem->idcTable == NULL) {
21761 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21762 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021763 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021764 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021765 } else {
21766 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021767
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021768 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021769 do {
21770 if (bind->definition == matcher->aidc->def)
21771 return(bind);
21772 if (bind->next == NULL) {
21773 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21774 if (bind->next == NULL)
21775 return (NULL);
21776 return(bind->next);
21777 }
21778 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021779 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021780 }
21781 return (NULL);
21782}
21783
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021784static xmlSchemaItemListPtr
21785xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21786 xmlSchemaIDCMatcherPtr matcher)
21787{
21788 if (matcher->targets == NULL)
21789 matcher->targets = xmlSchemaItemListCreate();
21790 return(matcher->targets);
21791}
21792
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021793/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021794 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021795 * @key: the IDC key
21796 *
21797 * Frees an IDC key together with its compiled value.
21798 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021799static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021800xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21801{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021802 if (key->val != NULL)
21803 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021804 xmlFree(key);
21805}
21806
21807/**
21808 * xmlSchemaIDCFreeBinding:
21809 *
21810 * Frees an IDC binding. Note that the node table-items
21811 * are not freed.
21812 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021813static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021814xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21815{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021816 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021817 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021818 if (bind->dupls != NULL)
21819 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021820 xmlFree(bind);
21821}
21822
21823/**
21824 * xmlSchemaIDCFreeIDCTable:
21825 * @bind: the first IDC binding in the list
21826 *
21827 * Frees an IDC table, i.e. all the IDC bindings in the list.
21828 */
21829static void
21830xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21831{
21832 xmlSchemaPSVIIDCBindingPtr prev;
21833
21834 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021835 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021836 bind = bind->next;
21837 xmlSchemaIDCFreeBinding(prev);
21838 }
21839}
21840
21841/**
21842 * xmlSchemaIDCFreeMatcherList:
21843 * @matcher: the first IDC matcher in the list
21844 *
21845 * Frees a list of IDC matchers.
21846 */
21847static void
21848xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21849{
21850 xmlSchemaIDCMatcherPtr next;
21851
21852 while (matcher != NULL) {
21853 next = matcher->next;
21854 if (matcher->keySeqs != NULL) {
21855 int i;
21856 for (i = 0; i < matcher->sizeKeySeqs; i++)
21857 if (matcher->keySeqs[i] != NULL)
21858 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021859 xmlFree(matcher->keySeqs);
21860 }
21861 if (matcher->targets != NULL) {
21862 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21863 int i;
21864 xmlSchemaPSVIIDCNodePtr idcNode;
21865 /*
21866 * Node-table items for keyrefs are not stored globally
21867 * to the validation context, since they are not bubbled.
21868 * We need to free them here.
21869 */
21870 for (i = 0; i < matcher->targets->nbItems; i++) {
21871 idcNode =
21872 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21873 xmlFree(idcNode->keys);
21874 xmlFree(idcNode);
21875 }
21876 }
21877 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021878 }
21879 xmlFree(matcher);
21880 matcher = next;
21881 }
21882}
21883
21884/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021885 * xmlSchemaIDCAddStateObject:
21886 * @vctxt: the WXS validation context
21887 * @matcher: the IDC matcher
21888 * @sel: the XPath information
21889 * @parent: the parent "selector" state object if any
21890 * @type: "selector" or "field"
21891 *
21892 * Creates/reuses and activates state objects for the given
21893 * XPath information; if the XPath expression consists of unions,
21894 * multiple state objects are created for every unioned expression.
21895 *
21896 * Returns 0 on success and -1 on internal errors.
21897 */
21898static int
21899xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21900 xmlSchemaIDCMatcherPtr matcher,
21901 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021902 int type)
21903{
21904 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021905
21906 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021907 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021908 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021909 if (vctxt->xpathStatePool != NULL) {
21910 sto = vctxt->xpathStatePool;
21911 vctxt->xpathStatePool = sto->next;
21912 sto->next = NULL;
21913 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021914 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021915 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021916 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021917 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21918 if (sto == NULL) {
21919 xmlSchemaVErrMemory(NULL,
21920 "allocating an IDC state object", NULL);
21921 return (-1);
21922 }
21923 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
21924 }
21925 /*
21926 * Add to global list.
21927 */
21928 if (vctxt->xpathStates != NULL)
21929 sto->next = vctxt->xpathStates;
21930 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021931
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021932 /*
21933 * Free the old xpath validation context.
21934 */
21935 if (sto->xpathCtxt != NULL)
21936 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
21937
21938 /*
21939 * Create a new XPath (pattern) validation context.
21940 */
21941 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
21942 (xmlPatternPtr) sel->xpathComp);
21943 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021944 VERROR_INT("xmlSchemaIDCAddStateObject",
21945 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021946 return (-1);
21947 }
21948 sto->type = type;
21949 sto->depth = vctxt->depth;
21950 sto->matcher = matcher;
21951 sto->sel = sel;
21952 sto->nbHistory = 0;
21953
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021954#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021955 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
21956 sto->sel->xpath);
21957#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021958 return (0);
21959}
21960
21961/**
21962 * xmlSchemaXPathEvaluate:
21963 * @vctxt: the WXS validation context
21964 * @nodeType: the nodeType of the current node
21965 *
21966 * Evaluates all active XPath state objects.
21967 *
21968 * Returns the number of IC "field" state objects which resolved to
21969 * this node, 0 if none resolved and -1 on internal errors.
21970 */
21971static int
21972xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021973 xmlElementType nodeType)
21974{
21975 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021976 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021977
21978 if (vctxt->xpathStates == NULL)
21979 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021980
21981 if (nodeType == XML_ATTRIBUTE_NODE)
21982 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021983#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021984 {
21985 xmlChar *str = NULL;
21986 xmlGenericError(xmlGenericErrorContext,
21987 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021988 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
21989 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021990 FREE_AND_NULL(str)
21991 }
21992#endif
21993 /*
21994 * Process all active XPath state objects.
21995 */
21996 first = vctxt->xpathStates;
21997 sto = first;
21998 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021999#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022000 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022001 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22002 sto->matcher->aidc->def->name, sto->sel->xpath);
22003 else
22004 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22005 sto->matcher->aidc->def->name, sto->sel->xpath);
22006#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022007 if (nodeType == XML_ELEMENT_NODE)
22008 res = xmlStreamPush((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 else
22011 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022012 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022013
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022014 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022015 VERROR_INT("xmlSchemaXPathEvaluate",
22016 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022017 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022018 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022019 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022020 goto next_sto;
22021 /*
22022 * Full match.
22023 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022024#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022025 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022026 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022027#endif
22028 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022029 * Register a match in the state object history.
22030 */
22031 if (sto->history == NULL) {
22032 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22033 if (sto->history == NULL) {
22034 xmlSchemaVErrMemory(NULL,
22035 "allocating the state object history", NULL);
22036 return(-1);
22037 }
22038 sto->sizeHistory = 10;
22039 } else if (sto->sizeHistory <= sto->nbHistory) {
22040 sto->sizeHistory *= 2;
22041 sto->history = (int *) xmlRealloc(sto->history,
22042 sto->sizeHistory * sizeof(int));
22043 if (sto->history == NULL) {
22044 xmlSchemaVErrMemory(NULL,
22045 "re-allocating the state object history", NULL);
22046 return(-1);
22047 }
22048 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022049 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022050
22051#ifdef DEBUG_IDC
22052 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22053 vctxt->depth);
22054#endif
22055
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022056 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22057 xmlSchemaIDCSelectPtr sel;
22058 /*
22059 * Activate state objects for the IDC fields of
22060 * the IDC selector.
22061 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022062#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022063 xmlGenericError(xmlGenericErrorContext, "IDC: "
22064 "activating field states\n");
22065#endif
22066 sel = sto->matcher->aidc->def->fields;
22067 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022068 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22069 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22070 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022071 sel = sel->next;
22072 }
22073 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22074 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022075 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022076 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022077#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022078 xmlGenericError(xmlGenericErrorContext,
22079 "IDC: key found\n");
22080#endif
22081 /*
22082 * Notify that the character value of this node is
22083 * needed.
22084 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022085 if (resolved == 0) {
22086 if ((vctxt->inode->flags &
22087 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22088 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22089 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022090 resolved++;
22091 }
22092next_sto:
22093 if (sto->next == NULL) {
22094 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022095 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022096 */
22097 head = first;
22098 sto = vctxt->xpathStates;
22099 } else
22100 sto = sto->next;
22101 }
22102 return (resolved);
22103}
22104
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022105static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022106xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022107 xmlChar **buf,
22108 xmlSchemaPSVIIDCKeyPtr *seq,
22109 int count)
22110{
22111 int i, res;
22112 const xmlChar *value = NULL;
22113
22114 *buf = xmlStrdup(BAD_CAST "[");
22115 for (i = 0; i < count; i++) {
22116 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022117 res = xmlSchemaGetCanonValueWhtsp(seq[i]->val, &value,
22118 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type));
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022119 if (res == 0)
22120 *buf = xmlStrcat(*buf, value);
22121 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022122 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22123 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022124 *buf = xmlStrcat(*buf, BAD_CAST "???");
22125 }
22126 if (i < count -1)
22127 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22128 else
22129 *buf = xmlStrcat(*buf, BAD_CAST "'");
22130 if (value != NULL) {
22131 xmlFree((xmlChar *) value);
22132 value = NULL;
22133 }
22134 }
22135 *buf = xmlStrcat(*buf, BAD_CAST "]");
22136
22137 return (BAD_CAST *buf);
22138}
22139
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022140/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022141 * xmlSchemaXPathPop:
22142 * @vctxt: the WXS validation context
22143 *
22144 * Pops all XPath states.
22145 *
22146 * Returns 0 on success and -1 on internal errors.
22147 */
22148static int
22149xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22150{
22151 xmlSchemaIDCStateObjPtr sto;
22152 int res;
22153
22154 if (vctxt->xpathStates == NULL)
22155 return(0);
22156 sto = vctxt->xpathStates;
22157 do {
22158 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22159 if (res == -1)
22160 return (-1);
22161 sto = sto->next;
22162 } while (sto != NULL);
22163 return(0);
22164}
22165
22166/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022167 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022168 * @vctxt: the WXS validation context
22169 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022170 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022171 *
22172 * Processes and pops the history items of the IDC state objects.
22173 * IDC key-sequences are validated/created on IDC bindings.
22174 *
22175 * Returns 0 on success and -1 on internal errors.
22176 */
22177static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022178xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022179 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022180{
22181 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022182 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022183 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022184 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022185
22186 if (vctxt->xpathStates == NULL)
22187 return (0);
22188 sto = vctxt->xpathStates;
22189
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022190#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022191 {
22192 xmlChar *str = NULL;
22193 xmlGenericError(xmlGenericErrorContext,
22194 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022195 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22196 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022197 FREE_AND_NULL(str)
22198 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022199#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022200 /*
22201 * Evaluate the state objects.
22202 */
22203 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022204 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22205 if (res == -1) {
22206 VERROR_INT("xmlSchemaXPathProcessHistory",
22207 "calling xmlStreamPop()");
22208 return (-1);
22209 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022210#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022211 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22212 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022213#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022214 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022215 goto deregister_check;
22216
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022217 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022218
22219 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022220 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022221 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022222 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022223 sto = sto->next;
22224 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022225 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022226 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022227 /*
22228 * NOTE: According to
22229 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22230 * ... the simple-content of complex types is also allowed.
22231 */
22232
22233 if (WXS_IS_COMPLEX(type)) {
22234 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22235 /*
22236 * Sanity check for complex types with simple content.
22237 */
22238 simpleType = type->contentTypeDef;
22239 if (simpleType == NULL) {
22240 VERROR_INT("xmlSchemaXPathProcessHistory",
22241 "field resolves to a CT with simple content "
22242 "but the CT is missing the ST definition");
22243 return (-1);
22244 }
22245 } else
22246 simpleType = NULL;
22247 } else
22248 simpleType = type;
22249 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022250 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022251
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022252 /*
22253 * Not qualified if the field resolves to a node of non
22254 * simple type.
22255 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022256 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022257 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022258 WXS_BASIC_CAST sto->matcher->aidc->def,
22259 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022260 "non-simple type",
22261 sto->sel->xpath,
22262 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22263 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022264 sto->nbHistory--;
22265 goto deregister_check;
22266 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022267
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022268 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022269 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022270 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022271 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022272 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022273 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022274 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022275 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022276 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022277 sto->nbHistory--;
22278 goto deregister_check;
22279 } else {
22280 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22281 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022282 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022283
22284 /*
22285 * The key will be anchored on the matcher's list of
22286 * key-sequences. The position in this list is determined
22287 * by the target node's depth relative to the matcher's
22288 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022289 *
22290 * Element Depth Pos List-entries
22291 * <scope> 0 NULL
22292 * <bar> 1 NULL
22293 * <target/> 2 2 target
22294 * <bar>
22295 * </scope>
22296 *
22297 * The size of the list is only dependant on the depth of
22298 * the tree.
22299 * An entry will be NULLed in selector_leave, i.e. when
22300 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022301 */
22302 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022303 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022304
22305 /*
22306 * Create/grow the array of key-sequences.
22307 */
22308 if (matcher->keySeqs == NULL) {
22309 if (pos > 9)
22310 matcher->sizeKeySeqs = pos * 2;
22311 else
22312 matcher->sizeKeySeqs = 10;
22313 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22314 xmlMalloc(matcher->sizeKeySeqs *
22315 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22316 if (matcher->keySeqs == NULL) {
22317 xmlSchemaVErrMemory(NULL,
22318 "allocating an array of key-sequences",
22319 NULL);
22320 return(-1);
22321 }
22322 memset(matcher->keySeqs, 0,
22323 matcher->sizeKeySeqs *
22324 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22325 } else if (pos >= matcher->sizeKeySeqs) {
22326 int i = matcher->sizeKeySeqs;
22327
22328 matcher->sizeKeySeqs *= 2;
22329 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22330 xmlRealloc(matcher->keySeqs,
22331 matcher->sizeKeySeqs *
22332 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022333 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022334 xmlSchemaVErrMemory(NULL,
22335 "reallocating an array of key-sequences",
22336 NULL);
22337 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022338 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022339 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022340 * The array needs to be NULLed.
22341 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022342 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022343 for (; i < matcher->sizeKeySeqs; i++)
22344 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022345 }
22346
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022347 /*
22348 * Get/create the key-sequence.
22349 */
22350 keySeq = matcher->keySeqs[pos];
22351 if (keySeq == NULL) {
22352 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022353 } else if (keySeq[idx] != NULL) {
22354 xmlChar *str = NULL;
22355 /*
22356 * cvc-identity-constraint:
22357 * 3 For each node in the ·target node set· all
22358 * of the {fields}, with that node as the context
22359 * node, evaluate to either an empty node-set or
22360 * a node-set with exactly one member, which must
22361 * have a simple type.
22362 *
22363 * The key was already set; report an error.
22364 */
22365 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22366 XML_SCHEMAV_CVC_IDC, NULL,
22367 WXS_BASIC_CAST matcher->aidc->def,
22368 "The XPath '%s' of a field of %s evaluates to a "
22369 "node-set with more than one member",
22370 sto->sel->xpath,
22371 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22372 FREE_AND_NULL(str);
22373 sto->nbHistory--;
22374 goto deregister_check;
22375 } else
22376 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022377
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022378create_sequence:
22379 /*
22380 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022381 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022382 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22383 matcher->aidc->def->nbFields *
22384 sizeof(xmlSchemaPSVIIDCKeyPtr));
22385 if (keySeq == NULL) {
22386 xmlSchemaVErrMemory(NULL,
22387 "allocating an IDC key-sequence", NULL);
22388 return(-1);
22389 }
22390 memset(keySeq, 0, matcher->aidc->def->nbFields *
22391 sizeof(xmlSchemaPSVIIDCKeyPtr));
22392 matcher->keySeqs[pos] = keySeq;
22393create_key:
22394 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022395 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022396 */
22397 if (key == NULL) {
22398 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22399 sizeof(xmlSchemaPSVIIDCKey));
22400 if (key == NULL) {
22401 xmlSchemaVErrMemory(NULL,
22402 "allocating a IDC key", NULL);
22403 xmlFree(keySeq);
22404 matcher->keySeqs[pos] = NULL;
22405 return(-1);
22406 }
22407 /*
22408 * Consume the compiled value.
22409 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022410 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022411 key->val = vctxt->inode->val;
22412 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022413 /*
22414 * Store the key in a global list.
22415 */
22416 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22417 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022418 return (-1);
22419 }
22420 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022421 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022422 }
22423 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022424
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022425 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022426 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022427 xmlSchemaPSVIIDCNodePtr ntItem;
22428 xmlSchemaIDCMatcherPtr matcher;
22429 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022430 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022431 int pos, i, j, nbKeys;
22432 /*
22433 * Here we have the following scenario:
22434 * An IDC 'selector' state object resolved to a target node,
22435 * during the time this target node was in the
22436 * ancestor-or-self axis, the 'field' state object(s) looked
22437 * out for matching nodes to create a key-sequence for this
22438 * target node. Now we are back to this target node and need
22439 * to put the key-sequence, together with the target node
22440 * itself, into the node-table of the corresponding IDC
22441 * binding.
22442 */
22443 matcher = sto->matcher;
22444 idc = matcher->aidc->def;
22445 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022446 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022447 /*
22448 * Check if the matcher has any key-sequences at all, plus
22449 * if it has a key-sequence for the current target node.
22450 */
22451 if ((matcher->keySeqs == NULL) ||
22452 (matcher->sizeKeySeqs <= pos)) {
22453 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22454 goto selector_key_error;
22455 else
22456 goto selector_leave;
22457 }
22458
22459 keySeq = &(matcher->keySeqs[pos]);
22460 if (*keySeq == NULL) {
22461 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22462 goto selector_key_error;
22463 else
22464 goto selector_leave;
22465 }
22466
22467 for (i = 0; i < nbKeys; i++) {
22468 if ((*keySeq)[i] == NULL) {
22469 /*
22470 * Not qualified, if not all fields did resolve.
22471 */
22472 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22473 /*
22474 * All fields of a "key" IDC must resolve.
22475 */
22476 goto selector_key_error;
22477 }
22478 goto selector_leave;
22479 }
22480 }
22481 /*
22482 * All fields did resolve.
22483 */
22484
22485 /*
22486 * 4.1 If the {identity-constraint category} is unique(/key),
22487 * then no two members of the ·qualified node set· have
22488 * ·key-sequences· whose members are pairwise equal, as
22489 * defined by Equal in [XML Schemas: Datatypes].
22490 *
22491 * Get the IDC binding from the matcher and check for
22492 * duplicate key-sequences.
22493 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022494#if 0
22495 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22496#endif
22497 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022498 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022499 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022500 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022501
22502 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022503 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022504 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022505 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022506 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022507 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022508 bkeySeq =
22509 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022510 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022511 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022512 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022513 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022514 if (res == -1) {
22515 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022516 } else if (res == 0) {
22517 /*
22518 * One of the keys differs, so the key-sequence
22519 * won't be equal; get out.
22520 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022521 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022522 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022523 }
22524 if (res == 1) {
22525 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022526 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022527 */
22528 break;
22529 }
22530 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022531 } while (i < targets->nbItems);
22532 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022533 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022534 /*
22535 * TODO: Try to report the key-sequence.
22536 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022537 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022538 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022539 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022540 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022541 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022542 (*keySeq), nbKeys),
22543 xmlSchemaGetIDCDesignation(&strB, idc));
22544 FREE_AND_NULL(str);
22545 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022546 goto selector_leave;
22547 }
22548 }
22549 /*
22550 * Add a node-table item to the IDC binding.
22551 */
22552 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22553 sizeof(xmlSchemaPSVIIDCNode));
22554 if (ntItem == NULL) {
22555 xmlSchemaVErrMemory(NULL,
22556 "allocating an IDC node-table item", NULL);
22557 xmlFree(*keySeq);
22558 *keySeq = NULL;
22559 return(-1);
22560 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022561 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022562
22563 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022564 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022565 */
22566 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22567 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22568 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022569 xmlFree(*keySeq);
22570 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022571 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022572 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022573 ntItem->nodeQNameID = -1;
22574 } else {
22575 /*
22576 * Save a cached QName for this node on the IDC node, to be
22577 * able to report it, even if the node is not saved.
22578 */
22579 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22580 vctxt->inode->localName, vctxt->inode->nsName);
22581 if (ntItem->nodeQNameID == -1) {
22582 xmlFree(ntItem);
22583 xmlFree(*keySeq);
22584 *keySeq = NULL;
22585 return (-1);
22586 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022587 }
22588 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022589 * Init the node-table item: Save the node, position and
22590 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022591 */
22592 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022593 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022594 ntItem->keys = *keySeq;
22595 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022596#if 0
22597 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22598#endif
22599 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022600 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22601 /*
22602 * Free the item, since keyref items won't be
22603 * put on a global list.
22604 */
22605 xmlFree(ntItem->keys);
22606 xmlFree(ntItem);
22607 }
22608 return (-1);
22609 }
22610
22611 goto selector_leave;
22612selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022613 {
22614 xmlChar *str = NULL;
22615 /*
22616 * 4.2.1 (KEY) The ·target node set· and the
22617 * ·qualified node set· are equal, that is, every
22618 * member of the ·target node set· is also a member
22619 * of the ·qualified node set· and vice versa.
22620 */
22621 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22622 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022623 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022624 "Not all fields of %s evaluate to a node",
22625 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22626 FREE_AND_NULL(str);
22627 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022628selector_leave:
22629 /*
22630 * Free the key-sequence if not added to the IDC table.
22631 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022632 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022633 xmlFree(*keySeq);
22634 *keySeq = NULL;
22635 }
22636 } /* if selector */
22637
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022638 sto->nbHistory--;
22639
22640deregister_check:
22641 /*
22642 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022643 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022644 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022645#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022646 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22647 sto->sel->xpath);
22648#endif
22649 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022650 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022651 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022652 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022653 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022654 nextsto = sto->next;
22655 /*
22656 * Unlink from the list of active XPath state objects.
22657 */
22658 vctxt->xpathStates = sto->next;
22659 sto->next = vctxt->xpathStatePool;
22660 /*
22661 * Link it to the pool of reusable state objects.
22662 */
22663 vctxt->xpathStatePool = sto;
22664 sto = nextsto;
22665 } else
22666 sto = sto->next;
22667 } /* while (sto != NULL) */
22668 return (0);
22669}
22670
22671/**
22672 * xmlSchemaIDCRegisterMatchers:
22673 * @vctxt: the WXS validation context
22674 * @elemDecl: the element declaration
22675 *
22676 * Creates helper objects to evaluate IDC selectors/fields
22677 * successively.
22678 *
22679 * Returns 0 if OK and -1 on internal errors.
22680 */
22681static int
22682xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22683 xmlSchemaElementPtr elemDecl)
22684{
22685 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22686 xmlSchemaIDCPtr idc, refIdc;
22687 xmlSchemaIDCAugPtr aidc;
22688
22689 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22690 if (idc == NULL)
22691 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022692
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022693#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022694 {
22695 xmlChar *str = NULL;
22696 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022697 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022698 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22699 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022700 FREE_AND_NULL(str)
22701 }
22702#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022703 if (vctxt->inode->idcMatchers != NULL) {
22704 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22705 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022706 return (-1);
22707 }
22708 do {
22709 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22710 /*
22711 * Since IDCs bubbles are expensive we need to know the
22712 * depth at which the bubbles should stop; this will be
22713 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022714 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022715 * be -1, indicating that no bubbles are needed.
22716 */
22717 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22718 if (refIdc != NULL) {
22719 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022720 * Remember that we have keyrefs on this node.
22721 */
22722 vctxt->inode->hasKeyrefs = 1;
22723 /*
22724 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022725 */
22726 aidc = vctxt->aidcs;
22727 while (aidc != NULL) {
22728 if (aidc->def == refIdc)
22729 break;
22730 aidc = aidc->next;
22731 }
22732 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022733 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022734 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022735 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022736 return (-1);
22737 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022738 if ((aidc->keyrefDepth == -1) ||
22739 (vctxt->depth < aidc->keyrefDepth))
22740 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022741 }
22742 }
22743 /*
22744 * Lookup the augmented IDC item for the IDC definition.
22745 */
22746 aidc = vctxt->aidcs;
22747 while (aidc != NULL) {
22748 if (aidc->def == idc)
22749 break;
22750 aidc = aidc->next;
22751 }
22752 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022753 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22754 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022755 return (-1);
22756 }
22757 /*
22758 * Create an IDC matcher for every IDC definition.
22759 */
22760 matcher = (xmlSchemaIDCMatcherPtr)
22761 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22762 if (matcher == NULL) {
22763 xmlSchemaVErrMemory(vctxt,
22764 "allocating an IDC matcher", NULL);
22765 return (-1);
22766 }
22767 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22768 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022769 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022770 else
22771 last->next = matcher;
22772 last = matcher;
22773
22774 matcher->type = IDC_MATCHER;
22775 matcher->depth = vctxt->depth;
22776 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022777 matcher->idcType = aidc->def->type;
22778#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022779 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22780#endif
22781 /*
22782 * Init the automaton state object.
22783 */
22784 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022785 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022786 return (-1);
22787
22788 idc = idc->next;
22789 } while (idc != NULL);
22790 return (0);
22791}
22792
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022793static int
22794xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22795 xmlSchemaNodeInfoPtr ielem)
22796{
22797 xmlSchemaPSVIIDCBindingPtr bind;
22798 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22799 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22800 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22801
22802 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22803 /* vctxt->createIDCNodeTables */
22804 while (matcher != NULL) {
22805 /*
22806 * Skip keyref IDCs and empty IDC target-lists.
22807 */
22808 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22809 WXS_ILIST_IS_EMPTY(matcher->targets))
22810 {
22811 matcher = matcher->next;
22812 continue;
22813 }
22814 /*
22815 * If we _want_ the IDC node-table to be created in any case
22816 * then do so. Otherwise create them only if keyrefs need them.
22817 */
22818 if ((! vctxt->createIDCNodeTables) &&
22819 ((matcher->aidc->keyrefDepth == -1) ||
22820 (matcher->aidc->keyrefDepth > vctxt->depth)))
22821 {
22822 matcher = matcher->next;
22823 continue;
22824 }
22825 /*
22826 * Get/create the IDC binding on this element for the IDC definition.
22827 */
22828 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22829
22830 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22831 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22832 nbDupls = bind->dupls->nbItems;
22833 } else {
22834 dupls = NULL;
22835 nbDupls = 0;
22836 }
22837 if (bind->nodeTable != NULL) {
22838 nbNodeTable = bind->nbNodes;
22839 } else {
22840 nbNodeTable = 0;
22841 }
22842
22843 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22844 /*
22845 * Transfer all IDC target-nodes to the IDC node-table.
22846 */
22847 bind->nodeTable =
22848 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22849 bind->sizeNodes = matcher->targets->sizeItems;
22850 bind->nbNodes = matcher->targets->nbItems;
22851
22852 matcher->targets->items = NULL;
22853 matcher->targets->sizeItems = 0;
22854 matcher->targets->nbItems = 0;
22855 } else {
22856 /*
22857 * Compare the key-sequences and add to the IDC node-table.
22858 */
22859 nbTargets = matcher->targets->nbItems;
22860 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22861 nbFields = matcher->aidc->def->nbFields;
22862 i = 0;
22863 do {
22864 keys = targets[i]->keys;
22865 if (nbDupls) {
22866 /*
22867 * Search in already found duplicates first.
22868 */
22869 j = 0;
22870 do {
22871 if (nbFields == 1) {
22872 res = xmlSchemaAreValuesEqual(keys[0]->val,
22873 dupls[j]->keys[0]->val);
22874 if (res == -1)
22875 goto internal_error;
22876 if (res == 1) {
22877 /*
22878 * Equal key-sequence.
22879 */
22880 goto next_target;
22881 }
22882 } else {
22883 res = 0;
22884 ntkeys = dupls[j]->keys;
22885 for (k = 0; k < nbFields; k++) {
22886 res = xmlSchemaAreValuesEqual(keys[k]->val,
22887 ntkeys[k]->val);
22888 if (res == -1)
22889 goto internal_error;
22890 if (res == 0) {
22891 /*
22892 * One of the keys differs.
22893 */
22894 break;
22895 }
22896 }
22897 if (res == 1) {
22898 /*
22899 * Equal key-sequence found.
22900 */
22901 goto next_target;
22902 }
22903 }
22904 j++;
22905 } while (j < nbDupls);
22906 }
22907 if (nbNodeTable) {
22908 j = 0;
22909 do {
22910 if (nbFields == 1) {
22911 res = xmlSchemaAreValuesEqual(keys[0]->val,
22912 bind->nodeTable[j]->keys[0]->val);
22913 if (res == -1)
22914 goto internal_error;
22915 if (res == 0) {
22916 /*
22917 * The key-sequence differs.
22918 */
22919 goto next_node_table_entry;
22920 }
22921 } else {
22922 res = 0;
22923 ntkeys = bind->nodeTable[j]->keys;
22924 for (k = 0; k < nbFields; k++) {
22925 res = xmlSchemaAreValuesEqual(keys[k]->val,
22926 ntkeys[k]->val);
22927 if (res == -1)
22928 goto internal_error;
22929 if (res == 0) {
22930 /*
22931 * One of the keys differs.
22932 */
22933 goto next_node_table_entry;
22934 }
22935 }
22936 }
22937 /*
22938 * Add the duplicate to the list of duplicates.
22939 */
22940 if (bind->dupls == NULL) {
22941 bind->dupls = xmlSchemaItemListCreate();
22942 if (bind->dupls == NULL)
22943 goto internal_error;
22944 }
22945 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
22946 goto internal_error;
22947 /*
22948 * Remove the duplicate entry from the IDC node-table.
22949 */
22950 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
22951 bind->nbNodes--;
22952
22953 goto next_target;
22954
22955next_node_table_entry:
22956 j++;
22957 } while (j < nbNodeTable);
22958 }
22959 /*
22960 * If everything is fine, then add the IDC target-node to
22961 * the IDC node-table.
22962 */
22963 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
22964 goto internal_error;
22965
22966next_target:
22967 i++;
22968 } while (i < nbTargets);
22969 }
22970 matcher = matcher->next;
22971 }
22972 return(0);
22973
22974internal_error:
22975 return(-1);
22976}
22977
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022978/**
22979 * xmlSchemaBubbleIDCNodeTables:
22980 * @depth: the current tree depth
22981 *
22982 * Merges IDC bindings of an element at @depth into the corresponding IDC
22983 * bindings of its parent element. If a duplicate note-table entry is found,
22984 * both, the parent node-table entry and child entry are discarded from the
22985 * node-table of the parent.
22986 *
22987 * Returns 0 if OK and -1 on internal errors.
22988 */
22989static int
22990xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
22991{
22992 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022993 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
22994 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022995 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022996 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022997
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022998 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022999 if (bind == NULL) {
23000 /* Fine, no table, no bubbles. */
23001 return (0);
23002 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023003
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023004 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23005 /*
23006 * Walk all bindings; create new or add to existing bindings.
23007 * Remove duplicate key-sequences.
23008 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023009 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023010
23011 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23012 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023013 /*
23014 * Check if the key/unique IDC table needs to be bubbled.
23015 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023016 if (! vctxt->createIDCNodeTables) {
23017 aidc = vctxt->aidcs;
23018 do {
23019 if (aidc->def == bind->definition) {
23020 if ((aidc->keyrefDepth == -1) ||
23021 (aidc->keyrefDepth >= vctxt->depth)) {
23022 goto next_binding;
23023 }
23024 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023025 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023026 aidc = aidc->next;
23027 } while (aidc != NULL);
23028 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023029
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023030 if (parTable != NULL)
23031 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023032 /*
23033 * Search a matching parent binding for the
23034 * IDC definition.
23035 */
23036 while (parBind != NULL) {
23037 if (parBind->definition == bind->definition)
23038 break;
23039 parBind = parBind->next;
23040 }
23041
23042 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023043 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023044 * Compare every node-table entry of the child node,
23045 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023046 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023047 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023048
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023049 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23050 oldDupls = parBind->dupls->nbItems;
23051 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23052 } else {
23053 dupls = NULL;
23054 oldDupls = 0;
23055 }
23056
23057 parNodes = parBind->nodeTable;
23058 nbFields = bind->definition->nbFields;
23059
23060 for (i = 0; i < bind->nbNodes; i++) {
23061 node = bind->nodeTable[i];
23062 if (node == NULL)
23063 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023064 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023065 * ...with every key-sequence of the parent node, already
23066 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023067 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023068 if (oldDupls) {
23069 j = 0;
23070 while (j < oldDupls) {
23071 if (nbFields == 1) {
23072 ret = xmlSchemaAreValuesEqual(
23073 node->keys[0]->val,
23074 dupls[j]->keys[0]->val);
23075 if (ret == -1)
23076 goto internal_error;
23077 if (ret == 0) {
23078 j++;
23079 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023080 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023081 } else {
23082 parNode = dupls[j];
23083 for (k = 0; k < nbFields; k++) {
23084 ret = xmlSchemaAreValuesEqual(
23085 node->keys[k]->val,
23086 parNode->keys[k]->val);
23087 if (ret == -1)
23088 goto internal_error;
23089 if (ret == 0)
23090 break;
23091 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023092 }
23093 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023094 /* Duplicate found. */
23095 break;
23096 j++;
23097 }
23098 if (j != oldDupls) {
23099 /* Duplicate found. Skip this entry. */
23100 continue;
23101 }
23102 }
23103 /*
23104 * ... and with every key-sequence of the parent node.
23105 */
23106 if (oldNum) {
23107 j = 0;
23108 while (j < oldNum) {
23109 if (nbFields == 1) {
23110 ret = xmlSchemaAreValuesEqual(
23111 node->keys[0]->val,
23112 parNodes[j]->keys[0]->val);
23113 if (ret == -1)
23114 goto internal_error;
23115 if (ret == 0) {
23116 j++;
23117 continue;
23118 }
23119 } else {
23120 parNode = parNodes[j];
23121 for (k = 0; k < nbFields; k++) {
23122 ret = xmlSchemaAreValuesEqual(
23123 node->keys[k]->val,
23124 parNode->keys[k]->val);
23125 if (ret == -1)
23126 goto internal_error;
23127 if (ret == 0)
23128 break;
23129 }
23130 }
23131 if (ret == 1)
23132 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023133 break;
23134 j++;
23135 }
23136 if (j != oldNum) {
23137 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023138 * Handle duplicates. Move the duplicate in
23139 * the parent's node-table to the list of
23140 * duplicates.
23141 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023142 oldNum--;
23143 parBind->nbNodes--;
23144 /*
23145 * Move last old item to pos of duplicate.
23146 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023147 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023148
23149 if (parBind->nbNodes != oldNum) {
23150 /*
23151 * If new items exist, move last new item to
23152 * last of old items.
23153 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023154 parNodes[oldNum] =
23155 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023156 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023157 if (parBind->dupls == NULL) {
23158 parBind->dupls = xmlSchemaItemListCreate();
23159 if (parBind->dupls == NULL)
23160 goto internal_error;
23161 }
23162 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023163 } else {
23164 /*
23165 * Add the node-table entry (node and key-sequence) of
23166 * the child node to the node table of the parent node.
23167 */
23168 if (parBind->nodeTable == NULL) {
23169 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023170 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023171 if (parBind->nodeTable == NULL) {
23172 xmlSchemaVErrMemory(NULL,
23173 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023174 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023175 }
23176 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023177 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023178 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023179 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23180 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23181 sizeof(xmlSchemaPSVIIDCNodePtr));
23182 if (parBind->nodeTable == NULL) {
23183 xmlSchemaVErrMemory(NULL,
23184 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023185 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023186 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023187 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023188 /*
23189 * Append the new node-table entry to the 'new node-table
23190 * entries' section.
23191 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023192 parBind->nodeTable[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023193 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023194
23195 }
23196
23197 }
23198 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023199 /*
23200 * No binding for the IDC was found: create a new one and
23201 * copy all node-tables.
23202 */
23203 parBind = xmlSchemaIDCNewBinding(bind->definition);
23204 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023205 goto internal_error;
23206
23207 /*
23208 * TODO: Hmm, how to optimize the initial number of
23209 * allocated entries?
23210 */
23211 if (bind->nbNodes != 0) {
23212 /*
23213 * Add all IDC node-table entries.
23214 */
23215 if (! vctxt->psviExposeIDCNodeTables) {
23216 /*
23217 * Just move the entries.
23218 * NOTE: this is quite save here, since
23219 * all the keyref lookups have already been
23220 * performed.
23221 */
23222 parBind->nodeTable = bind->nodeTable;
23223 bind->nodeTable = NULL;
23224 parBind->sizeNodes = bind->sizeNodes;
23225 bind->sizeNodes = 0;
23226 parBind->nbNodes = bind->nbNodes;
23227 bind->nbNodes = 0;
23228 } else {
23229 /*
23230 * Copy the entries.
23231 */
23232 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23233 xmlMalloc(bind->nbNodes *
23234 sizeof(xmlSchemaPSVIIDCNodePtr));
23235 if (parBind->nodeTable == NULL) {
23236 xmlSchemaVErrMemory(NULL,
23237 "allocating an array of IDC node-table "
23238 "items", NULL);
23239 xmlSchemaIDCFreeBinding(parBind);
23240 goto internal_error;
23241 }
23242 parBind->sizeNodes = bind->nbNodes;
23243 parBind->nbNodes = bind->nbNodes;
23244 memcpy(parBind->nodeTable, bind->nodeTable,
23245 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23246 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023247 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023248 if (bind->dupls) {
23249 /*
23250 * Move the duplicates.
23251 */
23252 if (parBind->dupls != NULL)
23253 xmlSchemaItemListFree(parBind->dupls);
23254 parBind->dupls = bind->dupls;
23255 bind->dupls = NULL;
23256 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023257 if (*parTable == NULL)
23258 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023259 else {
23260 parBind->next = *parTable;
23261 *parTable = parBind;
23262 }
23263 }
23264
23265next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023266 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023267 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023268 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023269
23270internal_error:
23271 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023272}
23273
23274/**
23275 * xmlSchemaCheckCVCIDCKeyRef:
23276 * @vctxt: the WXS validation context
23277 * @elemDecl: the element declaration
23278 *
23279 * Check the cvc-idc-keyref constraints.
23280 */
23281static int
23282xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23283{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023284 xmlSchemaIDCMatcherPtr matcher;
23285 xmlSchemaPSVIIDCBindingPtr bind;
23286
23287 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023288 /*
23289 * Find a keyref.
23290 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023291 while (matcher != NULL) {
23292 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23293 matcher->targets &&
23294 matcher->targets->nbItems)
23295 {
23296 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023297 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023298 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023299
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023300 nbFields = matcher->aidc->def->nbFields;
23301
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023302 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023303 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023304 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023305 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023306 while (bind != NULL) {
23307 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023308 bind->definition)
23309 break;
23310 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023311 }
23312 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023313 /*
23314 * Search for a matching key-sequences.
23315 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023316 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023317 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023318 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023319 if (bind != NULL) {
23320 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023321 for (j = 0; j < bind->nbNodes; j++) {
23322 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023323 for (k = 0; k < nbFields; k++) {
23324 res = xmlSchemaAreValuesEqual(keys[k]->val,
23325 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023326 if (res == 0)
23327 break;
23328 else if (res == -1) {
23329 return (-1);
23330 }
23331 }
23332 if (res == 1) {
23333 /*
23334 * Match found.
23335 */
23336 break;
23337 }
23338 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023339 if ((res == 0) && hasDupls) {
23340 /*
23341 * Search in duplicates
23342 */
23343 for (j = 0; j < bind->dupls->nbItems; j++) {
23344 keys = ((xmlSchemaPSVIIDCNodePtr)
23345 bind->dupls->items[j])->keys;
23346 for (k = 0; k < nbFields; k++) {
23347 res = xmlSchemaAreValuesEqual(keys[k]->val,
23348 refKeys[k]->val);
23349 if (res == 0)
23350 break;
23351 else if (res == -1) {
23352 return (-1);
23353 }
23354 }
23355 if (res == 1) {
23356 /*
23357 * Match in duplicates found.
23358 */
23359 xmlChar *str = NULL, *strB = NULL;
23360 xmlSchemaKeyrefErr(vctxt,
23361 XML_SCHEMAV_CVC_IDC, refNode,
23362 (xmlSchemaTypePtr) matcher->aidc->def,
23363 "More than one match found for "
23364 "key-sequence %s of keyref '%s'",
23365 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23366 refNode->keys, nbFields),
23367 xmlSchemaGetComponentQName(&strB,
23368 matcher->aidc->def));
23369 FREE_AND_NULL(str);
23370 FREE_AND_NULL(strB);
23371 break;
23372 }
23373 }
23374 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023375 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023376
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023377 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023378 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023379 xmlSchemaKeyrefErr(vctxt,
23380 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023381 (xmlSchemaTypePtr) matcher->aidc->def,
23382 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023383 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023384 refNode->keys, nbFields),
23385 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023386 FREE_AND_NULL(str);
23387 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023388 }
23389 }
23390 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023391 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023392 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023393 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023394 return (0);
23395}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023396
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023397/************************************************************************
23398 * *
23399 * XML Reader validation code *
23400 * *
23401 ************************************************************************/
23402
23403static xmlSchemaAttrInfoPtr
23404xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023405{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023406 xmlSchemaAttrInfoPtr iattr;
23407 /*
23408 * Grow/create list of attribute infos.
23409 */
23410 if (vctxt->attrInfos == NULL) {
23411 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23412 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23413 vctxt->sizeAttrInfos = 1;
23414 if (vctxt->attrInfos == NULL) {
23415 xmlSchemaVErrMemory(vctxt,
23416 "allocating attribute info list", NULL);
23417 return (NULL);
23418 }
23419 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23420 vctxt->sizeAttrInfos++;
23421 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23422 xmlRealloc(vctxt->attrInfos,
23423 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23424 if (vctxt->attrInfos == NULL) {
23425 xmlSchemaVErrMemory(vctxt,
23426 "re-allocating attribute info list", NULL);
23427 return (NULL);
23428 }
23429 } else {
23430 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23431 if (iattr->localName != NULL) {
23432 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23433 "attr info not cleared");
23434 return (NULL);
23435 }
23436 iattr->nodeType = XML_ATTRIBUTE_NODE;
23437 return (iattr);
23438 }
23439 /*
23440 * Create an attribute info.
23441 */
23442 iattr = (xmlSchemaAttrInfoPtr)
23443 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23444 if (iattr == NULL) {
23445 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23446 return (NULL);
23447 }
23448 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23449 iattr->nodeType = XML_ATTRIBUTE_NODE;
23450 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23451
23452 return (iattr);
23453}
23454
23455static int
23456xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23457 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023458 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023459 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023460 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023461 int ownedNames,
23462 xmlChar *value,
23463 int ownedValue)
23464{
23465 xmlSchemaAttrInfoPtr attr;
23466
23467 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23468 if (attr == NULL) {
23469 VERROR_INT("xmlSchemaPushAttribute",
23470 "calling xmlSchemaGetFreshAttrInfo()");
23471 return (-1);
23472 }
23473 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023474 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023475 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23476 attr->localName = localName;
23477 attr->nsName = nsName;
23478 if (ownedNames)
23479 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23480 /*
23481 * Evaluate if it's an XSI attribute.
23482 */
23483 if (nsName != NULL) {
23484 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23485 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23486 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23487 }
23488 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23489 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23490 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23491 }
23492 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23493 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23494 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23495 }
23496 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23497 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23498 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23499 }
23500 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23501 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23502 }
23503 }
23504 attr->value = value;
23505 if (ownedValue)
23506 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23507 if (attr->metaType != 0)
23508 attr->state = XML_SCHEMAS_ATTR_META;
23509 return (0);
23510}
23511
23512static void
23513xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23514{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023515 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023516 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023517 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23518 FREE_AND_NULL(ielem->localName);
23519 FREE_AND_NULL(ielem->nsName);
23520 } else {
23521 ielem->localName = NULL;
23522 ielem->nsName = NULL;
23523 }
23524 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23525 FREE_AND_NULL(ielem->value);
23526 } else {
23527 ielem->value = NULL;
23528 }
23529 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023530 /*
23531 * PSVI TODO: Be careful not to free it when the value is
23532 * exposed via PSVI.
23533 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023534 xmlSchemaFreeValue(ielem->val);
23535 ielem->val = NULL;
23536 }
23537 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023538 /*
23539 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23540 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023541 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23542 ielem->idcMatchers = NULL;
23543 }
23544 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023545 /*
23546 * OPTIMIZE TODO: Use a pool of IDC tables??.
23547 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023548 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23549 ielem->idcTable = NULL;
23550 }
23551 if (ielem->regexCtxt != NULL) {
23552 xmlRegFreeExecCtxt(ielem->regexCtxt);
23553 ielem->regexCtxt = NULL;
23554 }
23555 if (ielem->nsBindings != NULL) {
23556 xmlFree((xmlChar **)ielem->nsBindings);
23557 ielem->nsBindings = NULL;
23558 ielem->nbNsBindings = 0;
23559 ielem->sizeNsBindings = 0;
23560 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023561}
23562
23563/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023564 * xmlSchemaGetFreshElemInfo:
23565 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023566 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023567 * Creates/reuses and initializes the element info item for
23568 * the currect tree depth.
23569 *
23570 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023571 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023572static xmlSchemaNodeInfoPtr
23573xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023574{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023575 xmlSchemaNodeInfoPtr info = NULL;
23576
23577 if (vctxt->depth > vctxt->sizeElemInfos) {
23578 VERROR_INT("xmlSchemaGetFreshElemInfo",
23579 "inconsistent depth encountered");
23580 return (NULL);
23581 }
23582 if (vctxt->elemInfos == NULL) {
23583 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23584 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23585 if (vctxt->elemInfos == NULL) {
23586 xmlSchemaVErrMemory(vctxt,
23587 "allocating the element info array", NULL);
23588 return (NULL);
23589 }
23590 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23591 vctxt->sizeElemInfos = 10;
23592 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23593 int i = vctxt->sizeElemInfos;
23594
23595 vctxt->sizeElemInfos *= 2;
23596 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23597 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23598 sizeof(xmlSchemaNodeInfoPtr));
23599 if (vctxt->elemInfos == NULL) {
23600 xmlSchemaVErrMemory(vctxt,
23601 "re-allocating the element info array", NULL);
23602 return (NULL);
23603 }
23604 /*
23605 * We need the new memory to be NULLed.
23606 * TODO: Use memset instead?
23607 */
23608 for (; i < vctxt->sizeElemInfos; i++)
23609 vctxt->elemInfos[i] = NULL;
23610 } else
23611 info = vctxt->elemInfos[vctxt->depth];
23612
23613 if (info == NULL) {
23614 info = (xmlSchemaNodeInfoPtr)
23615 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23616 if (info == NULL) {
23617 xmlSchemaVErrMemory(vctxt,
23618 "allocating an element info", NULL);
23619 return (NULL);
23620 }
23621 vctxt->elemInfos[vctxt->depth] = info;
23622 } else {
23623 if (info->localName != NULL) {
23624 VERROR_INT("xmlSchemaGetFreshElemInfo",
23625 "elem info has not been cleared");
23626 return (NULL);
23627 }
23628 }
23629 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23630 info->nodeType = XML_ELEMENT_NODE;
23631 info->depth = vctxt->depth;
23632
23633 return (info);
23634}
23635
23636#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23637#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23638#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23639
23640static int
23641xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23642 xmlNodePtr node,
23643 xmlSchemaTypePtr type,
23644 xmlSchemaValType valType,
23645 const xmlChar * value,
23646 xmlSchemaValPtr val,
23647 unsigned long length,
23648 int fireErrors)
23649{
23650 int ret, error = 0;
23651
23652 xmlSchemaTypePtr tmpType;
23653 xmlSchemaFacetLinkPtr facetLink;
23654 xmlSchemaFacetPtr facet;
23655 unsigned long len = 0;
23656 xmlSchemaWhitespaceValueType ws;
23657
23658 /*
23659 * In Libxml2, derived built-in types have currently no explicit facets.
23660 */
23661 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023662 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023663
23664 /*
23665 * NOTE: Do not jump away, if the facetSet of the given type is
23666 * empty: until now, "pattern" and "enumeration" facets of the
23667 * *base types* need to be checked as well.
23668 */
23669 if (type->facetSet == NULL)
23670 goto pattern_and_enum;
23671
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023672 if (! WXS_IS_ATOMIC(type)) {
23673 if (WXS_IS_LIST(type))
23674 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023675 else
23676 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023677 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023678 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023679 * Whitespace handling is only of importance for string-based
23680 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023681 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023682 tmpType = xmlSchemaGetPrimitiveType(type);
23683 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023684 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023685 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23686 } else
23687 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23688 /*
23689 * If the value was not computed (for string or
23690 * anySimpleType based types), then use the provided
23691 * type.
23692 */
23693 if (val == NULL)
23694 valType = valType;
23695 else
23696 valType = xmlSchemaGetValType(val);
23697
23698 ret = 0;
23699 for (facetLink = type->facetSet; facetLink != NULL;
23700 facetLink = facetLink->next) {
23701 /*
23702 * Skip the pattern "whiteSpace": it is used to
23703 * format the character content beforehand.
23704 */
23705 switch (facetLink->facet->type) {
23706 case XML_SCHEMA_FACET_WHITESPACE:
23707 case XML_SCHEMA_FACET_PATTERN:
23708 case XML_SCHEMA_FACET_ENUMERATION:
23709 continue;
23710 case XML_SCHEMA_FACET_LENGTH:
23711 case XML_SCHEMA_FACET_MINLENGTH:
23712 case XML_SCHEMA_FACET_MAXLENGTH:
23713 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23714 valType, value, val, &len, ws);
23715 break;
23716 default:
23717 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23718 valType, value, val, ws);
23719 break;
23720 }
23721 if (ret < 0) {
23722 AERROR_INT("xmlSchemaValidateFacets",
23723 "validating against a atomic type facet");
23724 return (-1);
23725 } else if (ret > 0) {
23726 if (fireErrors)
23727 xmlSchemaFacetErr(actxt, ret, node,
23728 value, len, type, facetLink->facet, NULL, NULL, NULL);
23729 else
23730 return (ret);
23731 if (error == 0)
23732 error = ret;
23733 }
23734 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023735 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023736
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023737WXS_IS_LIST:
23738 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023739 goto pattern_and_enum;
23740 /*
23741 * "length", "minLength" and "maxLength" of list types.
23742 */
23743 ret = 0;
23744 for (facetLink = type->facetSet; facetLink != NULL;
23745 facetLink = facetLink->next) {
23746
23747 switch (facetLink->facet->type) {
23748 case XML_SCHEMA_FACET_LENGTH:
23749 case XML_SCHEMA_FACET_MINLENGTH:
23750 case XML_SCHEMA_FACET_MAXLENGTH:
23751 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23752 value, length, NULL);
23753 break;
23754 default:
23755 continue;
23756 }
23757 if (ret < 0) {
23758 AERROR_INT("xmlSchemaValidateFacets",
23759 "validating against a list type facet");
23760 return (-1);
23761 } else if (ret > 0) {
23762 if (fireErrors)
23763 xmlSchemaFacetErr(actxt, ret, node,
23764 value, length, type, facetLink->facet, NULL, NULL, NULL);
23765 else
23766 return (ret);
23767 if (error == 0)
23768 error = ret;
23769 }
23770 ret = 0;
23771 }
23772
23773pattern_and_enum:
23774 if (error >= 0) {
23775 int found = 0;
23776 /*
23777 * Process enumerations. Facet values are in the value space
23778 * of the defining type's base type. This seems to be a bug in the
23779 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23780 * Only the first set of enumerations in the ancestor-or-self axis
23781 * is used for validation.
23782 */
23783 ret = 0;
23784 tmpType = type;
23785 do {
23786 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23787 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23788 continue;
23789 found = 1;
23790 ret = xmlSchemaAreValuesEqual(facet->val, val);
23791 if (ret == 1)
23792 break;
23793 else if (ret < 0) {
23794 AERROR_INT("xmlSchemaValidateFacets",
23795 "validating against an enumeration facet");
23796 return (-1);
23797 }
23798 }
23799 if (ret != 0)
23800 break;
23801 tmpType = tmpType->baseType;
23802 } while ((tmpType != NULL) &&
23803 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23804 if (found && (ret == 0)) {
23805 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23806 if (fireErrors) {
23807 xmlSchemaFacetErr(actxt, ret, node,
23808 value, 0, type, NULL, NULL, NULL, NULL);
23809 } else
23810 return (ret);
23811 if (error == 0)
23812 error = ret;
23813 }
23814 }
23815
23816 if (error >= 0) {
23817 int found;
23818 /*
23819 * Process patters. Pattern facets are ORed at type level
23820 * and ANDed if derived. Walk the base type axis.
23821 */
23822 tmpType = type;
23823 facet = NULL;
23824 do {
23825 found = 0;
23826 for (facetLink = tmpType->facetSet; facetLink != NULL;
23827 facetLink = facetLink->next) {
23828 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23829 continue;
23830 found = 1;
23831 /*
23832 * NOTE that for patterns, @value needs to be the
23833 * normalized vaule.
23834 */
23835 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23836 if (ret == 1)
23837 break;
23838 else if (ret < 0) {
23839 AERROR_INT("xmlSchemaValidateFacets",
23840 "validating against a pattern facet");
23841 return (-1);
23842 } else {
23843 /*
23844 * Save the last non-validating facet.
23845 */
23846 facet = facetLink->facet;
23847 }
23848 }
23849 if (found && (ret != 1)) {
23850 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23851 if (fireErrors) {
23852 xmlSchemaFacetErr(actxt, ret, node,
23853 value, 0, type, facet, NULL, NULL, NULL);
23854 } else
23855 return (ret);
23856 if (error == 0)
23857 error = ret;
23858 break;
23859 }
23860 tmpType = tmpType->baseType;
23861 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23862 }
23863
23864 return (error);
23865}
23866
23867static xmlChar *
23868xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23869 const xmlChar *value)
23870{
23871 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23872 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23873 return (xmlSchemaCollapseString(value));
23874 case XML_SCHEMA_WHITESPACE_REPLACE:
23875 return (xmlSchemaWhiteSpaceReplace(value));
23876 default:
23877 return (NULL);
23878 }
23879}
23880
23881static int
23882xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23883 const xmlChar *value,
23884 xmlSchemaValPtr *val,
23885 int valNeeded)
23886{
23887 int ret;
23888 const xmlChar *nsName;
23889 xmlChar *local, *prefix = NULL;
23890
23891 ret = xmlValidateQName(value, 1);
23892 if (ret != 0) {
23893 if (ret == -1) {
23894 VERROR_INT("xmlSchemaValidateQName",
23895 "calling xmlValidateQName()");
23896 return (-1);
23897 }
23898 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23899 }
23900 /*
23901 * NOTE: xmlSplitQName2 will always return a duplicated
23902 * strings.
23903 */
23904 local = xmlSplitQName2(value, &prefix);
23905 if (local == NULL)
23906 local = xmlStrdup(value);
23907 /*
23908 * OPTIMIZE TODO: Use flags for:
23909 * - is there any namespace binding?
23910 * - is there a default namespace?
23911 */
23912 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23913
23914 if (prefix != NULL) {
23915 xmlFree(prefix);
23916 /*
23917 * A namespace must be found if the prefix is
23918 * NOT NULL.
23919 */
23920 if (nsName == NULL) {
23921 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023922 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023923 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023924 "The QName value '%s' has no "
23925 "corresponding namespace declaration in "
23926 "scope", value, NULL);
23927 if (local != NULL)
23928 xmlFree(local);
23929 return (ret);
23930 }
23931 }
23932 if (valNeeded && val) {
23933 if (nsName != NULL)
23934 *val = xmlSchemaNewQNameValue(
23935 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
23936 else
23937 *val = xmlSchemaNewQNameValue(NULL,
23938 BAD_CAST local);
23939 } else
23940 xmlFree(local);
23941 return (0);
23942}
23943
23944/*
23945* cvc-simple-type
23946*/
23947static int
23948xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
23949 xmlNodePtr node,
23950 xmlSchemaTypePtr type,
23951 const xmlChar *value,
23952 xmlSchemaValPtr *retVal,
23953 int fireErrors,
23954 int normalize,
23955 int isNormalized)
23956{
23957 int ret = 0, valNeeded = (retVal) ? 1 : 0;
23958 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023959 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023960 xmlChar *normValue = NULL;
23961
23962#define NORMALIZE(atype) \
23963 if ((! isNormalized) && \
23964 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
23965 normValue = xmlSchemaNormalizeValue(atype, value); \
23966 if (normValue != NULL) \
23967 value = normValue; \
23968 isNormalized = 1; \
23969 }
23970
23971 if ((retVal != NULL) && (*retVal != NULL)) {
23972 xmlSchemaFreeValue(*retVal);
23973 *retVal = NULL;
23974 }
23975 /*
23976 * 3.14.4 Simple Type Definition Validation Rules
23977 * Validation Rule: String Valid
23978 */
23979 /*
23980 * 1 It is schema-valid with respect to that definition as defined
23981 * by Datatype Valid in [XML Schemas: Datatypes].
23982 */
23983 /*
23984 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
23985 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
23986 * the string must be a ·declared entity name·.
23987 */
23988 /*
23989 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
23990 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
23991 * then every whitespace-delimited substring of the string must be a ·declared
23992 * entity name·.
23993 */
23994 /*
23995 * 2.3 otherwise no further condition applies.
23996 */
23997 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
23998 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000023999 if (value == NULL)
24000 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024001 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024002 xmlSchemaTypePtr biType; /* The built-in type. */
24003 /*
24004 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24005 * a literal in the ·lexical space· of {base type definition}"
24006 */
24007 /*
24008 * Whitespace-normalize.
24009 */
24010 NORMALIZE(type);
24011 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24012 /*
24013 * Get the built-in type.
24014 */
24015 biType = type->baseType;
24016 while ((biType != NULL) &&
24017 (biType->type != XML_SCHEMA_TYPE_BASIC))
24018 biType = biType->baseType;
24019
24020 if (biType == NULL) {
24021 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24022 "could not get the built-in type");
24023 goto internal_error;
24024 }
24025 } else
24026 biType = type;
24027 /*
24028 * NOTATIONs need to be processed here, since they need
24029 * to lookup in the hashtable of NOTATION declarations of the schema.
24030 */
24031 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24032 switch (biType->builtInType) {
24033 case XML_SCHEMAS_NOTATION:
24034 ret = xmlSchemaValidateNotation(
24035 (xmlSchemaValidCtxtPtr) actxt,
24036 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24037 NULL, value, &val, valNeeded);
24038 break;
24039 case XML_SCHEMAS_QNAME:
24040 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24041 value, &val, valNeeded);
24042 break;
24043 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024044 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024045 if (valNeeded)
24046 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24047 value, &val, NULL);
24048 else
24049 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24050 value, NULL, NULL);
24051 break;
24052 }
24053 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24054 switch (biType->builtInType) {
24055 case XML_SCHEMAS_NOTATION:
24056 ret = xmlSchemaValidateNotation(NULL,
24057 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24058 value, &val, valNeeded);
24059 break;
24060 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024061 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024062 if (valNeeded)
24063 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24064 value, &val, node);
24065 else
24066 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24067 value, NULL, node);
24068 break;
24069 }
24070 } else {
24071 /*
24072 * Validation via a public API is not implemented yet.
24073 */
24074 TODO
24075 goto internal_error;
24076 }
24077 if (ret != 0) {
24078 if (ret < 0) {
24079 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24080 "validating against a built-in type");
24081 goto internal_error;
24082 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024083 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024084 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24085 else
24086 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24087 }
24088 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24089 /*
24090 * Check facets.
24091 */
24092 ret = xmlSchemaValidateFacets(actxt, node, type,
24093 (xmlSchemaValType) biType->builtInType, value, val,
24094 0, fireErrors);
24095 if (ret != 0) {
24096 if (ret < 0) {
24097 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24098 "validating facets of atomic simple type");
24099 goto internal_error;
24100 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024101 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024102 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24103 else
24104 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24105 }
24106 }
24107 if (fireErrors && (ret > 0))
24108 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024109 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024110
24111 xmlSchemaTypePtr itemType;
24112 const xmlChar *cur, *end;
24113 xmlChar *tmpValue = NULL;
24114 unsigned long len = 0;
24115 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24116 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24117 * of white space separated tokens, each of which ·match·es a literal
24118 * in the ·lexical space· of {item type definition}
24119 */
24120 /*
24121 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24122 * the list type has an enum or pattern facet.
24123 */
24124 NORMALIZE(type);
24125 /*
24126 * VAL TODO: Optimize validation of empty values.
24127 * VAL TODO: We do not have computed values for lists.
24128 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024129 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024130 cur = value;
24131 do {
24132 while (IS_BLANK_CH(*cur))
24133 cur++;
24134 end = cur;
24135 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24136 end++;
24137 if (end == cur)
24138 break;
24139 tmpValue = xmlStrndup(cur, end - cur);
24140 len++;
24141
24142 if (valNeeded)
24143 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24144 tmpValue, &curVal, fireErrors, 0, 1);
24145 else
24146 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24147 tmpValue, NULL, fireErrors, 0, 1);
24148 FREE_AND_NULL(tmpValue);
24149 if (curVal != NULL) {
24150 /*
24151 * Add to list of computed values.
24152 */
24153 if (val == NULL)
24154 val = curVal;
24155 else
24156 xmlSchemaValueAppend(prevVal, curVal);
24157 prevVal = curVal;
24158 curVal = NULL;
24159 }
24160 if (ret != 0) {
24161 if (ret < 0) {
24162 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24163 "validating an item of list simple type");
24164 goto internal_error;
24165 }
24166 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24167 break;
24168 }
24169 cur = end;
24170 } while (*cur != 0);
24171 FREE_AND_NULL(tmpValue);
24172 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24173 /*
24174 * Apply facets (pattern, enumeration).
24175 */
24176 ret = xmlSchemaValidateFacets(actxt, node, type,
24177 XML_SCHEMAS_UNKNOWN, value, val,
24178 len, fireErrors);
24179 if (ret != 0) {
24180 if (ret < 0) {
24181 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24182 "validating facets of list simple type");
24183 goto internal_error;
24184 }
24185 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24186 }
24187 }
24188 if (fireErrors && (ret > 0)) {
24189 /*
24190 * Report the normalized value.
24191 */
24192 normalize = 1;
24193 NORMALIZE(type);
24194 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24195 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024196 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024197 xmlSchemaTypeLinkPtr memberLink;
24198 /*
24199 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24200 * not apply directly; however, the normalization behavior of ·union·
24201 * types is controlled by the value of whiteSpace on that one of the
24202 * ·memberTypes· against which the ·union· is successfully validated.
24203 *
24204 * This means that the value is normalized by the first validating
24205 * member type, then the facets of the union type are applied. This
24206 * needs changing of the value!
24207 */
24208
24209 /*
24210 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24211 * literal in the ·lexical space· of at least one member of
24212 * {member type definitions}
24213 */
24214 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24215 if (memberLink == NULL) {
24216 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24217 "union simple type has no member types");
24218 goto internal_error;
24219 }
24220 /*
24221 * Always normalize union type values, since we currently
24222 * cannot store the whitespace information with the value
24223 * itself; otherwise a later value-comparison would be
24224 * not possible.
24225 */
24226 while (memberLink != NULL) {
24227 if (valNeeded)
24228 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24229 memberLink->type, value, &val, 0, 1, 0);
24230 else
24231 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24232 memberLink->type, value, NULL, 0, 1, 0);
24233 if (ret <= 0)
24234 break;
24235 memberLink = memberLink->next;
24236 }
24237 if (ret != 0) {
24238 if (ret < 0) {
24239 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24240 "validating members of union simple type");
24241 goto internal_error;
24242 }
24243 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24244 }
24245 /*
24246 * Apply facets (pattern, enumeration).
24247 */
24248 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24249 /*
24250 * The normalization behavior of ·union· types is controlled by
24251 * the value of whiteSpace on that one of the ·memberTypes·
24252 * against which the ·union· is successfully validated.
24253 */
24254 NORMALIZE(memberLink->type);
24255 ret = xmlSchemaValidateFacets(actxt, node, type,
24256 XML_SCHEMAS_UNKNOWN, value, val,
24257 0, fireErrors);
24258 if (ret != 0) {
24259 if (ret < 0) {
24260 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24261 "validating facets of union simple type");
24262 goto internal_error;
24263 }
24264 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24265 }
24266 }
24267 if (fireErrors && (ret > 0))
24268 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24269 }
24270
24271 if (normValue != NULL)
24272 xmlFree(normValue);
24273 if (ret == 0) {
24274 if (retVal != NULL)
24275 *retVal = val;
24276 else if (val != NULL)
24277 xmlSchemaFreeValue(val);
24278 } else if (val != NULL)
24279 xmlSchemaFreeValue(val);
24280 return (ret);
24281internal_error:
24282 if (normValue != NULL)
24283 xmlFree(normValue);
24284 if (val != NULL)
24285 xmlSchemaFreeValue(val);
24286 return (-1);
24287}
24288
24289static int
24290xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24291 const xmlChar *value,
24292 const xmlChar **nsName,
24293 const xmlChar **localName)
24294{
24295 int ret = 0;
24296
24297 if ((nsName == NULL) || (localName == NULL))
24298 return (-1);
24299 *nsName = NULL;
24300 *localName = NULL;
24301
24302 ret = xmlValidateQName(value, 1);
24303 if (ret == -1)
24304 return (-1);
24305 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024306 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024307 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24308 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24309 return (1);
24310 }
24311 {
24312 xmlChar *local = NULL;
24313 xmlChar *prefix;
24314
24315 /*
24316 * NOTE: xmlSplitQName2 will return a duplicated
24317 * string.
24318 */
24319 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024320 if (local == NULL)
24321 *localName = xmlDictLookup(vctxt->dict, value, -1);
24322 else {
24323 *localName = xmlDictLookup(vctxt->dict, local, -1);
24324 xmlFree(local);
24325 }
24326
24327 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24328
24329 if (prefix != NULL) {
24330 xmlFree(prefix);
24331 /*
24332 * A namespace must be found if the prefix is NOT NULL.
24333 */
24334 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024335 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024336 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024337 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024338 "The QName value '%s' has no "
24339 "corresponding namespace declaration in scope",
24340 value, NULL);
24341 return (2);
24342 }
24343 }
24344 }
24345 return (0);
24346}
24347
24348static int
24349xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24350 xmlSchemaAttrInfoPtr iattr,
24351 xmlSchemaTypePtr *localType,
24352 xmlSchemaElementPtr elemDecl)
24353{
24354 int ret = 0;
24355 /*
24356 * cvc-elt (3.3.4) : (4)
24357 * AND
24358 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24359 * (1.2.1.2.1) - (1.2.1.2.4)
24360 * Handle 'xsi:type'.
24361 */
24362 if (localType == NULL)
24363 return (-1);
24364 *localType = NULL;
24365 if (iattr == NULL)
24366 return (0);
24367 else {
24368 const xmlChar *nsName = NULL, *local = NULL;
24369 /*
24370 * TODO: We should report a *warning* that the type was overriden
24371 * by the instance.
24372 */
24373 ACTIVATE_ATTRIBUTE(iattr);
24374 /*
24375 * (cvc-elt) (3.3.4) : (4.1)
24376 * (cvc-assess-elt) (1.2.1.2.2)
24377 */
24378 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24379 &nsName, &local);
24380 if (ret != 0) {
24381 if (ret < 0) {
24382 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24383 "calling xmlSchemaQNameExpand() to validate the "
24384 "attribute 'xsi:type'");
24385 goto internal_error;
24386 }
24387 goto exit;
24388 }
24389 /*
24390 * (cvc-elt) (3.3.4) : (4.2)
24391 * (cvc-assess-elt) (1.2.1.2.3)
24392 */
24393 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24394 if (*localType == NULL) {
24395 xmlChar *str = NULL;
24396
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024397 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024398 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024399 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024400 "The QName value '%s' of the xsi:type attribute does not "
24401 "resolve to a type definition",
24402 xmlSchemaFormatQName(&str, nsName, local), NULL);
24403 FREE_AND_NULL(str);
24404 ret = vctxt->err;
24405 goto exit;
24406 }
24407 if (elemDecl != NULL) {
24408 int set = 0;
24409
24410 /*
24411 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24412 * "The ·local type definition· must be validly
24413 * derived from the {type definition} given the union of
24414 * the {disallowed substitutions} and the {type definition}'s
24415 * {prohibited substitutions}, as defined in
24416 * Type Derivation OK (Complex) (§3.4.6)
24417 * (if it is a complex type definition),
24418 * or given {disallowed substitutions} as defined in Type
24419 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24420 * definition)."
24421 *
24422 * {disallowed substitutions}: the "block" on the element decl.
24423 * {prohibited substitutions}: the "block" on the type def.
24424 */
24425 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24426 (elemDecl->subtypes->flags &
24427 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24428 set |= SUBSET_EXTENSION;
24429
24430 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24431 (elemDecl->subtypes->flags &
24432 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24433 set |= SUBSET_RESTRICTION;
24434
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024435 if ((vctxt->pctxt == NULL) &&
24436 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24437 return (-1);
24438 if (xmlSchemaCheckCOSDerivedOK(vctxt->pctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024439 elemDecl->subtypes, set) != 0) {
24440 xmlChar *str = NULL;
24441
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024442 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024443 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24444 "The type definition '%s', specified by xsi:type, is "
24445 "blocked or not validly derived from the type definition "
24446 "of the element declaration",
24447 xmlSchemaFormatQName(&str,
24448 (*localType)->targetNamespace,
24449 (*localType)->name),
24450 NULL);
24451 FREE_AND_NULL(str);
24452 ret = vctxt->err;
24453 *localType = NULL;
24454 }
24455 }
24456 }
24457exit:
24458 ACTIVATE_ELEM;
24459 return (ret);
24460internal_error:
24461 ACTIVATE_ELEM;
24462 return (-1);
24463}
24464
24465static int
24466xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24467{
24468 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024469 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024470
24471 /*
24472 * cvc-elt (3.3.4) : 1
24473 */
24474 if (elemDecl == NULL) {
24475 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24476 "No matching declaration available");
24477 return (vctxt->err);
24478 }
24479 /*
24480 * cvc-elt (3.3.4) : 2
24481 */
24482 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24483 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24484 "The element declaration is abstract");
24485 return (vctxt->err);
24486 }
24487 if (actualType == NULL) {
24488 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24489 "The type definition is absent");
24490 return (XML_SCHEMAV_CVC_TYPE_1);
24491 }
24492 if (vctxt->nbAttrInfos != 0) {
24493 int ret;
24494 xmlSchemaAttrInfoPtr iattr;
24495 /*
24496 * cvc-elt (3.3.4) : 3
24497 * Handle 'xsi:nil'.
24498 */
24499 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24500 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24501 if (iattr) {
24502 ACTIVATE_ATTRIBUTE(iattr);
24503 /*
24504 * Validate the value.
24505 */
24506 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024507 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024508 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24509 iattr->value, &(iattr->val), 1, 0, 0);
24510 ACTIVATE_ELEM;
24511 if (ret < 0) {
24512 VERROR_INT("xmlSchemaValidateElemDecl",
24513 "calling xmlSchemaVCheckCVCSimpleType() to "
24514 "validate the attribute 'xsi:nil'");
24515 return (-1);
24516 }
24517 if (ret == 0) {
24518 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24519 /*
24520 * cvc-elt (3.3.4) : 3.1
24521 */
24522 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24523 "The element is not 'nillable'");
24524 /* Does not return an error on purpose. */
24525 } else {
24526 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24527 /*
24528 * cvc-elt (3.3.4) : 3.2.2
24529 */
24530 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24531 (elemDecl->value != NULL)) {
24532 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24533 "The element cannot be 'nilled' because "
24534 "there is a fixed value constraint defined "
24535 "for it");
24536 /* Does not return an error on purpose. */
24537 } else
24538 vctxt->inode->flags |=
24539 XML_SCHEMA_ELEM_INFO_NILLED;
24540 }
24541 }
24542 }
24543 }
24544 /*
24545 * cvc-elt (3.3.4) : 4
24546 * Handle 'xsi:type'.
24547 */
24548 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24549 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24550 if (iattr) {
24551 xmlSchemaTypePtr localType = NULL;
24552
24553 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24554 elemDecl);
24555 if (ret != 0) {
24556 if (ret == -1) {
24557 VERROR_INT("xmlSchemaValidateElemDecl",
24558 "calling xmlSchemaProcessXSIType() to "
24559 "process the attribute 'xsi:type'");
24560 return (-1);
24561 }
24562 /* Does not return an error on purpose. */
24563 }
24564 if (localType != NULL) {
24565 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24566 actualType = localType;
24567 }
24568 }
24569 }
24570 /*
24571 * IDC: Register identity-constraint XPath matchers.
24572 */
24573 if ((elemDecl->idcs != NULL) &&
24574 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24575 return (-1);
24576 /*
24577 * No actual type definition.
24578 */
24579 if (actualType == NULL) {
24580 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24581 "The type definition is absent");
24582 return (XML_SCHEMAV_CVC_TYPE_1);
24583 }
24584 /*
24585 * Remember the actual type definition.
24586 */
24587 vctxt->inode->typeDef = actualType;
24588
24589 return (0);
24590}
24591
24592static int
24593xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24594{
24595 xmlSchemaAttrInfoPtr iattr;
24596 int ret = 0, i;
24597
24598 /*
24599 * SPEC cvc-type (3.1.1)
24600 * "The attributes of must be empty, excepting those whose namespace
24601 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24602 * whose local name is one of type, nil, schemaLocation or
24603 * noNamespaceSchemaLocation."
24604 */
24605 if (vctxt->nbAttrInfos == 0)
24606 return (0);
24607 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24608 iattr = vctxt->attrInfos[i];
24609 if (! iattr->metaType) {
24610 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024611 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024612 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24613 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24614 }
24615 }
24616 ACTIVATE_ELEM
24617 return (ret);
24618}
24619
24620/*
24621* Cleanup currently used attribute infos.
24622*/
24623static void
24624xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24625{
24626 int i;
24627 xmlSchemaAttrInfoPtr attr;
24628
24629 if (vctxt->nbAttrInfos == 0)
24630 return;
24631 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24632 attr = vctxt->attrInfos[i];
24633 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24634 if (attr->localName != NULL)
24635 xmlFree((xmlChar *) attr->localName);
24636 if (attr->nsName != NULL)
24637 xmlFree((xmlChar *) attr->nsName);
24638 }
24639 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24640 if (attr->value != NULL)
24641 xmlFree((xmlChar *) attr->value);
24642 }
24643 if (attr->val != NULL) {
24644 xmlSchemaFreeValue(attr->val);
24645 attr->val = NULL;
24646 }
24647 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24648 }
24649 vctxt->nbAttrInfos = 0;
24650}
24651
24652/*
24653* 3.4.4 Complex Type Definition Validation Rules
24654* Element Locally Valid (Complex Type) (cvc-complex-type)
24655* 3.2.4 Attribute Declaration Validation Rules
24656* Validation Rule: Attribute Locally Valid (cvc-attribute)
24657* Attribute Locally Valid (Use) (cvc-au)
24658*
24659* Only "assessed" attribute information items will be visible to
24660* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24661*/
24662static int
24663xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24664{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024665 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24666 xmlSchemaItemListPtr attrUseList;
24667 xmlSchemaAttributeUsePtr attrUse = NULL;
24668 xmlSchemaAttributePtr attrDecl = NULL;
24669 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24670 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024671 int xpathRes = 0, res, wildIDs = 0, fixed;
24672
24673 /*
24674 * SPEC (cvc-attribute)
24675 * (1) "The declaration must not be ·absent· (see Missing
24676 * Sub-components (§5.3) for how this can fail to be
24677 * the case)."
24678 * (2) "Its {type definition} must not be absent."
24679 *
24680 * NOTE (1) + (2): This is not handled here, since we currently do not
24681 * allow validation against schemas which have missing sub-components.
24682 *
24683 * SPEC (cvc-complex-type)
24684 * (3) "For each attribute information item in the element information
24685 * item's [attributes] excepting those whose [namespace name] is
24686 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24687 * [local name] is one of type, nil, schemaLocation or
24688 * noNamespaceSchemaLocation, the appropriate case among the following
24689 * must be true:
24690 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024691 */
24692 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24693 /*
24694 * @nbAttrs is the number of attributes present in the instance.
24695 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024696 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024697 if (attrUseList != NULL)
24698 nbUses = attrUseList->nbItems;
24699 else
24700 nbUses = 0;
24701 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024702 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024703 attrUse = attrUseList->items[i];
24704 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24705 for (j = 0; j < nbAttrs; j++) {
24706 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024707 /*
24708 * SPEC (cvc-complex-type) (3)
24709 * Skip meta attributes.
24710 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024711 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024712 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024713 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024714 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024715 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024716 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024717 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024718 continue;
24719 found = 1;
24720 /*
24721 * SPEC (cvc-complex-type)
24722 * (3.1) "If there is among the {attribute uses} an attribute
24723 * use with an {attribute declaration} whose {name} matches
24724 * the attribute information item's [local name] and whose
24725 * {target namespace} is identical to the attribute information
24726 * item's [namespace name] (where an ·absent· {target namespace}
24727 * is taken to be identical to a [namespace name] with no value),
24728 * then the attribute information must be ·valid· with respect
24729 * to that attribute use as per Attribute Locally Valid (Use)
24730 * (§3.5.4). In this case the {attribute declaration} of that
24731 * attribute use is the ·context-determined declaration· for the
24732 * attribute information item with respect to Schema-Validity
24733 * Assessment (Attribute) (§3.2.4) and
24734 * Assessment Outcome (Attribute) (§3.2.5).
24735 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024736 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24737 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024738 /*
24739 * Context-determined declaration.
24740 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024741 iattr->decl = attrDecl;
24742 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024743 break;
24744 }
24745
24746 if (found)
24747 continue;
24748
24749 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24750 /*
24751 * Handle non-existent, required attributes.
24752 *
24753 * SPEC (cvc-complex-type)
24754 * (4) "The {attribute declaration} of each attribute use in
24755 * the {attribute uses} whose {required} is true matches one
24756 * of the attribute information items in the element information
24757 * item's [attributes] as per clause 3.1 above."
24758 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024759 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24760 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024761 VERROR_INT(
24762 "xmlSchemaVAttributesComplex",
24763 "calling xmlSchemaGetFreshAttrInfo()");
24764 return (-1);
24765 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024766 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24767 tmpiattr->use = attrUse;
24768 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024769 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24770 ((attrUse->defValue != NULL) ||
24771 (attrDecl->defValue != NULL))) {
24772 /*
24773 * Handle non-existent, optional, default/fixed attributes.
24774 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024775 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24776 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024777 VERROR_INT(
24778 "xmlSchemaVAttributesComplex",
24779 "calling xmlSchemaGetFreshAttrInfo()");
24780 return (-1);
24781 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024782 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24783 tmpiattr->use = attrUse;
24784 tmpiattr->decl = attrDecl;
24785 tmpiattr->typeDef = attrDecl->subtypes;
24786 tmpiattr->localName = attrDecl->name;
24787 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024788 }
24789 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024790
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024791 if (vctxt->nbAttrInfos == 0)
24792 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024793 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024794 /*
24795 * Validate against the wildcard.
24796 */
24797 if (type->attributeWildcard != NULL) {
24798 /*
24799 * SPEC (cvc-complex-type)
24800 * (3.2.1) "There must be an {attribute wildcard}."
24801 */
24802 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024803 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024804 /*
24805 * SPEC (cvc-complex-type) (3)
24806 * Skip meta attributes.
24807 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024808 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024809 continue;
24810 /*
24811 * SPEC (cvc-complex-type)
24812 * (3.2.2) "The attribute information item must be ·valid· with
24813 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24814 *
24815 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24816 * "... its [namespace name] must be ·valid· with respect to
24817 * the wildcard constraint, as defined in Wildcard allows
24818 * Namespace Name (§3.10.4)."
24819 */
24820 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024821 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024822 /*
24823 * Handle processContents.
24824 *
24825 * SPEC (cvc-wildcard):
24826 * processContents | context-determined declaration:
24827 * "strict" "mustFind"
24828 * "lax" "none"
24829 * "skip" "skip"
24830 */
24831 if (type->attributeWildcard->processContents ==
24832 XML_SCHEMAS_ANY_SKIP) {
24833 /*
24834 * context-determined declaration = "skip"
24835 *
24836 * SPEC PSVI Assessment Outcome (Attribute)
24837 * [validity] = "notKnown"
24838 * [validation attempted] = "none"
24839 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024840 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024841 continue;
24842 }
24843 /*
24844 * Find an attribute declaration.
24845 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024846 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24847 iattr->localName, iattr->nsName);
24848 if (iattr->decl != NULL) {
24849 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024850 /*
24851 * SPEC (cvc-complex-type)
24852 * (5) "Let [Definition:] the wild IDs be the set of
24853 * all attribute information item to which clause 3.2
24854 * applied and whose ·validation· resulted in a
24855 * ·context-determined declaration· of mustFind or no
24856 * ·context-determined declaration· at all, and whose
24857 * [local name] and [namespace name] resolve (as
24858 * defined by QName resolution (Instance) (§3.15.4)) to
24859 * an attribute declaration whose {type definition} is
24860 * or is derived from ID. Then all of the following
24861 * must be true:"
24862 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024863 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024864 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024865 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024866 /*
24867 * SPEC (5.1) "There must be no more than one
24868 * item in ·wild IDs·."
24869 */
24870 if (wildIDs != 0) {
24871 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024872 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024873 TODO
24874 continue;
24875 }
24876 wildIDs++;
24877 /*
24878 * SPEC (cvc-complex-type)
24879 * (5.2) "If ·wild IDs· is non-empty, there must not
24880 * be any attribute uses among the {attribute uses}
24881 * whose {attribute declaration}'s {type definition}
24882 * is or is derived from ID."
24883 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024884 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024885 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024886 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024887 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024888 /* URGENT VAL TODO: implement */
24889 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024890 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024891 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024892 }
24893 }
24894 }
24895 } else if (type->attributeWildcard->processContents ==
24896 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024897 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024898 /*
24899 * SPEC PSVI Assessment Outcome (Attribute)
24900 * [validity] = "notKnown"
24901 * [validation attempted] = "none"
24902 */
24903 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024904 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024905 }
24906 }
24907 }
24908 }
24909
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024910 if (vctxt->nbAttrInfos == 0)
24911 return (0);
24912
24913 /*
24914 * Validate values, create default attributes, evaluate IDCs.
24915 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024916 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024917 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024918 /*
24919 * VAL TODO: Note that we won't try to resolve IDCs to
24920 * "lax" and "skip" validated attributes. Check what to
24921 * do in this case.
24922 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024923 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
24924 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024925 continue;
24926 /*
24927 * VAL TODO: What to do if the type definition is missing?
24928 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024929 if (iattr->typeDef == NULL) {
24930 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024931 continue;
24932 }
24933
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024934 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000024935 fixed = 0;
24936 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024937
24938 if (vctxt->xpathStates != NULL) {
24939 /*
24940 * Evaluate IDCs.
24941 */
24942 xpathRes = xmlSchemaXPathEvaluate(vctxt,
24943 XML_ATTRIBUTE_NODE);
24944 if (xpathRes == -1) {
24945 VERROR_INT("xmlSchemaVAttributesComplex",
24946 "calling xmlSchemaXPathEvaluate()");
24947 goto internal_error;
24948 }
24949 }
24950
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024951 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024952 /*
24953 * Default/fixed attributes.
24954 */
24955 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024956 if (iattr->use->defValue != NULL) {
24957 iattr->value = (xmlChar *) iattr->use->defValue;
24958 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024959 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024960 iattr->value = (xmlChar *) iattr->decl->defValue;
24961 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024962 }
24963 /*
24964 * IDCs will consume the precomputed default value,
24965 * so we need to clone it.
24966 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024967 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024968 VERROR_INT("xmlSchemaVAttributesComplex",
24969 "default/fixed value on an attribute use was "
24970 "not precomputed");
24971 goto internal_error;
24972 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024973 iattr->val = xmlSchemaCopyValue(iattr->val);
24974 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024975 VERROR_INT("xmlSchemaVAttributesComplex",
24976 "calling xmlSchemaCopyValue()");
24977 goto internal_error;
24978 }
24979 }
24980 /*
24981 * PSVI: Add the default attribute to the current element.
24982 * VAL TODO: Should we use the *normalized* value? This currently
24983 * uses the *initial* value.
24984 */
24985 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024986 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024987 xmlChar *normValue;
24988 const xmlChar *value;
24989
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024990 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024991 /*
24992 * Normalize the value.
24993 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024994 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
24995 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024996 if (normValue != NULL)
24997 value = BAD_CAST normValue;
24998
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024999 if (iattr->nsName == NULL) {
25000 if (xmlNewProp(iattr->node->parent,
25001 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025002 VERROR_INT("xmlSchemaVAttributesComplex",
25003 "callling xmlNewProp()");
25004 if (normValue != NULL)
25005 xmlFree(normValue);
25006 goto internal_error;
25007 }
25008 } else {
25009 xmlNsPtr ns;
25010
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025011 ns = xmlSearchNsByHref(iattr->node->doc,
25012 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025013 if (ns == NULL) {
25014 xmlChar prefix[12];
25015 int counter = 0;
25016
25017 /*
25018 * Create a namespace declaration on the validation
25019 * root node if no namespace declaration is in scope.
25020 */
25021 do {
25022 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025023 ns = xmlSearchNs(iattr->node->doc,
25024 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025025 if (counter > 1000) {
25026 VERROR_INT(
25027 "xmlSchemaVAttributesComplex",
25028 "could not compute a ns prefix for a "
25029 "default/fixed attribute");
25030 if (normValue != NULL)
25031 xmlFree(normValue);
25032 goto internal_error;
25033 }
25034 } while (ns != NULL);
25035 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025036 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025037 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025038 /*
25039 * TODO:
25040 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25041 * If we have QNames: do we need to ensure there's a
25042 * prefix defined for the QName?
25043 */
25044 xmlNewNsProp(iattr->node->parent, ns,
25045 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025046 }
25047 if (normValue != NULL)
25048 xmlFree(normValue);
25049 }
25050 /*
25051 * Go directly to IDC evaluation.
25052 */
25053 goto eval_idcs;
25054 }
25055 /*
25056 * Validate the value.
25057 */
25058 if (vctxt->value != NULL) {
25059 /*
25060 * Free last computed value; just for safety reasons.
25061 */
25062 xmlSchemaFreeValue(vctxt->value);
25063 vctxt->value = NULL;
25064 }
25065 /*
25066 * Note that the attribute *use* can be unavailable, if
25067 * the attribute was a wild attribute.
25068 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025069 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25070 ((iattr->use != NULL) &&
25071 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025072 fixed = 1;
25073 else
25074 fixed = 0;
25075 /*
25076 * SPEC (cvc-attribute)
25077 * (3) "The item's ·normalized value· must be locally ·valid·
25078 * with respect to that {type definition} as per
25079 * String Valid (§3.14.4)."
25080 *
25081 * VAL TODO: Do we already have the
25082 * "normalized attribute value" here?
25083 */
25084 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025085 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025086 /*
25087 * Request a computed value.
25088 */
25089 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025090 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025091 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025092 1, 1, 0);
25093 } else {
25094 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025095 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025096 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025097 1, 0, 0);
25098 }
25099
25100 if (res != 0) {
25101 if (res == -1) {
25102 VERROR_INT("xmlSchemaVAttributesComplex",
25103 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25104 goto internal_error;
25105 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025106 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025107 /*
25108 * SPEC PSVI Assessment Outcome (Attribute)
25109 * [validity] = "invalid"
25110 */
25111 goto eval_idcs;
25112 }
25113
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025114 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025115 /*
25116 * SPEC Attribute Locally Valid (Use) (cvc-au)
25117 * "For an attribute information item to be·valid·
25118 * with respect to an attribute use its *normalized*
25119 * value· must match the *canonical* lexical
25120 * representation of the attribute use's {value
25121 * constraint}value, if it is present and fixed."
25122 *
25123 * VAL TODO: The requirement for the *canonical* value
25124 * will be removed in XML Schema 1.1.
25125 */
25126 /*
25127 * SPEC Attribute Locally Valid (cvc-attribute)
25128 * (4) "The item's *actual* value· must match the *value* of
25129 * the {value constraint}, if it is present and fixed."
25130 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025131 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025132 /* VAL TODO: A value was not precomputed. */
25133 TODO
25134 goto eval_idcs;
25135 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025136 if ((iattr->use != NULL) &&
25137 (iattr->use->defValue != NULL)) {
25138 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025139 /* VAL TODO: A default value was not precomputed. */
25140 TODO
25141 goto eval_idcs;
25142 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025143 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025144 /*
25145 if (xmlSchemaCompareValuesWhtsp(attr->val,
25146 (xmlSchemaWhitespaceValueType) ws,
25147 attr->use->defVal,
25148 (xmlSchemaWhitespaceValueType) ws) != 0) {
25149 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025150 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25151 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025152 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025153 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025154 /* VAL TODO: A default value was not precomputed. */
25155 TODO
25156 goto eval_idcs;
25157 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025158 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025159 /*
25160 if (xmlSchemaCompareValuesWhtsp(attr->val,
25161 (xmlSchemaWhitespaceValueType) ws,
25162 attrDecl->defVal,
25163 (xmlSchemaWhitespaceValueType) ws) != 0) {
25164 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025165 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25166 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025167 }
25168 /*
25169 * [validity] = "valid"
25170 */
25171 }
25172eval_idcs:
25173 /*
25174 * Evaluate IDCs.
25175 */
25176 if (xpathRes) {
25177 if (xmlSchemaXPathProcessHistory(vctxt,
25178 vctxt->depth +1) == -1) {
25179 VERROR_INT("xmlSchemaVAttributesComplex",
25180 "calling xmlSchemaXPathEvaluate()");
25181 goto internal_error;
25182 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025183 } else if (vctxt->xpathStates != NULL)
25184 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025185 }
25186
25187 /*
25188 * Report errors.
25189 */
25190 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025191 iattr = vctxt->attrInfos[i];
25192 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25193 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25194 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25195 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025196 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025197 ACTIVATE_ATTRIBUTE(iattr);
25198 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025199 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25200 xmlChar *str = NULL;
25201 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025202 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025203 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25204 "The attribute '%s' is required but missing",
25205 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025206 iattr->decl->targetNamespace,
25207 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025208 NULL);
25209 FREE_AND_NULL(str)
25210 break;
25211 }
25212 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25213 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25214 "The type definition is absent");
25215 break;
25216 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025217 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025218 XML_SCHEMAV_CVC_AU, NULL, NULL,
25219 "The value '%s' does not match the fixed "
25220 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025221 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025222 break;
25223 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25224 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25225 "No matching global attribute declaration available, but "
25226 "demanded by the strict wildcard");
25227 break;
25228 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025229 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025230 break;
25231 /*
25232 * MAYBE VAL TODO: One might report different error messages
25233 * for the following errors.
25234 */
25235 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025236 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025237 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025238 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025239 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025240 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025241 }
25242 break;
25243 default:
25244 break;
25245 }
25246 }
25247
25248 ACTIVATE_ELEM;
25249 return (0);
25250internal_error:
25251 ACTIVATE_ELEM;
25252 return (-1);
25253}
25254
25255static int
25256xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25257 int *skip)
25258{
25259 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25260 /*
25261 * The namespace of the element was already identified to be
25262 * matching the wildcard.
25263 */
25264 if ((skip == NULL) || (wild == NULL) ||
25265 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25266 VERROR_INT("xmlSchemaValidateElemWildcard",
25267 "bad arguments");
25268 return (-1);
25269 }
25270 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025271 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25272 /*
25273 * URGENT VAL TODO: Either we need to position the stream to the
25274 * next sibling, or walk the whole subtree.
25275 */
25276 *skip = 1;
25277 return (0);
25278 }
25279 {
25280 xmlSchemaElementPtr decl = NULL;
25281
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025282 decl = xmlSchemaGetElem(vctxt->schema,
25283 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025284 if (decl != NULL) {
25285 vctxt->inode->decl = decl;
25286 return (0);
25287 }
25288 }
25289 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25290 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025291 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025292 "No matching global element declaration available, but "
25293 "demanded by the strict wildcard");
25294 return (vctxt->err);
25295 }
25296 if (vctxt->nbAttrInfos != 0) {
25297 xmlSchemaAttrInfoPtr iattr;
25298 /*
25299 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25300 * (1.2.1.2.1) - (1.2.1.2.3 )
25301 *
25302 * Use the xsi:type attribute for the type definition.
25303 */
25304 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25305 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25306 if (iattr != NULL) {
25307 if (xmlSchemaProcessXSIType(vctxt, iattr,
25308 &(vctxt->inode->typeDef), NULL) == -1) {
25309 VERROR_INT("xmlSchemaValidateElemWildcard",
25310 "calling xmlSchemaProcessXSIType() to "
25311 "process the attribute 'xsi:nil'");
25312 return (-1);
25313 }
25314 /*
25315 * Don't return an error on purpose.
25316 */
25317 return (0);
25318 }
25319 }
25320 /*
25321 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25322 *
25323 * Fallback to "anyType".
25324 */
25325 vctxt->inode->typeDef =
25326 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25327 return (0);
25328}
25329
25330/*
25331* xmlSchemaCheckCOSValidDefault:
25332*
25333* This will be called if: not nilled, no content and a default/fixed
25334* value is provided.
25335*/
25336
25337static int
25338xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25339 const xmlChar *value,
25340 xmlSchemaValPtr *val)
25341{
25342 int ret = 0;
25343 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25344
25345 /*
25346 * cos-valid-default:
25347 * Schema Component Constraint: Element Default Valid (Immediate)
25348 * For a string to be a valid default with respect to a type
25349 * definition the appropriate case among the following must be true:
25350 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025351 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025352 /*
25353 * Complex type.
25354 *
25355 * SPEC (2.1) "its {content type} must be a simple type definition
25356 * or mixed."
25357 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25358 * type}'s particle must be ·emptiable· as defined by
25359 * Particle Emptiable (§3.9.6)."
25360 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025361 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25362 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25363 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025364 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25365 /* NOTE that this covers (2.2.2) as well. */
25366 VERROR(ret, NULL,
25367 "For a string to be a valid default, the type definition "
25368 "must be a simple type or a complex type with simple content "
25369 "or mixed content and a particle emptiable");
25370 return(ret);
25371 }
25372 }
25373 /*
25374 * 1 If the type definition is a simple type definition, then the string
25375 * must be ·valid· with respect to that definition as defined by String
25376 * Valid (§3.14.4).
25377 *
25378 * AND
25379 *
25380 * 2.2.1 If the {content type} is a simple type definition, then the
25381 * string must be ·valid· with respect to that simple type definition
25382 * as defined by String Valid (§3.14.4).
25383 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025384 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025385
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025386 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025387 NULL, inode->typeDef, value, val, 1, 1, 0);
25388
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025389 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025390
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025391 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025392 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25393 }
25394 if (ret < 0) {
25395 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25396 "calling xmlSchemaVCheckCVCSimpleType()");
25397 }
25398 return (ret);
25399}
25400
25401static void
25402xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25403 const xmlChar * name ATTRIBUTE_UNUSED,
25404 xmlSchemaElementPtr item,
25405 xmlSchemaNodeInfoPtr inode)
25406{
25407 inode->decl = item;
25408#ifdef DEBUG_CONTENT
25409 {
25410 xmlChar *str = NULL;
25411
25412 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25413 xmlGenericError(xmlGenericErrorContext,
25414 "AUTOMATON callback for '%s' [declaration]\n",
25415 xmlSchemaFormatQName(&str,
25416 inode->localName, inode->nsName));
25417 } else {
25418 xmlGenericError(xmlGenericErrorContext,
25419 "AUTOMATON callback for '%s' [wildcard]\n",
25420 xmlSchemaFormatQName(&str,
25421 inode->localName, inode->nsName));
25422
25423 }
25424 FREE_AND_NULL(str)
25425 }
25426#endif
25427}
25428
25429static int
25430xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025431{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025432 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25433 if (vctxt->inode == NULL) {
25434 VERROR_INT("xmlSchemaValidatorPushElem",
25435 "calling xmlSchemaGetFreshElemInfo()");
25436 return (-1);
25437 }
25438 vctxt->nbAttrInfos = 0;
25439 return (0);
25440}
25441
25442static int
25443xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25444 xmlSchemaNodeInfoPtr inode,
25445 xmlSchemaTypePtr type,
25446 const xmlChar *value)
25447{
25448 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
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, &(inode->val), 1, 1, 0));
25452 else
25453 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025454 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025455 type, value, NULL, 1, 0, 0));
25456}
25457
25458
25459
25460/*
25461* Process END of element.
25462*/
25463static int
25464xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25465{
25466 int ret = 0;
25467 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25468
25469 if (vctxt->nbAttrInfos != 0)
25470 xmlSchemaClearAttrInfos(vctxt);
25471 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25472 /*
25473 * This element was not expected;
25474 * we will not validate child elements of broken parents.
25475 * Skip validation of all content of the parent.
25476 */
25477 vctxt->skipDepth = vctxt->depth -1;
25478 goto end_elem;
25479 }
25480 if ((inode->typeDef == NULL) ||
25481 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25482 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025483 * 1. the type definition might be missing if the element was
25484 * error prone
25485 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025486 */
25487 goto end_elem;
25488 }
25489 /*
25490 * Check the content model.
25491 */
25492 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25493 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25494
25495 /*
25496 * Workaround for "anyType".
25497 */
25498 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25499 goto character_content;
25500
25501 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25502 xmlChar *values[10];
25503 int terminal, nbval = 10, nbneg;
25504
25505 if (inode->regexCtxt == NULL) {
25506 /*
25507 * Create the regex context.
25508 */
25509 inode->regexCtxt =
25510 xmlRegNewExecCtxt(inode->typeDef->contModel,
25511 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25512 vctxt);
25513 if (inode->regexCtxt == NULL) {
25514 VERROR_INT("xmlSchemaValidatorPopElem",
25515 "failed to create a regex context");
25516 goto internal_error;
25517 }
25518#ifdef DEBUG_AUTOMATA
25519 xmlGenericError(xmlGenericErrorContext,
25520 "AUTOMATON create on '%s'\n", inode->localName);
25521#endif
25522 }
25523 /*
25524 * Get hold of the still expected content, since a further
25525 * call to xmlRegExecPushString() will loose this information.
25526 */
25527 xmlRegExecNextValues(inode->regexCtxt,
25528 &nbval, &nbneg, &values[0], &terminal);
25529 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25530 if (ret <= 0) {
25531 /*
25532 * Still missing something.
25533 */
25534 ret = 1;
25535 inode->flags |=
25536 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025537 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025538 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25539 "Missing child element(s)",
25540 nbval, nbneg, values);
25541#ifdef DEBUG_AUTOMATA
25542 xmlGenericError(xmlGenericErrorContext,
25543 "AUTOMATON missing ERROR on '%s'\n",
25544 inode->localName);
25545#endif
25546 } else {
25547 /*
25548 * Content model is satisfied.
25549 */
25550 ret = 0;
25551#ifdef DEBUG_AUTOMATA
25552 xmlGenericError(xmlGenericErrorContext,
25553 "AUTOMATON succeeded on '%s'\n",
25554 inode->localName);
25555#endif
25556 }
25557
25558 }
25559 }
25560 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25561 goto end_elem;
25562
25563character_content:
25564
25565 if (vctxt->value != NULL) {
25566 xmlSchemaFreeValue(vctxt->value);
25567 vctxt->value = NULL;
25568 }
25569 /*
25570 * Check character content.
25571 */
25572 if (inode->decl == NULL) {
25573 /*
25574 * Speedup if no declaration exists.
25575 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025576 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025577 ret = xmlSchemaVCheckINodeDataType(vctxt,
25578 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025579 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025580 ret = xmlSchemaVCheckINodeDataType(vctxt,
25581 inode, inode->typeDef->contentTypeDef,
25582 inode->value);
25583 }
25584 if (ret < 0) {
25585 VERROR_INT("xmlSchemaValidatorPopElem",
25586 "calling xmlSchemaVCheckCVCSimpleType()");
25587 goto internal_error;
25588 }
25589 goto end_elem;
25590 }
25591 /*
25592 * cvc-elt (3.3.4) : 5
25593 * The appropriate case among the following must be true:
25594 */
25595 /*
25596 * cvc-elt (3.3.4) : 5.1
25597 * If the declaration has a {value constraint},
25598 * the item has neither element nor character [children] and
25599 * clause 3.2 has not applied, then all of the following must be true:
25600 */
25601 if ((inode->decl->value != NULL) &&
25602 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25603 (! INODE_NILLED(inode))) {
25604 /*
25605 * cvc-elt (3.3.4) : 5.1.1
25606 * If the ·actual type definition· is a ·local type definition·
25607 * then the canonical lexical representation of the {value constraint}
25608 * value must be a valid default for the ·actual type definition· as
25609 * defined in Element Default Valid (Immediate) (§3.3.6).
25610 */
25611 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025612 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025613 * NOTE: Although the *canonical* value is stated, it is not
25614 * relevant if canonical or not. Additionally XML Schema 1.1
25615 * will removed this requirement as well.
25616 */
25617 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25618
25619 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25620 inode->decl->value, &(inode->val));
25621 if (ret != 0) {
25622 if (ret < 0) {
25623 VERROR_INT("xmlSchemaValidatorPopElem",
25624 "calling xmlSchemaCheckCOSValidDefault()");
25625 goto internal_error;
25626 }
25627 goto end_elem;
25628 }
25629 /*
25630 * Stop here, to avoid redundant validation of the value
25631 * (see following).
25632 */
25633 goto default_psvi;
25634 }
25635 /*
25636 * cvc-elt (3.3.4) : 5.1.2
25637 * The element information item with the canonical lexical
25638 * representation of the {value constraint} value used as its
25639 * ·normalized value· must be ·valid· with respect to the
25640 * ·actual type definition· as defined by Element Locally Valid (Type)
25641 * (§3.3.4).
25642 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025643 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025644 ret = xmlSchemaVCheckINodeDataType(vctxt,
25645 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025646 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025647 ret = xmlSchemaVCheckINodeDataType(vctxt,
25648 inode, inode->typeDef->contentTypeDef,
25649 inode->decl->value);
25650 }
25651 if (ret != 0) {
25652 if (ret < 0) {
25653 VERROR_INT("xmlSchemaValidatorPopElem",
25654 "calling xmlSchemaVCheckCVCSimpleType()");
25655 goto internal_error;
25656 }
25657 goto end_elem;
25658 }
25659
25660default_psvi:
25661 /*
25662 * PSVI: Create a text node on the instance element.
25663 */
25664 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25665 (inode->node != NULL)) {
25666 xmlNodePtr textChild;
25667 xmlChar *normValue;
25668 /*
25669 * VAL TODO: Normalize the value.
25670 */
25671 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25672 inode->decl->value);
25673 if (normValue != NULL) {
25674 textChild = xmlNewText(BAD_CAST normValue);
25675 xmlFree(normValue);
25676 } else
25677 textChild = xmlNewText(inode->decl->value);
25678 if (textChild == NULL) {
25679 VERROR_INT("xmlSchemaValidatorPopElem",
25680 "calling xmlNewText()");
25681 goto internal_error;
25682 } else
25683 xmlAddChild(inode->node, textChild);
25684 }
25685
25686 } else if (! INODE_NILLED(inode)) {
25687 /*
25688 * 5.2.1 The element information item must be ·valid· with respect
25689 * to the ·actual type definition· as defined by Element Locally
25690 * Valid (Type) (§3.3.4).
25691 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025692 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025693 /*
25694 * SPEC (cvc-type) (3.1)
25695 * "If the type definition is a simple type definition, ..."
25696 * (3.1.3) "If clause 3.2 of Element Locally Valid
25697 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25698 * must be ·valid· with respect to the type definition as defined
25699 * by String Valid (§3.14.4).
25700 */
25701 ret = xmlSchemaVCheckINodeDataType(vctxt,
25702 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025703 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025704 /*
25705 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25706 * definition, then the element information item must be
25707 * ·valid· with respect to the type definition as per
25708 * Element Locally Valid (Complex Type) (§3.4.4);"
25709 *
25710 * SPEC (cvc-complex-type) (2.2)
25711 * "If the {content type} is a simple type definition, ...
25712 * the ·normalized value· of the element information item is
25713 * ·valid· with respect to that simple type definition as
25714 * defined by String Valid (§3.14.4)."
25715 */
25716 ret = xmlSchemaVCheckINodeDataType(vctxt,
25717 inode, inode->typeDef->contentTypeDef, inode->value);
25718 }
25719 if (ret != 0) {
25720 if (ret < 0) {
25721 VERROR_INT("xmlSchemaValidatorPopElem",
25722 "calling xmlSchemaVCheckCVCSimpleType()");
25723 goto internal_error;
25724 }
25725 goto end_elem;
25726 }
25727 /*
25728 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25729 * not applied, all of the following must be true:
25730 */
25731 if ((inode->decl->value != NULL) &&
25732 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25733
25734 /*
25735 * TODO: We will need a computed value, when comparison is
25736 * done on computed values.
25737 */
25738 /*
25739 * 5.2.2.1 The element information item must have no element
25740 * information item [children].
25741 */
25742 if (inode->flags &
25743 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25744 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25745 VERROR(ret, NULL,
25746 "The content must not containt element nodes since "
25747 "there is a fixed value constraint");
25748 goto end_elem;
25749 } else {
25750 /*
25751 * 5.2.2.2 The appropriate case among the following must
25752 * be true:
25753 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025754 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025755 /*
25756 * 5.2.2.2.1 If the {content type} of the ·actual type
25757 * definition· is mixed, then the *initial value* of the
25758 * item must match the canonical lexical representation
25759 * of the {value constraint} value.
25760 *
25761 * ... the *initial value* of an element information
25762 * item is the string composed of, in order, the
25763 * [character code] of each character information item in
25764 * the [children] of that element information item.
25765 */
25766 if (! xmlStrEqual(inode->value, inode->decl->value)){
25767 /*
25768 * VAL TODO: Report invalid & expected values as well.
25769 * VAL TODO: Implement the canonical stuff.
25770 */
25771 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025772 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025773 ret, NULL, NULL,
25774 "The initial value '%s' does not match the fixed "
25775 "value constraint '%s'",
25776 inode->value, inode->decl->value);
25777 goto end_elem;
25778 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025779 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025780 /*
25781 * 5.2.2.2.2 If the {content type} of the ·actual type
25782 * definition· is a simple type definition, then the
25783 * *actual value* of the item must match the canonical
25784 * lexical representation of the {value constraint} value.
25785 */
25786 /*
25787 * VAL TODO: *actual value* is the normalized value, impl.
25788 * this.
25789 * VAL TODO: Report invalid & expected values as well.
25790 * VAL TODO: Implement a comparison with the computed values.
25791 */
25792 if (! xmlStrEqual(inode->value,
25793 inode->decl->value)) {
25794 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025795 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025796 ret, NULL, NULL,
25797 "The actual value '%s' does not match the fixed "
25798 "value constraint '%s'",
25799 inode->value,
25800 inode->decl->value);
25801 goto end_elem;
25802 }
25803 }
25804 }
25805 }
25806 }
25807
25808end_elem:
25809 if (vctxt->depth < 0) {
25810 /* TODO: raise error? */
25811 return (0);
25812 }
25813 if (vctxt->depth == vctxt->skipDepth)
25814 vctxt->skipDepth = -1;
25815 /*
25816 * Evaluate the history of XPath state objects.
25817 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025818 if (inode->appliedXPath &&
25819 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025820 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025821 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025822 * MAYBE TODO:
25823 * SPEC (6) "The element information item must be ·valid· with
25824 * respect to each of the {identity-constraint definitions} as per
25825 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025826 */
25827 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025828 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25829 * need to be built in any case.
25830 * We will currently build IDC node-tables and bubble them only if
25831 * keyrefs do exist.
25832 */
25833
25834 /*
25835 * Add the current IDC target-nodes to the IDC node-tables.
25836 */
25837 if ((inode->idcMatchers != NULL) &&
25838 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25839 {
25840 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25841 goto internal_error;
25842 }
25843 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025844 * Validate IDC keyrefs.
25845 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025846 if (vctxt->inode->hasKeyrefs)
25847 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25848 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025849 /*
25850 * Merge/free the IDC table.
25851 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025852 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025853#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025854 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025855 inode->nsName,
25856 inode->localName,
25857 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025858#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025859 if ((vctxt->depth > 0) &&
25860 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25861 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025862 /*
25863 * Merge the IDC node table with the table of the parent node.
25864 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025865 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25866 goto internal_error;
25867 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025868 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025869 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025870 * Clear the current ielem.
25871 * VAL TODO: Don't free the PSVI IDC tables if they are
25872 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025873 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025874 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025875 /*
25876 * Skip further processing if we are on the validation root.
25877 */
25878 if (vctxt->depth == 0) {
25879 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025880 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025881 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025882 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025883 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025884 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025885 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025886 if (vctxt->aidcs != NULL) {
25887 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25888 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025889 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025890 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025891 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025892 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025893 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025894 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025895 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025896 }
25897 aidc = aidc->next;
25898 } while (aidc != NULL);
25899 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025900 vctxt->depth--;
25901 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025902 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025903 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025904 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25905 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025906 return (ret);
25907
25908internal_error:
25909 vctxt->err = -1;
25910 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000025911}
25912
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025913/*
25914* 3.4.4 Complex Type Definition Validation Rules
25915* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
25916*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000025917static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025918xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000025919{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025920 xmlSchemaNodeInfoPtr pielem;
25921 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000025922 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000025923
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025924 if (vctxt->depth <= 0) {
25925 VERROR_INT("xmlSchemaValidateChildElem",
25926 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025927 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025928 }
25929 pielem = vctxt->elemInfos[vctxt->depth -1];
25930 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
25931 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025932 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025933 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025934 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025935 if (INODE_NILLED(pielem)) {
25936 /*
25937 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
25938 */
25939 ACTIVATE_PARENT_ELEM;
25940 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
25941 VERROR(ret, NULL,
25942 "Neither character nor element content is allowed, "
25943 "because the element was 'nilled'");
25944 ACTIVATE_ELEM;
25945 goto unexpected_elem;
25946 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025947
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025948 ptype = pielem->typeDef;
25949
25950 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
25951 /*
25952 * Workaround for "anyType": we have currently no content model
25953 * assigned for "anyType", so handle it explicitely.
25954 * "anyType" has an unbounded, lax "any" wildcard.
25955 */
25956 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
25957 vctxt->inode->localName,
25958 vctxt->inode->nsName);
25959
25960 if (vctxt->inode->decl == NULL) {
25961 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025962 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025963 * Process "xsi:type".
25964 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025965 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025966 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25967 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25968 if (iattr != NULL) {
25969 ret = xmlSchemaProcessXSIType(vctxt, iattr,
25970 &(vctxt->inode->typeDef), NULL);
25971 if (ret != 0) {
25972 if (ret == -1) {
25973 VERROR_INT("xmlSchemaValidateChildElem",
25974 "calling xmlSchemaProcessXSIType() to "
25975 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025976 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000025977 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025978 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000025979 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025980 } else {
25981 /*
25982 * Fallback to "anyType".
25983 *
25984 * SPEC (cvc-assess-elt)
25985 * "If the item cannot be ·strictly assessed·, [...]
25986 * an element information item's schema validity may be laxly
25987 * assessed if its ·context-determined declaration· is not
25988 * skip by ·validating· with respect to the ·ur-type
25989 * definition· as per Element Locally Valid (Type) (§3.3.4)."
25990 */
25991 vctxt->inode->typeDef =
25992 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025993 }
25994 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025995 return (0);
25996 }
25997
25998 switch (ptype->contentType) {
25999 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026000 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026001 * SPEC (2.1) "If the {content type} is empty, then the
26002 * element information item has no character or element
26003 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026004 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026005 ACTIVATE_PARENT_ELEM
26006 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26007 VERROR(ret, NULL,
26008 "Element content is not allowed, "
26009 "because the content type is empty");
26010 ACTIVATE_ELEM
26011 goto unexpected_elem;
26012 break;
26013
26014 case XML_SCHEMA_CONTENT_MIXED:
26015 case XML_SCHEMA_CONTENT_ELEMENTS: {
26016 xmlRegExecCtxtPtr regexCtxt;
26017 xmlChar *values[10];
26018 int terminal, nbval = 10, nbneg;
26019
26020 /* VAL TODO: Optimized "anyType" validation.*/
26021
26022 if (ptype->contModel == NULL) {
26023 VERROR_INT("xmlSchemaValidateChildElem",
26024 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026025 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026026 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026027 /*
26028 * Safety belf for evaluation if the cont. model was already
26029 * examined to be invalid.
26030 */
26031 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26032 VERROR_INT("xmlSchemaValidateChildElem",
26033 "validating elem, but elem content is already invalid");
26034 return (-1);
26035 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026036
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026037 regexCtxt = pielem->regexCtxt;
26038 if (regexCtxt == NULL) {
26039 /*
26040 * Create the regex context.
26041 */
26042 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26043 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26044 vctxt);
26045 if (regexCtxt == NULL) {
26046 VERROR_INT("xmlSchemaValidateChildElem",
26047 "failed to create a regex context");
26048 return (-1);
26049 }
26050 pielem->regexCtxt = regexCtxt;
26051#ifdef DEBUG_AUTOMATA
26052 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26053 pielem->localName);
26054#endif
26055 }
26056
26057 /*
26058 * SPEC (2.4) "If the {content type} is element-only or mixed,
26059 * then the sequence of the element information item's
26060 * element information item [children], if any, taken in
26061 * order, is ·valid· with respect to the {content type}'s
26062 * particle, as defined in Element Sequence Locally Valid
26063 * (Particle) (§3.9.4)."
26064 */
26065 ret = xmlRegExecPushString2(regexCtxt,
26066 vctxt->inode->localName,
26067 vctxt->inode->nsName,
26068 vctxt->inode);
26069#ifdef DEBUG_AUTOMATA
26070 if (ret < 0)
26071 xmlGenericError(xmlGenericErrorContext,
26072 "AUTOMATON push ERROR for '%s' on '%s'\n",
26073 vctxt->inode->localName, pielem->localName);
26074 else
26075 xmlGenericError(xmlGenericErrorContext,
26076 "AUTOMATON push OK for '%s' on '%s'\n",
26077 vctxt->inode->localName, pielem->localName);
26078#endif
26079 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26080 VERROR_INT("xmlSchemaValidateChildElem",
26081 "calling xmlRegExecPushString2()");
26082 return (-1);
26083 }
26084 if (ret < 0) {
26085 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26086 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026087 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026088 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26089 "This element is not expected",
26090 nbval, nbneg, values);
26091 ret = vctxt->err;
26092 goto unexpected_elem;
26093 } else
26094 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026095 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026096 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026097 case XML_SCHEMA_CONTENT_SIMPLE:
26098 case XML_SCHEMA_CONTENT_BASIC:
26099 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026100 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026101 /*
26102 * SPEC (cvc-complex-type) (2.2)
26103 * "If the {content type} is a simple type definition, then
26104 * the element information item has no element information
26105 * item [children], ..."
26106 */
26107 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26108 VERROR(ret, NULL, "Element content is not allowed, "
26109 "because the content type is a simple type definition");
26110 } else {
26111 /*
26112 * SPEC (cvc-type) (3.1.2) "The element information item must
26113 * have no element information item [children]."
26114 */
26115 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26116 VERROR(ret, NULL, "Element content is not allowed, "
26117 "because the type definition is simple");
26118 }
26119 ACTIVATE_ELEM
26120 ret = vctxt->err;
26121 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026122 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026123
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026124 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026125 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026126 }
26127 return (ret);
26128unexpected_elem:
26129 /*
26130 * Pop this element and set the skipDepth to skip
26131 * all further content of the parent element.
26132 */
26133 vctxt->skipDepth = vctxt->depth;
26134 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26135 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26136 return (ret);
26137}
26138
26139#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26140#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26141#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26142
26143static int
26144xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26145 int nodeType, const xmlChar *value, int len,
26146 int mode, int *consumed)
26147{
26148 /*
26149 * Unfortunately we have to duplicate the text sometimes.
26150 * OPTIMIZE: Maybe we could skip it, if:
26151 * 1. content type is simple
26152 * 2. whitespace is "collapse"
26153 * 3. it consists of whitespace only
26154 *
26155 * Process character content.
26156 */
26157 if (consumed != NULL)
26158 *consumed = 0;
26159 if (INODE_NILLED(vctxt->inode)) {
26160 /*
26161 * SPEC cvc-elt (3.3.4 - 3.2.1)
26162 * "The element information item must have no character or
26163 * element information item [children]."
26164 */
26165 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26166 "Neither character nor element content is allowed "
26167 "because the element is 'nilled'");
26168 return (vctxt->err);
26169 }
26170 /*
26171 * SPEC (2.1) "If the {content type} is empty, then the
26172 * element information item has no character or element
26173 * information item [children]."
26174 */
26175 if (vctxt->inode->typeDef->contentType ==
26176 XML_SCHEMA_CONTENT_EMPTY) {
26177 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26178 "Character content is not allowed, "
26179 "because the content type is empty");
26180 return (vctxt->err);
26181 }
26182
26183 if (vctxt->inode->typeDef->contentType ==
26184 XML_SCHEMA_CONTENT_ELEMENTS) {
26185 if ((nodeType != XML_TEXT_NODE) ||
26186 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26187 /*
26188 * SPEC cvc-complex-type (2.3)
26189 * "If the {content type} is element-only, then the
26190 * element information item has no character information
26191 * item [children] other than those whose [character
26192 * code] is defined as a white space in [XML 1.0 (Second
26193 * Edition)]."
26194 */
26195 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26196 "Character content other than whitespace is not allowed "
26197 "because the content type is 'element-only'");
26198 return (vctxt->err);
26199 }
26200 return (0);
26201 }
26202
26203 if ((value == NULL) || (value[0] == 0))
26204 return (0);
26205 /*
26206 * Save the value.
26207 * NOTE that even if the content type is *mixed*, we need the
26208 * *initial value* for default/fixed value constraints.
26209 */
26210 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26211 ((vctxt->inode->decl == NULL) ||
26212 (vctxt->inode->decl->value == NULL)))
26213 return (0);
26214
26215 if (vctxt->inode->value == NULL) {
26216 /*
26217 * Set the value.
26218 */
26219 switch (mode) {
26220 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26221 /*
26222 * When working on a tree.
26223 */
26224 vctxt->inode->value = value;
26225 break;
26226 case XML_SCHEMA_PUSH_TEXT_CREATED:
26227 /*
26228 * When working with the reader.
26229 * The value will be freed by the element info.
26230 */
26231 vctxt->inode->value = value;
26232 if (consumed != NULL)
26233 *consumed = 1;
26234 vctxt->inode->flags |=
26235 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26236 break;
26237 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26238 /*
26239 * When working with SAX.
26240 * The value will be freed by the element info.
26241 */
26242 if (len != -1)
26243 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26244 else
26245 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26246 vctxt->inode->flags |=
26247 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26248 break;
26249 default:
26250 break;
26251 }
26252 } else {
26253 /*
26254 * Concat the value.
26255 */
26256 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026257 vctxt->inode->value = BAD_CAST xmlStrncat(
26258 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026259 } else {
26260 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026261 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026262 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26263 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026264 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026265
26266 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026267}
26268
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026269static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026270xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026271{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026272 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026273
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026274 if ((vctxt->skipDepth != -1) &&
26275 (vctxt->depth >= vctxt->skipDepth)) {
26276 VERROR_INT("xmlSchemaValidateElem",
26277 "in skip-state");
26278 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026279 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026280 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026281 /*
26282 * URGENT TODO: Better to fully stop validation
26283 * if there was an error during dynamic schema construction.
26284 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026285 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26286 goto internal_error;
26287 }
26288 if (vctxt->depth > 0) {
26289 /*
26290 * Validate this element against the content model
26291 * of the parent.
26292 */
26293 ret = xmlSchemaValidateChildElem(vctxt);
26294 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026295 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026296 VERROR_INT("xmlSchemaValidateElem",
26297 "calling xmlSchemaStreamValidateChildElement()");
26298 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026299 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026300 goto exit;
26301 }
26302 if (vctxt->depth == vctxt->skipDepth)
26303 goto exit;
26304 if ((vctxt->inode->decl == NULL) &&
26305 (vctxt->inode->typeDef == NULL)) {
26306 VERROR_INT("xmlSchemaValidateElem",
26307 "the child element was valid but neither the "
26308 "declaration nor the type was set");
26309 goto internal_error;
26310 }
26311 } else {
26312 /*
26313 * Get the declaration of the validation root.
26314 */
26315 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26316 vctxt->inode->localName,
26317 vctxt->inode->nsName);
26318 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026319 ret = XML_SCHEMAV_CVC_ELT_1;
26320 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026321 "No matching global declaration available "
26322 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026323 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026324 }
26325 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026326
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026327 if (vctxt->inode->decl == NULL)
26328 goto type_validation;
26329
26330 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26331 int skip;
26332 /*
26333 * Wildcards.
26334 */
26335 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26336 if (ret != 0) {
26337 if (ret < 0) {
26338 VERROR_INT("xmlSchemaValidateElem",
26339 "calling xmlSchemaValidateElemWildcard()");
26340 goto internal_error;
26341 }
26342 goto exit;
26343 }
26344 if (skip) {
26345 vctxt->skipDepth = vctxt->depth;
26346 goto exit;
26347 }
26348 /*
26349 * The declaration might be set by the wildcard validation,
26350 * when the processContents is "lax" or "strict".
26351 */
26352 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26353 /*
26354 * Clear the "decl" field to not confuse further processing.
26355 */
26356 vctxt->inode->decl = NULL;
26357 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026358 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026359 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026360 /*
26361 * Validate against the declaration.
26362 */
26363 ret = xmlSchemaValidateElemDecl(vctxt);
26364 if (ret != 0) {
26365 if (ret < 0) {
26366 VERROR_INT("xmlSchemaValidateElem",
26367 "calling xmlSchemaValidateElemDecl()");
26368 goto internal_error;
26369 }
26370 goto exit;
26371 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026372 /*
26373 * Validate against the type definition.
26374 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026375type_validation:
26376
26377 if (vctxt->inode->typeDef == NULL) {
26378 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26379 ret = XML_SCHEMAV_CVC_TYPE_1;
26380 VERROR(ret, NULL,
26381 "The type definition is absent");
26382 goto exit;
26383 }
26384 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26385 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26386 ret = XML_SCHEMAV_CVC_TYPE_2;
26387 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026388 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026389 goto exit;
26390 }
26391 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026392 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026393 * during validation against the declaration. This must be done
26394 * _before_ attribute validation.
26395 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026396 if (vctxt->xpathStates != NULL) {
26397 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026398 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026399 if (ret == -1) {
26400 VERROR_INT("xmlSchemaValidateElem",
26401 "calling xmlSchemaXPathEvaluate()");
26402 goto internal_error;
26403 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026404 }
26405 /*
26406 * Validate attributes.
26407 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026408 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026409 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026410 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026411
26412 ret = xmlSchemaVAttributesComplex(vctxt);
26413 }
26414 } else if (vctxt->nbAttrInfos != 0) {
26415
26416 ret = xmlSchemaVAttributesSimple(vctxt);
26417 }
26418 /*
26419 * Clear registered attributes.
26420 */
26421 if (vctxt->nbAttrInfos != 0)
26422 xmlSchemaClearAttrInfos(vctxt);
26423 if (ret == -1) {
26424 VERROR_INT("xmlSchemaValidateElem",
26425 "calling attributes validation");
26426 goto internal_error;
26427 }
26428 /*
26429 * Don't return an error if attributes are invalid on purpose.
26430 */
26431 ret = 0;
26432
26433exit:
26434 if (ret != 0)
26435 vctxt->skipDepth = vctxt->depth;
26436 return (ret);
26437internal_error:
26438 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026439}
26440
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026441#ifdef XML_SCHEMA_READER_ENABLED
26442static int
26443xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026444{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026445 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26446 int depth, nodeType, ret = 0, consumed;
26447 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026448
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026449 vctxt->depth = -1;
26450 ret = xmlTextReaderRead(vctxt->reader);
26451 /*
26452 * Move to the document element.
26453 */
26454 while (ret == 1) {
26455 nodeType = xmlTextReaderNodeType(vctxt->reader);
26456 if (nodeType == XML_ELEMENT_NODE)
26457 goto root_found;
26458 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026459 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026460 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026461
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026462root_found:
26463
26464 do {
26465 depth = xmlTextReaderDepth(vctxt->reader);
26466 nodeType = xmlTextReaderNodeType(vctxt->reader);
26467
26468 if (nodeType == XML_ELEMENT_NODE) {
26469
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026470 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026471 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26472 VERROR_INT("xmlSchemaVReaderWalk",
26473 "calling xmlSchemaValidatorPushElem()");
26474 goto internal_error;
26475 }
26476 ielem = vctxt->inode;
26477 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26478 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26479 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26480 /*
26481 * Is the element empty?
26482 */
26483 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26484 if (ret == -1) {
26485 VERROR_INT("xmlSchemaVReaderWalk",
26486 "calling xmlTextReaderIsEmptyElement()");
26487 goto internal_error;
26488 }
26489 if (ret) {
26490 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26491 }
26492 /*
26493 * Register attributes.
26494 */
26495 vctxt->nbAttrInfos = 0;
26496 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26497 if (ret == -1) {
26498 VERROR_INT("xmlSchemaVReaderWalk",
26499 "calling xmlTextReaderMoveToFirstAttribute()");
26500 goto internal_error;
26501 }
26502 if (ret == 1) {
26503 do {
26504 /*
26505 * VAL TODO: How do we know that the reader works on a
26506 * node tree, to be able to pass a node here?
26507 */
26508 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26509 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26510 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26511 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26512
26513 VERROR_INT("xmlSchemaVReaderWalk",
26514 "calling xmlSchemaValidatorPushAttribute()");
26515 goto internal_error;
26516 }
26517 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26518 if (ret == -1) {
26519 VERROR_INT("xmlSchemaVReaderWalk",
26520 "calling xmlTextReaderMoveToFirstAttribute()");
26521 goto internal_error;
26522 }
26523 } while (ret == 1);
26524 /*
26525 * Back to element position.
26526 */
26527 ret = xmlTextReaderMoveToElement(vctxt->reader);
26528 if (ret == -1) {
26529 VERROR_INT("xmlSchemaVReaderWalk",
26530 "calling xmlTextReaderMoveToElement()");
26531 goto internal_error;
26532 }
26533 }
26534 /*
26535 * Validate the element.
26536 */
26537 ret= xmlSchemaValidateElem(vctxt);
26538 if (ret != 0) {
26539 if (ret == -1) {
26540 VERROR_INT("xmlSchemaVReaderWalk",
26541 "calling xmlSchemaValidateElem()");
26542 goto internal_error;
26543 }
26544 goto exit;
26545 }
26546 if (vctxt->depth == vctxt->skipDepth) {
26547 int curDepth;
26548 /*
26549 * Skip all content.
26550 */
26551 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26552 ret = xmlTextReaderRead(vctxt->reader);
26553 curDepth = xmlTextReaderDepth(vctxt->reader);
26554 while ((ret == 1) && (curDepth != depth)) {
26555 ret = xmlTextReaderRead(vctxt->reader);
26556 curDepth = xmlTextReaderDepth(vctxt->reader);
26557 }
26558 if (ret < 0) {
26559 /*
26560 * VAL TODO: A reader error occured; what to do here?
26561 */
26562 ret = 1;
26563 goto exit;
26564 }
26565 }
26566 goto leave_elem;
26567 }
26568 /*
26569 * READER VAL TODO: Is an END_ELEM really never called
26570 * if the elem is empty?
26571 */
26572 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26573 goto leave_elem;
26574 } else if (nodeType == END_ELEM) {
26575 /*
26576 * Process END of element.
26577 */
26578leave_elem:
26579 ret = xmlSchemaValidatorPopElem(vctxt);
26580 if (ret != 0) {
26581 if (ret < 0) {
26582 VERROR_INT("xmlSchemaVReaderWalk",
26583 "calling xmlSchemaValidatorPopElem()");
26584 goto internal_error;
26585 }
26586 goto exit;
26587 }
26588 if (vctxt->depth >= 0)
26589 ielem = vctxt->inode;
26590 else
26591 ielem = NULL;
26592 } else if ((nodeType == XML_TEXT_NODE) ||
26593 (nodeType == XML_CDATA_SECTION_NODE) ||
26594 (nodeType == WHTSP) ||
26595 (nodeType == SIGN_WHTSP)) {
26596 /*
26597 * Process character content.
26598 */
26599 xmlChar *value;
26600
26601 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26602 nodeType = XML_TEXT_NODE;
26603
26604 value = xmlTextReaderValue(vctxt->reader);
26605 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26606 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26607 if (! consumed)
26608 xmlFree(value);
26609 if (ret == -1) {
26610 VERROR_INT("xmlSchemaVReaderWalk",
26611 "calling xmlSchemaVPushText()");
26612 goto internal_error;
26613 }
26614 } else if ((nodeType == XML_ENTITY_NODE) ||
26615 (nodeType == XML_ENTITY_REF_NODE)) {
26616 /*
26617 * VAL TODO: What to do with entities?
26618 */
26619 TODO
26620 }
26621 /*
26622 * Read next node.
26623 */
26624 ret = xmlTextReaderRead(vctxt->reader);
26625 } while (ret == 1);
26626
26627exit:
26628 return (ret);
26629internal_error:
26630 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026631}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026632#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026633
26634/************************************************************************
26635 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026636 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026637 * *
26638 ************************************************************************/
26639
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026640/*
26641* Process text content.
26642*/
26643static void
26644xmlSchemaSAXHandleText(void *ctx,
26645 const xmlChar * ch,
26646 int len)
26647{
26648 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26649
26650 if (vctxt->depth < 0)
26651 return;
26652 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26653 return;
26654 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26655 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26656 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26657 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26658 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26659 "calling xmlSchemaVPushText()");
26660 vctxt->err = -1;
26661 xmlStopParser(vctxt->parserCtxt);
26662 }
26663}
26664
26665/*
26666* Process CDATA content.
26667*/
26668static void
26669xmlSchemaSAXHandleCDataSection(void *ctx,
26670 const xmlChar * ch,
26671 int len)
26672{
26673 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26674
26675 if (vctxt->depth < 0)
26676 return;
26677 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26678 return;
26679 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26680 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26681 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26682 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26683 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26684 "calling xmlSchemaVPushText()");
26685 vctxt->err = -1;
26686 xmlStopParser(vctxt->parserCtxt);
26687 }
26688}
26689
26690static void
26691xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26692 const xmlChar * name ATTRIBUTE_UNUSED)
26693{
26694 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26695
26696 if (vctxt->depth < 0)
26697 return;
26698 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26699 return;
26700 /* SAX VAL TODO: What to do here? */
26701 TODO
26702}
26703
26704static void
26705xmlSchemaSAXHandleStartElementNs(void *ctx,
26706 const xmlChar * localname,
26707 const xmlChar * prefix ATTRIBUTE_UNUSED,
26708 const xmlChar * URI,
26709 int nb_namespaces,
26710 const xmlChar ** namespaces,
26711 int nb_attributes,
26712 int nb_defaulted ATTRIBUTE_UNUSED,
26713 const xmlChar ** attributes)
26714{
26715 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26716 int ret;
26717 xmlSchemaNodeInfoPtr ielem;
26718 int i, j;
26719
26720 /*
26721 * SAX VAL TODO: What to do with nb_defaulted?
26722 */
26723 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026724 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026725 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026726 vctxt->depth++;
26727 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026728 return;
26729 /*
26730 * Push the element.
26731 */
26732 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26733 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26734 "calling xmlSchemaValidatorPushElem()");
26735 goto internal_error;
26736 }
26737 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026738 /*
26739 * TODO: Is this OK?
26740 */
26741 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026742 ielem->localName = localname;
26743 ielem->nsName = URI;
26744 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26745 /*
26746 * Register namespaces on the elem info.
26747 */
26748 if (nb_namespaces != 0) {
26749 /*
26750 * Although the parser builds its own namespace list,
26751 * we have no access to it, so we'll use an own one.
26752 */
26753 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26754 /*
26755 * Store prefix and namespace name.
26756 */
26757 if (ielem->nsBindings == NULL) {
26758 ielem->nsBindings =
26759 (const xmlChar **) xmlMalloc(10 *
26760 sizeof(const xmlChar *));
26761 if (ielem->nsBindings == NULL) {
26762 xmlSchemaVErrMemory(vctxt,
26763 "allocating namespace bindings for SAX validation",
26764 NULL);
26765 goto internal_error;
26766 }
26767 ielem->nbNsBindings = 0;
26768 ielem->sizeNsBindings = 5;
26769 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26770 ielem->sizeNsBindings *= 2;
26771 ielem->nsBindings =
26772 (const xmlChar **) xmlRealloc(
26773 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026774 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026775 if (ielem->nsBindings == NULL) {
26776 xmlSchemaVErrMemory(vctxt,
26777 "re-allocating namespace bindings for SAX validation",
26778 NULL);
26779 goto internal_error;
26780 }
26781 }
26782
26783 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26784 if (namespaces[j+1][0] == 0) {
26785 /*
26786 * Handle xmlns="".
26787 */
26788 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26789 } else
26790 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26791 namespaces[j+1];
26792 ielem->nbNsBindings++;
26793 }
26794 }
26795 /*
26796 * Register attributes.
26797 * SAX VAL TODO: We are not adding namespace declaration
26798 * attributes yet.
26799 */
26800 if (nb_attributes != 0) {
26801 xmlChar *value;
26802
26803 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26804 /*
26805 * Duplicate the value.
26806 */
26807 value = xmlStrndup(attributes[j+3],
26808 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026809 /*
26810 * TODO: Set the node line.
26811 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026812 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026813 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026814 value, 1);
26815 if (ret == -1) {
26816 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26817 "calling xmlSchemaValidatorPushAttribute()");
26818 goto internal_error;
26819 }
26820 }
26821 }
26822 /*
26823 * Validate the element.
26824 */
26825 ret = xmlSchemaValidateElem(vctxt);
26826 if (ret != 0) {
26827 if (ret == -1) {
26828 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26829 "calling xmlSchemaValidateElem()");
26830 goto internal_error;
26831 }
26832 goto exit;
26833 }
26834
26835exit:
26836 return;
26837internal_error:
26838 vctxt->err = -1;
26839 xmlStopParser(vctxt->parserCtxt);
26840 return;
26841}
26842
26843static void
26844xmlSchemaSAXHandleEndElementNs(void *ctx,
26845 const xmlChar * localname ATTRIBUTE_UNUSED,
26846 const xmlChar * prefix ATTRIBUTE_UNUSED,
26847 const xmlChar * URI ATTRIBUTE_UNUSED)
26848{
26849 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26850 int res;
26851
26852 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026853 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026854 */
26855 if (vctxt->skipDepth != -1) {
26856 if (vctxt->depth > vctxt->skipDepth) {
26857 vctxt->depth--;
26858 return;
26859 } else
26860 vctxt->skipDepth = -1;
26861 }
26862 /*
26863 * SAX VAL TODO: Just a temporary check.
26864 */
26865 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26866 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26867 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26868 "elem pop mismatch");
26869 }
26870 res = xmlSchemaValidatorPopElem(vctxt);
26871 if (res != 0) {
26872 if (res < 0) {
26873 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26874 "calling xmlSchemaValidatorPopElem()");
26875 goto internal_error;
26876 }
26877 goto exit;
26878 }
26879exit:
26880 return;
26881internal_error:
26882 vctxt->err = -1;
26883 xmlStopParser(vctxt->parserCtxt);
26884 return;
26885}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026886
Daniel Veillard4255d502002-04-16 15:50:10 +000026887/************************************************************************
26888 * *
26889 * Validation interfaces *
26890 * *
26891 ************************************************************************/
26892
26893/**
26894 * xmlSchemaNewValidCtxt:
26895 * @schema: a precompiled XML Schemas
26896 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026897 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026898 *
26899 * Returns the validation context or NULL in case of error
26900 */
26901xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026902xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26903{
Daniel Veillard4255d502002-04-16 15:50:10 +000026904 xmlSchemaValidCtxtPtr ret;
26905
26906 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26907 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026908 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026909 return (NULL);
26910 }
26911 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026912 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026913 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000026914 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026915 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000026916 return (ret);
26917}
26918
26919/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026920 * xmlSchemaClearValidCtxt:
26921 * @ctxt: the schema validation context
26922 *
26923 * Free the resources associated to the schema validation context;
26924 * leaves some fields alive intended for reuse of the context.
26925 */
26926static void
26927xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
26928{
26929 if (vctxt == NULL)
26930 return;
26931
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026932 /*
26933 * TODO: Should we clear the flags?
26934 * Might be problematic if one reuses the context
26935 * and assumes that the options remain the same.
26936 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026937 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026938 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026939 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026940#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026941 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026942#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026943 vctxt->hasKeyrefs = 0;
26944
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026945 if (vctxt->value != NULL) {
26946 xmlSchemaFreeValue(vctxt->value);
26947 vctxt->value = NULL;
26948 }
26949 /*
26950 * Augmented IDC information.
26951 */
26952 if (vctxt->aidcs != NULL) {
26953 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
26954 do {
26955 next = cur->next;
26956 xmlFree(cur);
26957 cur = next;
26958 } while (cur != NULL);
26959 vctxt->aidcs = NULL;
26960 }
26961 if (vctxt->idcNodes != NULL) {
26962 int i;
26963 xmlSchemaPSVIIDCNodePtr item;
26964
26965 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026966 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026967 xmlFree(item->keys);
26968 xmlFree(item);
26969 }
26970 xmlFree(vctxt->idcNodes);
26971 vctxt->idcNodes = NULL;
26972 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026973 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026974 * Note that we won't delete the XPath state pool here.
26975 */
26976 if (vctxt->xpathStates != NULL) {
26977 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
26978 vctxt->xpathStates = NULL;
26979 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026980 /*
26981 * Attribute info.
26982 */
26983 if (vctxt->nbAttrInfos != 0) {
26984 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026985 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026986 /*
26987 * Element info.
26988 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026989 if (vctxt->elemInfos != NULL) {
26990 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026991 xmlSchemaNodeInfoPtr ei;
26992
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026993 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026994 ei = vctxt->elemInfos[i];
26995 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026996 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026997 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026998 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026999 }
27000 xmlSchemaItemListClear(vctxt->nodeQNames);
27001 /* Recreate the dict. */
27002 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027003 /*
27004 * TODO: Is is save to recreate it? Do we have a scenario
27005 * where the user provides the dict?
27006 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027007 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027008}
27009
27010/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027011 * xmlSchemaFreeValidCtxt:
27012 * @ctxt: the schema validation context
27013 *
27014 * Free the resources associated to the schema validation context
27015 */
27016void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027017xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27018{
Daniel Veillard4255d502002-04-16 15:50:10 +000027019 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027020 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027021 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027022 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027023 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027024 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027025 if (ctxt->idcNodes != NULL) {
27026 int i;
27027 xmlSchemaPSVIIDCNodePtr item;
27028
27029 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027030 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027031 xmlFree(item->keys);
27032 xmlFree(item);
27033 }
27034 xmlFree(ctxt->idcNodes);
27035 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027036 if (ctxt->idcKeys != NULL) {
27037 int i;
27038 for (i = 0; i < ctxt->nbIdcKeys; i++)
27039 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27040 xmlFree(ctxt->idcKeys);
27041 }
27042
27043 if (ctxt->xpathStates != NULL)
27044 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27045 if (ctxt->xpathStatePool != NULL)
27046 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27047
27048 /*
27049 * Augmented IDC information.
27050 */
27051 if (ctxt->aidcs != NULL) {
27052 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27053 do {
27054 next = cur->next;
27055 xmlFree(cur);
27056 cur = next;
27057 } while (cur != NULL);
27058 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027059 if (ctxt->attrInfos != NULL) {
27060 int i;
27061 xmlSchemaAttrInfoPtr attr;
27062
27063 /* Just a paranoid call to the cleanup. */
27064 if (ctxt->nbAttrInfos != 0)
27065 xmlSchemaClearAttrInfos(ctxt);
27066 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27067 attr = ctxt->attrInfos[i];
27068 xmlFree(attr);
27069 }
27070 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027071 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027072 if (ctxt->elemInfos != NULL) {
27073 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027074 xmlSchemaNodeInfoPtr ei;
27075
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027076 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027077 ei = ctxt->elemInfos[i];
27078 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027079 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027080 xmlSchemaClearElemInfo(ei);
27081 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027082 }
27083 xmlFree(ctxt->elemInfos);
27084 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027085 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027086 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027087 if (ctxt->dict != NULL)
27088 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027089 xmlFree(ctxt);
27090}
27091
27092/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027093 * xmlSchemaIsValid:
27094 * @ctxt: the schema validation context
27095 *
27096 * Check if any error was detected during validation.
27097 *
27098 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27099 * of internal error.
27100 */
27101int
27102xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27103{
27104 if (ctxt == NULL)
27105 return(-1);
27106 return(ctxt->err == 0);
27107}
27108
27109/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027110 * xmlSchemaSetValidErrors:
27111 * @ctxt: a schema validation context
27112 * @err: the error function
27113 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027114 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027115 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027116 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027117 */
27118void
27119xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027120 xmlSchemaValidityErrorFunc err,
27121 xmlSchemaValidityWarningFunc warn, void *ctx)
27122{
Daniel Veillard4255d502002-04-16 15:50:10 +000027123 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027124 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027125 ctxt->error = err;
27126 ctxt->warning = warn;
27127 ctxt->userData = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027128 if (ctxt->pctxt != NULL)
27129 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027130}
27131
27132/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027133 * xmlSchemaSetValidStructuredErrors:
27134 * @ctxt: a schema validation context
27135 * @serror: the structured error function
27136 * @ctx: the functions context
27137 *
27138 * Set the structured error callback
27139 */
27140void
27141xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
27142 xmlStructuredErrorFunc serror, void *ctx)
27143{
27144 if (ctxt == NULL)
27145 return;
27146 ctxt->serror = serror;
27147 ctxt->error = NULL;
27148 ctxt->warning = NULL;
27149 ctxt->userData = ctx;
27150}
27151
27152/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027153 * xmlSchemaGetValidErrors:
27154 * @ctxt: a XML-Schema validation context
27155 * @err: the error function result
27156 * @warn: the warning function result
27157 * @ctx: the functions context result
27158 *
27159 * Get the error and warning callback informations
27160 *
27161 * Returns -1 in case of error and 0 otherwise
27162 */
27163int
27164xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
27165 xmlSchemaValidityErrorFunc * err,
27166 xmlSchemaValidityWarningFunc * warn, void **ctx)
27167{
27168 if (ctxt == NULL)
27169 return (-1);
27170 if (err != NULL)
27171 *err = ctxt->error;
27172 if (warn != NULL)
27173 *warn = ctxt->warning;
27174 if (ctx != NULL)
27175 *ctx = ctxt->userData;
27176 return (0);
27177}
27178
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027179
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027180/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027181 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027182 * @ctxt: a schema validation context
27183 * @options: a combination of xmlSchemaValidOption
27184 *
27185 * Sets the options to be used during the validation.
27186 *
27187 * Returns 0 in case of success, -1 in case of an
27188 * API error.
27189 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027190int
27191xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27192 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027193
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027194{
27195 int i;
27196
27197 if (ctxt == NULL)
27198 return (-1);
27199 /*
27200 * WARNING: Change the start value if adding to the
27201 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027202 * TODO: Is there an other, more easy to maintain,
27203 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027204 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027205 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027206 if (options & 1<<i)
27207 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027208 }
27209 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027210 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027211}
27212
27213/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027214 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027215 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027216 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027217 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027218 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027219 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027220 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027221int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027222xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027223
27224{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027225 if (ctxt == NULL)
27226 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027227 else
27228 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027229}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027230
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027231static int
27232xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27233{
27234 xmlAttrPtr attr;
27235 int ret = 0;
27236 xmlSchemaNodeInfoPtr ielem = NULL;
27237 xmlNodePtr node, valRoot;
27238 const xmlChar *nsName;
27239
27240 /* DOC VAL TODO: Move this to the start function. */
27241 valRoot = xmlDocGetRootElement(vctxt->doc);
27242 if (valRoot == NULL) {
27243 /* VAL TODO: Error code? */
27244 VERROR(1, NULL, "The document has no document element");
27245 return (1);
27246 }
27247 vctxt->depth = -1;
27248 vctxt->validationRoot = valRoot;
27249 node = valRoot;
27250 while (node != NULL) {
27251 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27252 goto next_sibling;
27253 if (node->type == XML_ELEMENT_NODE) {
27254
27255 /*
27256 * Init the node-info.
27257 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027258 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027259 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27260 goto internal_error;
27261 ielem = vctxt->inode;
27262 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027263 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027264 ielem->localName = node->name;
27265 if (node->ns != NULL)
27266 ielem->nsName = node->ns->href;
27267 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27268 /*
27269 * Register attributes.
27270 * DOC VAL TODO: We do not register namespace declaration
27271 * attributes yet.
27272 */
27273 vctxt->nbAttrInfos = 0;
27274 if (node->properties != NULL) {
27275 attr = node->properties;
27276 do {
27277 if (attr->ns != NULL)
27278 nsName = attr->ns->href;
27279 else
27280 nsName = NULL;
27281 ret = xmlSchemaValidatorPushAttribute(vctxt,
27282 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027283 /*
27284 * Note that we give it the line number of the
27285 * parent element.
27286 */
27287 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027288 attr->name, nsName, 0,
27289 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27290 if (ret == -1) {
27291 VERROR_INT("xmlSchemaDocWalk",
27292 "calling xmlSchemaValidatorPushAttribute()");
27293 goto internal_error;
27294 }
27295 attr = attr->next;
27296 } while (attr);
27297 }
27298 /*
27299 * Validate the element.
27300 */
27301 ret = xmlSchemaValidateElem(vctxt);
27302 if (ret != 0) {
27303 if (ret == -1) {
27304 VERROR_INT("xmlSchemaDocWalk",
27305 "calling xmlSchemaValidateElem()");
27306 goto internal_error;
27307 }
27308 /*
27309 * Don't stop validation; just skip the content
27310 * of this element.
27311 */
27312 goto leave_node;
27313 }
27314 if ((vctxt->skipDepth != -1) &&
27315 (vctxt->depth >= vctxt->skipDepth))
27316 goto leave_node;
27317 } else if ((node->type == XML_TEXT_NODE) ||
27318 (node->type == XML_CDATA_SECTION_NODE)) {
27319 /*
27320 * Process character content.
27321 */
27322 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27323 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27324 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27325 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27326 if (ret < 0) {
27327 VERROR_INT("xmlSchemaVDocWalk",
27328 "calling xmlSchemaVPushText()");
27329 goto internal_error;
27330 }
27331 /*
27332 * DOC VAL TODO: Should we skip further validation of the
27333 * element content here?
27334 */
27335 } else if ((node->type == XML_ENTITY_NODE) ||
27336 (node->type == XML_ENTITY_REF_NODE)) {
27337 /*
27338 * DOC VAL TODO: What to do with entities?
27339 */
27340 TODO
27341 } else {
27342 goto leave_node;
27343 /*
27344 * DOC VAL TODO: XInclude nodes, etc.
27345 */
27346 }
27347 /*
27348 * Walk the doc.
27349 */
27350 if (node->children != NULL) {
27351 node = node->children;
27352 continue;
27353 }
27354leave_node:
27355 if (node->type == XML_ELEMENT_NODE) {
27356 /*
27357 * Leaving the scope of an element.
27358 */
27359 if (node != vctxt->inode->node) {
27360 VERROR_INT("xmlSchemaVDocWalk",
27361 "element position mismatch");
27362 goto internal_error;
27363 }
27364 ret = xmlSchemaValidatorPopElem(vctxt);
27365 if (ret != 0) {
27366 if (ret < 0) {
27367 VERROR_INT("xmlSchemaVDocWalk",
27368 "calling xmlSchemaValidatorPopElem()");
27369 goto internal_error;
27370 }
27371 }
27372 if (node == valRoot)
27373 goto exit;
27374 }
27375next_sibling:
27376 if (node->next != NULL)
27377 node = node->next;
27378 else {
27379 node = node->parent;
27380 goto leave_node;
27381 }
27382 }
27383
27384exit:
27385 return (ret);
27386internal_error:
27387 return (-1);
27388}
27389
27390static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027391xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027392 /*
27393 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027394 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027395 vctxt->err = 0;
27396 vctxt->nberrors = 0;
27397 vctxt->depth = -1;
27398 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027399 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027400 vctxt->hasKeyrefs = 0;
27401#ifdef ENABLE_IDC_NODE_TABLES
27402 vctxt->createIDCNodeTables = 1;
27403#else
27404 vctxt->createIDCNodeTables = 0;
27405#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027406 /*
27407 * Create a schema + parser if necessary.
27408 */
27409 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027410 xmlSchemaParserCtxtPtr pctxt;
27411
27412 vctxt->xsiAssemble = 1;
27413 /*
27414 * If not schema was given then we will create a schema
27415 * dynamically using XSI schema locations.
27416 *
27417 * Create the schema parser context.
27418 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027419 if ((vctxt->pctxt == NULL) &&
27420 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27421 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027422 pctxt = vctxt->pctxt;
27423 pctxt->xsiAssemble = 1;
27424 /*
27425 * Create the schema.
27426 */
27427 vctxt->schema = xmlSchemaNewSchema(pctxt);
27428 if (vctxt->schema == NULL)
27429 return (-1);
27430 /*
27431 * Create the schema construction context.
27432 */
27433 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27434 if (pctxt->constructor == NULL)
27435 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027436 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027437 /*
27438 * Take ownership of the constructor to be able to free it.
27439 */
27440 pctxt->ownsConstructor = 1;
27441 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027442 /*
27443 * Augment the IDC definitions.
27444 */
27445 if (vctxt->schema->idcDef != NULL) {
27446 xmlHashScan(vctxt->schema->idcDef,
27447 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27448 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027449 return(0);
27450}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027451
Daniel Veillardf10ae122005-07-10 19:03:16 +000027452static void
27453xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027454 if (vctxt->xsiAssemble) {
27455 if (vctxt->schema != NULL) {
27456 xmlSchemaFree(vctxt->schema);
27457 vctxt->schema = NULL;
27458 }
27459 }
27460 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027461}
27462
27463static int
27464xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27465{
27466 int ret = 0;
27467
27468 if (xmlSchemaPreRun(vctxt) < 0)
27469 return(-1);
27470
27471 if (vctxt->doc != NULL) {
27472 /*
27473 * Tree validation.
27474 */
27475 ret = xmlSchemaVDocWalk(vctxt);
27476#ifdef LIBXML_READER_ENABLED
27477 } else if (vctxt->reader != NULL) {
27478 /*
27479 * XML Reader validation.
27480 */
27481#ifdef XML_SCHEMA_READER_ENABLED
27482 ret = xmlSchemaVReaderWalk(vctxt);
27483#endif
27484#endif
27485 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27486 /*
27487 * SAX validation.
27488 */
27489 ret = xmlParseDocument(vctxt->parserCtxt);
27490 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027491 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027492 "no instance to validate");
27493 ret = -1;
27494 }
27495
27496 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027497 if (ret == 0)
27498 ret = vctxt->err;
27499 return (ret);
27500}
27501
27502/**
27503 * xmlSchemaValidateOneElement:
27504 * @ctxt: a schema validation context
27505 * @elem: an element node
27506 *
27507 * Validate a branch of a tree, starting with the given @elem.
27508 *
27509 * Returns 0 if the element and its subtree is valid, a positive error
27510 * code number otherwise and -1 in case of an internal or API error.
27511 */
27512int
27513xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27514{
27515 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27516 return (-1);
27517
27518 if (ctxt->schema == NULL)
27519 return (-1);
27520
27521 ctxt->doc = elem->doc;
27522 ctxt->node = elem;
27523 ctxt->validationRoot = elem;
27524 return(xmlSchemaVStart(ctxt));
27525}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027526
Daniel Veillard259f0df2004-08-18 09:13:18 +000027527/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027528 * xmlSchemaValidateDoc:
27529 * @ctxt: a schema validation context
27530 * @doc: a parsed document tree
27531 *
27532 * Validate a document tree in memory.
27533 *
27534 * Returns 0 if the document is schemas valid, a positive error code
27535 * number otherwise and -1 in case of internal or API error.
27536 */
27537int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027538xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27539{
Daniel Veillard4255d502002-04-16 15:50:10 +000027540 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027541 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027542
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027543 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027544 ctxt->node = xmlDocGetRootElement(doc);
27545 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027546 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027547 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27548 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027549 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027550 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027551 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027552 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027553 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027554}
27555
Daniel Veillardcdc82732005-07-08 15:04:06 +000027556
27557/************************************************************************
27558 * *
27559 * Function and data for SAX streaming API *
27560 * *
27561 ************************************************************************/
27562typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27563typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27564
27565struct _xmlSchemaSplitSAXData {
27566 xmlSAXHandlerPtr user_sax;
27567 void *user_data;
27568 xmlSchemaValidCtxtPtr ctxt;
27569 xmlSAXHandlerPtr schemas_sax;
27570};
27571
Daniel Veillard971771e2005-07-09 17:32:57 +000027572#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27573
27574struct _xmlSchemaSAXPlug {
27575 unsigned int magic;
27576
27577 /* the original callbacks informations */
27578 xmlSAXHandlerPtr *user_sax_ptr;
27579 xmlSAXHandlerPtr user_sax;
27580 void **user_data_ptr;
27581 void *user_data;
27582
27583 /* the block plugged back and validation informations */
27584 xmlSAXHandler schemas_sax;
27585 xmlSchemaValidCtxtPtr ctxt;
27586};
27587
Daniel Veillardcdc82732005-07-08 15:04:06 +000027588/* All those functions just bounces to the user provided SAX handlers */
27589static void
27590internalSubsetSplit(void *ctx, const xmlChar *name,
27591 const xmlChar *ExternalID, const xmlChar *SystemID)
27592{
Daniel Veillard971771e2005-07-09 17:32:57 +000027593 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027594 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27595 (ctxt->user_sax->internalSubset != NULL))
27596 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27597 SystemID);
27598}
27599
27600static int
27601isStandaloneSplit(void *ctx)
27602{
Daniel Veillard971771e2005-07-09 17:32:57 +000027603 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027604 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27605 (ctxt->user_sax->isStandalone != NULL))
27606 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27607 return(0);
27608}
27609
27610static int
27611hasInternalSubsetSplit(void *ctx)
27612{
Daniel Veillard971771e2005-07-09 17:32:57 +000027613 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027614 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27615 (ctxt->user_sax->hasInternalSubset != NULL))
27616 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27617 return(0);
27618}
27619
27620static int
27621hasExternalSubsetSplit(void *ctx)
27622{
Daniel Veillard971771e2005-07-09 17:32:57 +000027623 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027624 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27625 (ctxt->user_sax->hasExternalSubset != NULL))
27626 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27627 return(0);
27628}
27629
27630static void
27631externalSubsetSplit(void *ctx, const xmlChar *name,
27632 const xmlChar *ExternalID, const xmlChar *SystemID)
27633{
Daniel Veillard971771e2005-07-09 17:32:57 +000027634 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027635 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27636 (ctxt->user_sax->internalSubset != NULL))
27637 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27638 SystemID);
27639}
27640
27641static xmlParserInputPtr
27642resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27643{
Daniel Veillard971771e2005-07-09 17:32:57 +000027644 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027645 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27646 (ctxt->user_sax->resolveEntity != NULL))
27647 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27648 systemId));
27649 return(NULL);
27650}
27651
27652static xmlEntityPtr
27653getEntitySplit(void *ctx, const xmlChar *name)
27654{
Daniel Veillard971771e2005-07-09 17:32:57 +000027655 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027656 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27657 (ctxt->user_sax->getEntity != NULL))
27658 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27659 return(NULL);
27660}
27661
27662static xmlEntityPtr
27663getParameterEntitySplit(void *ctx, const xmlChar *name)
27664{
Daniel Veillard971771e2005-07-09 17:32:57 +000027665 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027666 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27667 (ctxt->user_sax->getParameterEntity != NULL))
27668 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27669 return(NULL);
27670}
27671
27672
27673static void
27674entityDeclSplit(void *ctx, const xmlChar *name, int type,
27675 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27676{
Daniel Veillard971771e2005-07-09 17:32:57 +000027677 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027678 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27679 (ctxt->user_sax->entityDecl != NULL))
27680 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27681 systemId, content);
27682}
27683
27684static void
27685attributeDeclSplit(void *ctx, const xmlChar * elem,
27686 const xmlChar * name, int type, int def,
27687 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27688{
Daniel Veillard971771e2005-07-09 17:32:57 +000027689 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027690 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27691 (ctxt->user_sax->attributeDecl != NULL)) {
27692 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27693 def, defaultValue, tree);
27694 } else {
27695 xmlFreeEnumeration(tree);
27696 }
27697}
27698
27699static void
27700elementDeclSplit(void *ctx, const xmlChar *name, int type,
27701 xmlElementContentPtr content)
27702{
Daniel Veillard971771e2005-07-09 17:32:57 +000027703 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027704 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27705 (ctxt->user_sax->elementDecl != NULL))
27706 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27707}
27708
27709static void
27710notationDeclSplit(void *ctx, const xmlChar *name,
27711 const xmlChar *publicId, const xmlChar *systemId)
27712{
Daniel Veillard971771e2005-07-09 17:32:57 +000027713 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027714 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27715 (ctxt->user_sax->notationDecl != NULL))
27716 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27717 systemId);
27718}
27719
27720static void
27721unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27722 const xmlChar *publicId, const xmlChar *systemId,
27723 const xmlChar *notationName)
27724{
Daniel Veillard971771e2005-07-09 17:32:57 +000027725 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027726 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27727 (ctxt->user_sax->unparsedEntityDecl != NULL))
27728 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27729 systemId, notationName);
27730}
27731
27732static void
27733setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27734{
Daniel Veillard971771e2005-07-09 17:32:57 +000027735 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027736 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27737 (ctxt->user_sax->setDocumentLocator != NULL))
27738 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27739}
27740
27741static void
27742startDocumentSplit(void *ctx)
27743{
Daniel Veillard971771e2005-07-09 17:32:57 +000027744 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027745 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27746 (ctxt->user_sax->startDocument != NULL))
27747 ctxt->user_sax->startDocument(ctxt->user_data);
27748}
27749
27750static void
27751endDocumentSplit(void *ctx)
27752{
Daniel Veillard971771e2005-07-09 17:32:57 +000027753 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027754 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27755 (ctxt->user_sax->endDocument != NULL))
27756 ctxt->user_sax->endDocument(ctxt->user_data);
27757}
27758
27759static void
27760processingInstructionSplit(void *ctx, const xmlChar *target,
27761 const xmlChar *data)
27762{
Daniel Veillard971771e2005-07-09 17:32:57 +000027763 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027764 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27765 (ctxt->user_sax->processingInstruction != NULL))
27766 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27767}
27768
27769static void
27770commentSplit(void *ctx, const xmlChar *value)
27771{
Daniel Veillard971771e2005-07-09 17:32:57 +000027772 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027773 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27774 (ctxt->user_sax->comment != NULL))
27775 ctxt->user_sax->comment(ctxt->user_data, value);
27776}
27777
27778/*
27779 * Varargs error callbacks to the user application, harder ...
27780 */
27781
Daniel Veillardffa3c742005-07-21 13:24:09 +000027782static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027783warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027784 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027785 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27786 (ctxt->user_sax->warning != NULL)) {
27787 TODO
27788 }
27789}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027790static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027791errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027792 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027793 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27794 (ctxt->user_sax->error != NULL)) {
27795 TODO
27796 }
27797}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027798static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027799fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027800 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027801 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27802 (ctxt->user_sax->fatalError != NULL)) {
27803 TODO
27804 }
27805}
27806
27807/*
27808 * Those are function where both the user handler and the schemas handler
27809 * need to be called.
27810 */
27811static void
27812charactersSplit(void *ctx, const xmlChar *ch, int len)
27813{
Daniel Veillard971771e2005-07-09 17:32:57 +000027814 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027815 if (ctxt == NULL)
27816 return;
27817 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27818 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27819 if (ctxt->ctxt != NULL)
27820 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27821}
27822
27823static void
27824ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27825{
Daniel Veillard971771e2005-07-09 17:32:57 +000027826 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027827 if (ctxt == NULL)
27828 return;
27829 if ((ctxt->user_sax != NULL) &&
27830 (ctxt->user_sax->ignorableWhitespace != NULL))
27831 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27832 if (ctxt->ctxt != NULL)
27833 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27834}
27835
27836static void
27837cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27838{
Daniel Veillard971771e2005-07-09 17:32:57 +000027839 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027840 if (ctxt == NULL)
27841 return;
27842 if ((ctxt->user_sax != NULL) &&
27843 (ctxt->user_sax->ignorableWhitespace != NULL))
27844 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27845 if (ctxt->ctxt != NULL)
27846 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27847}
27848
27849static void
27850referenceSplit(void *ctx, const xmlChar *name)
27851{
Daniel Veillard971771e2005-07-09 17:32:57 +000027852 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027853 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27854 (ctxt->user_sax->reference != NULL))
27855 ctxt->user_sax->reference(ctxt->user_data, name);
27856 if (ctxt->ctxt != NULL)
27857 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27858}
27859
27860static void
27861startElementNsSplit(void *ctx, const xmlChar * localname,
27862 const xmlChar * prefix, const xmlChar * URI,
27863 int nb_namespaces, const xmlChar ** namespaces,
27864 int nb_attributes, int nb_defaulted,
27865 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027866 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027867 if (ctxt == NULL)
27868 return;
27869 if ((ctxt->user_sax != NULL) &&
27870 (ctxt->user_sax->startElementNs != NULL))
27871 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27872 URI, nb_namespaces, namespaces,
27873 nb_attributes, nb_defaulted,
27874 attributes);
27875 if (ctxt->ctxt != NULL)
27876 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27877 URI, nb_namespaces, namespaces,
27878 nb_attributes, nb_defaulted,
27879 attributes);
27880}
27881
27882static void
27883endElementNsSplit(void *ctx, const xmlChar * localname,
27884 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027885 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027886 if (ctxt == NULL)
27887 return;
27888 if ((ctxt->user_sax != NULL) &&
27889 (ctxt->user_sax->endElementNs != NULL))
27890 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27891 if (ctxt->ctxt != NULL)
27892 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27893}
27894
Daniel Veillard4255d502002-04-16 15:50:10 +000027895/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027896 * xmlSchemaSAXPlug:
27897 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027898 * @sax: a pointer to the original xmlSAXHandlerPtr
27899 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027900 *
27901 * Plug a SAX based validation layer in a SAX parsing event flow.
27902 * The original @saxptr and @dataptr data are replaced by new pointers
27903 * but the calls to the original will be maintained.
27904 *
27905 * Returns a pointer to a data structure needed to unplug the validation layer
27906 * or NULL in case of errors.
27907 */
27908xmlSchemaSAXPlugPtr
27909xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
27910 xmlSAXHandlerPtr *sax, void **user_data)
27911{
27912 xmlSchemaSAXPlugPtr ret;
27913 xmlSAXHandlerPtr old_sax;
27914
27915 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
27916 return(NULL);
27917
27918 /*
27919 * We only allow to plug into SAX2 event streams
27920 */
27921 old_sax = *sax;
27922 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
27923 return(NULL);
27924 if ((old_sax != NULL) &&
27925 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
27926 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
27927 return(NULL);
27928
27929 /*
27930 * everything seems right allocate the local data needed for that layer
27931 */
27932 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
27933 if (ret == NULL) {
27934 return(NULL);
27935 }
27936 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
27937 ret->magic = XML_SAX_PLUG_MAGIC;
27938 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
27939 ret->ctxt = ctxt;
27940 ret->user_sax_ptr = sax;
27941 ret->user_sax = old_sax;
27942 if (old_sax == NULL) {
27943 /*
27944 * go direct, no need for the split block and functions.
27945 */
27946 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
27947 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
27948 /*
27949 * Note that we use the same text-function for both, to prevent
27950 * the parser from testing for ignorable whitespace.
27951 */
27952 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
27953 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
27954
27955 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
27956 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
27957
27958 ret->user_data = ctxt;
27959 *user_data = ctxt;
27960 } else {
27961 /*
27962 * for each callback unused by Schemas initialize it to the Split
27963 * routine only if non NULL in the user block, this can speed up
27964 * things at the SAX level.
27965 */
27966 if (old_sax->internalSubset != NULL)
27967 ret->schemas_sax.internalSubset = internalSubsetSplit;
27968 if (old_sax->isStandalone != NULL)
27969 ret->schemas_sax.isStandalone = isStandaloneSplit;
27970 if (old_sax->hasInternalSubset != NULL)
27971 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
27972 if (old_sax->hasExternalSubset != NULL)
27973 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
27974 if (old_sax->resolveEntity != NULL)
27975 ret->schemas_sax.resolveEntity = resolveEntitySplit;
27976 if (old_sax->getEntity != NULL)
27977 ret->schemas_sax.getEntity = getEntitySplit;
27978 if (old_sax->entityDecl != NULL)
27979 ret->schemas_sax.entityDecl = entityDeclSplit;
27980 if (old_sax->notationDecl != NULL)
27981 ret->schemas_sax.notationDecl = notationDeclSplit;
27982 if (old_sax->attributeDecl != NULL)
27983 ret->schemas_sax.attributeDecl = attributeDeclSplit;
27984 if (old_sax->elementDecl != NULL)
27985 ret->schemas_sax.elementDecl = elementDeclSplit;
27986 if (old_sax->unparsedEntityDecl != NULL)
27987 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
27988 if (old_sax->setDocumentLocator != NULL)
27989 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
27990 if (old_sax->startDocument != NULL)
27991 ret->schemas_sax.startDocument = startDocumentSplit;
27992 if (old_sax->endDocument != NULL)
27993 ret->schemas_sax.endDocument = endDocumentSplit;
27994 if (old_sax->processingInstruction != NULL)
27995 ret->schemas_sax.processingInstruction = processingInstructionSplit;
27996 if (old_sax->comment != NULL)
27997 ret->schemas_sax.comment = commentSplit;
27998 if (old_sax->warning != NULL)
27999 ret->schemas_sax.warning = warningSplit;
28000 if (old_sax->error != NULL)
28001 ret->schemas_sax.error = errorSplit;
28002 if (old_sax->fatalError != NULL)
28003 ret->schemas_sax.fatalError = fatalErrorSplit;
28004 if (old_sax->getParameterEntity != NULL)
28005 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28006 if (old_sax->externalSubset != NULL)
28007 ret->schemas_sax.externalSubset = externalSubsetSplit;
28008
28009 /*
28010 * the 6 schemas callback have to go to the splitter functions
28011 * Note that we use the same text-function for ignorableWhitespace
28012 * if possible, to prevent the parser from testing for ignorable
28013 * whitespace.
28014 */
28015 ret->schemas_sax.characters = charactersSplit;
28016 if ((old_sax->ignorableWhitespace != NULL) &&
28017 (old_sax->ignorableWhitespace != old_sax->characters))
28018 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28019 else
28020 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28021 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28022 ret->schemas_sax.reference = referenceSplit;
28023 ret->schemas_sax.startElementNs = startElementNsSplit;
28024 ret->schemas_sax.endElementNs = endElementNsSplit;
28025
28026 ret->user_data_ptr = user_data;
28027 ret->user_data = *user_data;
28028 *user_data = ret;
28029 }
28030
28031 /*
28032 * plug the pointers back.
28033 */
28034 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028035 ctxt->sax = *sax;
28036 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28037 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028038 return(ret);
28039}
28040
28041/**
28042 * xmlSchemaSAXUnplug:
28043 * @plug: a data structure returned by xmlSchemaSAXPlug
28044 *
28045 * Unplug a SAX based validation layer in a SAX parsing event flow.
28046 * The original pointers used in the call are restored.
28047 *
28048 * Returns 0 in case of success and -1 in case of failure.
28049 */
28050int
28051xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28052{
28053 xmlSAXHandlerPtr *sax;
28054 void **user_data;
28055
28056 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28057 return(-1);
28058 plug->magic = 0;
28059
Daniel Veillardf10ae122005-07-10 19:03:16 +000028060 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028061 /* restore the data */
28062 sax = plug->user_sax_ptr;
28063 *sax = plug->user_sax;
28064 if (plug->user_sax != NULL) {
28065 user_data = plug->user_data_ptr;
28066 *user_data = plug->user_data;
28067 }
28068
28069 /* free and return */
28070 xmlFree(plug);
28071 return(0);
28072}
28073
28074/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028075 * xmlSchemaValidateStream:
28076 * @ctxt: a schema validation context
28077 * @input: the input to use for reading the data
28078 * @enc: an optional encoding information
28079 * @sax: a SAX handler for the resulting events
28080 * @user_data: the context to provide to the SAX handler.
28081 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028082 * Validate an input based on a flow of SAX event from the parser
28083 * and forward the events to the @sax handler with the provided @user_data
28084 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028085 *
28086 * Returns 0 if the document is schemas valid, a positive error code
28087 * number otherwise and -1 in case of internal or API error.
28088 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028089int
Daniel Veillard4255d502002-04-16 15:50:10 +000028090xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028091 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28092 xmlSAXHandlerPtr sax, void *user_data)
28093{
Daniel Veillard971771e2005-07-09 17:32:57 +000028094 xmlSchemaSAXPlugPtr plug = NULL;
28095 xmlSAXHandlerPtr old_sax = NULL;
28096 xmlParserCtxtPtr pctxt = NULL;
28097 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028098 int ret;
28099
Daniel Veillard4255d502002-04-16 15:50:10 +000028100 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028101 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028102
Daniel Veillardcdc82732005-07-08 15:04:06 +000028103 /*
28104 * prepare the parser
28105 */
28106 pctxt = xmlNewParserCtxt();
28107 if (pctxt == NULL)
28108 return (-1);
28109 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028110 pctxt->sax = sax;
28111 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028112#if 0
28113 if (options)
28114 xmlCtxtUseOptions(pctxt, options);
28115#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028116 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028117
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028118 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028119 if (inputStream == NULL) {
28120 ret = -1;
28121 goto done;
28122 }
28123 inputPush(pctxt, inputStream);
28124 ctxt->parserCtxt = pctxt;
28125 ctxt->input = input;
28126
28127 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028128 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028129 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028130 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28131 if (plug == NULL) {
28132 ret = -1;
28133 goto done;
28134 }
28135 ctxt->input = input;
28136 ctxt->enc = enc;
28137 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028138 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28139 ret = xmlSchemaVStart(ctxt);
28140
28141 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28142 ret = ctxt->parserCtxt->errNo;
28143 if (ret == 0)
28144 ret = 1;
28145 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028146
28147done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028148 ctxt->parserCtxt = NULL;
28149 ctxt->sax = NULL;
28150 ctxt->input = NULL;
28151 if (plug != NULL) {
28152 xmlSchemaSAXUnplug(plug);
28153 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028154 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028155 if (pctxt != NULL) {
28156 pctxt->sax = old_sax;
28157 xmlFreeParserCtxt(pctxt);
28158 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028159 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028160}
28161
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028162/**
28163 * xmlSchemaValidateFile:
28164 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028165 * @filename: the URI of the instance
28166 * @options: a future set of options, currently unused
28167 *
28168 * Do a schemas validation of the given resource, it will use the
28169 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028170 *
28171 * Returns 0 if the document is valid, a positive error code
28172 * number otherwise and -1 in case of an internal or API error.
28173 */
28174int
28175xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028176 const char * filename,
28177 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028178{
28179 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028180 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028181
28182 if ((ctxt == NULL) || (filename == NULL))
28183 return (-1);
28184
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028185 input = xmlParserInputBufferCreateFilename(filename,
28186 XML_CHAR_ENCODING_NONE);
28187 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028188 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028189 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28190 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028191 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028192}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028193
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028194#define bottom_xmlschemas
28195#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028196#endif /* LIBXML_SCHEMAS_ENABLED */