blob: 6a2a6be3a8d1c285a91ba8b27a22c45ed713c4cb [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
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004096static void
4097xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4098
Daniel Veillard4255d502002-04-16 15:50:10 +00004099/**
4100 * xmlSchemaElementDump:
4101 * @elem: an element
4102 * @output: the file output
4103 *
4104 * Dump the element
4105 */
4106static void
4107xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004108 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004109 const xmlChar * namespace ATTRIBUTE_UNUSED,
4110 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004111{
4112 if (elem == NULL)
4113 return;
4114
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004115
4116 fprintf(output, "Element");
4117 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4118 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004119 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004120 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004121 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004122 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004123#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004124 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004125 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004126 if (elem->maxOccurs >= UNBOUNDED)
4127 fprintf(output, "max: unbounded\n");
4128 else if (elem->maxOccurs != 1)
4129 fprintf(output, "max: %d\n", elem->maxOccurs);
4130 else
4131 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004132 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004133#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004134 /*
4135 * Misc other properties.
4136 */
4137 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4138 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4139 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004140 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004141 fprintf(output, " props: ");
4142 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4143 fprintf(output, "[fixed] ");
4144 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4145 fprintf(output, "[default] ");
4146 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4147 fprintf(output, "[abstract] ");
4148 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4149 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004150 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004151 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004152 /*
4153 * Default/fixed value.
4154 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004155 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004156 fprintf(output, " value: '%s'\n", elem->value);
4157 /*
4158 * Type.
4159 */
4160 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004161 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004162 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004163 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004164 else
4165 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004166 } else if (elem->subtypes != NULL) {
4167 /*
4168 * Dump local types.
4169 */
4170 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004171 }
4172 /*
4173 * Substitution group.
4174 */
4175 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004176 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004177 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004178 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004179 else
4180 fprintf(output, "\n");
4181 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004182}
4183
4184/**
4185 * xmlSchemaAnnotDump:
4186 * @output: the file output
4187 * @annot: a annotation
4188 *
4189 * Dump the annotation
4190 */
4191static void
4192xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4193{
4194 xmlChar *content;
4195
4196 if (annot == NULL)
4197 return;
4198
4199 content = xmlNodeGetContent(annot->content);
4200 if (content != NULL) {
4201 fprintf(output, " Annot: %s\n", content);
4202 xmlFree(content);
4203 } else
4204 fprintf(output, " Annot: empty\n");
4205}
4206
4207/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004208 * xmlSchemaContentModelDump:
4209 * @particle: the schema particle
4210 * @output: the file output
4211 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004212 *
4213 * Dump a SchemaType structure
4214 */
4215static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004216xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4217{
4218 xmlChar *str = NULL;
4219 xmlSchemaTreeItemPtr term;
4220 char shift[100];
4221 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004222
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004223 if (particle == NULL)
4224 return;
4225 for (i = 0;((i < depth) && (i < 25));i++)
4226 shift[2 * i] = shift[2 * i + 1] = ' ';
4227 shift[2 * i] = shift[2 * i + 1] = 0;
4228 fprintf(output, shift);
4229 if (particle->children == NULL) {
4230 fprintf(output, "MISSING particle term\n");
4231 return;
4232 }
4233 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004234 if (term == NULL) {
4235 fprintf(output, "(NULL)");
4236 } else {
4237 switch (term->type) {
4238 case XML_SCHEMA_TYPE_ELEMENT:
4239 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4240 ((xmlSchemaElementPtr)term)->targetNamespace,
4241 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004242 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004243 break;
4244 case XML_SCHEMA_TYPE_SEQUENCE:
4245 fprintf(output, "SEQUENCE");
4246 break;
4247 case XML_SCHEMA_TYPE_CHOICE:
4248 fprintf(output, "CHOICE");
4249 break;
4250 case XML_SCHEMA_TYPE_ALL:
4251 fprintf(output, "ALL");
4252 break;
4253 case XML_SCHEMA_TYPE_ANY:
4254 fprintf(output, "ANY");
4255 break;
4256 default:
4257 fprintf(output, "UNKNOWN\n");
4258 return;
4259 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004260 }
4261 if (particle->minOccurs != 1)
4262 fprintf(output, " min: %d", particle->minOccurs);
4263 if (particle->maxOccurs >= UNBOUNDED)
4264 fprintf(output, " max: unbounded");
4265 else if (particle->maxOccurs != 1)
4266 fprintf(output, " max: %d", particle->maxOccurs);
4267 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004268 if (term &&
4269 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4270 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4271 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4272 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004273 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4274 output, depth +1);
4275 }
4276 if (particle->next != NULL)
4277 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4278 output, depth);
4279}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004280
4281/**
4282 * xmlSchemaAttrUsesDump:
4283 * @uses: attribute uses list
4284 * @output: the file output
4285 *
4286 * Dumps a list of attribute use components.
4287 */
4288static void
4289xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4290{
4291 xmlSchemaAttributeUsePtr use;
4292 xmlSchemaAttributeUseProhibPtr prohib;
4293 xmlSchemaQNameRefPtr ref;
4294 const xmlChar *name, *tns;
4295 xmlChar *str = NULL;
4296 int i;
4297
4298 if ((uses == NULL) || (uses->nbItems == 0))
4299 return;
4300
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004301 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004302 for (i = 0; i < uses->nbItems; i++) {
4303 use = uses->items[i];
4304 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4305 fprintf(output, " [prohibition] ");
4306 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4307 name = prohib->name;
4308 tns = prohib->targetNamespace;
4309 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4310 fprintf(output, " [reference] ");
4311 ref = (xmlSchemaQNameRefPtr) use;
4312 name = ref->name;
4313 tns = ref->targetNamespace;
4314 } else {
4315 fprintf(output, " [use] ");
4316 name = WXS_ATTRUSE_DECL_NAME(use);
4317 tns = WXS_ATTRUSE_DECL_TNS(use);
4318 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004319 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004320 (const char *) xmlSchemaFormatQName(&str, tns, name));
4321 FREE_AND_NULL(str);
4322 }
4323}
4324
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004325/**
4326 * xmlSchemaTypeDump:
4327 * @output: the file output
4328 * @type: a type structure
4329 *
4330 * Dump a SchemaType structure
4331 */
4332static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004333xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4334{
4335 if (type == NULL) {
4336 fprintf(output, "Type: NULL\n");
4337 return;
4338 }
4339 fprintf(output, "Type: ");
4340 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004341 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004342 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004343 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004344 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004345 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004346 switch (type->type) {
4347 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004348 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004349 break;
4350 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004351 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004352 break;
4353 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004354 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004355 break;
4356 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004357 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004358 break;
4359 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004360 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004361 break;
4362 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004363 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004364 break;
4365 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004366 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004367 break;
4368 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004369 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004370 break;
4371 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004372 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004373 break;
4374 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004375 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004376 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004377 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004378 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004379 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004380 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004381 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004382 break;
4383 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004384 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004385 break;
4386 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004387 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004388 break;
4389 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004390 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004391 break;
4392 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004393 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004394 break;
4395 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004396 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004397 break;
4398 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004399 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004400 break;
4401 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004402 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004403 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 }
4405 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004407 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004408 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004409 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004410 else
4411 fprintf(output, "\n");
4412 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004413 if (type->attrUses != NULL)
4414 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004415 if (type->annot != NULL)
4416 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004417#ifdef DUMP_CONTENT_MODEL
4418 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4419 (type->subtypes != NULL)) {
4420 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4421 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004422 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004423#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004424}
4425
4426/**
4427 * xmlSchemaDump:
4428 * @output: the file output
4429 * @schema: a schema structure
4430 *
4431 * Dump a Schema structure.
4432 */
4433void
4434xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4435{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004436 if (output == NULL)
4437 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004438 if (schema == NULL) {
4439 fprintf(output, "Schemas: NULL\n");
4440 return;
4441 }
4442 fprintf(output, "Schemas: ");
4443 if (schema->name != NULL)
4444 fprintf(output, "%s, ", schema->name);
4445 else
4446 fprintf(output, "no name, ");
4447 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004448 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004449 else
4450 fprintf(output, "no target namespace");
4451 fprintf(output, "\n");
4452 if (schema->annot != NULL)
4453 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004454 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4455 output);
4456 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004457 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004458}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004459
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004460#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004461/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004462 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004463 * @vctxt: the WXS validation context
4464 *
4465 * Displays the current IDC table for debug purposes.
4466 */
4467static void
4468xmlSchemaDebugDumpIDCTable(FILE * output,
4469 const xmlChar *namespaceName,
4470 const xmlChar *localName,
4471 xmlSchemaPSVIIDCBindingPtr bind)
4472{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004473 xmlChar *str = NULL;
4474 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004475 xmlSchemaPSVIIDCNodePtr tab;
4476 xmlSchemaPSVIIDCKeyPtr key;
4477 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004478
4479 fprintf(output, "IDC: TABLES on %s\n",
4480 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004481 FREE_AND_NULL(str)
4482
4483 if (bind == NULL)
4484 return;
4485 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004486 fprintf(output, "IDC: BINDING %s\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004487 xmlSchemaGetComponentQName(&str,
4488 bind->definition));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004489 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004490 for (i = 0; i < bind->nbNodes; i++) {
4491 tab = bind->nodeTable[i];
4492 fprintf(output, " ( ");
4493 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004494 key = tab->keys[j];
4495 if ((key != NULL) && (key->val != NULL)) {
4496 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004497 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004498 fprintf(output, "\"%s\" ", value);
4499 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004500 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004501 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004502 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004503 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004504 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004505 else
4506 fprintf(output, "(key missing), ");
4507 }
4508 fprintf(output, ")\n");
4509 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004510 if (bind->dupls && bind->dupls->nbItems) {
4511 fprintf(output, "IDC: dupls:\n");
4512 for (i = 0; i < bind->dupls->nbItems; i++) {
4513 tab = bind->dupls->items[i];
4514 fprintf(output, " ( ");
4515 for (j = 0; j < bind->definition->nbFields; j++) {
4516 key = tab->keys[j];
4517 if ((key != NULL) && (key->val != NULL)) {
4518 res = xmlSchemaGetCanonValue(key->val, &value);
4519 if (res >= 0)
4520 fprintf(output, "\"%s\" ", value);
4521 else
4522 fprintf(output, "CANON-VALUE-FAILED ");
4523 if (res == 0)
4524 FREE_AND_NULL(value)
4525 } else if (key != NULL)
4526 fprintf(output, "(no val), ");
4527 else
4528 fprintf(output, "(key missing), ");
4529 }
4530 fprintf(output, ")\n");
4531 }
4532 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004533 bind = bind->next;
4534 } while (bind != NULL);
4535}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004536#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004537#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004538
4539/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004540 * *
4541 * Utilities *
4542 * *
4543 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004544
Daniel Veillardc0826a72004-08-10 14:17:33 +00004545/**
4546 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004547 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004548 * @name: the name of the attribute
4549 *
4550 * Seeks an attribute with a name of @name in
4551 * no namespace.
4552 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004553 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004554 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004555static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004556xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004557{
4558 xmlAttrPtr prop;
4559
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004560 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004561 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004562 prop = node->properties;
4563 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004564 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004565 return(prop);
4566 prop = prop->next;
4567 }
4568 return (NULL);
4569}
4570
4571/**
4572 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004573 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004574 * @uri: the uri
4575 * @name: the name of the attribute
4576 *
4577 * Seeks an attribute with a local name of @name and
4578 * a namespace URI of @uri.
4579 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004580 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004581 */
4582static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004583xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004584{
4585 xmlAttrPtr prop;
4586
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004587 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004588 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004589 prop = node->properties;
4590 while (prop != NULL) {
4591 if ((prop->ns != NULL) &&
4592 xmlStrEqual(prop->name, BAD_CAST name) &&
4593 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004594 return(prop);
4595 prop = prop->next;
4596 }
4597 return (NULL);
4598}
4599
4600static const xmlChar *
4601xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4602{
4603 xmlChar *val;
4604 const xmlChar *ret;
4605
4606 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004607 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004608 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004609 ret = xmlDictLookup(ctxt->dict, val, -1);
4610 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004611 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004612}
4613
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004614static const xmlChar *
4615xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4616{
4617 return((const xmlChar*) xmlNodeGetContent(node));
4618}
4619
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004620/**
4621 * xmlSchemaGetProp:
4622 * @ctxt: the parser context
4623 * @node: the node
4624 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004625 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004626 * Read a attribute value and internalize the string
4627 *
4628 * Returns the string or NULL if not present.
4629 */
4630static const xmlChar *
4631xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4632 const char *name)
4633{
4634 xmlChar *val;
4635 const xmlChar *ret;
4636
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004637 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004638 if (val == NULL)
4639 return(NULL);
4640 ret = xmlDictLookup(ctxt->dict, val, -1);
4641 xmlFree(val);
4642 return(ret);
4643}
4644
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004645/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004646 * *
4647 * Parsing functions *
4648 * *
4649 ************************************************************************/
4650
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004651#define WXS_FIND_GLOBAL_ITEM(slot) \
4652 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4653 ret = xmlHashLookup(schema->slot, name); \
4654 if (ret != NULL) goto exit; \
4655 } \
4656 if (xmlHashSize(schema->schemasImports) > 1) { \
4657 xmlSchemaImportPtr import; \
4658 if (nsName == NULL) \
4659 import = xmlHashLookup(schema->schemasImports, \
4660 XML_SCHEMAS_NO_NAMESPACE); \
4661 else \
4662 import = xmlHashLookup(schema->schemasImports, nsName); \
4663 if (import == NULL) \
4664 goto exit; \
4665 ret = xmlHashLookup(import->schema->slot, name); \
4666 }
4667
Daniel Veillard4255d502002-04-16 15:50:10 +00004668/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004669 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004670 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004671 * @name: the element name
4672 * @ns: the element namespace
4673 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004674 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004675 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004676 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004677 */
4678static xmlSchemaElementPtr
4679xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004680 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004681{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004682 xmlSchemaElementPtr ret = NULL;
4683
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004684 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004685 return(NULL);
4686 if (schema != NULL) {
4687 WXS_FIND_GLOBAL_ITEM(elemDecl)
4688 }
4689exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004690#ifdef DEBUG
4691 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004692 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004693 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004694 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004695 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004696 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004697 }
4698#endif
4699 return (ret);
4700}
4701
4702/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004703 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004704 * @schema: the main schema
4705 * @name: the type's name
4706 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004707 *
4708 * Lookup a type in the schemas or the predefined types
4709 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004710 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004711 */
4712static xmlSchemaTypePtr
4713xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004714 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004715{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004716 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004717
4718 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004719 return (NULL);
4720 /* First try the built-in types. */
4721 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4722 ret = xmlSchemaGetPredefinedType(name, nsName);
4723 if (ret != NULL)
4724 goto exit;
4725 /*
4726 * Note that we try the parsed schemas as well here
4727 * since one might have parsed the S4S, which contain more
4728 * than the built-in types.
4729 * TODO: Can we optimize this?
4730 */
4731 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004732 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004733 WXS_FIND_GLOBAL_ITEM(typeDecl)
4734 }
4735exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004736
Daniel Veillard4255d502002-04-16 15:50:10 +00004737#ifdef DEBUG
4738 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004739 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004740 fprintf(stderr, "Unable to lookup type %s", name);
4741 else
4742 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004743 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004744 }
4745#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004746 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004747}
4748
Daniel Veillard3646d642004-06-02 19:19:14 +00004749/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004750 * xmlSchemaGetAttributeDecl:
4751 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004752 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004753 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004754 *
4755 * Lookup a an attribute in the schema or imported schemas
4756 *
4757 * Returns the attribute declaration or NULL if not found.
4758 */
4759static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004760xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004761 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004762{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004763 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004764
4765 if ((name == NULL) || (schema == NULL))
4766 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004767 if (schema != NULL) {
4768 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004769 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004770exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004771#ifdef DEBUG
4772 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004773 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004774 fprintf(stderr, "Unable to lookup attribute %s", name);
4775 else
4776 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004777 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004778 }
4779#endif
4780 return (ret);
4781}
4782
4783/**
4784 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004785 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004786 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004787 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004788 *
4789 * Lookup a an attribute group in the schema or imported schemas
4790 *
4791 * Returns the attribute group definition or NULL if not found.
4792 */
4793static xmlSchemaAttributeGroupPtr
4794xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004795 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004796{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004797 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004798
4799 if ((name == NULL) || (schema == NULL))
4800 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004801 if (schema != NULL) {
4802 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4803 }
4804exit:
4805 /* TODO:
4806 if ((ret != NULL) && (ret->redef != NULL)) {
4807 * Return the last redefinition. *
4808 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004809 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004810 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004811#ifdef DEBUG
4812 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004813 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004814 fprintf(stderr, "Unable to lookup attribute group %s", name);
4815 else
4816 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004817 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 }
4819#endif
4820 return (ret);
4821}
4822
4823/**
4824 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004825 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004826 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004827 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004828 *
4829 * Lookup a group in the schema or imported schemas
4830 *
4831 * Returns the group definition or NULL if not found.
4832 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004833static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004834xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004835 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004836{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004837 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004838
4839 if ((name == NULL) || (schema == NULL))
4840 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004841 if (schema != NULL) {
4842 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004843 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004844exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004845
Daniel Veillard3646d642004-06-02 19:19:14 +00004846#ifdef DEBUG
4847 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004848 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004849 fprintf(stderr, "Unable to lookup group %s", name);
4850 else
4851 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004852 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004853 }
4854#endif
4855 return (ret);
4856}
4857
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004858static xmlSchemaNotationPtr
4859xmlSchemaGetNotation(xmlSchemaPtr schema,
4860 const xmlChar *name,
4861 const xmlChar *nsName)
4862{
4863 xmlSchemaNotationPtr ret = NULL;
4864
4865 if ((name == NULL) || (schema == NULL))
4866 return (NULL);
4867 if (schema != NULL) {
4868 WXS_FIND_GLOBAL_ITEM(notaDecl)
4869 }
4870exit:
4871 return (ret);
4872}
4873
4874static xmlSchemaIDCPtr
4875xmlSchemaGetIDC(xmlSchemaPtr schema,
4876 const xmlChar *name,
4877 const xmlChar *nsName)
4878{
4879 xmlSchemaIDCPtr ret = NULL;
4880
4881 if ((name == NULL) || (schema == NULL))
4882 return (NULL);
4883 if (schema != NULL) {
4884 WXS_FIND_GLOBAL_ITEM(idcDef)
4885 }
4886exit:
4887 return (ret);
4888}
4889
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004890/**
4891 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004892 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004893 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004894 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004895 *
4896 * Lookup a group in the schema or imported schemas
4897 *
4898 * Returns the group definition or NULL if not found.
4899 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004900static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004901xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4902 xmlSchemaTypeType itemType,
4903 const xmlChar *name,
4904 const xmlChar *targetNs)
4905{
4906 switch (itemType) {
4907 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004908 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004909 name, targetNs));
4910 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004911 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004912 name, targetNs));
4913 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004914 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004915 return (NULL);
4916 }
4917}
4918
Daniel Veillard4255d502002-04-16 15:50:10 +00004919/************************************************************************
4920 * *
4921 * Parsing functions *
4922 * *
4923 ************************************************************************/
4924
4925#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004926 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004927
4928/**
4929 * xmlSchemaIsBlank:
4930 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004931 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004932 *
4933 * Check if a string is ignorable
4934 *
4935 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4936 */
4937static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004938xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004939{
Daniel Veillard4255d502002-04-16 15:50:10 +00004940 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004941 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004942 if (len < 0) {
4943 while (*str != 0) {
4944 if (!(IS_BLANK_CH(*str)))
4945 return (0);
4946 str++;
4947 }
4948 } else while ((*str != 0) && (len != 0)) {
4949 if (!(IS_BLANK_CH(*str)))
4950 return (0);
4951 str++;
4952 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004953 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004954
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004955 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004956}
4957
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004958#define WXS_COMP_NAME(c, t) ((t) (c))->name
4959#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4960/*
4961* xmlSchemaFindRedefCompInGraph:
4962* ATTENTION TODO: This uses pointer comp. for strings.
4963*/
4964static xmlSchemaBasicItemPtr
4965xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4966 xmlSchemaTypeType type,
4967 const xmlChar *name,
4968 const xmlChar *nsName)
4969{
4970 xmlSchemaBasicItemPtr ret;
4971 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004972
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004973 if ((bucket == NULL) || (name == NULL))
4974 return(NULL);
4975 if ((bucket->globals == NULL) ||
4976 (bucket->globals->nbItems == 0))
4977 goto subschemas;
4978 /*
4979 * Search in global components.
4980 */
4981 for (i = 0; i < bucket->globals->nbItems; i++) {
4982 ret = bucket->globals->items[i];
4983 if (ret->type == type) {
4984 switch (type) {
4985 case XML_SCHEMA_TYPE_COMPLEX:
4986 case XML_SCHEMA_TYPE_SIMPLE:
4987 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
4988 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
4989 nsName))
4990 {
4991 return(ret);
4992 }
4993 break;
4994 case XML_SCHEMA_TYPE_GROUP:
4995 if ((WXS_COMP_NAME(ret,
4996 xmlSchemaModelGroupDefPtr) == name) &&
4997 (WXS_COMP_TNS(ret,
4998 xmlSchemaModelGroupDefPtr) == nsName))
4999 {
5000 return(ret);
5001 }
5002 break;
5003 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5004 if ((WXS_COMP_NAME(ret,
5005 xmlSchemaAttributeGroupPtr) == name) &&
5006 (WXS_COMP_TNS(ret,
5007 xmlSchemaAttributeGroupPtr) == nsName))
5008 {
5009 return(ret);
5010 }
5011 default:
5012 /* Should not be hit. */
5013 return(NULL);
5014 }
5015 }
5016 }
5017subschemas:
5018 /*
5019 * Process imported/included schemas.
5020 */
5021 if (bucket->relations != NULL) {
5022 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5023
5024 /*
5025 * TODO: Marking the bucket will not avoid multiple searches
5026 * in the same schema, but avoids at least circularity.
5027 */
5028 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5029 do {
5030 if ((rel->bucket != NULL) &&
5031 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5032 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5033 type, name, nsName);
5034 if (ret != NULL)
5035 return(ret);
5036 }
5037 rel = rel->next;
5038 } while (rel != NULL);
5039 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5040 }
5041 return(NULL);
5042}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005043
5044/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005045 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005046 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005047 * @schema: the schema being built
5048 * @name: the item name
5049 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005050 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005051 * *WARNING* this interface is highly subject to change
5052 *
5053 * Returns the new struture or NULL in case of error
5054 */
5055static xmlSchemaNotationPtr
5056xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005057 const xmlChar *name, const xmlChar *nsName,
5058 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005059{
5060 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005061
5062 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5063 return (NULL);
5064
Daniel Veillard4255d502002-04-16 15:50:10 +00005065 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5066 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005067 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005068 return (NULL);
5069 }
5070 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005071 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005072 ret->name = name;
5073 ret->targetNamespace = nsName;
5074 /* TODO: do we need the node to be set?
5075 * ret->node = node;*/
5076 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005077 return (ret);
5078}
5079
Daniel Veillard4255d502002-04-16 15:50:10 +00005080/**
5081 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005082 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 * @schema: the schema being built
5084 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005085 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005086 *
5087 * Add an XML schema Attrribute declaration
5088 * *WARNING* this interface is highly subject to change
5089 *
5090 * Returns the new struture or NULL in case of error
5091 */
5092static xmlSchemaAttributePtr
5093xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005094 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005095 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005096{
5097 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005098
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005099 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005100 return (NULL);
5101
5102 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5103 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005104 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005105 return (NULL);
5106 }
5107 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005108 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5109 ret->node = node;
5110 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005111 ret->targetNamespace = nsName;
5112
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005113 if (topLevel)
5114 WXS_ADD_GLOBAL(ctxt, ret);
5115 else
5116 WXS_ADD_LOCAL(ctxt, ret);
5117 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 return (ret);
5119}
5120
5121/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005122 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005123 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005124 * @schema: the schema being built
5125 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005126 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005127 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005128 * Add an XML schema Attrribute declaration
5129 * *WARNING* this interface is highly subject to change
5130 *
5131 * Returns the new struture or NULL in case of error
5132 */
5133static xmlSchemaAttributeUsePtr
5134xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5135 xmlNodePtr node)
5136{
5137 xmlSchemaAttributeUsePtr ret = NULL;
5138
5139 if (pctxt == NULL)
5140 return (NULL);
5141
5142 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5143 if (ret == NULL) {
5144 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5145 return (NULL);
5146 }
5147 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5148 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5149 ret->node = node;
5150
5151 WXS_ADD_LOCAL(pctxt, ret);
5152 return (ret);
5153}
5154
5155/*
5156* xmlSchemaAddRedef:
5157*
5158* Adds a redefinition information. This is used at a later stage to:
5159* resolve references to the redefined components and to check constraints.
5160*/
5161static xmlSchemaRedefPtr
5162xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5163 xmlSchemaBucketPtr targetBucket,
5164 void *item,
5165 const xmlChar *refName,
5166 const xmlChar *refTargetNs)
5167{
5168 xmlSchemaRedefPtr ret;
5169
5170 ret = (xmlSchemaRedefPtr)
5171 xmlMalloc(sizeof(xmlSchemaRedef));
5172 if (ret == NULL) {
5173 xmlSchemaPErrMemory(pctxt,
5174 "allocating redefinition info", NULL);
5175 return (NULL);
5176 }
5177 memset(ret, 0, sizeof(xmlSchemaRedef));
5178 ret->item = item;
5179 ret->targetBucket = targetBucket;
5180 ret->refName = refName;
5181 ret->refTargetNs = refTargetNs;
5182 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5183 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5184 else
5185 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5186 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5187
5188 return (ret);
5189}
5190
5191/**
5192 * xmlSchemaAddAttributeGroupDefinition:
5193 * @ctxt: a schema parser context
5194 * @schema: the schema being built
5195 * @name: the item name
5196 * @nsName: the target namespace
5197 * @node: the corresponding node
5198 *
5199 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005200 *
5201 * Returns the new struture or NULL in case of error
5202 */
5203static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005204xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5205 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5206 const xmlChar *name,
5207 const xmlChar *nsName,
5208 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005209{
5210 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005211
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005212 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005213 return (NULL);
5214
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005215 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005216 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005217 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005218 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005219 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005220 }
5221 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005222 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005223 ret->name = name;
5224 ret->targetNamespace = nsName;
5225 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005226
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005227 /* TODO: Remove the flag. */
5228 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5229 if (pctxt->isRedefine) {
5230 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5231 ret, name, nsName);
5232 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005233 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005234 return(NULL);
5235 }
5236 pctxt->redefCounter = 0;
5237 }
5238 WXS_ADD_GLOBAL(pctxt, ret);
5239 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005240 return (ret);
5241}
5242
5243/**
5244 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005245 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005246 * @schema: the schema being built
5247 * @name: the type name
5248 * @namespace: the type namespace
5249 *
5250 * Add an XML schema Element declaration
5251 * *WARNING* this interface is highly subject to change
5252 *
5253 * Returns the new struture or NULL in case of error
5254 */
5255static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005256xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005257 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005258 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005259{
5260 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005261
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005262 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005263 return (NULL);
5264
Daniel Veillard4255d502002-04-16 15:50:10 +00005265 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5266 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005267 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005268 return (NULL);
5269 }
5270 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005271 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5272 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005273 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005274 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005275
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005276 if (topLevel)
5277 WXS_ADD_GLOBAL(ctxt, ret);
5278 else
5279 WXS_ADD_LOCAL(ctxt, ret);
5280 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005281 return (ret);
5282}
5283
5284/**
5285 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005286 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 * @schema: the schema being built
5288 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005289 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005290 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005291 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005292 * *WARNING* this interface is highly subject to change
5293 *
5294 * Returns the new struture or NULL in case of error
5295 */
5296static xmlSchemaTypePtr
5297xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005298 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005299 const xmlChar * name, const xmlChar * nsName,
5300 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005301{
5302 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005303
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005304 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005305 return (NULL);
5306
5307 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5308 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005309 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005310 return (NULL);
5311 }
5312 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005313 ret->type = type;
5314 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005315 ret->targetNamespace = nsName;
5316 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005317 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005318 if (ctxt->isRedefine) {
5319 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5320 ret, name, nsName);
5321 if (ctxt->redef == NULL) {
5322 xmlFree(ret);
5323 return(NULL);
5324 }
5325 ctxt->redefCounter = 0;
5326 }
5327 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005328 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005329 WXS_ADD_LOCAL(ctxt, ret);
5330 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005331 return (ret);
5332}
5333
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005334static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005335xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005336 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005337 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005338 const xmlChar *refNs)
5339{
5340 xmlSchemaQNameRefPtr ret;
5341
5342 ret = (xmlSchemaQNameRefPtr)
5343 xmlMalloc(sizeof(xmlSchemaQNameRef));
5344 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005345 xmlSchemaPErrMemory(pctxt,
5346 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005347 return (NULL);
5348 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005349 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005350 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5351 ret->name = refName;
5352 ret->targetNamespace = refNs;
5353 ret->item = NULL;
5354 ret->itemType = refType;
5355 /*
5356 * Store the reference item in the schema.
5357 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005358 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005359 return (ret);
5360}
5361
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005362static xmlSchemaAttributeUseProhibPtr
5363xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5364{
5365 xmlSchemaAttributeUseProhibPtr ret;
5366
5367 ret = (xmlSchemaAttributeUseProhibPtr)
5368 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5369 if (ret == NULL) {
5370 xmlSchemaPErrMemory(pctxt,
5371 "allocating attribute use prohibition", NULL);
5372 return (NULL);
5373 }
5374 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5375 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5376 WXS_ADD_LOCAL(pctxt, ret);
5377 return (ret);
5378}
5379
5380
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005381/**
5382 * xmlSchemaAddModelGroup:
5383 * @ctxt: a schema parser context
5384 * @schema: the schema being built
5385 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005386 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005387 *
5388 * Adds a schema model group
5389 * *WARNING* this interface is highly subject to change
5390 *
5391 * Returns the new struture or NULL in case of error
5392 */
5393static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005394xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5395 xmlSchemaPtr schema,
5396 xmlSchemaTypeType type,
5397 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005398{
5399 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005400
5401 if ((ctxt == NULL) || (schema == NULL))
5402 return (NULL);
5403
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005404 ret = (xmlSchemaModelGroupPtr)
5405 xmlMalloc(sizeof(xmlSchemaModelGroup));
5406 if (ret == NULL) {
5407 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5408 NULL);
5409 return (NULL);
5410 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005411 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005412 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005413 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005414 WXS_ADD_LOCAL(ctxt, ret);
5415 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5416 (type == XML_SCHEMA_TYPE_CHOICE))
5417 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005418 return (ret);
5419}
5420
5421
5422/**
5423 * xmlSchemaAddParticle:
5424 * @ctxt: a schema parser context
5425 * @schema: the schema being built
5426 * @node: the corresponding node in the schema doc
5427 * @min: the minOccurs
5428 * @max: the maxOccurs
5429 *
5430 * Adds an XML schema particle component.
5431 * *WARNING* this interface is highly subject to change
5432 *
5433 * Returns the new struture or NULL in case of error
5434 */
5435static xmlSchemaParticlePtr
5436xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5437 xmlNodePtr node, int min, int max)
5438{
5439 xmlSchemaParticlePtr ret = NULL;
5440 if ((ctxt == NULL) || (schema == NULL))
5441 return (NULL);
5442
5443#ifdef DEBUG
5444 fprintf(stderr, "Adding particle component\n");
5445#endif
5446 ret = (xmlSchemaParticlePtr)
5447 xmlMalloc(sizeof(xmlSchemaParticle));
5448 if (ret == NULL) {
5449 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5450 NULL);
5451 return (NULL);
5452 }
5453 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5454 ret->annot = NULL;
5455 ret->node = node;
5456 ret->minOccurs = min;
5457 ret->maxOccurs = max;
5458 ret->next = NULL;
5459 ret->children = NULL;
5460
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005461 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005462 /*
5463 * Note that addition to pending components will be done locally
5464 * to the specific parsing function, since the most particles
5465 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005466 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005467 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005468 return (ret);
5469}
5470
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005471/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005472 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005473 * @ctxt: a schema validation context
5474 * @schema: the schema being built
5475 * @name: the group name
5476 *
5477 * Add an XML schema Group definition
5478 *
5479 * Returns the new struture or NULL in case of error
5480 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005481static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005482xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5483 xmlSchemaPtr schema,
5484 const xmlChar *name,
5485 const xmlChar *nsName,
5486 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005487{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005488 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005489
5490 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5491 return (NULL);
5492
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005493 ret = (xmlSchemaModelGroupDefPtr)
5494 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005495 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005496 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005497 return (NULL);
5498 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005499 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005500 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005501 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005502 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005503 ret->targetNamespace = nsName;
5504
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005505 if (ctxt->isRedefine) {
5506 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5507 ret, name, nsName);
5508 if (ctxt->redef == NULL) {
5509 xmlFree(ret);
5510 return(NULL);
5511 }
5512 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005513 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005514 WXS_ADD_GLOBAL(ctxt, ret);
5515 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005516 return (ret);
5517}
5518
Daniel Veillard3646d642004-06-02 19:19:14 +00005519/**
5520 * xmlSchemaNewWildcardNs:
5521 * @ctxt: a schema validation context
5522 *
5523 * Creates a new wildcard namespace constraint.
5524 *
5525 * Returns the new struture or NULL in case of error
5526 */
5527static xmlSchemaWildcardNsPtr
5528xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5529{
5530 xmlSchemaWildcardNsPtr ret;
5531
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005532 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005533 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5534 if (ret == NULL) {
5535 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005536 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005537 }
5538 ret->value = NULL;
5539 ret->next = NULL;
5540 return (ret);
5541}
5542
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005543static xmlSchemaIDCPtr
5544xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5545 const xmlChar *name, const xmlChar *nsName,
5546 int category, xmlNodePtr node)
5547{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005548 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005549
5550 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5551 return (NULL);
5552
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005553 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5554 if (ret == NULL) {
5555 xmlSchemaPErrMemory(ctxt,
5556 "allocating an identity-constraint definition", NULL);
5557 return (NULL);
5558 }
5559 memset(ret, 0, sizeof(xmlSchemaIDC));
5560 /* The target namespace of the parent element declaration. */
5561 ret->targetNamespace = nsName;
5562 ret->name = name;
5563 ret->type = category;
5564 ret->node = node;
5565
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005566 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005567 /*
5568 * Only keyrefs need to be fixup up.
5569 */
5570 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005571 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005572 return (ret);
5573}
5574
Daniel Veillard3646d642004-06-02 19:19:14 +00005575/**
5576 * xmlSchemaAddWildcard:
5577 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005578 * @schema: a schema
5579 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005580 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005581 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005582 *
5583 * Returns the new struture or NULL in case of error
5584 */
5585static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005586xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5587 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005588{
5589 xmlSchemaWildcardPtr ret = NULL;
5590
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005591 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005592 return (NULL);
5593
5594 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5595 if (ret == NULL) {
5596 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5597 return (NULL);
5598 }
5599 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005600 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005601 ret->node = node;
5602 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005603 return (ret);
5604}
5605
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005606static void
5607xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5608{
5609 if (group == NULL)
5610 return;
5611 if (group->members != NULL)
5612 xmlSchemaItemListFree(group->members);
5613 xmlFree(group);
5614}
5615
5616static xmlSchemaSubstGroupPtr
5617xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5618 xmlSchemaElementPtr head)
5619{
5620 xmlSchemaSubstGroupPtr ret;
5621
5622 /* Init subst group hash. */
5623 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5624 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5625 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5626 return(NULL);
5627 }
5628 /* Create a new substitution group. */
5629 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5630 if (ret == NULL) {
5631 xmlSchemaPErrMemory(NULL,
5632 "allocating a substitution group container", NULL);
5633 return(NULL);
5634 }
5635 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5636 ret->head = head;
5637 /* Create list of members. */
5638 ret->members = xmlSchemaItemListCreate();
5639 if (ret->members == NULL) {
5640 xmlSchemaSubstGroupFree(ret);
5641 return(NULL);
5642 }
5643 /* Add subst group to hash. */
5644 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5645 head->name, head->targetNamespace, ret) != 0) {
5646 PERROR_INT("xmlSchemaSubstGroupAdd",
5647 "failed to add a new substitution container");
5648 xmlSchemaSubstGroupFree(ret);
5649 return(NULL);
5650 }
5651 return(ret);
5652}
5653
5654static xmlSchemaSubstGroupPtr
5655xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5656 xmlSchemaElementPtr head)
5657{
5658 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5659 return(NULL);
5660 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5661 head->name, head->targetNamespace));
5662
5663}
5664
5665/**
5666 * xmlSchemaAddElementSubstitutionMember:
5667 * @pctxt: a schema parser context
5668 * @head: the head of the substitution group
5669 * @member: the new member of the substitution group
5670 *
5671 * Allocate a new annotation structure.
5672 *
5673 * Returns the newly allocated structure or NULL in case or error
5674 */
5675static int
5676xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5677 xmlSchemaElementPtr head,
5678 xmlSchemaElementPtr member)
5679{
5680 xmlSchemaSubstGroupPtr substGroup = NULL;
5681
5682 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5683 return (-1);
5684
5685 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5686 if (substGroup == NULL)
5687 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5688 if (substGroup == NULL)
5689 return(-1);
5690 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5691 return(-1);
5692 return(0);
5693}
5694
Daniel Veillard4255d502002-04-16 15:50:10 +00005695/************************************************************************
5696 * *
5697 * Utilities for parsing *
5698 * *
5699 ************************************************************************/
5700
Daniel Veillard4255d502002-04-16 15:50:10 +00005701/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005702 * xmlSchemaPValAttrNodeQNameValue:
5703 * @ctxt: a schema parser context
5704 * @schema: the schema context
5705 * @ownerDes: the designation of the parent element
5706 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005707 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005708 * @local: the resulting local part if found, the attribute value otherwise
5709 * @uri: the resulting namespace URI if found
5710 *
5711 * Extracts the local name and the URI of a QName value and validates it.
5712 * This one is intended to be used on attribute values that
5713 * should resolve to schema components.
5714 *
5715 * Returns 0, in case the QName is valid, a positive error code
5716 * if not valid and -1 if an internal error occurs.
5717 */
5718static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005719xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005720 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005721 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005722 xmlAttrPtr attr,
5723 const xmlChar *value,
5724 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005725 const xmlChar **local)
5726{
5727 const xmlChar *pref;
5728 xmlNsPtr ns;
5729 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005730
Daniel Veillardc0826a72004-08-10 14:17:33 +00005731 *uri = NULL;
5732 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005733 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005734 if (ret > 0) {
5735 xmlSchemaPSimpleTypeErr(ctxt,
5736 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5737 ownerItem, (xmlNodePtr) attr,
5738 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5739 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005740 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005741 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005742 } else if (ret < 0)
5743 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005744
5745 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005746 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005747 if (ns)
5748 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5749 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005750 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5751 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005752 /*
5753 * This one takes care of included schemas with no
5754 * target namespace.
5755 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005756 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005757 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005758 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005759 return (0);
5760 }
5761 /*
5762 * At this point xmlSplitQName3 has to return a local name.
5763 */
5764 *local = xmlSplitQName3(value, &len);
5765 *local = xmlDictLookup(ctxt->dict, *local, -1);
5766 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005767 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5768 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005769 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005770 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005771 ownerItem, (xmlNodePtr) attr,
5772 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5773 "The value '%s' of simple type 'xs:QName' has no "
5774 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005775 return (ctxt->err);
5776 } else {
5777 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005778 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005779 return (0);
5780}
5781
5782/**
5783 * xmlSchemaPValAttrNodeQName:
5784 * @ctxt: a schema parser context
5785 * @schema: the schema context
5786 * @ownerDes: the designation of the owner element
5787 * @ownerItem: the owner as a schema object
5788 * @attr: the attribute node
5789 * @local: the resulting local part if found, the attribute value otherwise
5790 * @uri: the resulting namespace URI if found
5791 *
5792 * Extracts and validates the QName of an attribute value.
5793 * This one is intended to be used on attribute values that
5794 * should resolve to schema components.
5795 *
5796 * Returns 0, in case the QName is valid, a positive error code
5797 * if not valid and -1 if an internal error occurs.
5798 */
5799static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005800xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005801 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005802 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005803 xmlAttrPtr attr,
5804 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005805 const xmlChar **local)
5806{
5807 const xmlChar *value;
5808
5809 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005810 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005811 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005812}
5813
5814/**
5815 * xmlSchemaPValAttrQName:
5816 * @ctxt: a schema parser context
5817 * @schema: the schema context
5818 * @ownerDes: the designation of the parent element
5819 * @ownerItem: the owner as a schema object
5820 * @ownerElem: the parent node of the attribute
5821 * @name: the name of the attribute
5822 * @local: the resulting local part if found, the attribute value otherwise
5823 * @uri: the resulting namespace URI if found
5824 *
5825 * Extracts and validates the QName of an attribute value.
5826 *
5827 * Returns 0, in case the QName is valid, a positive error code
5828 * if not valid and -1 if an internal error occurs.
5829 */
5830static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005831xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5832 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005833 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005834 xmlNodePtr ownerElem,
5835 const char *name,
5836 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005837 const xmlChar **local)
5838{
5839 xmlAttrPtr attr;
5840
5841 attr = xmlSchemaGetPropNode(ownerElem, name);
5842 if (attr == NULL) {
5843 *local = NULL;
5844 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005845 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005846 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005847 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005848 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005849}
5850
5851/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005852 * xmlSchemaPValAttrID:
5853 * @ctxt: a schema parser context
5854 * @schema: the schema context
5855 * @ownerDes: the designation of the parent element
5856 * @ownerItem: the owner as a schema object
5857 * @ownerElem: the parent node of the attribute
5858 * @name: the name of the attribute
5859 *
5860 * Extracts and validates the ID of an attribute value.
5861 *
5862 * Returns 0, in case the ID is valid, a positive error code
5863 * if not valid and -1 if an internal error occurs.
5864 */
5865static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005866xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005867{
5868 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005869 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005870
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005871 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005872 return(0);
5873 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5874 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005875 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005876 /*
5877 * NOTE: the IDness might have already be declared in the DTD
5878 */
5879 if (attr->atype != XML_ATTRIBUTE_ID) {
5880 xmlIDPtr res;
5881 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005882
5883 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005884 * TODO: Use xmlSchemaStrip here; it's not exported at this
5885 * moment.
5886 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005887 strip = xmlSchemaCollapseString(value);
5888 if (strip != NULL) {
5889 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005890 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005891 }
5892 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005893 if (res == NULL) {
5894 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005895 xmlSchemaPSimpleTypeErr(ctxt,
5896 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005897 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005898 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5899 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005900 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005901 } else
5902 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005903 }
5904 } else if (ret > 0) {
5905 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005906 xmlSchemaPSimpleTypeErr(ctxt,
5907 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005908 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005909 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5910 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5911 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005912 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005913 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005914 if (value != NULL)
5915 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005916
5917 return (ret);
5918}
5919
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005920static int
5921xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5922 xmlNodePtr ownerElem,
5923 const xmlChar *name)
5924{
5925 xmlAttrPtr attr;
5926
5927 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5928 if (attr == NULL)
5929 return(0);
5930 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5931
5932}
5933
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005934/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005935 * xmlGetMaxOccurs:
5936 * @ctxt: a schema validation context
5937 * @node: a subtree containing XML Schema informations
5938 *
5939 * Get the maxOccurs property
5940 *
5941 * Returns the default if not found, or the value
5942 */
5943static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005944xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5945 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005946{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005947 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005948 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005949 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005950
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005951 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5952 if (attr == NULL)
5953 return (def);
5954 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005955
5956 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005957 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005958 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005959 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5960 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005961 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005962 val, NULL, NULL, NULL);
5963 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005964 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005965 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005966 }
5967
5968 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005969 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005970 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005971 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005972 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005973 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5974 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005975 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005976 val, NULL, NULL, NULL);
5977 return (def);
5978 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005979 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005980 ret = ret * 10 + (*cur - '0');
5981 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005982 }
William M. Brack76e95df2003-10-18 16:20:14 +00005983 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005984 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005985 /*
5986 * TODO: Restrict the maximal value to Integer.
5987 */
5988 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005989 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005990 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5991 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005992 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005993 val, NULL, NULL, NULL);
5994 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005995 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005996 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005997}
5998
5999/**
6000 * xmlGetMinOccurs:
6001 * @ctxt: a schema validation context
6002 * @node: a subtree containing XML Schema informations
6003 *
6004 * Get the minOccurs property
6005 *
6006 * Returns the default if not found, or the value
6007 */
6008static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006009xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006010 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006011{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006012 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006013 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006014 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006015
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006016 attr = xmlSchemaGetPropNode(node, "minOccurs");
6017 if (attr == NULL)
6018 return (def);
6019 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006020 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006021 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006022 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006023 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006024 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006025 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6026 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006027 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006028 val, NULL, NULL, NULL);
6029 return (def);
6030 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006031 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006032 ret = ret * 10 + (*cur - '0');
6033 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006034 }
William M. Brack76e95df2003-10-18 16:20:14 +00006035 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006036 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006037 /*
6038 * TODO: Restrict the maximal value to Integer.
6039 */
6040 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006041 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006042 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6043 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006044 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006045 val, NULL, NULL, NULL);
6046 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006047 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006048 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006049}
6050
6051/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006052 * xmlSchemaPGetBoolNodeValue:
6053 * @ctxt: a schema validation context
6054 * @ownerDes: owner designation
6055 * @ownerItem: the owner as a schema item
6056 * @node: the node holding the value
6057 *
6058 * Converts a boolean string value into 1 or 0.
6059 *
6060 * Returns 0 or 1.
6061 */
6062static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006063xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006064 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006065 xmlNodePtr node)
6066{
6067 xmlChar *value = NULL;
6068 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006069
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006070 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006071 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006072 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006073 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006074 * can have the following legal literals {true, false, 1, 0}.
6075 */
6076 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6077 res = 1;
6078 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6079 res = 0;
6080 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6081 res = 1;
6082 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006083 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006084 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006085 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006086 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006087 ownerItem, node,
6088 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6089 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006090 NULL, NULL, NULL);
6091 }
6092 if (value != NULL)
6093 xmlFree(value);
6094 return (res);
6095}
6096
6097/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006098 * xmlGetBooleanProp:
6099 * @ctxt: a schema validation context
6100 * @node: a subtree containing XML Schema informations
6101 * @name: the attribute name
6102 * @def: the default value
6103 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006104 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006105 *
6106 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006107 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006108 */
6109static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006110xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006111 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006112 const char *name, int def)
6113{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006114 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006115
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006116 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006117 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006118 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006119 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006120 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006121 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006122 * can have the following legal literals {true, false, 1, 0}.
6123 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006124 if (xmlStrEqual(val, BAD_CAST "true"))
6125 def = 1;
6126 else if (xmlStrEqual(val, BAD_CAST "false"))
6127 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006128 else if (xmlStrEqual(val, BAD_CAST "1"))
6129 def = 1;
6130 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006131 def = 0;
6132 else {
6133 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006134 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006135 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006136 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006137 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6138 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006139 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006140 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006141}
6142
6143/************************************************************************
6144 * *
6145 * Shema extraction from an Infoset *
6146 * *
6147 ************************************************************************/
6148static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6149 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006150 xmlNodePtr node,
6151 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006152static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6153 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006154 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006155 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006156 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006157static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6158 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006159 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006160 xmlNodePtr node,
6161 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006162static xmlSchemaBasicItemPtr
6163xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6164 xmlSchemaPtr schema,
6165 xmlNodePtr node,
6166 xmlSchemaItemListPtr uses,
6167 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006168static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6169 xmlSchemaPtr schema,
6170 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006171static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006172xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6173 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006174
6175/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006176 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006177 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006178 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006179 * @ownerDes: the designation of the parent element
6180 * @ownerItem: the schema object owner if existent
6181 * @attr: the schema attribute node being validated
6182 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006183 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006184 *
6185 * Validates a value against the given built-in type.
6186 * This one is intended to be used internally for validation
6187 * of schema attribute values during parsing of the schema.
6188 *
6189 * Returns 0 if the value is valid, a positive error code
6190 * number otherwise and -1 in case of an internal or API error.
6191 */
6192static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006193xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006194 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006195 xmlAttrPtr attr,
6196 const xmlChar *value,
6197 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006198{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006199
6200 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006201
6202 /*
6203 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6204 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006205 */
6206 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006207 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006208 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6209 PERROR_INT("xmlSchemaPValAttrNodeValue",
6210 "the given type is not a built-in type");
6211 return (-1);
6212 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006213 switch (type->builtInType) {
6214 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006215 case XML_SCHEMAS_QNAME:
6216 case XML_SCHEMAS_ANYURI:
6217 case XML_SCHEMAS_TOKEN:
6218 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006219 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6220 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006221 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006222 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006223 PERROR_INT("xmlSchemaPValAttrNodeValue",
6224 "validation using the given type is not supported");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006225 return (-1);
6226 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006227 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006228 /*
6229 * TODO: Should we use the S4S error codes instead?
6230 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006231 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006232 PERROR_INT("xmlSchemaPValAttrNodeValue",
6233 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006234 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006235 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006236 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006237 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6238 else
6239 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6240 xmlSchemaPSimpleTypeErr(pctxt,
6241 ret, ownerItem, (xmlNodePtr) attr,
6242 type, NULL, value, NULL, NULL, NULL);
6243 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006244 return (ret);
6245}
6246
6247/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006248 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006249 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006250 * @ctxt: a schema parser context
6251 * @ownerDes: the designation of the parent element
6252 * @ownerItem: the schema object owner if existent
6253 * @attr: the schema attribute node being validated
6254 * @type: the built-in type to be validated against
6255 * @value: the resulting value if any
6256 *
6257 * Extracts and validates a value against the given built-in type.
6258 * This one is intended to be used internally for validation
6259 * of schema attribute values during parsing of the schema.
6260 *
6261 * Returns 0 if the value is valid, a positive error code
6262 * number otherwise and -1 in case of an internal or API error.
6263 */
6264static int
6265xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006266 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006267 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006268 xmlSchemaTypePtr type,
6269 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006270{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006271 const xmlChar *val;
6272
6273 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006274 return (-1);
6275
Daniel Veillardc0826a72004-08-10 14:17:33 +00006276 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6277 if (value != NULL)
6278 *value = val;
6279
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006280 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006281 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006282}
6283
6284/**
6285 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006286 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006287 * @ctxt: a schema parser context
6288 * @node: the element node of the attribute
6289 * @ownerDes: the designation of the parent element
6290 * @ownerItem: the schema object owner if existent
6291 * @ownerElem: the owner element node
6292 * @name: the name of the schema attribute node
6293 * @type: the built-in type to be validated against
6294 * @value: the resulting value if any
6295 *
6296 * Extracts and validates a value against the given built-in type.
6297 * This one is intended to be used internally for validation
6298 * of schema attribute values during parsing of the schema.
6299 *
6300 * Returns 0 if the value is valid, a positive error code
6301 * number otherwise and -1 in case of an internal or API error.
6302 */
6303static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006304xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6305 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006306 xmlNodePtr ownerElem,
6307 const char *name,
6308 xmlSchemaTypePtr type,
6309 const xmlChar **value)
6310{
6311 xmlAttrPtr attr;
6312
6313 if ((ctxt == NULL) || (type == NULL)) {
6314 if (value != NULL)
6315 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006316 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006317 }
6318 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6319 if (value != NULL)
6320 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006321 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006322 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006323 "Internal error: xmlSchemaPValAttr, the given "
6324 "type '%s' is not a built-in type.\n",
6325 type->name, NULL);
6326 return (-1);
6327 }
6328 attr = xmlSchemaGetPropNode(ownerElem, name);
6329 if (attr == NULL) {
6330 if (value != NULL)
6331 *value = NULL;
6332 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006333 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006334 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006335 type, value));
6336}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006337
6338static int
6339xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006340 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006341 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006342 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006343 const xmlChar *namespaceName)
6344{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006345 /* TODO: Pointer comparison instead? */
6346 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006347 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006348 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006349 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006350 /*
6351 * Check if the referenced namespace was <import>ed.
6352 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006353 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006354 xmlSchemaSchemaRelationPtr rel;
6355
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006356 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006357 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006358 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006359 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006360 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006361 rel = rel->next;
6362 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006363 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006364 /*
6365 * No matching <import>ed namespace found.
6366 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006367 {
6368 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6369
6370 if (namespaceName == NULL)
6371 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6372 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6373 "References from this schema to components in no "
6374 "namespace are not allowed, since not indicated by an "
6375 "import statement", NULL, NULL);
6376 else
6377 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6378 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6379 "References from this schema to components in the "
6380 "namespace '%s' are not allowed, since not indicated by an "
6381 "import statement", namespaceName, NULL);
6382 }
6383 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006384}
6385
Daniel Veillardc0826a72004-08-10 14:17:33 +00006386/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006387 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006388 * @ctxt: a schema validation context
6389 * @schema: the schema being built
6390 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006391 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006392 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006393 * Parses attribute uses and attribute declarations and
6394 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006395 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006396static int
6397xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6398 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6399 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006400{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006401 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006402
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006403 while ((IS_SCHEMA((*child), "attribute")) ||
6404 (IS_SCHEMA((*child), "attributeGroup"))) {
6405 if (IS_SCHEMA((*child), "attribute")) {
6406 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6407 *list, parentType);
6408 } else {
6409 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6410 if ((item != NULL) && (hasRefs != NULL))
6411 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006412 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006413 if (item != NULL) {
6414 if (*list == NULL) {
6415 /* TODO: Customize grow factor. */
6416 *list = xmlSchemaItemListCreate();
6417 if (*list == NULL)
6418 return(-1);
6419 }
6420 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6421 return(-1);
6422 }
6423 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006424 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006425 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006426}
6427
6428/**
6429 * xmlSchemaParseAnnotation:
6430 * @ctxt: a schema validation context
6431 * @schema: the schema being built
6432 * @node: a subtree containing XML Schema informations
6433 *
6434 * parse a XML schema Attrribute declaration
6435 * *WARNING* this interface is highly subject to change
6436 *
William M. Bracke7091952004-05-11 15:09:58 +00006437 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006438 * 1 in case of success.
6439 */
6440static xmlSchemaAnnotPtr
6441xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006442 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006443{
6444 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006445 xmlNodePtr child = NULL;
6446 xmlAttrPtr attr;
6447 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006448
Daniel Veillardc0826a72004-08-10 14:17:33 +00006449 /*
6450 * INFO: S4S completed.
6451 */
6452 /*
6453 * id = ID
6454 * {any attributes with non-schema namespace . . .}>
6455 * Content: (appinfo | documentation)*
6456 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006457 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6458 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006459 if (needed)
6460 ret = xmlSchemaNewAnnot(ctxt, node);
6461 else
6462 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006463 attr = node->properties;
6464 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006465 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006466 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006467 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006468 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006469
6470 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006471 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006472 }
6473 attr = attr->next;
6474 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006475 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006476 /*
6477 * And now for the children...
6478 */
6479 child = node->children;
6480 while (child != NULL) {
6481 if (IS_SCHEMA(child, "appinfo")) {
6482 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006483 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006484 * source = anyURI
6485 * {any attributes with non-schema namespace . . .}>
6486 * Content: ({any})*
6487 */
6488 attr = child->properties;
6489 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006490 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006491 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006492 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006493 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006494
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006495 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006496 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006497 }
6498 attr = attr->next;
6499 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006500 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006501 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006502 child = child->next;
6503 } else if (IS_SCHEMA(child, "documentation")) {
6504 /* TODO: make available the content of "documentation". */
6505 /*
6506 * source = anyURI
6507 * {any attributes with non-schema namespace . . .}>
6508 * Content: ({any})*
6509 */
6510 attr = child->properties;
6511 while (attr != NULL) {
6512 if (attr->ns == NULL) {
6513 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006514 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006515 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006516 }
6517 } else {
6518 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6519 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6520 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006521
6522 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006523 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006524 }
6525 }
6526 attr = attr->next;
6527 }
6528 /*
6529 * Attribute "xml:lang".
6530 */
6531 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6532 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006533 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006534 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006535 child = child->next;
6536 } else {
6537 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006538 xmlSchemaPContentErr(ctxt,
6539 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006540 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006541 barked = 1;
6542 child = child->next;
6543 }
6544 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006545
Daniel Veillard4255d502002-04-16 15:50:10 +00006546 return (ret);
6547}
6548
6549/**
6550 * xmlSchemaParseFacet:
6551 * @ctxt: a schema validation context
6552 * @schema: the schema being built
6553 * @node: a subtree containing XML Schema informations
6554 *
6555 * parse a XML schema Facet declaration
6556 * *WARNING* this interface is highly subject to change
6557 *
6558 * Returns the new type structure or NULL in case of error
6559 */
6560static xmlSchemaFacetPtr
6561xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006562 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006563{
6564 xmlSchemaFacetPtr facet;
6565 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006566 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006567
6568 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6569 return (NULL);
6570
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006571 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006572 if (facet == NULL) {
6573 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6574 return (NULL);
6575 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006576 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006577 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006578 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006579 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6580 "Facet %s has no value\n", node->name, NULL);
6581 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006582 return (NULL);
6583 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006584 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006585 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006586 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006587 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006588 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006589 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006590 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006591 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006592 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006593 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006594 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006595 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006596 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006597 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006598 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006599 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006600 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006601 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006602 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006603 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006604 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006605 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6606 } else if (IS_SCHEMA(node, "minLength")) {
6607 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6608 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006609 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6610 "Unknown facet type %s\n", node->name, NULL);
6611 xmlSchemaFreeFacet(facet);
6612 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006613 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006614 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006615 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006616 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6617 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6618 const xmlChar *fixed;
6619
6620 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6621 if (fixed != NULL) {
6622 if (xmlStrEqual(fixed, BAD_CAST "true"))
6623 facet->fixed = 1;
6624 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006625 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 child = node->children;
6627
6628 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006629 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006630 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006631 }
6632 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006633 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6634 "Facet %s has unexpected child content\n",
6635 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006636 }
6637 return (facet);
6638}
6639
6640/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006641 * xmlSchemaParseWildcardNs:
6642 * @ctxt: a schema parser context
6643 * @wildc: the wildcard, already created
6644 * @node: a subtree containing XML Schema informations
6645 *
6646 * Parses the attribute "processContents" and "namespace"
6647 * of a xsd:anyAttribute and xsd:any.
6648 * *WARNING* this interface is highly subject to change
6649 *
6650 * Returns 0 if everything goes fine, a positive error code
6651 * if something is not valid and -1 if an internal error occurs.
6652 */
6653static int
6654xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006655 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006656 xmlSchemaWildcardPtr wildc,
6657 xmlNodePtr node)
6658{
6659 const xmlChar *pc, *ns, *dictnsItem;
6660 int ret = 0;
6661 xmlChar *nsItem;
6662 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6663 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006664
Daniel Veillardc0826a72004-08-10 14:17:33 +00006665 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6666 if ((pc == NULL)
6667 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6668 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6669 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6670 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6671 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6672 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6673 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006674 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006675 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006676 NULL, node,
6677 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006678 NULL, NULL, NULL);
6679 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006680 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006681 }
6682 /*
6683 * Build the namespace constraints.
6684 */
6685 attr = xmlSchemaGetPropNode(node, "namespace");
6686 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006687 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006688 wildc->any = 1;
6689 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6690 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006691 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006692 return (-1);
6693 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006694 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006695 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006696 const xmlChar *end, *cur;
6697
6698 cur = ns;
6699 do {
6700 while (IS_BLANK_CH(*cur))
6701 cur++;
6702 end = cur;
6703 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6704 end++;
6705 if (end == cur)
6706 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006707 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006708 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6709 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006710 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006711 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006712 NULL, (xmlNodePtr) attr,
6713 NULL,
6714 "((##any | ##other) | List of (xs:anyURI | "
6715 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006716 nsItem, NULL, NULL, NULL);
6717 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6718 } else {
6719 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006720 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006721 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6722 dictnsItem = NULL;
6723 } else {
6724 /*
6725 * Validate the item (anyURI).
6726 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006727 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006728 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6729 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6730 }
6731 /*
6732 * Avoid dublicate namespaces.
6733 */
6734 tmp = wildc->nsSet;
6735 while (tmp != NULL) {
6736 if (dictnsItem == tmp->value)
6737 break;
6738 tmp = tmp->next;
6739 }
6740 if (tmp == NULL) {
6741 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6742 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006743 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006744 return (-1);
6745 }
6746 tmp->value = dictnsItem;
6747 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006748 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006749 wildc->nsSet = tmp;
6750 else
6751 lastNs->next = tmp;
6752 lastNs = tmp;
6753 }
6754
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006755 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006756 xmlFree(nsItem);
6757 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006758 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006759 }
6760 return (ret);
6761}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006762
6763static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006764xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6765 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006766 xmlNodePtr node,
6767 int minOccurs,
6768 int maxOccurs) {
6769
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006770 if ((maxOccurs == 0) && ( minOccurs == 0))
6771 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006772 if (maxOccurs != UNBOUNDED) {
6773 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006774 * TODO: Maybe we should better not create the particle,
6775 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006776 * content model.
6777 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006778 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006779 * 3.9.6 Schema Component Constraint: Particle Correct
6780 *
6781 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006782 if (maxOccurs < 1) {
6783 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006784 * 2.2 {max occurs} must be greater than or equal to 1.
6785 */
6786 xmlSchemaPCustomAttrErr(ctxt,
6787 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006788 NULL, NULL,
6789 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006790 "The value must be greater than or equal to 1");
6791 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6792 } else if (minOccurs > maxOccurs) {
6793 /*
6794 * 2.1 {min occurs} must not be greater than {max occurs}.
6795 */
6796 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006797 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006798 NULL, NULL,
6799 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006800 "The value must not be greater than the value of 'maxOccurs'");
6801 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6802 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006803 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006804 return (0);
6805}
6806
Daniel Veillardc0826a72004-08-10 14:17:33 +00006807/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006808 * xmlSchemaParseAny:
6809 * @ctxt: a schema validation context
6810 * @schema: the schema being built
6811 * @node: a subtree containing XML Schema informations
6812 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006813 * Parsea a XML schema <any> element. A particle and wildcard
6814 * will be created (except if minOccurs==maxOccurs==0, in this case
6815 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006816 * *WARNING* this interface is highly subject to change
6817 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006818 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006819 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006820static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006821xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6822 xmlNodePtr node)
6823{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006824 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006825 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006826 xmlSchemaWildcardPtr wild;
6827 int min, max;
6828 xmlAttrPtr attr;
6829 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006830
6831 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6832 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006833 /*
6834 * Check for illegal attributes.
6835 */
6836 attr = node->properties;
6837 while (attr != NULL) {
6838 if (attr->ns == NULL) {
6839 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6840 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6841 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6842 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6843 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006844 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006845 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006846 }
6847 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006848 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006849 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006850 }
6851 attr = attr->next;
6852 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006853 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006854 /*
6855 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006856 */
6857 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6858 "(xs:nonNegativeInteger | unbounded)");
6859 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6860 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006861 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6862 /*
6863 * Create & parse the wildcard.
6864 */
6865 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6866 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006867 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006868 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006869 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006870 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006871 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006872 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006873 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006874 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006875 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006876 }
6877 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006878 xmlSchemaPContentErr(ctxt,
6879 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006880 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006881 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006882 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006883 /*
6884 * No component if minOccurs==maxOccurs==0.
6885 */
6886 if ((min == 0) && (max == 0)) {
6887 /* Don't free the wildcard, since it's already on the list. */
6888 return (NULL);
6889 }
6890 /*
6891 * Create the particle.
6892 */
6893 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6894 if (particle == NULL)
6895 return (NULL);
6896 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006897 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006898
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006899 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006900}
6901
6902/**
6903 * xmlSchemaParseNotation:
6904 * @ctxt: a schema validation context
6905 * @schema: the schema being built
6906 * @node: a subtree containing XML Schema informations
6907 *
6908 * parse a XML schema Notation declaration
6909 *
6910 * Returns the new structure or NULL in case of error
6911 */
6912static xmlSchemaNotationPtr
6913xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006914 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006915{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006916 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006917 xmlSchemaNotationPtr ret;
6918 xmlNodePtr child = NULL;
6919
6920 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6921 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006922 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006923 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006924 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6925 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006926 return (NULL);
6927 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006928 ret = xmlSchemaAddNotation(ctxt, schema, name,
6929 ctxt->targetNamespace, node);
6930 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006931 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006932 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006933
6934 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006935 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006936 child = child->next;
6937 }
6938
Daniel Veillard4255d502002-04-16 15:50:10 +00006939 child = node->children;
6940 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006941 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006942 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006943 }
6944 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006945 xmlSchemaPContentErr(ctxt,
6946 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006947 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006948 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006949 }
6950
6951 return (ret);
6952}
6953
6954/**
6955 * xmlSchemaParseAnyAttribute:
6956 * @ctxt: a schema validation context
6957 * @schema: the schema being built
6958 * @node: a subtree containing XML Schema informations
6959 *
6960 * parse a XML schema AnyAttrribute declaration
6961 * *WARNING* this interface is highly subject to change
6962 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006963 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006964 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006965static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006966xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6967 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006968{
Daniel Veillard3646d642004-06-02 19:19:14 +00006969 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006970 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006971 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006972
6973 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6974 return (NULL);
6975
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006976 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6977 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006978 if (ret == NULL) {
6979 return (NULL);
6980 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006981 /*
6982 * Check for illegal attributes.
6983 */
6984 attr = node->properties;
6985 while (attr != NULL) {
6986 if (attr->ns == NULL) {
6987 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6988 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6989 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006990 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006991 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006992 }
6993 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006994 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006995 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006996 }
6997 attr = attr->next;
6998 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006999 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007000 /*
7001 * Parse the namespace list.
7002 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007003 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007004 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007005 /*
7006 * And now for the children...
7007 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007008 child = node->children;
7009 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007010 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007011 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007012 }
7013 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007014 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007015 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007016 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007017 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007018 }
7019
7020 return (ret);
7021}
7022
7023
7024/**
7025 * xmlSchemaParseAttribute:
7026 * @ctxt: a schema validation context
7027 * @schema: the schema being built
7028 * @node: a subtree containing XML Schema informations
7029 *
7030 * parse a XML schema Attrribute declaration
7031 * *WARNING* this interface is highly subject to change
7032 *
William M. Bracke7091952004-05-11 15:09:58 +00007033 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007034 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007035static xmlSchemaBasicItemPtr
7036xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7037 xmlSchemaPtr schema,
7038 xmlNodePtr node,
7039 xmlSchemaItemListPtr uses,
7040 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007041{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007042 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7043 xmlSchemaAttributeUsePtr use = NULL;
7044 xmlNodePtr child = NULL;
7045 xmlAttrPtr attr;
7046 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7047 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7048 int nberrors, hasForm = 0, defValueType = 0;
7049
7050#define WXS_ATTR_DEF_VAL_DEFAULT 1
7051#define WXS_ATTR_DEF_VAL_FIXED 2
7052
7053 /*
7054 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7055 */
7056
7057 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7058 return (NULL);
7059 attr = xmlSchemaGetPropNode(node, "ref");
7060 if (attr != NULL) {
7061 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7062 NULL, attr, &tmpNs, &tmpName) != 0) {
7063 return (NULL);
7064 }
7065 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7066 return(NULL);
7067 isRef = 1;
7068 }
7069 nberrors = pctxt->nberrors;
7070 /*
7071 * Check for illegal attributes.
7072 */
7073 attr = node->properties;
7074 while (attr != NULL) {
7075 if (attr->ns == NULL) {
7076 if (isRef) {
7077 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7078 xmlSchemaPValAttrNodeID(pctxt, attr);
7079 goto attr_next;
7080 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7081 goto attr_next;
7082 }
7083 } else {
7084 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7085 goto attr_next;
7086 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7087 xmlSchemaPValAttrNodeID(pctxt, attr);
7088 goto attr_next;
7089 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7090 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7091 attr, &tmpNs, &tmpName);
7092 goto attr_next;
7093 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7094 /*
7095 * Evaluate the target namespace
7096 */
7097 hasForm = 1;
7098 attrValue = xmlSchemaGetNodeContent(pctxt,
7099 (xmlNodePtr) attr);
7100 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7101 ns = pctxt->targetNamespace;
7102 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7103 {
7104 xmlSchemaPSimpleTypeErr(pctxt,
7105 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7106 NULL, (xmlNodePtr) attr,
7107 NULL, "(qualified | unqualified)",
7108 attrValue, NULL, NULL, NULL);
7109 }
7110 goto attr_next;
7111 }
7112 }
7113 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7114
7115 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7116 /* TODO: Maybe we need to normalize the value beforehand. */
7117 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7118 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7119 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7120 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7121 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7122 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7123 else {
7124 xmlSchemaPSimpleTypeErr(pctxt,
7125 XML_SCHEMAP_INVALID_ATTR_USE,
7126 NULL, (xmlNodePtr) attr,
7127 NULL, "(optional | prohibited | required)",
7128 attrValue, NULL, NULL, NULL);
7129 }
7130 goto attr_next;
7131 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7132 /*
7133 * 3.2.3 : 1
7134 * default and fixed must not both be present.
7135 */
7136 if (defValue) {
7137 xmlSchemaPMutualExclAttrErr(pctxt,
7138 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7139 NULL, attr, "default", "fixed");
7140 } else {
7141 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7142 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7143 }
7144 goto attr_next;
7145 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7146 /*
7147 * 3.2.3 : 1
7148 * default and fixed must not both be present.
7149 */
7150 if (defValue) {
7151 xmlSchemaPMutualExclAttrErr(pctxt,
7152 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7153 NULL, attr, "default", "fixed");
7154 } else {
7155 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7156 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7157 }
7158 goto attr_next;
7159 }
7160 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7161 goto attr_next;
7162
7163 xmlSchemaPIllegalAttrErr(pctxt,
7164 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7165
7166attr_next:
7167 attr = attr->next;
7168 }
7169 /*
7170 * 3.2.3 : 2
7171 * If default and use are both present, use must have
7172 * the actual value optional.
7173 */
7174 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7175 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7176 xmlSchemaPSimpleTypeErr(pctxt,
7177 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7178 NULL, node, NULL,
7179 "(optional | prohibited | required)", NULL,
7180 "The value of the attribute 'use' must be 'optional' "
7181 "if the attribute 'default' is present",
7182 NULL, NULL);
7183 }
7184 /*
7185 * We want correct attributes.
7186 */
7187 if (nberrors != pctxt->nberrors)
7188 return(NULL);
7189 if (! isRef) {
7190 xmlSchemaAttributePtr attrDecl;
7191
7192 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7193 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7194 ns = pctxt->targetNamespace;
7195 /*
7196 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7197 * TODO: Move this to the component layer.
7198 */
7199 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7200 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7201 XML_SCHEMAP_NO_XSI,
7202 node, NULL,
7203 "The target namespace must not match '%s'",
7204 xmlSchemaInstanceNs, NULL);
7205 }
7206 attr = xmlSchemaGetPropNode(node, "name");
7207 if (attr == NULL) {
7208 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7209 NULL, node, "name", NULL);
7210 return (NULL);
7211 }
7212 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7213 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7214 return (NULL);
7215 }
7216 /*
7217 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7218 * TODO: Move this to the component layer.
7219 */
7220 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7221 xmlSchemaPSimpleTypeErr(pctxt,
7222 XML_SCHEMAP_NO_XMLNS,
7223 NULL, (xmlNodePtr) attr,
7224 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7225 "The value of the attribute must not match 'xmlns'",
7226 NULL, NULL);
7227 return (NULL);
7228 }
7229 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7230 goto check_children;
7231 /*
7232 * Create the attribute use component.
7233 */
7234 use = xmlSchemaAddAttributeUse(pctxt, node);
7235 if (use == NULL)
7236 return(NULL);
7237 use->occurs = occurs;
7238 /*
7239 * Create the attribute declaration.
7240 */
7241 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7242 if (attrDecl == NULL)
7243 return (NULL);
7244 if (tmpName != NULL) {
7245 attrDecl->typeName = tmpName;
7246 attrDecl->typeNs = tmpNs;
7247 }
7248 use->attrDecl = attrDecl;
7249 /*
7250 * Value constraint.
7251 */
7252 if (defValue != NULL) {
7253 attrDecl->defValue = defValue;
7254 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7255 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7256 }
7257 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7258 xmlSchemaQNameRefPtr ref;
7259
7260 /*
7261 * Create the attribute use component.
7262 */
7263 use = xmlSchemaAddAttributeUse(pctxt, node);
7264 if (use == NULL)
7265 return(NULL);
7266 /*
7267 * We need to resolve the reference at later stage.
7268 */
7269 WXS_ADD_PENDING(pctxt, use);
7270 use->occurs = occurs;
7271 /*
7272 * Create a QName reference to the attribute declaration.
7273 */
7274 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7275 tmpName, tmpNs);
7276 if (ref == NULL)
7277 return(NULL);
7278 /*
7279 * Assign the reference. This will be substituted for the
7280 * referenced attribute declaration when the QName is resolved.
7281 */
7282 use->attrDecl = WXS_ATTR_CAST ref;
7283 /*
7284 * Value constraint.
7285 */
7286 if (defValue != NULL)
7287 use->defValue = defValue;
7288 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7289 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7290 }
7291
7292check_children:
7293 /*
7294 * And now for the children...
7295 */
7296 child = node->children;
7297 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7298 xmlSchemaAttributeUseProhibPtr prohib;
7299
7300 if (IS_SCHEMA(child, "annotation")) {
7301 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7302 child = child->next;
7303 }
7304 if (child != NULL) {
7305 xmlSchemaPContentErr(pctxt,
7306 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7307 NULL, node, child, NULL,
7308 "(annotation?)");
7309 }
7310 /*
7311 * Check for pointlessness of attribute prohibitions.
7312 */
7313 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7314 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7315 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7316 node, NULL,
7317 "Skipping attribute use prohibition, since it is "
7318 "pointless inside an <attributeGroup>",
7319 NULL, NULL, NULL);
7320 return(NULL);
7321 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7322 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7323 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7324 node, NULL,
7325 "Skipping attribute use prohibition, since it is "
7326 "pointless when extending a type",
7327 NULL, NULL, NULL);
7328 return(NULL);
7329 }
7330 if (! isRef) {
7331 tmpName = name;
7332 tmpNs = ns;
7333 }
7334 /*
7335 * Check for duplicate attribute prohibitions.
7336 */
7337 if (uses) {
7338 int i;
7339
7340 for (i = 0; i < uses->nbItems; i++) {
7341 use = uses->items[i];
7342 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7343 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7344 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7345 {
7346 xmlChar *str = NULL;
7347
7348 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7349 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7350 node, NULL,
7351 "Skipping duplicate attribute use prohibition '%s'",
7352 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7353 NULL, NULL);
7354 FREE_AND_NULL(str)
7355 return(NULL);
7356 }
7357 }
7358 }
7359 /*
7360 * Create the attribute prohibition helper component.
7361 */
7362 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7363 if (prohib == NULL)
7364 return(NULL);
7365 prohib->node = node;
7366 prohib->name = tmpName;
7367 prohib->targetNamespace = tmpNs;
7368 if (isRef) {
7369 /*
7370 * We need at least to resolve to the attribute declaration.
7371 */
7372 WXS_ADD_PENDING(pctxt, prohib);
7373 }
7374 return(WXS_BASIC_CAST prohib);
7375 } else {
7376 if (IS_SCHEMA(child, "annotation")) {
7377 /*
7378 * TODO: Should this go into the attr decl?
7379 */
7380 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7381 child = child->next;
7382 }
7383 if (isRef) {
7384 if (child != NULL) {
7385 if (IS_SCHEMA(child, "simpleType"))
7386 /*
7387 * 3.2.3 : 3.2
7388 * If ref is present, then all of <simpleType>,
7389 * form and type must be absent.
7390 */
7391 xmlSchemaPContentErr(pctxt,
7392 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7393 NULL, node, child, NULL,
7394 "(annotation?)");
7395 else
7396 xmlSchemaPContentErr(pctxt,
7397 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7398 NULL, node, child, NULL,
7399 "(annotation?)");
7400 }
7401 } else {
7402 if (IS_SCHEMA(child, "simpleType")) {
7403 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7404 /*
7405 * 3.2.3 : 4
7406 * type and <simpleType> must not both be present.
7407 */
7408 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7409 NULL, node, child,
7410 "The attribute 'type' and the <simpleType> child "
7411 "are mutually exclusive", NULL);
7412 } else
7413 WXS_ATTRUSE_TYPEDEF(use) =
7414 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7415 child = child->next;
7416 }
7417 if (child != NULL)
7418 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7419 NULL, node, child, NULL,
7420 "(annotation?, simpleType?)");
7421 }
7422 }
7423 return (WXS_BASIC_CAST use);
7424}
7425
7426
7427static xmlSchemaAttributePtr
7428xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7429 xmlSchemaPtr schema,
7430 xmlNodePtr node)
7431{
7432 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007433 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007434 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007435 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007436
7437 /*
7438 * Note that the w3c spec assumes the schema to be validated with schema
7439 * for schemas beforehand.
7440 *
7441 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007442 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007443 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7444 return (NULL);
7445 /*
7446 * 3.2.3 : 3.1
7447 * One of ref or name must be present, but not both
7448 */
7449 attr = xmlSchemaGetPropNode(node, "name");
7450 if (attr == NULL) {
7451 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7452 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007453 return (NULL);
7454 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007455 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7456 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7457 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007458 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007459 /*
7460 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7461 * TODO: Move this to the component layer.
7462 */
7463 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7464 xmlSchemaPSimpleTypeErr(pctxt,
7465 XML_SCHEMAP_NO_XMLNS,
7466 NULL, (xmlNodePtr) attr,
7467 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7468 "The value of the attribute must not match 'xmlns'",
7469 NULL, NULL);
7470 return (NULL);
7471 }
7472 /*
7473 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7474 * TODO: Move this to the component layer.
7475 * Or better leave it here and add it to the component layer
7476 * if we have a schema construction API.
7477 */
7478 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7479 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7480 XML_SCHEMAP_NO_XSI, node, NULL,
7481 "The target namespace must not match '%s'",
7482 xmlSchemaInstanceNs, NULL);
7483 }
7484
7485 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7486 pctxt->targetNamespace, node, 1);
7487 if (ret == NULL)
7488 return (NULL);
7489 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7490
7491 /*
7492 * Check for illegal attributes.
7493 */
7494 attr = node->properties;
7495 while (attr != NULL) {
7496 if (attr->ns == NULL) {
7497 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7498 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7499 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7500 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7501 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7502 {
7503 xmlSchemaPIllegalAttrErr(pctxt,
7504 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7505 }
7506 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7507 xmlSchemaPIllegalAttrErr(pctxt,
7508 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7509 }
7510 attr = attr->next;
7511 }
7512 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7513 node, "type", &ret->typeNs, &ret->typeName);
7514
7515 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007516 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007517 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007518 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007519 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007520 if (ret->defValue != NULL)
7521 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007522 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007523 * Attribute "default".
7524 */
7525 attr = xmlSchemaGetPropNode(node, "default");
7526 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007527 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007528 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007529 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007530 */
7531 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007532 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7533 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007534 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007535 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007536 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007537 /*
7538 * And now for the children...
7539 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007540 child = node->children;
7541 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007542 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007543 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007544 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007545 if (IS_SCHEMA(child, "simpleType")) {
7546 if (ret->typeName != NULL) {
7547 /*
7548 * 3.2.3 : 4
7549 * type and <simpleType> must not both be present.
7550 */
7551 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7552 NULL, node, child,
7553 "The attribute 'type' and the <simpleType> child "
7554 "are mutually exclusive", NULL);
7555 } else
7556 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7557 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007558 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007559 if (child != NULL)
7560 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7561 NULL, node, child, NULL,
7562 "(annotation?, simpleType?)");
7563
Daniel Veillard4255d502002-04-16 15:50:10 +00007564 return (ret);
7565}
7566
7567/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007568 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007569 * @ctxt: a schema validation context
7570 * @schema: the schema being built
7571 * @node: a subtree containing XML Schema informations
7572 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007573 * Parse an attribute group definition reference.
7574 * Note that a reference to an attribute group does not
7575 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007576 * *WARNING* this interface is highly subject to change
7577 *
7578 * Returns the attribute group or NULL in case of error.
7579 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007580static xmlSchemaQNameRefPtr
7581xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7582 xmlSchemaPtr schema,
7583 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007584{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007585 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007586 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007587 xmlAttrPtr attr;
7588 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007589
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007590 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007591 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007592
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007593 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007594 if (attr == NULL) {
7595 xmlSchemaPMissingAttrErr(pctxt,
7596 XML_SCHEMAP_S4S_ATTR_MISSING,
7597 NULL, node, "ref", NULL);
7598 return (NULL);
7599 }
7600 xmlSchemaPValAttrNodeQName(pctxt, schema,
7601 NULL, attr, &refNs, &ref);
7602 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7603 return(NULL);
7604
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007605 /*
7606 * Check for illegal attributes.
7607 */
7608 attr = node->properties;
7609 while (attr != NULL) {
7610 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007611 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007612 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007613 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007614 xmlSchemaPIllegalAttrErr(pctxt,
7615 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007616 }
7617 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007618 xmlSchemaPIllegalAttrErr(pctxt,
7619 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007620 }
7621 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007622 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007623 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007624 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7625
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007626 /*
7627 * And now for the children...
7628 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007629 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007630 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007631 /*
7632 * TODO: We do not have a place to store the annotation, do we?
7633 */
7634 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007635 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007636 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007637 if (child != NULL) {
7638 xmlSchemaPContentErr(pctxt,
7639 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7640 NULL, node, child, NULL,
7641 "(annotation?)");
7642 }
7643
7644 /*
7645 * Handle attribute group redefinitions.
7646 */
7647 if (pctxt->isRedefine && pctxt->redef &&
7648 (pctxt->redef->item->type ==
7649 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7650 (ref == pctxt->redef->refName) &&
7651 (refNs == pctxt->redef->refTargetNs))
7652 {
7653 /*
7654 * SPEC src-redefine:
7655 * (7.1) "If it has an <attributeGroup> among its contents
7656 * the ·actual value· of whose ref [attribute] is the same
7657 * as the ·actual value· of its own name attribute plus
7658 * target namespace, then it must have exactly one such group."
7659 */
7660 if (pctxt->redefCounter != 0) {
7661 xmlChar *str = NULL;
7662
7663 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7664 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7665 "The redefining attribute group definition "
7666 "'%s' must not contain more than one "
7667 "reference to the redefined definition",
7668 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7669 FREE_AND_NULL(str);
7670 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007671 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007672 pctxt->redefCounter++;
7673 /*
7674 * URGENT TODO: How to ensure that the reference will not be
7675 * handled by the normal component resolution mechanism?
7676 */
7677 ret = xmlSchemaNewQNameRef(pctxt,
7678 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7679 if (ret == NULL)
7680 return(NULL);
7681 ret->node = node;
7682 pctxt->redef->reference = WXS_BASIC_CAST ret;
7683 } else {
7684 /*
7685 * Create a QName-reference helper component. We will substitute this
7686 * component for the attribute uses of the referenced attribute group
7687 * definition.
7688 */
7689 ret = xmlSchemaNewQNameRef(pctxt,
7690 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7691 if (ret == NULL)
7692 return(NULL);
7693 ret->node = node;
7694 /* Add to pending items, to be able to resolve the reference. */
7695 WXS_ADD_PENDING(pctxt, ret);
7696 }
7697 return (ret);
7698}
7699
7700/**
7701 * xmlSchemaParseAttributeGroupDefinition:
7702 * @pctxt: a schema validation context
7703 * @schema: the schema being built
7704 * @node: a subtree containing XML Schema informations
7705 *
7706 * parse a XML schema Attribute Group declaration
7707 * *WARNING* this interface is highly subject to change
7708 *
7709 * Returns the attribute group definition or NULL in case of error.
7710 */
7711static xmlSchemaAttributeGroupPtr
7712xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7713 xmlSchemaPtr schema,
7714 xmlNodePtr node)
7715{
7716 const xmlChar *name;
7717 xmlSchemaAttributeGroupPtr ret;
7718 xmlNodePtr child = NULL;
7719 xmlAttrPtr attr;
7720 int hasRefs = 0;
7721
7722 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7723 return (NULL);
7724
7725 attr = xmlSchemaGetPropNode(node, "name");
7726 if (attr == NULL) {
7727 xmlSchemaPMissingAttrErr(pctxt,
7728 XML_SCHEMAP_S4S_ATTR_MISSING,
7729 NULL, node, "name", NULL);
7730 return (NULL);
7731 }
7732 /*
7733 * The name is crucial, exit if invalid.
7734 */
7735 if (xmlSchemaPValAttrNode(pctxt,
7736 NULL, attr,
7737 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7738 return (NULL);
7739 }
7740 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7741 name, pctxt->targetNamespace, node);
7742 if (ret == NULL)
7743 return (NULL);
7744 /*
7745 * Check for illegal attributes.
7746 */
7747 attr = node->properties;
7748 while (attr != NULL) {
7749 if (attr->ns == NULL) {
7750 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7751 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7752 {
7753 xmlSchemaPIllegalAttrErr(pctxt,
7754 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7755 }
7756 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7757 xmlSchemaPIllegalAttrErr(pctxt,
7758 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7759 }
7760 attr = attr->next;
7761 }
7762 /* Attribute ID */
7763 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7764 /*
7765 * And now for the children...
7766 */
7767 child = node->children;
7768 if (IS_SCHEMA(child, "annotation")) {
7769 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7770 child = child->next;
7771 }
7772 /*
7773 * Parse contained attribute decls/refs.
7774 */
7775 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7776 (xmlSchemaItemListPtr *) &(ret->attrUses),
7777 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7778 return(NULL);
7779 if (hasRefs)
7780 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7781 /*
7782 * Parse the attribute wildcard.
7783 */
7784 if (IS_SCHEMA(child, "anyAttribute")) {
7785 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7786 schema, child);
7787 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007788 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007789 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007790 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007791 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007792 NULL, node, child, NULL,
7793 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007794 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007795 return (ret);
7796}
7797
7798/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007799 * xmlSchemaPValAttrFormDefault:
7800 * @value: the value
7801 * @flags: the flags to be modified
7802 * @flagQualified: the specific flag for "qualified"
7803 *
7804 * Returns 0 if the value is valid, 1 otherwise.
7805 */
7806static int
7807xmlSchemaPValAttrFormDefault(const xmlChar *value,
7808 int *flags,
7809 int flagQualified)
7810{
7811 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7812 if ((*flags & flagQualified) == 0)
7813 *flags |= flagQualified;
7814 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007815 return (1);
7816
William M. Brack2f2a6632004-08-20 23:09:47 +00007817 return (0);
7818}
7819
7820/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007821 * xmlSchemaPValAttrBlockFinal:
7822 * @value: the value
7823 * @flags: the flags to be modified
7824 * @flagAll: the specific flag for "#all"
7825 * @flagExtension: the specific flag for "extension"
7826 * @flagRestriction: the specific flag for "restriction"
7827 * @flagSubstitution: the specific flag for "substitution"
7828 * @flagList: the specific flag for "list"
7829 * @flagUnion: the specific flag for "union"
7830 *
7831 * Validates the value of the attribute "final" and "block". The value
7832 * is converted into the specified flag values and returned in @flags.
7833 *
7834 * Returns 0 if the value is valid, 1 otherwise.
7835 */
7836
7837static int
7838xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007839 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007840 int flagAll,
7841 int flagExtension,
7842 int flagRestriction,
7843 int flagSubstitution,
7844 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007845 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007846{
7847 int ret = 0;
7848
7849 /*
7850 * TODO: This does not check for dublicate entries.
7851 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007852 if ((flags == NULL) || (value == NULL))
7853 return (-1);
7854 if (value[0] == 0)
7855 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007856 if (xmlStrEqual(value, BAD_CAST "#all")) {
7857 if (flagAll != -1)
7858 *flags |= flagAll;
7859 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007860 if (flagExtension != -1)
7861 *flags |= flagExtension;
7862 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007863 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007864 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007865 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007866 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007867 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007868 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007869 *flags |= flagUnion;
7870 }
7871 } else {
7872 const xmlChar *end, *cur = value;
7873 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007874
Daniel Veillardc0826a72004-08-10 14:17:33 +00007875 do {
7876 while (IS_BLANK_CH(*cur))
7877 cur++;
7878 end = cur;
7879 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7880 end++;
7881 if (end == cur)
7882 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007883 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007884 if (xmlStrEqual(item, BAD_CAST "extension")) {
7885 if (flagExtension != -1) {
7886 if ((*flags & flagExtension) == 0)
7887 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007888 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007889 ret = 1;
7890 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7891 if (flagRestriction != -1) {
7892 if ((*flags & flagRestriction) == 0)
7893 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007894 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007895 ret = 1;
7896 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7897 if (flagSubstitution != -1) {
7898 if ((*flags & flagSubstitution) == 0)
7899 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007900 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 ret = 1;
7902 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7903 if (flagList != -1) {
7904 if ((*flags & flagList) == 0)
7905 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007906 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007907 ret = 1;
7908 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7909 if (flagUnion != -1) {
7910 if ((*flags & flagUnion) == 0)
7911 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007912 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007913 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007914 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007915 ret = 1;
7916 if (item != NULL)
7917 xmlFree(item);
7918 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007919 } while ((ret == 0) && (*cur != 0));
7920 }
7921
Daniel Veillardc0826a72004-08-10 14:17:33 +00007922 return (ret);
7923}
7924
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007925static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007926xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007927 xmlSchemaIDCPtr idc,
7928 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007929 xmlAttrPtr attr,
7930 int isField)
7931{
7932 xmlNodePtr node;
7933
7934 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007935 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007936 * Schema Component Constraint: Selector Value OK
7937 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007938 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007939 * in [XPath].
7940 */
7941 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007942 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007943 XML_SCHEMAP_INTERNAL,
7944 "Internal error: xmlSchemaCheckCSelectorXPath, "
7945 "the selector is not specified.\n", NULL, NULL);
7946 return (-1);
7947 }
7948 if (attr == NULL)
7949 node = idc->node;
7950 else
7951 node = (xmlNodePtr) attr;
7952 if (selector->xpath == NULL) {
7953 xmlSchemaPCustomErr(ctxt,
7954 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007955 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007956 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007957 "The XPath expression of the selector is not valid", NULL);
7958 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7959 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007960 const xmlChar **nsArray = NULL;
7961 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007962 /*
7963 * Compile the XPath expression.
7964 */
7965 /*
7966 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007967 * TODO: Call xmlPatterncompile with different options for selector/
7968 * field.
7969 */
7970 nsList = xmlGetNsList(attr->doc, attr->parent);
7971 /*
7972 * Build an array of prefixes and namespaces.
7973 */
7974 if (nsList != NULL) {
7975 int i, count = 0;
7976 xmlNsPtr ns;
7977
7978 for (i = 0; nsList[i] != NULL; i++)
7979 count++;
7980
7981 nsArray = (const xmlChar **) xmlMalloc(
7982 (count * 2 + 1) * sizeof(const xmlChar *));
7983 if (nsArray == NULL) {
7984 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
7985 NULL);
7986 return (-1);
7987 }
7988 for (i = 0; i < count; i++) {
7989 ns = nsList[i];
7990 nsArray[2 * i] = nsList[i]->href;
7991 nsArray[2 * i + 1] = nsList[i]->prefix;
7992 }
7993 nsArray[count * 2] = NULL;
7994 xmlFree(nsList);
7995 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007996 /*
7997 * TODO: Differentiate between "selector" and "field".
7998 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007999 if (isField)
8000 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008001 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008002 else
8003 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008004 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008005 if (nsArray != NULL)
8006 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008007
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008008 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008009 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008010 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008011 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008012 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008013 "The XPath expression '%s' could not be "
8014 "compiled", selector->xpath);
8015 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008016 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008017 }
8018 return (0);
8019}
8020
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008021#define ADD_ANNOTATION(annot) \
8022 xmlSchemaAnnotPtr cur = item->annot; \
8023 if (item->annot == NULL) { \
8024 item->annot = annot; \
8025 return (annot); \
8026 } \
8027 cur = item->annot; \
8028 if (cur->next != NULL) { \
8029 cur = cur->next; \
8030 } \
8031 cur->next = annot;
8032
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008033/**
8034 * xmlSchemaAssignAnnotation:
8035 * @item: the schema component
8036 * @annot: the annotation
8037 *
8038 * Adds the annotation to the given schema component.
8039 *
8040 * Returns the given annotaion.
8041 */
8042static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008043xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8044 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008045{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008046 if ((annItem == NULL) || (annot == NULL))
8047 return (NULL);
8048 switch (annItem->type) {
8049 case XML_SCHEMA_TYPE_ELEMENT: {
8050 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8051 ADD_ANNOTATION(annot)
8052 }
8053 break;
8054 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8055 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8056 ADD_ANNOTATION(annot)
8057 }
8058 break;
8059 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8060 case XML_SCHEMA_TYPE_ANY: {
8061 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8062 ADD_ANNOTATION(annot)
8063 }
8064 break;
8065 case XML_SCHEMA_TYPE_PARTICLE:
8066 case XML_SCHEMA_TYPE_IDC_KEY:
8067 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008068 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008069 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8070 ADD_ANNOTATION(annot)
8071 }
8072 break;
8073 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8074 xmlSchemaAttributeGroupPtr item =
8075 (xmlSchemaAttributeGroupPtr) annItem;
8076 ADD_ANNOTATION(annot)
8077 }
8078 break;
8079 case XML_SCHEMA_TYPE_NOTATION: {
8080 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8081 ADD_ANNOTATION(annot)
8082 }
8083 break;
8084 case XML_SCHEMA_FACET_MININCLUSIVE:
8085 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8086 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8087 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8088 case XML_SCHEMA_FACET_TOTALDIGITS:
8089 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8090 case XML_SCHEMA_FACET_PATTERN:
8091 case XML_SCHEMA_FACET_ENUMERATION:
8092 case XML_SCHEMA_FACET_WHITESPACE:
8093 case XML_SCHEMA_FACET_LENGTH:
8094 case XML_SCHEMA_FACET_MAXLENGTH:
8095 case XML_SCHEMA_FACET_MINLENGTH: {
8096 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8097 ADD_ANNOTATION(annot)
8098 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008099 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008100 case XML_SCHEMA_TYPE_SIMPLE:
8101 case XML_SCHEMA_TYPE_COMPLEX: {
8102 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8103 ADD_ANNOTATION(annot)
8104 }
8105 break;
8106 case XML_SCHEMA_TYPE_GROUP: {
8107 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8108 ADD_ANNOTATION(annot)
8109 }
8110 break;
8111 case XML_SCHEMA_TYPE_SEQUENCE:
8112 case XML_SCHEMA_TYPE_CHOICE:
8113 case XML_SCHEMA_TYPE_ALL: {
8114 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8115 ADD_ANNOTATION(annot)
8116 }
8117 break;
8118 default:
8119 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008120 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008121 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008122 "Internal error: xmlSchemaAddAnnotation, "
8123 "The item is not a annotated schema component", NULL);
8124 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008125 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008126 return (annot);
8127}
8128
8129/**
8130 * xmlSchemaParseIDCSelectorAndField:
8131 * @ctxt: a schema validation context
8132 * @schema: the schema being built
8133 * @node: a subtree containing XML Schema informations
8134 *
8135 * Parses a XML Schema identity-contraint definition's
8136 * <selector> and <field> elements.
8137 *
8138 * Returns the parsed identity-constraint definition.
8139 */
8140static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008141xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008142 xmlSchemaPtr schema,
8143 xmlSchemaIDCPtr idc,
8144 xmlNodePtr node,
8145 int isField)
8146{
8147 xmlSchemaIDCSelectPtr item;
8148 xmlNodePtr child = NULL;
8149 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008150
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008151 /*
8152 * Check for illegal attributes.
8153 */
8154 attr = node->properties;
8155 while (attr != NULL) {
8156 if (attr->ns == NULL) {
8157 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8158 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008159 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008160 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008161 }
8162 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008163 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008164 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008165 }
8166 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008167 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008168 /*
8169 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008170 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008171 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8172 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008173 xmlSchemaPErrMemory(ctxt,
8174 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008175 NULL);
8176 return (NULL);
8177 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008178 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008179 /*
8180 * Attribute "xpath" (mandatory).
8181 */
8182 attr = xmlSchemaGetPropNode(node, "xpath");
8183 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008184 xmlSchemaPMissingAttrErr(ctxt,
8185 XML_SCHEMAP_S4S_ATTR_MISSING,
8186 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008187 "name", NULL);
8188 } else {
8189 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8190 /*
8191 * URGENT TODO: "field"s have an other syntax than "selector"s.
8192 */
8193
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008194 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8195 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196 xmlSchemaPErr(ctxt,
8197 (xmlNodePtr) attr,
8198 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008199 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008200 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008201 NULL, NULL);
8202 }
8203
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008204 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008205 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008206 /*
8207 * And now for the children...
8208 */
8209 child = node->children;
8210 if (IS_SCHEMA(child, "annotation")) {
8211 /*
8212 * Add the annotation to the parent IDC.
8213 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008214 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008215 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008216 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008217 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008218 if (child != NULL) {
8219 xmlSchemaPContentErr(ctxt,
8220 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008221 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008222 NULL, "(annotation?)");
8223 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008224
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008225 return (item);
8226}
8227
8228/**
8229 * xmlSchemaParseIDC:
8230 * @ctxt: a schema validation context
8231 * @schema: the schema being built
8232 * @node: a subtree containing XML Schema informations
8233 *
8234 * Parses a XML Schema identity-contraint definition.
8235 *
8236 * Returns the parsed identity-constraint definition.
8237 */
8238static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008239xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008240 xmlSchemaPtr schema,
8241 xmlNodePtr node,
8242 xmlSchemaTypeType idcCategory,
8243 const xmlChar *targetNamespace)
8244{
8245 xmlSchemaIDCPtr item = NULL;
8246 xmlNodePtr child = NULL;
8247 xmlAttrPtr attr;
8248 const xmlChar *name = NULL;
8249 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008250
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008251 /*
8252 * Check for illegal attributes.
8253 */
8254 attr = node->properties;
8255 while (attr != NULL) {
8256 if (attr->ns == NULL) {
8257 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8258 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8259 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8260 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008261 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008262 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008263 }
8264 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008265 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008266 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008267 }
8268 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008269 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008270 /*
8271 * Attribute "name" (mandatory).
8272 */
8273 attr = xmlSchemaGetPropNode(node, "name");
8274 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008275 xmlSchemaPMissingAttrErr(ctxt,
8276 XML_SCHEMAP_S4S_ATTR_MISSING,
8277 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008278 "name", NULL);
8279 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008280 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008281 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008282 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8283 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008284 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008285 /* Create the component. */
8286 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8287 idcCategory, node);
8288 if (item == NULL)
8289 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008290
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008291 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008292 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8293 /*
8294 * Attribute "refer" (mandatory).
8295 */
8296 attr = xmlSchemaGetPropNode(node, "refer");
8297 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008298 xmlSchemaPMissingAttrErr(ctxt,
8299 XML_SCHEMAP_S4S_ATTR_MISSING,
8300 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008301 "refer", NULL);
8302 } else {
8303 /*
8304 * Create a reference item.
8305 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008306 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008307 NULL, NULL);
8308 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008309 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008310 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008311 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008312 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008313 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008314 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008315 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008316 }
8317 }
8318 /*
8319 * And now for the children...
8320 */
8321 child = node->children;
8322 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008323 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008324 child = child->next;
8325 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008326 if (child == NULL) {
8327 xmlSchemaPContentErr(ctxt,
8328 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008329 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008330 "A child element is missing",
8331 "(annotation?, (selector, field+))");
8332 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008333 /*
8334 * Child element <selector>.
8335 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008336 if (IS_SCHEMA(child, "selector")) {
8337 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008338 item, child, 0);
8339 child = child->next;
8340 /*
8341 * Child elements <field>.
8342 */
8343 if (IS_SCHEMA(child, "field")) {
8344 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008345 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008346 item, child, 1);
8347 if (field != NULL) {
8348 field->index = item->nbFields;
8349 item->nbFields++;
8350 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008351 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008352 else
8353 item->fields = field;
8354 lastField = field;
8355 }
8356 child = child->next;
8357 } while (IS_SCHEMA(child, "field"));
8358 } else {
8359 xmlSchemaPContentErr(ctxt,
8360 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008361 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008362 NULL, "(annotation?, (selector, field+))");
8363 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008364 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008365 if (child != NULL) {
8366 xmlSchemaPContentErr(ctxt,
8367 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008368 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008369 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008370 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008371
8372 return (item);
8373}
8374
Daniel Veillardc0826a72004-08-10 14:17:33 +00008375/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008376 * xmlSchemaParseElement:
8377 * @ctxt: a schema validation context
8378 * @schema: the schema being built
8379 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008380 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008381 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008382 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008383 * *WARNING* this interface is highly subject to change
8384 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008385 * Returns the element declaration or a particle; NULL in case
8386 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008387 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008388static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008389xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008390 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008391{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008392 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008393 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008394 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008395 xmlNodePtr child = NULL;
8396 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008397 int min, max, isRef = 0;
8398 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008399
8400 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8401 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008402
Daniel Veillard4255d502002-04-16 15:50:10 +00008403 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008404 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008405
8406 if (isElemRef != NULL)
8407 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008408 /*
8409 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008410 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008411 * robust.
8412 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008413 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008414 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008415 if ((topLevel) || (attr == NULL)) {
8416 if (nameAttr == NULL) {
8417 xmlSchemaPMissingAttrErr(ctxt,
8418 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008419 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008420 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008421 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008422 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008423 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008424
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008425 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008426 child = node->children;
8427 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008428 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008429 child = child->next;
8430 }
8431 /*
8432 * Skip particle part if a global declaration.
8433 */
8434 if (topLevel)
8435 goto declaration_part;
8436 /*
8437 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008438 */
8439 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8440 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8441 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008442 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8443 if (particle == NULL)
8444 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008445
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008446 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8447
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008448 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008449 const xmlChar *refNs = NULL, *ref = NULL;
8450 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008451 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008452 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008453 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008454 if (isElemRef != NULL)
8455 *isElemRef = 1;
8456
Daniel Veillardc0826a72004-08-10 14:17:33 +00008457 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008458 NULL, attr, &refNs, &ref);
8459 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008460 /*
8461 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008462 */
8463 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008464 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008465 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008466 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008467 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008468 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008469 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008470 attr = node->properties;
8471 while (attr != NULL) {
8472 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008473 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8474 xmlStrEqual(attr->name, BAD_CAST "name") ||
8475 xmlStrEqual(attr->name, BAD_CAST "id") ||
8476 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8477 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8478 {
8479 attr = attr->next;
8480 continue;
8481 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008482 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008483 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008484 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008485 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008486 "Only the attributes 'minOccurs', 'maxOccurs' and "
8487 "'id' are allowed in addition to 'ref'");
8488 break;
8489 }
8490 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8491 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008492 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008493 }
8494 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008495 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008496 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008497 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008498 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008499 if (child != NULL) {
8500 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008501 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008502 }
8503 if ((min == 0) && (max == 0))
8504 goto return_null;
8505 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008506 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008507 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008508 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008509 ref, refNs);
8510 if (refer == NULL)
8511 goto return_null;
8512 particle->children = (xmlSchemaTreeItemPtr) refer;
8513 particle->annot = annot;
8514 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008515 * Add the particle to pending components, since the reference
8516 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008517 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008518 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008519 return ((xmlSchemaBasicItemPtr) particle);
8520 }
8521 /*
8522 * The declaration part ===============================================
8523 */
8524declaration_part:
8525 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008526 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008527 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8528
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008529 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008530 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008531 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008532 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008533 * Evaluate the target namespace.
8534 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008535 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008536 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008537 } else {
8538 attr = xmlSchemaGetPropNode(node, "form");
8539 if (attr != NULL) {
8540 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8541 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008542 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008543 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008544 xmlSchemaPSimpleTypeErr(ctxt,
8545 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8546 NULL, (xmlNodePtr) attr,
8547 NULL, "(qualified | unqualified)",
8548 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008549 }
8550 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008551 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008552 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008553 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008554 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008555 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008556 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008557 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008558 * Check for illegal attributes.
8559 */
William M. Bracke7091952004-05-11 15:09:58 +00008560 attr = node->properties;
8561 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008562 if (attr->ns == NULL) {
8563 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8564 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008565 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008566 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008567 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008568 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008569 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8570 {
8571 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008572 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008573 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008574 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008575 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008576 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008577 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008578 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008579 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8580 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008581 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8582
8583 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008584 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008585 }
8586 }
8587 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008588
Daniel Veillardc0826a72004-08-10 14:17:33 +00008589 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008590 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008591 }
8592 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008593 }
William M. Bracke7091952004-05-11 15:09:58 +00008594 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008595 * Extract/validate attributes.
8596 */
8597 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008599 * Process top attributes of global element declarations here.
8600 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008601 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8602 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008603 xmlSchemaPValAttrQName(ctxt, schema,
8604 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008605 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008606 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008607 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008608 /*
8609 * Attribute "final".
8610 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008611 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008612 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008613 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8614 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8615 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8616 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008617 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008618 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8619 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008620 -1,
8621 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8622 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008623 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008624 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008625 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008626 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008627 attrValue, NULL, NULL, NULL);
8628 }
8629 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008630 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008631 /*
8632 * Attribute "block".
8633 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008634 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008635 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008636 /*
8637 * Apply default "block" values.
8638 */
8639 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8640 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8641 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8642 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8643 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8644 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008646 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8647 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008648 -1,
8649 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008650 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008651 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8652 xmlSchemaPSimpleTypeErr(ctxt,
8653 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008654 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008655 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008656 "restriction | substitution))", attrValue,
8657 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008658 }
8659 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008660 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008661 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008662
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008663 attr = xmlSchemaGetPropNode(node, "type");
8664 if (attr != NULL) {
8665 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008666 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008667 &(decl->namedTypeNs), &(decl->namedType));
8668 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008669 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008670 }
8671 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8672 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008673 if (attr != NULL) {
8674 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008675 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008676 /*
8677 * 3.3.3 : 1
8678 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008679 */
8680 xmlSchemaPMutualExclAttrErr(ctxt,
8681 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008682 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008683 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008684 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8685 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008686 }
William M. Bracke7091952004-05-11 15:09:58 +00008687 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008688 /*
8689 * And now for the children...
8690 */
William M. Bracke7091952004-05-11 15:09:58 +00008691 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008692 /*
8693 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008694 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008695 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008696 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008697 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008698 xmlSchemaPContentErr(ctxt,
8699 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008700 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008701 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008702 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008703 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008704 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008705 child = child->next;
8706 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 /*
8708 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008709 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008710 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008711 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008712 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008713 xmlSchemaPContentErr(ctxt,
8714 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008715 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008716 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008717 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008718 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008719 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008720 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008721 }
William M. Bracke7091952004-05-11 15:09:58 +00008722 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008723 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008724 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008725 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008726 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008727 } else if (IS_SCHEMA(child, "key")) {
8728 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008729 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008730 } else if (IS_SCHEMA(child, "keyref")) {
8731 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008732 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008733 }
8734 if (lastIDC != NULL)
8735 lastIDC->next = curIDC;
8736 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008737 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008738 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008739 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008740 }
8741 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008742 xmlSchemaPContentErr(ctxt,
8743 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008744 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008745 NULL, "(annotation?, ((simpleType | complexType)?, "
8746 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008747 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008748 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008749 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008750 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008751 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008752 * different layer.
8753 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008754 FREE_AND_NULL(des)
8755 if (topLevel)
8756 return ((xmlSchemaBasicItemPtr) decl);
8757 else {
8758 particle->children = (xmlSchemaTreeItemPtr) decl;
8759 return ((xmlSchemaBasicItemPtr) particle);
8760 }
8761
8762return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008763 FREE_AND_NULL(des);
8764 if (annot != NULL) {
8765 if (particle != NULL)
8766 particle->annot = NULL;
8767 if (decl != NULL)
8768 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008769 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008770 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008771 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008772}
8773
8774/**
8775 * xmlSchemaParseUnion:
8776 * @ctxt: a schema validation context
8777 * @schema: the schema being built
8778 * @node: a subtree containing XML Schema informations
8779 *
8780 * parse a XML schema Union definition
8781 * *WARNING* this interface is highly subject to change
8782 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008783 * Returns -1 in case of internal error, 0 in case of success and a positive
8784 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008785 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008786static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008787xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008788 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008789{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008790 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008791 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008792 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008793 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008794
8795 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008796 return (-1);
8797 /* Not a component, don't create it. */
8798 type = ctxt->ctxtType;
8799 /*
8800 * Mark the simple type as being of variety "union".
8801 */
8802 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008803 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008804 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8805 * then the ·simple ur-type definition·."
8806 */
8807 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008808 /*
8809 * Check for illegal attributes.
8810 */
8811 attr = node->properties;
8812 while (attr != NULL) {
8813 if (attr->ns == NULL) {
8814 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8815 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008816 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008817 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008818 }
8819 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008820 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008821 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008822 }
8823 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008824 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008825 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008826 /*
8827 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008828 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008829 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008830 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008831 if (attr != NULL) {
8832 const xmlChar *end;
8833 xmlChar *tmp;
8834 const xmlChar *localName, *nsName;
8835 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8836 xmlSchemaQNameRefPtr ref;
8837
8838 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008839 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008840 do {
8841 while (IS_BLANK_CH(*cur))
8842 cur++;
8843 end = cur;
8844 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8845 end++;
8846 if (end == cur)
8847 break;
8848 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008849 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008850 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008851 /*
8852 * Create the member type link.
8853 */
8854 link = (xmlSchemaTypeLinkPtr)
8855 xmlMalloc(sizeof(xmlSchemaTypeLink));
8856 if (link == NULL) {
8857 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8858 "allocating a type link", NULL);
8859 return (-1);
8860 }
8861 link->type = NULL;
8862 link->next = NULL;
8863 if (lastLink == NULL)
8864 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008865 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008866 lastLink->next = link;
8867 lastLink = link;
8868 /*
8869 * Create a reference item.
8870 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008871 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008872 localName, nsName);
8873 if (ref == NULL) {
8874 FREE_AND_NULL(tmp)
8875 return (-1);
8876 }
8877 /*
8878 * Assign the reference to the link, it will be resolved
8879 * later during fixup of the union simple type.
8880 */
8881 link->type = (xmlSchemaTypePtr) ref;
8882 }
8883 FREE_AND_NULL(tmp)
8884 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008885 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008886
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008887 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008888 /*
8889 * And now for the children...
8890 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008891 child = node->children;
8892 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008893 /*
8894 * Add the annotation to the simple type ancestor.
8895 */
8896 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008897 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008898 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008899 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008900 if (IS_SCHEMA(child, "simpleType")) {
8901 xmlSchemaTypePtr subtype, last = NULL;
8902
8903 /*
8904 * Anchor the member types in the "subtypes" field of the
8905 * simple type.
8906 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008907 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008908 subtype = (xmlSchemaTypePtr)
8909 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8910 if (subtype != NULL) {
8911 if (last == NULL) {
8912 type->subtypes = subtype;
8913 last = subtype;
8914 } else {
8915 last->next = subtype;
8916 last = subtype;
8917 }
8918 last->next = NULL;
8919 }
8920 child = child->next;
8921 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008922 }
8923 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008924 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008925 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008926 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008927 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008928 if ((attr == NULL) && (type->subtypes == NULL)) {
8929 /*
8930 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008931 * Either the memberTypes [attribute] of the <union> element must
8932 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008933 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008934 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008935 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008936 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008937 "Either the attribute 'memberTypes' or "
8938 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008939 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008940 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008941}
8942
8943/**
8944 * xmlSchemaParseList:
8945 * @ctxt: a schema validation context
8946 * @schema: the schema being built
8947 * @node: a subtree containing XML Schema informations
8948 *
8949 * parse a XML schema List definition
8950 * *WARNING* this interface is highly subject to change
8951 *
William M. Bracke7091952004-05-11 15:09:58 +00008952 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008953 * 1 in case of success.
8954 */
8955static xmlSchemaTypePtr
8956xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008957 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008958{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008959 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008960 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008961 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008962
8963 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8964 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008965 /* Not a component, don't create it. */
8966 type = ctxt->ctxtType;
8967 /*
8968 * Mark the type as being of variety "list".
8969 */
8970 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008971 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008972 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8973 * then the ·simple ur-type definition·."
8974 */
8975 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008976 /*
8977 * Check for illegal attributes.
8978 */
8979 attr = node->properties;
8980 while (attr != NULL) {
8981 if (attr->ns == NULL) {
8982 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8983 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008984 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008985 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008986 }
8987 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008988 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008989 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008990 }
8991 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008992 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008993
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008994 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008995
William M. Brack2f2a6632004-08-20 23:09:47 +00008996 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008997 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
8998 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008999 *
9000 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9001 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009003 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9004 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009005 /*
9006 * And now for the children...
9007 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009008 child = node->children;
9009 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009010 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009011 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009012 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009013 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009014 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009015 /*
9016 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 * Either the itemType [attribute] or the <simpleType> [child] of
9018 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009019 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009020 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009021 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009022 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009023 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009024 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009025 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009026 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009027 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009028 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009029 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009030 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009031 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009032 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009033 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009034 "Either the attribute 'itemType' or the <simpleType> child "
9035 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009036 }
9037 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009038 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009039 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009040 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009041 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009042 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009043 (type->subtypes == NULL) &&
9044 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009045 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009046 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009047 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009048 "Either the attribute 'itemType' or the <simpleType> child "
9049 "must be present", NULL);
9050 }
9051 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009052}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009053
Daniel Veillard4255d502002-04-16 15:50:10 +00009054/**
9055 * xmlSchemaParseSimpleType:
9056 * @ctxt: a schema validation context
9057 * @schema: the schema being built
9058 * @node: a subtree containing XML Schema informations
9059 *
9060 * parse a XML schema Simple Type definition
9061 * *WARNING* this interface is highly subject to change
9062 *
William M. Bracke7091952004-05-11 15:09:58 +00009063 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009064 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009065 */
9066static xmlSchemaTypePtr
9067xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009068 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009069{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009070 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009071 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009072 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009073 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009074 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009075
9076 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9077 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009078
Daniel Veillardc0826a72004-08-10 14:17:33 +00009079 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009080 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009081 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009082 xmlSchemaPMissingAttrErr(ctxt,
9083 XML_SCHEMAP_S4S_ATTR_MISSING,
9084 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009085 "name", NULL);
9086 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009087 } else {
9088 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009089 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009090 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9091 return (NULL);
9092 /*
9093 * Skip built-in types.
9094 */
9095 if (ctxt->isS4S) {
9096 xmlSchemaTypePtr biType;
9097
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009098 if (ctxt->isRedefine) {
9099 /*
9100 * REDEFINE: Disallow redefinition of built-in-types.
9101 * TODO: It seems that the spec does not say anything
9102 * about this case.
9103 */
9104 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009105 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009106 "Redefinition of built-in simple types is not "
9107 "supported", NULL);
9108 return(NULL);
9109 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009110 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9111 if (biType != NULL)
9112 return (biType);
9113 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009114 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009115 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009116 /*
9117 * TargetNamespace:
9118 * SPEC "The ·actual value· of the targetNamespace [attribute]
9119 * of the <schema> ancestor element information item if present,
9120 * otherwise ·absent·.
9121 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009122 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009123#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009124 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009125#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009126 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009127 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009128 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009129#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009130 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009131 type = xmlSchemaAddType(ctxt, schema,
9132 XML_SCHEMA_TYPE_SIMPLE,
9133 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009134 ctxt->targetNamespace, node, 0);
9135#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009136 type = xmlSchemaAddType(ctxt, schema,
9137 XML_SCHEMA_TYPE_SIMPLE,
9138 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009139#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009140 if (type == NULL)
9141 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009142 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009143 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009144 /*
9145 * Check for illegal attributes.
9146 */
9147 attr = node->properties;
9148 while (attr != NULL) {
9149 if (attr->ns == NULL) {
9150 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009151 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009152 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009153 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009154 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009155 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009156 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009157 }
9158 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009159 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009160 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 /*
9162 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009163 *
9164 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009165 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009166 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9167 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009168 if (type == NULL)
9169 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009170 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009171 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009172 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9173 /*
9174 * Check for illegal attributes.
9175 */
9176 attr = node->properties;
9177 while (attr != NULL) {
9178 if (attr->ns == NULL) {
9179 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9180 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009181 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009182 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009183 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009184 }
9185 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009186 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009187 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009188 }
9189 attr = attr->next;
9190 }
9191 /*
9192 * Attribute "final".
9193 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009194 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009195 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009196 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9197 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9198 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9199 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9200 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9201 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009202 } else {
9203 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009204 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9205 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009206 XML_SCHEMAS_TYPE_FINAL_LIST,
9207 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9208
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009209 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009210 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009211 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009212 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009213 attrValue, NULL, NULL, NULL);
9214 }
9215 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009216 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009217 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009218 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009219 /*
9220 * And now for the children...
9221 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009222 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009223
Daniel Veillardc0826a72004-08-10 14:17:33 +00009224 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009225
Daniel Veillard4255d502002-04-16 15:50:10 +00009226 child = node->children;
9227 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009228 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009229 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009230 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009231 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009232 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009233 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009234 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009235 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009236 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009237 XML_SCHEMA_TYPE_SIMPLE);
9238 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009239 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009240 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009241 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009242 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009243 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009244 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009245 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009246 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009247 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009248 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009249 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009250 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009251 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009252 /*
9253 * REDEFINE: SPEC src-redefine (5)
9254 * "Within the [children], each <simpleType> must have a
9255 * <restriction> among its [children] ... the ·actual value· of whose
9256 * base [attribute] must be the same as the ·actual value· of its own
9257 * name attribute plus target namespace;"
9258 */
9259 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9260 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009261 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009262 "<simpleType> must have a <restriction> child", NULL);
9263 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009264
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009265 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009266 return (type);
9267}
9268
Daniel Veillard4255d502002-04-16 15:50:10 +00009269/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009270 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009271 * @ctxt: the parser context
9272 * @schema: the schema being built
9273 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009274 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009275 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009276 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009277 * We will return a particle component with a qname-component or
9278 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009279 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009280static xmlSchemaTreeItemPtr
9281xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9282 xmlSchemaPtr schema,
9283 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009284{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009285 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009286 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009287 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009288 const xmlChar *ref = NULL, *refNs = NULL;
9289 int min, max;
9290
9291 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009292 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009293
9294 attr = xmlSchemaGetPropNode(node, "ref");
9295 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009296 xmlSchemaPMissingAttrErr(ctxt,
9297 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009298 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009299 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009300 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009301 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009302 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009303 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009304 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009305 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009306 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009307 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009308 /*
9309 * Check for illegal attributes.
9310 */
9311 attr = node->properties;
9312 while (attr != NULL) {
9313 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009314 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009315 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9316 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9317 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009318 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009319 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009320 }
9321 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009322 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009323 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009324 }
9325 attr = attr->next;
9326 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009327 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009328 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9329 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009330 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009331 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009332 * Create a qname-reference and set as the term; it will be substituted
9333 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009334 */
9335 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009336 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009337 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9338 /*
9339 * And now for the children...
9340 */
9341 child = node->children;
9342 /* TODO: Is annotation even allowed for a model group reference? */
9343 if (IS_SCHEMA(child, "annotation")) {
9344 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009345 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009346 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009347 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009348 child = child->next;
9349 }
9350 if (child != NULL) {
9351 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009352 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009353 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009354 "(annotation?)");
9355 }
9356 /*
9357 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9358 */
9359 if ((min == 0) && (max == 0))
9360 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009361
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009362 return ((xmlSchemaTreeItemPtr) item);
9363}
9364
9365/**
9366 * xmlSchemaParseModelGroupDefinition:
9367 * @ctxt: a schema validation context
9368 * @schema: the schema being built
9369 * @node: a subtree containing XML Schema informations
9370 *
9371 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009372 *
9373 * Note that the contraint src-redefine (6.2) can't be applied until
9374 * references have been resolved. So we will do this at the
9375 * component fixup level.
9376 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009377 * *WARNING* this interface is highly subject to change
9378 *
9379 * Returns -1 in case of error, 0 if the declaration is improper and
9380 * 1 in case of success.
9381 */
9382static xmlSchemaModelGroupDefPtr
9383xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9384 xmlSchemaPtr schema,
9385 xmlNodePtr node)
9386{
9387 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009388 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009389 xmlAttrPtr attr;
9390 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009391
9392 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009393 return (NULL);
9394
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009395 attr = xmlSchemaGetPropNode(node, "name");
9396 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009397 xmlSchemaPMissingAttrErr(ctxt,
9398 XML_SCHEMAP_S4S_ATTR_MISSING,
9399 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 "name", NULL);
9401 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009402 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009403 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9404 return (NULL);
9405 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009406 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9407 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009408 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009409 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009410 /*
9411 * Check for illegal attributes.
9412 */
9413 attr = node->properties;
9414 while (attr != NULL) {
9415 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009416 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009417 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009418 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009419 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009420 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009421 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009422 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009423 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009424 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009425 attr = attr->next;
9426 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009427 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009428 /*
9429 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009430 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009431 child = node->children;
9432 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009433 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009434 child = child->next;
9435 }
9436 if (IS_SCHEMA(child, "all")) {
9437 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9438 XML_SCHEMA_TYPE_ALL, 0);
9439 child = child->next;
9440 } else if (IS_SCHEMA(child, "choice")) {
9441 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9442 XML_SCHEMA_TYPE_CHOICE, 0);
9443 child = child->next;
9444 } else if (IS_SCHEMA(child, "sequence")) {
9445 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9446 XML_SCHEMA_TYPE_SEQUENCE, 0);
9447 child = child->next;
9448 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009449
9450
9451
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009452 if (child != NULL) {
9453 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009454 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009455 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009456 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009457 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009458 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009459}
9460
9461/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009462 * xmlSchemaCleanupDoc:
9463 * @ctxt: a schema validation context
9464 * @node: the root of the document.
9465 *
9466 * removes unwanted nodes in a schemas document tree
9467 */
9468static void
9469xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9470{
9471 xmlNodePtr delete, cur;
9472
9473 if ((ctxt == NULL) || (root == NULL)) return;
9474
9475 /*
9476 * Remove all the blank text nodes
9477 */
9478 delete = NULL;
9479 cur = root;
9480 while (cur != NULL) {
9481 if (delete != NULL) {
9482 xmlUnlinkNode(delete);
9483 xmlFreeNode(delete);
9484 delete = NULL;
9485 }
9486 if (cur->type == XML_TEXT_NODE) {
9487 if (IS_BLANK_NODE(cur)) {
9488 if (xmlNodeGetSpacePreserve(cur) != 1) {
9489 delete = cur;
9490 }
9491 }
9492 } else if ((cur->type != XML_ELEMENT_NODE) &&
9493 (cur->type != XML_CDATA_SECTION_NODE)) {
9494 delete = cur;
9495 goto skip_children;
9496 }
9497
9498 /*
9499 * Skip to next node
9500 */
9501 if (cur->children != NULL) {
9502 if ((cur->children->type != XML_ENTITY_DECL) &&
9503 (cur->children->type != XML_ENTITY_REF_NODE) &&
9504 (cur->children->type != XML_ENTITY_NODE)) {
9505 cur = cur->children;
9506 continue;
9507 }
9508 }
9509 skip_children:
9510 if (cur->next != NULL) {
9511 cur = cur->next;
9512 continue;
9513 }
9514
9515 do {
9516 cur = cur->parent;
9517 if (cur == NULL)
9518 break;
9519 if (cur == root) {
9520 cur = NULL;
9521 break;
9522 }
9523 if (cur->next != NULL) {
9524 cur = cur->next;
9525 break;
9526 }
9527 } while (cur != NULL);
9528 }
9529 if (delete != NULL) {
9530 xmlUnlinkNode(delete);
9531 xmlFreeNode(delete);
9532 delete = NULL;
9533 }
9534}
9535
William M. Brack2f2a6632004-08-20 23:09:47 +00009536
William M. Brack2f2a6632004-08-20 23:09:47 +00009537static void
9538xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9539{
9540 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9541 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9542
9543 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9544 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9545
9546 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9547 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9548 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9549 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9550 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9551 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9552 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9553 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9554
9555 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9556 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9557 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9558 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9559 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9560 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9561}
9562
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009563static int
9564xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009565 xmlSchemaPtr schema,
9566 xmlNodePtr node)
9567{
9568 xmlAttrPtr attr;
9569 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009570 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009571
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009572 /*
9573 * Those flags should be moved to the parser context flags,
9574 * since they are not visible at the component level. I.e.
9575 * they are used if processing schema *documents* only.
9576 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009577 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009578 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009579
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009580 /*
9581 * Since the version is of type xs:token, we won't bother to
9582 * check it.
9583 */
9584 /* REMOVED:
9585 attr = xmlSchemaGetPropNode(node, "version");
9586 if (attr != NULL) {
9587 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9588 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9589 HFAILURE;
9590 }
9591 */
9592 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9593 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009594 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009595 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9596 HFAILURE;
9597 if (res != 0) {
9598 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9599 goto exit;
9600 }
9601 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009602 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009603 if (attr != NULL) {
9604 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009605 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9606 XML_SCHEMAS_QUALIF_ELEM);
9607 HFAILURE;
9608 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009609 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009610 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009611 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009612 "(qualified | unqualified)", val, NULL, NULL, NULL);
9613 }
9614 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009615 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009616 if (attr != NULL) {
9617 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009618 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9619 XML_SCHEMAS_QUALIF_ATTR);
9620 HFAILURE;
9621 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009622 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009623 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009624 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009625 "(qualified | unqualified)", val, NULL, NULL, NULL);
9626 }
9627 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009628 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009629 if (attr != NULL) {
9630 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009631 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009632 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9633 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9634 -1,
9635 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009636 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9637 HFAILURE;
9638 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009639 xmlSchemaPSimpleTypeErr(ctxt,
9640 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009641 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009642 "(#all | List of (extension | restriction | list | union))",
9643 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009644 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009645 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009646 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009647 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009648 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9649 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009650 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9651 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009652 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9653 HFAILURE;
9654 if (res != 0) {
9655 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009656 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009657 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009658 "(#all | List of (extension | restriction | substitution))",
9659 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009660 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009661 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009662
9663exit:
9664 if (oldErrs != ctxt->nberrors)
9665 res = ctxt->err;
9666 return(res);
9667exit_failure:
9668 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009669}
9670
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009671/**
9672 * xmlSchemaParseSchemaTopLevel:
9673 * @ctxt: a schema validation context
9674 * @schema: the schemas
9675 * @nodes: the list of top level nodes
9676 *
9677 * Returns the internal XML Schema structure built from the resource or
9678 * NULL in case of error
9679 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009680static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009681xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9682 xmlSchemaPtr schema, xmlNodePtr nodes)
9683{
9684 xmlNodePtr child;
9685 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009686 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009687
9688 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009689 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009690
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009691 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009692 child = nodes;
9693 while ((IS_SCHEMA(child, "include")) ||
9694 (IS_SCHEMA(child, "import")) ||
9695 (IS_SCHEMA(child, "redefine")) ||
9696 (IS_SCHEMA(child, "annotation"))) {
9697 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009698 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009699 if (schema->annot == NULL)
9700 schema->annot = annot;
9701 else
9702 xmlSchemaFreeAnnot(annot);
9703 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009704 tmpOldErrs = ctxt->nberrors;
9705 res = xmlSchemaParseImport(ctxt, schema, child);
9706 HFAILURE;
9707 HSTOP(ctxt);
9708 if (tmpOldErrs != ctxt->nberrors)
9709 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009710 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009711 tmpOldErrs = ctxt->nberrors;
9712 res = xmlSchemaParseInclude(ctxt, schema, child);
9713 HFAILURE;
9714 HSTOP(ctxt);
9715 if (tmpOldErrs != ctxt->nberrors)
9716 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009717 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009718 tmpOldErrs = ctxt->nberrors;
9719 res = xmlSchemaParseRedefine(ctxt, schema, child);
9720 HFAILURE;
9721 HSTOP(ctxt);
9722 if (tmpOldErrs != ctxt->nberrors)
9723 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009724 }
9725 child = child->next;
9726 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009727 /*
9728 * URGENT TODO: Change the functions to return int results.
9729 * We need especially to catch internal errors.
9730 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009731 while (child != NULL) {
9732 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009733 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009734 child = child->next;
9735 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009736 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009737 child = child->next;
9738 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009739 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009740 child = child->next;
9741 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009742 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009743 child = child->next;
9744 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009745 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009746 child = child->next;
9747 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009748 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009749 child = child->next;
9750 } else if (IS_SCHEMA(child, "notation")) {
9751 xmlSchemaParseNotation(ctxt, schema, child);
9752 child = child->next;
9753 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009754 xmlSchemaPContentErr(ctxt,
9755 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009756 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009757 NULL, "((include | import | redefine | annotation)*, "
9758 "(((simpleType | complexType | group | attributeGroup) "
9759 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009760 child = child->next;
9761 }
9762 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009763 /*
9764 * TODO: We should add all annotations.
9765 */
9766 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009767 if (schema->annot == NULL)
9768 schema->annot = annot;
9769 else
9770 xmlSchemaFreeAnnot(annot);
9771 child = child->next;
9772 }
9773 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009774exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009775 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009776 if (oldErrs != ctxt->nberrors)
9777 res = ctxt->err;
9778 return(res);
9779exit_failure:
9780 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009781}
9782
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009783static xmlSchemaSchemaRelationPtr
9784xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009785{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009786 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009787
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009788 ret = (xmlSchemaSchemaRelationPtr)
9789 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009790 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009792 return(NULL);
9793 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009794 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009795 return(ret);
9796}
9797
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009798#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009799static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009800xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009801{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009802 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009803}
9804#endif
9805
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009806static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009807xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9808{
9809 xmlSchemaRedefPtr prev;
9810
9811 while (redef != NULL) {
9812 prev = redef;
9813 redef = redef->next;
9814 xmlFree(prev);
9815 }
9816}
9817
9818static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009819xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9820{
9821 /*
9822 * After the construction context has been freed, there will be
9823 * no schema graph available any more. Only the schema buckets
9824 * will stay alive, which are put into the "schemasImports" and
9825 * "includes" slots of the xmlSchema.
9826 */
9827 if (con->buckets != NULL)
9828 xmlSchemaItemListFree(con->buckets);
9829 if (con->pending != NULL)
9830 xmlSchemaItemListFree(con->pending);
9831 if (con->substGroups != NULL)
9832 xmlHashFree(con->substGroups,
9833 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009834 if (con->redefs != NULL)
9835 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009836 if (con->dict != NULL)
9837 xmlDictFree(con->dict);
9838 xmlFree(con);
9839}
9840
9841static xmlSchemaConstructionCtxtPtr
9842xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9843{
9844 xmlSchemaConstructionCtxtPtr ret;
9845
9846 ret = (xmlSchemaConstructionCtxtPtr)
9847 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9848 if (ret == NULL) {
9849 xmlSchemaPErrMemory(NULL,
9850 "allocating schema construction context", NULL);
9851 return (NULL);
9852 }
9853 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9854
9855 ret->buckets = xmlSchemaItemListCreate();
9856 if (ret->buckets == NULL) {
9857 xmlSchemaPErrMemory(NULL,
9858 "allocating list of schema buckets", NULL);
9859 xmlFree(ret);
9860 return (NULL);
9861 }
9862 ret->pending = xmlSchemaItemListCreate();
9863 if (ret->pending == NULL) {
9864 xmlSchemaPErrMemory(NULL,
9865 "allocating list of pending global components", NULL);
9866 xmlSchemaConstructionCtxtFree(ret);
9867 return (NULL);
9868 }
9869 ret->dict = dict;
9870 xmlDictReference(dict);
9871 return(ret);
9872}
9873
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009874static xmlSchemaParserCtxtPtr
9875xmlSchemaParserCtxtCreate(void)
9876{
9877 xmlSchemaParserCtxtPtr ret;
9878
9879 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9880 if (ret == NULL) {
9881 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9882 NULL);
9883 return (NULL);
9884 }
9885 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9886 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009887 ret->attrProhibs = xmlSchemaItemListCreate();
9888 if (ret->attrProhibs == NULL) {
9889 xmlFree(ret);
9890 return(NULL);
9891 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009892 return(ret);
9893}
9894
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009895/**
9896 * xmlSchemaNewParserCtxtUseDict:
9897 * @URL: the location of the schema
9898 * @dict: the dictionary to be used
9899 *
9900 * Create an XML Schemas parse context for that file/resource expected
9901 * to contain an XML Schemas file.
9902 *
9903 * Returns the parser context or NULL in case of error
9904 */
9905static xmlSchemaParserCtxtPtr
9906xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9907{
9908 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009909
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009910 ret = xmlSchemaParserCtxtCreate();
9911 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009912 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009913 ret->dict = dict;
9914 xmlDictReference(dict);
9915 if (URL != NULL)
9916 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009917 return (ret);
9918}
9919
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009920static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009921xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9922{
9923 if (vctxt->pctxt == NULL) {
9924 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009925 vctxt->pctxt =
9926 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009927 else
9928 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9929 if (vctxt->pctxt == NULL) {
9930 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9931 "failed to create a temp. parser context");
9932 return (-1);
9933 }
9934 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009935 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
9936 vctxt->warning, vctxt->userData);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009937 }
9938 return (0);
9939}
9940
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009941/**
9942 * xmlSchemaGetSchemaBucket:
9943 * @pctxt: the schema parser context
9944 * @schemaLocation: the URI of the schema document
9945 *
9946 * Returns a schema bucket if it was already parsed.
9947 *
9948 * Returns a schema bucket if it was already parsed from
9949 * @schemaLocation, NULL otherwise.
9950 */
9951static xmlSchemaBucketPtr
9952xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9953 const xmlChar *schemaLocation)
9954{
9955 xmlSchemaBucketPtr cur;
9956 xmlSchemaItemListPtr list;
9957
9958 list = pctxt->constructor->buckets;
9959 if (list->nbItems == 0)
9960 return(NULL);
9961 else {
9962 int i;
9963 for (i = 0; i < list->nbItems; i++) {
9964 cur = (xmlSchemaBucketPtr) list->items[i];
9965 /* Pointer comparison! */
9966 if (cur->schemaLocation == schemaLocation)
9967 return(cur);
9968 }
9969 }
9970 return(NULL);
9971}
9972
9973static xmlSchemaBucketPtr
9974xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9975 const xmlChar *schemaLocation,
9976 const xmlChar *targetNamespace)
9977{
9978 xmlSchemaBucketPtr cur;
9979 xmlSchemaItemListPtr list;
9980
9981 list = pctxt->constructor->buckets;
9982 if (list->nbItems == 0)
9983 return(NULL);
9984 else {
9985 int i;
9986 for (i = 0; i < list->nbItems; i++) {
9987 cur = (xmlSchemaBucketPtr) list->items[i];
9988 /* Pointer comparison! */
9989 if ((cur->origTargetNamespace == NULL) &&
9990 (cur->schemaLocation == schemaLocation) &&
9991 (cur->targetNamespace == targetNamespace))
9992 return(cur);
9993 }
9994 }
9995 return(NULL);
9996}
9997
9998
9999#define IS_BAD_SCHEMA_DOC(b) \
10000 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10001
10002static xmlSchemaBucketPtr
10003xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10004 const xmlChar *targetNamespace,
10005 int imported)
10006{
10007 xmlSchemaBucketPtr cur;
10008 xmlSchemaItemListPtr list;
10009
10010 list = pctxt->constructor->buckets;
10011 if (list->nbItems == 0)
10012 return(NULL);
10013 else {
10014 int i;
10015 for (i = 0; i < list->nbItems; i++) {
10016 cur = (xmlSchemaBucketPtr) list->items[i];
10017 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10018 (cur->origTargetNamespace == targetNamespace) &&
10019 ((imported && cur->imported) ||
10020 ((!imported) && (!cur->imported))))
10021 return(cur);
10022 }
10023 }
10024 return(NULL);
10025}
10026
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010027static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010028xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10029 xmlSchemaPtr schema,
10030 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010031{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010032 int oldFlags;
10033 xmlDocPtr oldDoc;
10034 xmlNodePtr node;
10035 int ret, oldErrs;
10036 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10037
10038 /*
10039 * Save old values; reset the *main* schema.
10040 * URGENT TODO: This is not good; move the per-document information
10041 * to the parser.
10042 */
10043 oldFlags = schema->flags;
10044 oldDoc = schema->doc;
10045 if (schema->flags != 0)
10046 xmlSchemaClearSchemaDefaults(schema);
10047 schema->doc = bucket->doc;
10048 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10049 pctxt->schema = schema;
10050 /*
10051 * Keep the current target namespace on the parser *not* on the
10052 * main schema.
10053 */
10054 pctxt->targetNamespace = bucket->targetNamespace;
10055 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010056
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010057 if ((bucket->targetNamespace != NULL) &&
10058 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010059 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010060 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010061 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010062 pctxt->isS4S = 1;
10063 }
10064 /* Mark it as parsed, even if parsing fails. */
10065 bucket->parsed++;
10066 /* Compile the schema doc. */
10067 node = xmlDocGetRootElement(bucket->doc);
10068 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10069 if (ret != 0)
10070 goto exit;
10071 /* An empty schema; just get out. */
10072 if (node->children == NULL)
10073 goto exit;
10074 oldErrs = pctxt->nberrors;
10075 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10076 if (ret != 0)
10077 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010078 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010079 * TODO: Not nice, but I'm not 100% sure we will get always an error
10080 * as a result of the obove functions; so better rely on pctxt->err
10081 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010082 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010083 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10084 ret = pctxt->err;
10085 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010086 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010087
10088exit:
10089 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10090 /* Restore schema values. */
10091 schema->doc = oldDoc;
10092 schema->flags = oldFlags;
10093 return(ret);
10094}
10095
10096static int
10097xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10098 xmlSchemaPtr schema,
10099 xmlSchemaBucketPtr bucket)
10100{
10101 xmlSchemaParserCtxtPtr newpctxt;
10102 int res = 0;
10103
10104 if (bucket == NULL)
10105 return(0);
10106 if (bucket->parsed) {
10107 PERROR_INT("xmlSchemaParseNewDoc",
10108 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010109 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010110 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010111 if (bucket->doc == NULL) {
10112 PERROR_INT("xmlSchemaParseNewDoc",
10113 "parsing a schema doc, but there's no doc");
10114 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010115 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010116 if (pctxt->constructor == NULL) {
10117 PERROR_INT("xmlSchemaParseNewDoc",
10118 "no constructor");
10119 return(-1);
10120 }
10121 /* Create and init the temporary parser context. */
10122 newpctxt = xmlSchemaNewParserCtxtUseDict(
10123 (const char *) bucket->schemaLocation, pctxt->dict);
10124 if (newpctxt == NULL)
10125 return(-1);
10126 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010127 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010128 * TODO: Can we avoid that the parser knows about the main schema?
10129 * It would be better if he knows about the current schema bucket
10130 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010131 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010132 newpctxt->schema = schema;
10133 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
10134 pctxt->userData);
10135 newpctxt->counter = pctxt->counter;
10136
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010137
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010138 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10139
10140 /* Channel back errors and cleanup the temporary parser context. */
10141 if (res != 0)
10142 pctxt->err = res;
10143 pctxt->nberrors += newpctxt->nberrors;
10144 pctxt->counter = newpctxt->counter;
10145 newpctxt->constructor = NULL;
10146 /* Free the parser context. */
10147 xmlSchemaFreeParserCtxt(newpctxt);
10148 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010149}
William M. Brack2f2a6632004-08-20 23:09:47 +000010150
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010151static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010152xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10153 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010154{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010155 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010156
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010157 if (cur == NULL) {
10158 bucket->relations = rel;
10159 return;
10160 }
10161 while (cur->next != NULL)
10162 cur = cur->next;
10163 cur->next = rel;
10164}
10165
10166
10167static const xmlChar *
10168xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10169 xmlNodePtr ctxtNode)
10170{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010171 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010172 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010173 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010174 if (location != NULL) {
10175 if (ctxtNode == NULL)
10176 return(location);
10177 else {
10178 xmlChar *base, *URI;
10179 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010180
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010181 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10182 if (base == NULL) {
10183 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10184 } else {
10185 URI = xmlBuildURI(location, base);
10186 xmlFree(base);
10187 }
10188 if (URI != NULL) {
10189 ret = xmlDictLookup(dict, URI, -1);
10190 xmlFree(URI);
10191 return(ret);
10192 }
10193 }
10194 }
10195 return(NULL);
10196}
10197
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010198
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010199
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010200/**
10201 * xmlSchemaAddSchemaDoc:
10202 * @pctxt: a schema validation context
10203 * @schema: the schema being built
10204 * @node: a subtree containing XML Schema informations
10205 *
10206 * Parse an included (and to-be-redefined) XML schema document.
10207 *
10208 * Returns 0 on success, a positive error code on errors and
10209 * -1 in case of an internal or API error.
10210 */
10211
10212static int
10213xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10214 int type, /* import or include or redefine */
10215 const xmlChar *schemaLocation,
10216 xmlDocPtr schemaDoc,
10217 const char *schemaBuffer,
10218 int schemaBufferLen,
10219 xmlNodePtr invokingNode,
10220 const xmlChar *sourceTargetNamespace,
10221 const xmlChar *importNamespace,
10222 xmlSchemaBucketPtr *bucket)
10223{
10224 const xmlChar *targetNamespace = NULL;
10225 xmlSchemaSchemaRelationPtr relation = NULL;
10226 xmlDocPtr doc = NULL;
10227 int res = 0, err = 0, located = 0, preserveDoc = 0;
10228 xmlSchemaBucketPtr bkt = NULL;
10229
10230 if (bucket != NULL)
10231 *bucket = NULL;
10232
10233 switch (type) {
10234 case XML_SCHEMA_SCHEMA_IMPORT:
10235 case XML_SCHEMA_SCHEMA_MAIN:
10236 err = XML_SCHEMAP_SRC_IMPORT;
10237 break;
10238 case XML_SCHEMA_SCHEMA_INCLUDE:
10239 err = XML_SCHEMAP_SRC_INCLUDE;
10240 break;
10241 case XML_SCHEMA_SCHEMA_REDEFINE:
10242 err = XML_SCHEMAP_SRC_REDEFINE;
10243 break;
10244 }
10245
10246
10247 /* Special handling for the main schema:
10248 * skip the location and relation logic and just parse the doc.
10249 * We need just a bucket to be returned in this case.
10250 */
10251 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10252 goto doc_load;
10253
10254 /* Note that we expect the location to be an absulute URI. */
10255 if (schemaLocation != NULL) {
10256 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10257 if ((bkt != NULL) &&
10258 (pctxt->constructor->bucket == bkt)) {
10259 /* Report self-imports/inclusions/redefinitions. */
10260
10261 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10262 invokingNode, NULL,
10263 "The schema must not import/include/redefine itself",
10264 NULL, NULL);
10265 goto exit;
10266 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010267 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010268 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010269 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010270 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010271 relation = xmlSchemaSchemaRelationCreate();
10272 if (relation == NULL)
10273 return(-1);
10274 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10275 relation);
10276 relation->type = type;
10277
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010278 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010279 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010280 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010281 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010282 relation->importNamespace = importNamespace;
10283 if (schemaLocation == NULL) {
10284 /*
10285 * No location; this is just an import of the namespace.
10286 * Note that we don't assign a bucket to the relation
10287 * in this case.
10288 */
10289 goto exit;
10290 }
10291 targetNamespace = importNamespace;
10292 }
10293
10294 /* Did we already fetch the doc? */
10295 if (bkt != NULL) {
10296 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010297 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010298 /* We included/redefined and then try to import a schema. */
10299 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10300 invokingNode, NULL,
10301 "The schema document '%s' cannot be imported, since "
10302 "it was already included or redefined",
10303 schemaLocation, NULL);
10304 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010305 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010306 /* We imported and then try to include/redefine a schema. */
10307 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10308 invokingNode, NULL,
10309 "The schema document '%s' cannot be included or "
10310 "redefined, since it was already imported",
10311 schemaLocation, NULL);
10312 goto exit;
10313 }
10314 }
10315
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010316 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010317 /*
10318 * Given that the schemaLocation [attribute] is only a hint, it is open
10319 * to applications to ignore all but the first <import> for a given
10320 * namespace, regardless of the ·actual value· of schemaLocation, but
10321 * such a strategy risks missing useful information when new
10322 * schemaLocations are offered.
10323 *
10324 * We will use the first <import> that comes with a location.
10325 * Further <import>s *with* a location, will result in an error.
10326 * TODO: Better would be to just report a warning here, but
10327 * we'll try it this way until someone complains.
10328 *
10329 * Schema Document Location Strategy:
10330 * 3 Based on the namespace name, identify an existing schema document,
10331 * either as a resource which is an XML document or a <schema> element
10332 * information item, in some local schema repository;
10333 * 5 Attempt to resolve the namespace name to locate such a resource.
10334 *
10335 * NOTE: (3) and (5) are not supported.
10336 */
10337 if (bkt != NULL) {
10338 relation->bucket = bkt;
10339 goto exit;
10340 }
10341 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10342 importNamespace, 1);
10343
10344 if (bkt != NULL) {
10345 relation->bucket = bkt;
10346 if (bkt->schemaLocation == NULL) {
10347 /* First given location of the schema; load the doc. */
10348 bkt->schemaLocation = schemaLocation;
10349 } else {
10350 if (!xmlStrEqual(schemaLocation,
10351 bkt->schemaLocation)) {
10352 /*
10353 * Additional location given; just skip it.
10354 * URGENT TODO: We should report a warning here.
10355 * res = XML_SCHEMAP_SRC_IMPORT;
10356 */
10357 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10358 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10359 invokingNode, NULL,
10360 "Skipping import of schema located at '%s' for the "
10361 "namespace '%s', since this namespace was already "
10362 "imported with the schema located at '%s'",
10363 schemaLocation, importNamespace, bkt->schemaLocation);
10364 }
10365 goto exit;
10366 }
10367 }
10368 /*
10369 * No bucket + first location: load the doc and create a
10370 * bucket.
10371 */
10372 } else {
10373 /* <include> and <redefine> */
10374 if (bkt != NULL) {
10375
10376 if ((bkt->origTargetNamespace == NULL) &&
10377 (bkt->targetNamespace != sourceTargetNamespace)) {
10378 xmlSchemaBucketPtr chamel;
10379
10380 /*
10381 * Chameleon include/redefine: skip loading only if it was
10382 * aleady build for the targetNamespace of the including
10383 * schema.
10384 */
10385 /*
10386 * URGENT TODO: If the schema is a chameleon-include then copy
10387 * the components into the including schema and modify the
10388 * targetNamespace of those components, do nothing otherwise.
10389 * NOTE: This is currently worked-around by compiling the
10390 * chameleon for every destinct including targetNamespace; thus
10391 * not performant at the moment.
10392 * TODO: Check when the namespace in wildcards for chameleons
10393 * needs to be converted: before we built wildcard intersections
10394 * or after.
10395 * Answer: after!
10396 */
10397 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10398 schemaLocation, sourceTargetNamespace);
10399 if (chamel != NULL) {
10400 /* A fitting chameleon was already parsed; NOP. */
10401 relation->bucket = chamel;
10402 goto exit;
10403 }
10404 /*
10405 * We need to parse the chameleon again for a different
10406 * targetNamespace.
10407 * CHAMELEON TODO: Optimize this by only parsing the
10408 * chameleon once, and then copying the components to
10409 * the new targetNamespace.
10410 */
10411 bkt = NULL;
10412 } else {
10413 relation->bucket = bkt;
10414 goto exit;
10415 }
10416 }
10417 }
10418 if ((bkt != NULL) && (bkt->doc != NULL)) {
10419 PERROR_INT("xmlSchemaAddSchemaDoc",
10420 "trying to load a schema doc, but a doc is already "
10421 "assigned to the schema bucket");
10422 goto exit_failure;
10423 }
10424
10425doc_load:
10426 /*
10427 * Load the document.
10428 */
10429 if (schemaDoc != NULL) {
10430 doc = schemaDoc;
10431 /* Don' free this one, since it was provided by the caller. */
10432 preserveDoc = 1;
10433 /* TODO: Does the context or the doc hold the location? */
10434 if (schemaDoc->URL != NULL)
10435 schemaLocation = xmlDictLookup(pctxt->dict,
10436 schemaDoc->URL, -1);
10437
10438 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10439 xmlParserCtxtPtr parserCtxt;
10440
10441 parserCtxt = xmlNewParserCtxt();
10442 if (parserCtxt == NULL) {
10443 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10444 "allocating a parser context", NULL);
10445 goto exit_failure;
10446 }
10447 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10448 /*
10449 * TODO: Do we have to burden the schema parser dict with all
10450 * the content of the schema doc?
10451 */
10452 xmlDictFree(parserCtxt->dict);
10453 parserCtxt->dict = pctxt->dict;
10454 xmlDictReference(parserCtxt->dict);
10455 }
10456 if (schemaLocation != NULL) {
10457 /* Parse from file. */
10458 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10459 NULL, SCHEMAS_PARSE_OPTIONS);
10460 } else if (schemaBuffer != NULL) {
10461 /* Parse from memory buffer. */
10462 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10463 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10464 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10465 if (doc != NULL)
10466 doc->URL = schemaLocation;
10467 }
10468 /*
10469 * For <import>:
10470 * 2.1 The referent is (a fragment of) a resource which is an
10471 * XML document (see clause 1.1), which in turn corresponds to
10472 * a <schema> element information item in a well-formed information
10473 * set, which in turn corresponds to a valid schema.
10474 * TODO: (2.1) fragments of XML documents are not supported.
10475 *
10476 * 2.2 The referent is a <schema> element information item in
10477 * a well-formed information set, which in turn corresponds
10478 * to a valid schema.
10479 * TODO: (2.2) is not supported.
10480 */
10481 if (doc == NULL) {
10482 xmlErrorPtr lerr;
10483 lerr = xmlGetLastError();
10484 /*
10485 * Check if this a parser error, or if the document could
10486 * just not be located.
10487 * TODO: Try to find specific error codes to react only on
10488 * localisation failures.
10489 */
10490 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10491 /*
10492 * We assume a parser error here.
10493 */
10494 located = 1;
10495 /* TODO: Error code ?? */
10496 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10497 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10498 invokingNode, NULL,
10499 "Failed to parse the XML resource '%s'",
10500 schemaLocation, NULL);
10501 }
10502 }
10503 xmlFreeParserCtxt(parserCtxt);
10504 if ((doc == NULL) && located)
10505 goto exit_error;
10506 } else {
10507 xmlSchemaPErr(pctxt, NULL,
10508 XML_SCHEMAP_NOTHING_TO_PARSE,
10509 "No information for parsing was provided with the "
10510 "given schema parser context.\n",
10511 NULL, NULL);
10512 goto exit_failure;
10513 }
10514 /*
10515 * Preprocess the document.
10516 */
10517 if (doc != NULL) {
10518 xmlNodePtr docElem = NULL;
10519
10520 located = 1;
10521 docElem = xmlDocGetRootElement(doc);
10522 if (docElem == NULL) {
10523 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10524 invokingNode, NULL,
10525 "The document '%s' has no document element",
10526 schemaLocation, NULL);
10527 xmlFreeDoc(doc);
10528 doc = NULL;
10529 goto exit_error;
10530 }
10531 /*
10532 * Remove all the blank text nodes.
10533 */
10534 xmlSchemaCleanupDoc(pctxt, docElem);
10535 /*
10536 * Check the schema's top level element.
10537 */
10538 if (!IS_SCHEMA(docElem, "schema")) {
10539 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10540 invokingNode, NULL,
10541 "The XML document '%s' is not a schema document",
10542 schemaLocation, NULL);
10543 xmlFreeDoc(doc);
10544 doc = NULL;
10545 goto exit_error;
10546 }
10547 /*
10548 * Note that we don't apply a type check for the
10549 * targetNamespace value here.
10550 */
10551 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10552 "targetNamespace");
10553 }
10554
10555/* after_doc_loading: */
10556 if ((bkt == NULL) && located) {
10557 /* Only create a bucket if the schema was located. */
10558 bkt = xmlSchemaBucketCreate(pctxt, type,
10559 targetNamespace);
10560 if (bkt == NULL)
10561 goto exit_failure;
10562 }
10563 if (bkt != NULL) {
10564 bkt->schemaLocation = schemaLocation;
10565 bkt->located = located;
10566 if (doc != NULL) {
10567 bkt->doc = doc;
10568 bkt->targetNamespace = targetNamespace;
10569 bkt->origTargetNamespace = targetNamespace;
10570 if (preserveDoc)
10571 bkt->preserveDoc = 1;
10572 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010573 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010574 bkt->imported++;
10575 /*
10576 * Add it to the graph of schemas.
10577 */
10578 if (relation != NULL)
10579 relation->bucket = bkt;
10580 }
10581
10582exit:
10583 /*
10584 * Return the bucket explicitely; this is needed for the
10585 * main schema.
10586 */
10587 if (bucket != NULL)
10588 *bucket = bkt;
10589 return (0);
10590
10591exit_error:
10592 if ((doc != NULL) && (! preserveDoc)) {
10593 xmlFreeDoc(doc);
10594 if (bkt != NULL)
10595 bkt->doc = NULL;
10596 }
10597 return(pctxt->err);
10598
10599exit_failure:
10600 if ((doc != NULL) && (! preserveDoc)) {
10601 xmlFreeDoc(doc);
10602 if (bkt != NULL)
10603 bkt->doc = NULL;
10604 }
10605 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010606}
10607
William M. Brack2f2a6632004-08-20 23:09:47 +000010608/**
10609 * xmlSchemaParseImport:
10610 * @ctxt: a schema validation context
10611 * @schema: the schema being built
10612 * @node: a subtree containing XML Schema informations
10613 *
10614 * parse a XML schema Import definition
10615 * *WARNING* this interface is highly subject to change
10616 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010617 * Returns 0 in case of success, a positive error code if
10618 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010619 */
10620static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010621xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010622 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010623{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010624 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010625 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10626 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010627 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010628 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010629 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010630
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010631 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010632 return (-1);
10633
10634 /*
10635 * Check for illegal attributes.
10636 */
10637 attr = node->properties;
10638 while (attr != NULL) {
10639 if (attr->ns == NULL) {
10640 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10641 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10642 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010643 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010644 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010645 }
10646 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010647 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010648 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010649 }
10650 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010651 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010652 /*
10653 * Extract and validate attributes.
10654 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010655 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010656 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010657 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010658 xmlSchemaPSimpleTypeErr(pctxt,
10659 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010660 NULL, node,
10661 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010662 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010663 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010664 }
10665
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010666 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010667 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010668 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010669 xmlSchemaPSimpleTypeErr(pctxt,
10670 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010671 NULL, node,
10672 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010673 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010674 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010675 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010676 /*
10677 * And now for the children...
10678 */
10679 child = node->children;
10680 if (IS_SCHEMA(child, "annotation")) {
10681 /*
10682 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010683 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010684 */
10685 child = child->next;
10686 }
10687 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010688 xmlSchemaPContentErr(pctxt,
10689 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010690 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010691 "(annotation?)");
10692 }
10693 /*
10694 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010695 *
10696 * Note that it is important to use the original @targetNamespace
10697 * (or none at all), to rule out imports of schemas _with_ a
10698 * @targetNamespace if the importing schema is a chameleon schema
10699 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010700 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010701 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010702 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010703 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010704 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10705 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010706 * targetNamespace [attribute].
10707 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010708 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010709 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010710 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010711 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010712 "The value of the attribute 'namespace' must not match "
10713 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010714 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010715 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010716 }
10717 } else {
10718 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010719 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010720 * <schema> must have a targetNamespace [attribute].
10721 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010722 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010723 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010724 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010725 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010726 "The attribute 'namespace' must be existent if "
10727 "the importing schema has no target namespace",
10728 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010729 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010730 }
10731 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010732 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010733 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010734 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010735 if (schemaLocation != NULL)
10736 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10737 schemaLocation, node);
10738 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010739 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010740 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010741
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010742 if (ret != 0)
10743 return(ret);
10744
10745 /*
10746 * For <import>: "It is *not* an error for the application
10747 * schema reference strategy to fail."
10748 * So just don't parse if no schema document was found.
10749 * Note that we will get no bucket if the schema could not be
10750 * located or if there was no schemaLocation.
10751 */
10752 if ((bucket == NULL) && (schemaLocation != NULL)) {
10753 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10754 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10755 node, NULL,
10756 "Failed to locate a schema at location '%s'. "
10757 "Skipping the import", schemaLocation, NULL, NULL);
10758 }
10759
10760 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10761 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10762 }
10763
10764 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010765}
10766
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010767static int
10768xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10769 xmlSchemaPtr schema,
10770 xmlNodePtr node,
10771 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010772 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010773{
10774 xmlAttrPtr attr;
10775
10776 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10777 (schemaLocation == NULL))
10778 return (-1);
10779
10780 *schemaLocation = NULL;
10781 /*
10782 * Check for illegal attributes.
10783 * Applies for both <include> and <redefine>.
10784 */
10785 attr = node->properties;
10786 while (attr != NULL) {
10787 if (attr->ns == NULL) {
10788 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10789 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10790 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010791 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010792 }
10793 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10794 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010795 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010796 }
10797 attr = attr->next;
10798 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010799 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010800 /*
10801 * Preliminary step, extract the URI-Reference and make an URI
10802 * from the base.
10803 */
10804 /*
10805 * Attribute "schemaLocation" is mandatory.
10806 */
10807 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10808 if (attr != NULL) {
10809 xmlChar *base = NULL;
10810 xmlChar *uri = NULL;
10811
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010812 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010813 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10814 (const xmlChar **) schemaLocation) != 0)
10815 goto exit_error;
10816 base = xmlNodeGetBase(node->doc, node);
10817 if (base == NULL) {
10818 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10819 } else {
10820 uri = xmlBuildURI(*schemaLocation, base);
10821 xmlFree(base);
10822 }
10823 if (uri == NULL) {
10824 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10825 "could not build an URI from the schemaLocation")
10826 goto exit_failure;
10827 }
10828 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10829 xmlFree(uri);
10830 } else {
10831 xmlSchemaPMissingAttrErr(pctxt,
10832 XML_SCHEMAP_S4S_ATTR_MISSING,
10833 NULL, node, "schemaLocation", NULL);
10834 goto exit_error;
10835 }
10836 /*
10837 * Report self-inclusion and self-redefinition.
10838 */
10839 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010840 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010841 xmlSchemaPCustomErr(pctxt,
10842 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010843 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010844 "The schema document '%s' cannot redefine itself.",
10845 *schemaLocation);
10846 } else {
10847 xmlSchemaPCustomErr(pctxt,
10848 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010849 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010850 "The schema document '%s' cannot include itself.",
10851 *schemaLocation);
10852 }
10853 goto exit_error;
10854 }
10855
10856 return(0);
10857exit_error:
10858 return(pctxt->err);
10859exit_failure:
10860 return(-1);
10861}
10862
10863static int
10864xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10865 xmlSchemaPtr schema,
10866 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010867 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010868{
10869 xmlNodePtr child = NULL;
10870 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010871 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010872 int isChameleon = 0, wasChameleon = 0;
10873 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010874
10875 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10876 return (-1);
10877
10878 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010879 * Parse attributes. Note that the returned schemaLocation will
10880 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010881 */
10882 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010883 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010884 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010885 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010886 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010887 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010888 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010889 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10890 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010891 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010892 return(res);
10893 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010894 * If we get no schema bucket back, then this means that the schema
10895 * document could not be located or was broken XML or was not
10896 * a schema document.
10897 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010898 if ((bucket == NULL) || (bucket->doc == NULL)) {
10899 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10900 /*
10901 * WARNING for <include>:
10902 * We will raise an error if the schema cannot be located
10903 * for inclusions, since the that was the feedback from the
10904 * schema people. I.e. the following spec piece will *not* be
10905 * satisfied:
10906 * SPEC src-include: "It is not an error for the ·actual value· of the
10907 * schemaLocation [attribute] to fail to resolve it all, in which
10908 * case no corresponding inclusion is performed.
10909 * So do we need a warning report here?"
10910 */
10911 res = XML_SCHEMAP_SRC_INCLUDE;
10912 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10913 node, NULL,
10914 "Failed to load the document '%s' for inclusion",
10915 schemaLocation, NULL);
10916 } else {
10917 /*
10918 * NOTE: This was changed to raise an error even if no redefinitions
10919 * are specified.
10920 *
10921 * SPEC src-redefine (1)
10922 * "If there are any element information items among the [children]
10923 * other than <annotation> then the ·actual value· of the
10924 * schemaLocation [attribute] must successfully resolve."
10925 * TODO: Ask the WG if a the location has always to resolve
10926 * here as well!
10927 */
10928 res = XML_SCHEMAP_SRC_REDEFINE;
10929 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10930 node, NULL,
10931 "Failed to load the document '%s' for redefinition",
10932 schemaLocation, NULL);
10933 }
10934 } else {
10935 /*
10936 * Check targetNamespace sanity before parsing the new schema.
10937 * TODO: Note that we won't check further content if the
10938 * targetNamespace was bad.
10939 */
10940 if (bucket->origTargetNamespace != NULL) {
10941 /*
10942 * SPEC src-include (2.1)
10943 * "SII has a targetNamespace [attribute], and its ·actual
10944 * value· is identical to the ·actual value· of the targetNamespace
10945 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10946 */
10947 if (pctxt->targetNamespace == NULL) {
10948 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10949 XML_SCHEMAP_SRC_INCLUDE,
10950 node, NULL,
10951 "The target namespace of the included/redefined schema "
10952 "'%s' has to be absent, since the including/redefining "
10953 "schema has no target namespace",
10954 schemaLocation, NULL);
10955 goto exit_error;
10956 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10957 pctxt->targetNamespace)) {
10958 /* TODO: Change error function. */
10959 xmlSchemaPCustomErrExt(pctxt,
10960 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010961 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010962 "The target namespace '%s' of the included/redefined "
10963 "schema '%s' differs from '%s' of the "
10964 "including/redefining schema",
10965 bucket->origTargetNamespace, schemaLocation,
10966 pctxt->targetNamespace);
10967 goto exit_error;
10968 }
10969 } else if (pctxt->targetNamespace != NULL) {
10970 /*
10971 * Chameleons: the original target namespace will
10972 * differ from the resulting namespace.
10973 */
10974 isChameleon = 1;
10975 if (bucket->parsed &&
10976 (bucket->targetNamespace != pctxt->targetNamespace)) {
10977 /*
10978 * This is a sanity check, I dunno yet if this can happen.
10979 */
10980 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10981 "trying to use an already parsed schema for a "
10982 "different targetNamespace");
10983 return(-1);
10984 }
10985 bucket->targetNamespace = pctxt->targetNamespace;
10986 }
10987 }
10988 /*
10989 * Parse the schema.
10990 */
10991 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
10992 if (isChameleon) {
10993 /* TODO: Get rid of this flag on the schema itself. */
10994 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
10995 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
10996 } else
10997 wasChameleon = 1;
10998 }
10999 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11000 /* Restore chameleon flag. */
11001 if (isChameleon && (!wasChameleon))
11002 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11003 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011004 /*
11005 * And now for the children...
11006 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011007 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011008 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011009 /*
11010 * Parse (simpleType | complexType | group | attributeGroup))*
11011 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011012 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011013 /*
11014 * How to proceed if the redefined schema was not located?
11015 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011016 pctxt->isRedefine = 1;
11017 while (IS_SCHEMA(child, "annotation") ||
11018 IS_SCHEMA(child, "simpleType") ||
11019 IS_SCHEMA(child, "complexType") ||
11020 IS_SCHEMA(child, "group") ||
11021 IS_SCHEMA(child, "attributeGroup")) {
11022 if (IS_SCHEMA(child, "annotation")) {
11023 /*
11024 * TODO: discard or not?
11025 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011026 } else if (IS_SCHEMA(child, "simpleType")) {
11027 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11028 } else if (IS_SCHEMA(child, "complexType")) {
11029 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011030 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011031 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011032 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011033 xmlSchemaParseModelGroupDefinition(pctxt,
11034 schema, child);
11035 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011036 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011037 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11038 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011039 }
11040 child = child->next;
11041 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011042 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011043 pctxt->isRedefine = 0;
11044 } else {
11045 if (IS_SCHEMA(child, "annotation")) {
11046 /*
11047 * TODO: discard or not?
11048 */
11049 child = child->next;
11050 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011051 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011052 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011053 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011054 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11055 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011056 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011057 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11058 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011059 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011060 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011061 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011062 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011063 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011064 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011065
11066exit_error:
11067 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011068}
11069
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011070static int
11071xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11072 xmlNodePtr node)
11073{
11074 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011075#ifndef ENABLE_REDEFINE
11076 TODO
11077 return(0);
11078#endif
11079 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11080 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011081 if (res != 0)
11082 return(res);
11083 return(0);
11084}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011085
11086static int
11087xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11088 xmlNodePtr node)
11089{
11090 int res;
11091
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011092 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11093 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011094 if (res != 0)
11095 return(res);
11096 return(0);
11097}
11098
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011099/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011100 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011101 * @ctxt: a schema validation context
11102 * @schema: the schema being built
11103 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011104 * @type: the "compositor" type
11105 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011106 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011107 * parse a XML schema Sequence definition.
11108 * Applies parts of:
11109 * Schema Representation Constraint:
11110 * Redefinition Constraints and Semantics (src-redefine)
11111 * (6.1), (6.1.1), (6.1.2)
11112 *
11113 * Schema Component Constraint:
11114 * All Group Limited (cos-all-limited) (2)
11115 * TODO: Actually this should go to component-level checks,
11116 * but is done here due to performance. Move it to an other layer
11117 * is schema construction via an API is implemented.
11118 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011119 * *WARNING* this interface is highly subject to change
11120 *
William M. Bracke7091952004-05-11 15:09:58 +000011121 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011122 * 1 in case of success.
11123 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011124static xmlSchemaTreeItemPtr
11125xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11126 xmlNodePtr node, xmlSchemaTypeType type,
11127 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011128{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011129 xmlSchemaModelGroupPtr item;
11130 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011131 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011132 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011133 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011134
11135 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011136 return (NULL);
11137 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011138 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011139 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011140 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011141 if (item == NULL)
11142 return (NULL);
11143
11144 if (withParticle) {
11145 if (type == XML_SCHEMA_TYPE_ALL) {
11146 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011147 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011148 } else {
11149 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011150 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11151 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11152 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011153 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011154 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11155 /*
11156 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011157 */
11158 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11159 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011160 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011161 particle->children = (xmlSchemaTreeItemPtr) item;
11162 /*
11163 * Check for illegal attributes.
11164 */
11165 attr = node->properties;
11166 while (attr != NULL) {
11167 if (attr->ns == NULL) {
11168 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11169 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11170 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011171 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011172 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011173 }
11174 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011175 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011176 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011177 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011178 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011179 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011180 } else {
11181 /*
11182 * Check for illegal attributes.
11183 */
11184 attr = node->properties;
11185 while (attr != NULL) {
11186 if (attr->ns == NULL) {
11187 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011188 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011189 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011190 }
11191 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011192 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011193 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011194 }
11195 attr = attr->next;
11196 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011197 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011198
William M. Brack2f2a6632004-08-20 23:09:47 +000011199 /*
11200 * Extract and validate attributes.
11201 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011202 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011203 /*
11204 * And now for the children...
11205 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011206 child = node->children;
11207 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011208 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011209 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011210 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011211 if (type == XML_SCHEMA_TYPE_ALL) {
11212 xmlSchemaParticlePtr part, last = NULL;
11213
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011214 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011215 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011216 schema, child, &isElemRef, 0);
11217 /*
11218 * SPEC cos-all-limited (2)
11219 * "The {max occurs} of all the particles in the {particles}
11220 * of the ('all') group must be 0 or 1.
11221 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011222 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011223 if (isElemRef)
11224 hasRefs++;
11225 if (part->minOccurs > 1) {
11226 xmlSchemaPCustomErr(ctxt,
11227 XML_SCHEMAP_COS_ALL_LIMITED,
11228 NULL, child,
11229 "Invalid value for minOccurs (must be 0 or 1)",
11230 NULL);
11231 /* Reset to 1. */
11232 part->minOccurs = 1;
11233 }
11234 if (part->maxOccurs > 1) {
11235 xmlSchemaPCustomErr(ctxt,
11236 XML_SCHEMAP_COS_ALL_LIMITED,
11237 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011238 "Invalid value for maxOccurs (must be 0 or 1)",
11239 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011240 /* Reset to 1. */
11241 part->maxOccurs = 1;
11242 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011243 if (last == NULL)
11244 item->children = (xmlSchemaTreeItemPtr) part;
11245 else
11246 last->next = (xmlSchemaTreeItemPtr) part;
11247 last = part;
11248 }
11249 child = child->next;
11250 }
11251 if (child != NULL) {
11252 xmlSchemaPContentErr(ctxt,
11253 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011254 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011255 "(annotation?, (annotation?, element*)");
11256 }
11257 } else {
11258 /* choice + sequence */
11259 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11260
11261 while ((IS_SCHEMA(child, "element")) ||
11262 (IS_SCHEMA(child, "group")) ||
11263 (IS_SCHEMA(child, "any")) ||
11264 (IS_SCHEMA(child, "choice")) ||
11265 (IS_SCHEMA(child, "sequence"))) {
11266
11267 if (IS_SCHEMA(child, "element")) {
11268 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011269 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11270 if (part && isElemRef)
11271 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011272 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011273 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011274 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011275 if (part != NULL)
11276 hasRefs++;
11277 /*
11278 * Handle redefinitions.
11279 */
11280 if (ctxt->isRedefine && ctxt->redef &&
11281 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11282 part && part->children)
11283 {
11284 if ((xmlSchemaGetQNameRefName(part->children) ==
11285 ctxt->redef->refName) &&
11286 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11287 ctxt->redef->refTargetNs))
11288 {
11289 /*
11290 * SPEC src-redefine:
11291 * (6.1) "If it has a <group> among its contents at
11292 * some level the ·actual value· of whose ref
11293 * [attribute] is the same as the ·actual value· of
11294 * its own name attribute plus target namespace, then
11295 * all of the following must be true:"
11296 * (6.1.1) "It must have exactly one such group."
11297 */
11298 if (ctxt->redefCounter != 0) {
11299 xmlChar *str = NULL;
11300
11301 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11302 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11303 "The redefining model group definition "
11304 "'%s' must not contain more than one "
11305 "reference to the redefined definition",
11306 xmlSchemaFormatQName(&str,
11307 ctxt->redef->refTargetNs,
11308 ctxt->redef->refName),
11309 NULL);
11310 FREE_AND_NULL(str)
11311 part = NULL;
11312 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11313 ((WXS_PARTICLE(part))->maxOccurs != 1))
11314 {
11315 xmlChar *str = NULL;
11316 /*
11317 * SPEC src-redefine:
11318 * (6.1.2) "The ·actual value· of both that
11319 * group's minOccurs and maxOccurs [attribute]
11320 * must be 1 (or ·absent·).
11321 */
11322 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11323 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11324 "The redefining model group definition "
11325 "'%s' must not contain a reference to the "
11326 "redefined definition with a "
11327 "maxOccurs/minOccurs other than 1",
11328 xmlSchemaFormatQName(&str,
11329 ctxt->redef->refTargetNs,
11330 ctxt->redef->refName),
11331 NULL);
11332 FREE_AND_NULL(str)
11333 part = NULL;
11334 }
11335 ctxt->redef->reference = WXS_BASIC_CAST part;
11336 ctxt->redefCounter++;
11337 }
11338 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011339 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011340 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011341 xmlSchemaParseAny(ctxt, schema, child);
11342 } else if (IS_SCHEMA(child, "choice")) {
11343 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11344 XML_SCHEMA_TYPE_CHOICE, 1);
11345 } else if (IS_SCHEMA(child, "sequence")) {
11346 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11347 XML_SCHEMA_TYPE_SEQUENCE, 1);
11348 }
11349 if (part != NULL) {
11350 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011351 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011352 else
11353 last->next = part;
11354 last = part;
11355 }
11356 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011357 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011358 if (child != NULL) {
11359 xmlSchemaPContentErr(ctxt,
11360 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011361 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011362 "(annotation?, (element | group | choice | sequence | any)*)");
11363 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011364 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011365 if ((max == 0) && (min == 0))
11366 return (NULL);
11367 if (hasRefs) {
11368 /*
11369 * We need to resolve references.
11370 */
11371 WXS_ADD_PENDING(ctxt, item);
11372 }
11373 if (withParticle)
11374 return ((xmlSchemaTreeItemPtr) particle);
11375 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011376 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011377}
11378
11379/**
11380 * xmlSchemaParseRestriction:
11381 * @ctxt: a schema validation context
11382 * @schema: the schema being built
11383 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011384 *
11385 * parse a XML schema Restriction definition
11386 * *WARNING* this interface is highly subject to change
11387 *
11388 * Returns the type definition or NULL in case of error
11389 */
11390static xmlSchemaTypePtr
11391xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011392 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011393{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011394 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011395 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011396 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011397
11398 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11399 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011400 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011401 type = ctxt->ctxtType;
11402 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011403
11404 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011405 * Check for illegal attributes.
11406 */
11407 attr = node->properties;
11408 while (attr != NULL) {
11409 if (attr->ns == NULL) {
11410 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11411 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011412 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011413 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011414 }
11415 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011416 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011417 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011418 }
11419 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011420 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011421 /*
11422 * Extract and validate attributes.
11423 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011424 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011425 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011426 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011427 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011428 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011429 * Extract the base type. The "base" attribute is mandatory if inside
11430 * a complex type or if redefining.
11431 *
11432 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011433 * among its [children]), the simple type definition which is
11434 * the {content type} of the type definition ·resolved· to by
11435 * the ·actual value· of the base [attribute]"
11436 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011437 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011438 &(type->baseNs), &(type->base)) == 0)
11439 {
11440 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11441 xmlSchemaPMissingAttrErr(ctxt,
11442 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011443 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011444 } else if ((ctxt->isRedefine) &&
11445 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11446 {
11447 if (type->base == NULL) {
11448 xmlSchemaPMissingAttrErr(ctxt,
11449 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011450 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011451 } else if ((! xmlStrEqual(type->base, type->name)) ||
11452 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11453 {
11454 xmlChar *str1 = NULL, *str2 = NULL;
11455 /*
11456 * REDEFINE: SPEC src-redefine (5)
11457 * "Within the [children], each <simpleType> must have a
11458 * <restriction> among its [children] ... the ·actual value· of
11459 * whose base [attribute] must be the same as the ·actual value·
11460 * of its own name attribute plus target namespace;"
11461 */
11462 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011463 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011464 "value '%s' of the 'base' attribute does not match the "
11465 "type's designation '%s'",
11466 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11467 xmlSchemaFormatQName(&str1, type->targetNamespace,
11468 type->name), NULL);
11469 FREE_AND_NULL(str1);
11470 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011471 /* Avoid confusion and erase the values. */
11472 type->base = NULL;
11473 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011474 }
11475 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011476 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011477 /*
11478 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011479 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011480 child = node->children;
11481 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011482 /*
11483 * Add the annotation to the simple type ancestor.
11484 */
11485 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011486 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011487 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011488 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011489 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11490 /*
11491 * Corresponds to <simpleType><restriction><simpleType>.
11492 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011493 if (IS_SCHEMA(child, "simpleType")) {
11494 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011495 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011496 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011497 * Either the base [attribute] or the simpleType [child] of the
11498 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011499 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011500 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011501 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011502 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011503 "The attribute 'base' and the <simpleType> child are "
11504 "mutually exclusive", NULL);
11505 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011506 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011507 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011508 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011509 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011510 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011511 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011512 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011513 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011514 "Either the attribute 'base' or a <simpleType> child "
11515 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011516 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011517 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11518 /*
11519 * Corresponds to <complexType><complexContent><restriction>...
11520 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011521 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011522 * Model groups <all>, <choice> and <sequence>.
11523 */
11524 if (IS_SCHEMA(child, "all")) {
11525 type->subtypes = (xmlSchemaTypePtr)
11526 xmlSchemaParseModelGroup(ctxt, schema, child,
11527 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011528 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011529 } else if (IS_SCHEMA(child, "choice")) {
11530 type->subtypes = (xmlSchemaTypePtr)
11531 xmlSchemaParseModelGroup(ctxt,
11532 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11533 child = child->next;
11534 } else if (IS_SCHEMA(child, "sequence")) {
11535 type->subtypes = (xmlSchemaTypePtr)
11536 xmlSchemaParseModelGroup(ctxt, schema, child,
11537 XML_SCHEMA_TYPE_SEQUENCE, 1);
11538 child = child->next;
11539 /*
11540 * Model group reference <group>.
11541 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011542 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011543 type->subtypes = (xmlSchemaTypePtr)
11544 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011545 /*
11546 * Note that the reference will be resolved in
11547 * xmlSchemaResolveTypeReferences();
11548 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011549 child = child->next;
11550 }
11551 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011552 /*
11553 * Corresponds to <complexType><simpleContent><restriction>...
11554 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011555 * "1.1 the simple type definition corresponding to the <simpleType>
11556 * among the [children] of <restriction> if there is one;"
11557 */
11558 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011559 /*
11560 * We will store the to-be-restricted simple type in
11561 * type->contentTypeDef *temporarily*.
11562 */
11563 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011564 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011565 if ( type->contentTypeDef == NULL)
11566 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011567 child = child->next;
11568 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011569 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011570
11571 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011572 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011573 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011574 /*
11575 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011576 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011577 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011578
Daniel Veillard01fa6152004-06-29 17:04:39 +000011579 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011580 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011581 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011582 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011583 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11584 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011585 * *Single Facet Value*
11586 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011587 while ((IS_SCHEMA(child, "minInclusive")) ||
11588 (IS_SCHEMA(child, "minExclusive")) ||
11589 (IS_SCHEMA(child, "maxInclusive")) ||
11590 (IS_SCHEMA(child, "maxExclusive")) ||
11591 (IS_SCHEMA(child, "totalDigits")) ||
11592 (IS_SCHEMA(child, "fractionDigits")) ||
11593 (IS_SCHEMA(child, "pattern")) ||
11594 (IS_SCHEMA(child, "enumeration")) ||
11595 (IS_SCHEMA(child, "whiteSpace")) ||
11596 (IS_SCHEMA(child, "length")) ||
11597 (IS_SCHEMA(child, "maxLength")) ||
11598 (IS_SCHEMA(child, "minLength"))) {
11599 facet = xmlSchemaParseFacet(ctxt, schema, child);
11600 if (facet != NULL) {
11601 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011602 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011603 else
11604 lastfacet->next = facet;
11605 lastfacet = facet;
11606 lastfacet->next = NULL;
11607 }
11608 child = child->next;
11609 }
11610 /*
11611 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011612 */
11613 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011614 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11615
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011616 facet = type->facets;
11617 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011618 facetLink = (xmlSchemaFacetLinkPtr)
11619 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011620 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011621 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011622 xmlFree(facetLink);
11623 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011624 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011625 facetLink->facet = facet;
11626 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011627 if (lastFacetLink == NULL)
11628 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011629 else
11630 lastFacetLink->next = facetLink;
11631 lastFacetLink = facetLink;
11632 facet = facet->next;
11633 } while (facet != NULL);
11634 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011635 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011636 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11637 /*
11638 * Attribute uses/declarations.
11639 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011640 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11641 (xmlSchemaItemListPtr *) &(type->attrUses),
11642 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11643 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011644 /*
11645 * Attribute wildcard.
11646 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011647 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011648 type->attributeWildcard =
11649 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011650 child = child->next;
11651 }
11652 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011653 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011654 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11655 xmlSchemaPContentErr(ctxt,
11656 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011657 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011658 "annotation?, (group | all | choice | sequence)?, "
11659 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011660 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
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)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11668 } else {
11669 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011670 xmlSchemaPContentErr(ctxt,
11671 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011672 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011673 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11674 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11675 "length | minLength | maxLength | enumeration | whiteSpace | "
11676 "pattern)*))");
11677 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011678 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011679 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011680}
11681
11682/**
11683 * xmlSchemaParseExtension:
11684 * @ctxt: a schema validation context
11685 * @schema: the schema being built
11686 * @node: a subtree containing XML Schema informations
11687 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011688 * Parses an <extension>, which is found inside a
11689 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011690 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011691 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011692 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011693 */
11694static xmlSchemaTypePtr
11695xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011696 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011697{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011698 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011699 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011700 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011701
11702 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11703 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011704 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011705 type = ctxt->ctxtType;
11706 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011707
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011708 /*
11709 * Check for illegal attributes.
11710 */
11711 attr = node->properties;
11712 while (attr != NULL) {
11713 if (attr->ns == NULL) {
11714 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11715 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011716 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011717 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011718 }
11719 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011720 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011721 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011722 }
11723 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011724 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011725
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011726 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011727
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011728 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011729 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011730 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011731 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11732 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011733 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011734 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011735 XML_SCHEMAP_S4S_ATTR_MISSING,
11736 NULL, node, "base", NULL);
11737 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011738 /*
11739 * And now for the children...
11740 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011741 child = node->children;
11742 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011743 /*
11744 * Add the annotation to the type ancestor.
11745 */
11746 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011747 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011748 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011749 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011750 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11751 /*
11752 * Corresponds to <complexType><complexContent><extension>... and:
11753 *
11754 * Model groups <all>, <choice>, <sequence> and <group>.
11755 */
11756 if (IS_SCHEMA(child, "all")) {
11757 type->subtypes = (xmlSchemaTypePtr)
11758 xmlSchemaParseModelGroup(ctxt, schema,
11759 child, XML_SCHEMA_TYPE_ALL, 1);
11760 child = child->next;
11761 } else if (IS_SCHEMA(child, "choice")) {
11762 type->subtypes = (xmlSchemaTypePtr)
11763 xmlSchemaParseModelGroup(ctxt, schema,
11764 child, XML_SCHEMA_TYPE_CHOICE, 1);
11765 child = child->next;
11766 } else if (IS_SCHEMA(child, "sequence")) {
11767 type->subtypes = (xmlSchemaTypePtr)
11768 xmlSchemaParseModelGroup(ctxt, schema,
11769 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11770 child = child->next;
11771 } else if (IS_SCHEMA(child, "group")) {
11772 type->subtypes = (xmlSchemaTypePtr)
11773 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011774 /*
11775 * Note that the reference will be resolved in
11776 * xmlSchemaResolveTypeReferences();
11777 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011778 child = child->next;
11779 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011780 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011781 if (child != NULL) {
11782 /*
11783 * Attribute uses/declarations.
11784 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011785 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11786 (xmlSchemaItemListPtr *) &(type->attrUses),
11787 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11788 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011789 /*
11790 * Attribute wildcard.
11791 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011792 if (IS_SCHEMA(child, "anyAttribute")) {
11793 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011794 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11795 child = child->next;
11796 }
11797 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011798 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011799 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11800 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011801 xmlSchemaPContentErr(ctxt,
11802 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011803 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011804 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011805 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011806 } else {
11807 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011808 xmlSchemaPContentErr(ctxt,
11809 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011810 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011811 "(annotation?, ((attribute | attributeGroup)*, "
11812 "anyAttribute?))");
11813 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011814 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011815 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011816}
11817
11818/**
11819 * xmlSchemaParseSimpleContent:
11820 * @ctxt: a schema validation context
11821 * @schema: the schema being built
11822 * @node: a subtree containing XML Schema informations
11823 *
11824 * parse a XML schema SimpleContent definition
11825 * *WARNING* this interface is highly subject to change
11826 *
11827 * Returns the type definition or NULL in case of error
11828 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011829static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011830xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011831 xmlSchemaPtr schema, xmlNodePtr node,
11832 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011833{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011834 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011835 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011836 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011837
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011838 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11839 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011840 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011841 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011842 /* Not a component, don't create it. */
11843 type = ctxt->ctxtType;
11844 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11845 /*
11846 * Check for illegal attributes.
11847 */
11848 attr = node->properties;
11849 while (attr != NULL) {
11850 if (attr->ns == NULL) {
11851 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011852 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011853 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011854 }
11855 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011856 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011857 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011858 }
11859 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011860 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011861
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011862 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011863
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011864 /*
11865 * And now for the children...
11866 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011867 child = node->children;
11868 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011869 /*
11870 * Add the annotation to the complex type ancestor.
11871 */
11872 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011873 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011874 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011875 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011876 if (child == NULL) {
11877 xmlSchemaPContentErr(ctxt,
11878 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011879 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011880 "(annotation?, (restriction | extension))");
11881 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011882 if (child == NULL) {
11883 xmlSchemaPContentErr(ctxt,
11884 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011885 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011886 "(annotation?, (restriction | extension))");
11887 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011888 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011889 xmlSchemaParseRestriction(ctxt, schema, child,
11890 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011891 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011892 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011893 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011894 xmlSchemaParseExtension(ctxt, schema, child,
11895 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011896 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011897 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011898 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011899 if (child != NULL) {
11900 xmlSchemaPContentErr(ctxt,
11901 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011902 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011903 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011904 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011905 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011906}
11907
11908/**
11909 * xmlSchemaParseComplexContent:
11910 * @ctxt: a schema validation context
11911 * @schema: the schema being built
11912 * @node: a subtree containing XML Schema informations
11913 *
11914 * parse a XML schema ComplexContent definition
11915 * *WARNING* this interface is highly subject to change
11916 *
11917 * Returns the type definition or NULL in case of error
11918 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011919static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011920xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011921 xmlSchemaPtr schema, xmlNodePtr node,
11922 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011923{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011924 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011925 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011926 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011927
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011928 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11929 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011930 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011931 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011932 /* Not a component, don't create it. */
11933 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011934 /*
11935 * Check for illegal attributes.
11936 */
11937 attr = node->properties;
11938 while (attr != NULL) {
11939 if (attr->ns == NULL) {
11940 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011941 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011942 {
11943 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011944 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011945 }
11946 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11947 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011948 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011949 }
11950 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011951 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011952
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011953 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011954
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011955 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011956 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011957 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011958 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011959 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11960 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011961 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011962 child = node->children;
11963 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011964 /*
11965 * Add the annotation to the complex type ancestor.
11966 */
11967 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011968 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011969 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011970 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011971 if (child == NULL) {
11972 xmlSchemaPContentErr(ctxt,
11973 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011974 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011975 NULL, "(annotation?, (restriction | extension))");
11976 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011977 if (child == NULL) {
11978 xmlSchemaPContentErr(ctxt,
11979 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011980 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011981 NULL, "(annotation?, (restriction | extension))");
11982 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011983 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011984 xmlSchemaParseRestriction(ctxt, schema, child,
11985 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011986 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011987 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011988 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011989 xmlSchemaParseExtension(ctxt, schema, child,
11990 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011991 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011992 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011993 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011994 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011995 xmlSchemaPContentErr(ctxt,
11996 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011997 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011998 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011999 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012000 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012001}
12002
12003/**
12004 * xmlSchemaParseComplexType:
12005 * @ctxt: a schema validation context
12006 * @schema: the schema being built
12007 * @node: a subtree containing XML Schema informations
12008 *
12009 * parse a XML schema Complex Type definition
12010 * *WARNING* this interface is highly subject to change
12011 *
12012 * Returns the type definition or NULL in case of error
12013 */
12014static xmlSchemaTypePtr
12015xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012016 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012017{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012018 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012019 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012020 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012021 xmlAttrPtr attr;
12022 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012023#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012024 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012025#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012026 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012027
Daniel Veillard4255d502002-04-16 15:50:10 +000012028
12029 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12030 return (NULL);
12031
Daniel Veillard01fa6152004-06-29 17:04:39 +000012032 ctxtType = ctxt->ctxtType;
12033
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012034 if (topLevel) {
12035 attr = xmlSchemaGetPropNode(node, "name");
12036 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012037 xmlSchemaPMissingAttrErr(ctxt,
12038 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012039 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012040 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012041 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12042 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012043 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012044 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012045
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012046 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012047 /*
12048 * Parse as local complex type definition.
12049 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012050#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012051 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012052 type = xmlSchemaAddType(ctxt, schema,
12053 XML_SCHEMA_TYPE_COMPLEX,
12054 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012055 ctxt->targetNamespace, node, 0);
12056#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012057 type = xmlSchemaAddType(ctxt, schema,
12058 XML_SCHEMA_TYPE_COMPLEX,
12059 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012060#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012061 if (type == NULL)
12062 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012063 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012064 type->node = node;
12065 type->type = XML_SCHEMA_TYPE_COMPLEX;
12066 /*
12067 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012068 */
12069 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012070 /*
12071 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012072 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012073 type = xmlSchemaAddType(ctxt, schema,
12074 XML_SCHEMA_TYPE_COMPLEX,
12075 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012076 if (type == NULL)
12077 return (NULL);
12078 type->node = node;
12079 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012080 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012081 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012082 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012083 /*
12084 * Handle attributes.
12085 */
12086 attr = node->properties;
12087 while (attr != NULL) {
12088 if (attr->ns == NULL) {
12089 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12090 /*
12091 * Attribute "id".
12092 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012093 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012094 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12095 /*
12096 * Attribute "mixed".
12097 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012098 if (xmlSchemaPGetBoolNodeValue(ctxt,
12099 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012100 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12101 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012102 /*
12103 * Attributes of global complex type definitions.
12104 */
12105 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12106 /* Pass. */
12107 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12108 /*
12109 * Attribute "abstract".
12110 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012111 if (xmlSchemaPGetBoolNodeValue(ctxt,
12112 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012113 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12114 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12115 /*
12116 * Attribute "final".
12117 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012118 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012119 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012120 if (xmlSchemaPValAttrBlockFinal(attrValue,
12121 &(type->flags),
12122 -1,
12123 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12124 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12125 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012126 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012127 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012128 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012129 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012130 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012131 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012132 } else
12133 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012134 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12135 /*
12136 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012137 */
12138 attrValue = xmlSchemaGetNodeContent(ctxt,
12139 (xmlNodePtr) attr);
12140 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012141 -1,
12142 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012143 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012144 -1, -1, -1) != 0) {
12145 xmlSchemaPSimpleTypeErr(ctxt,
12146 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012147 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012148 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012149 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012150 } else
12151 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012152 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012153 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012154 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012155 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012156 } else {
12157 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012158 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012159 }
12160 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012161 xmlSchemaPIllegalAttrErr(ctxt,
12162 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012163 }
12164 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012165 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012166 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012167 /*
12168 * Apply default "block" values.
12169 */
12170 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12171 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12172 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12173 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12174 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012175 if (! final) {
12176 /*
12177 * Apply default "block" values.
12178 */
12179 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12180 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12181 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12182 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12183 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012184 /*
12185 * And now for the children...
12186 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012187 child = node->children;
12188 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012189 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012190 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012191 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012192 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012193 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012194 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012195 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012196 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012197 * Specifying mixed='true' when the <simpleContent>
12198 * alternative is chosen has no effect
12199 */
William M. Bracke7091952004-05-11 15:09:58 +000012200 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12201 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012202 xmlSchemaParseSimpleContent(ctxt, schema, child,
12203 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012204 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012205 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012206 /*
12207 * <complexType><complexContent>...
12208 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012209 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012210 xmlSchemaParseComplexContent(ctxt, schema, child,
12211 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012212 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012213 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012214 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012215 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12216 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012217 * SPEC
12218 * "...the third alternative (neither <simpleContent> nor
12219 * <complexContent>) is chosen. This case is understood as shorthand
12220 * for complex content restricting the ·ur-type definition·, and the
12221 * details of the mappings should be modified as necessary.
12222 */
12223 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12224 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012225 /*
12226 * Parse model groups.
12227 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012228 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012229 type->subtypes = (xmlSchemaTypePtr)
12230 xmlSchemaParseModelGroup(ctxt, schema, child,
12231 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012232 child = child->next;
12233 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012234 type->subtypes = (xmlSchemaTypePtr)
12235 xmlSchemaParseModelGroup(ctxt, schema, child,
12236 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012237 child = child->next;
12238 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012239 type->subtypes = (xmlSchemaTypePtr)
12240 xmlSchemaParseModelGroup(ctxt, schema, child,
12241 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012242 child = child->next;
12243 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012244 type->subtypes = (xmlSchemaTypePtr)
12245 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012246 /*
12247 * Note that the reference will be resolved in
12248 * xmlSchemaResolveTypeReferences();
12249 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012250 child = child->next;
12251 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012252 /*
12253 * Parse attribute decls/refs.
12254 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012255 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12256 (xmlSchemaItemListPtr *) &(type->attrUses),
12257 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12258 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012259 /*
12260 * Parse attribute wildcard.
12261 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012262 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012263 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12264 child = child->next;
12265 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012266 }
12267 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012268 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012269 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012270 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012271 NULL, "(annotation?, (simpleContent | complexContent | "
12272 "((group | all | choice | sequence)?, ((attribute | "
12273 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012274 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012275 /*
12276 * REDEFINE: SPEC src-redefine (5)
12277 */
12278 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12279 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012280 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012281 "<complexType> must have a <restriction> or <extension> "
12282 "grand-child", NULL);
12283 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012284 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012285 return (type);
12286}
12287
Daniel Veillard4255d502002-04-16 15:50:10 +000012288/************************************************************************
12289 * *
12290 * Validating using Schemas *
12291 * *
12292 ************************************************************************/
12293
12294/************************************************************************
12295 * *
12296 * Reading/Writing Schemas *
12297 * *
12298 ************************************************************************/
12299
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012300#if 0 /* Will be enabled if it is clear what options are needed. */
12301/**
12302 * xmlSchemaParserCtxtSetOptions:
12303 * @ctxt: a schema parser context
12304 * @options: a combination of xmlSchemaParserOption
12305 *
12306 * Sets the options to be used during the parse.
12307 *
12308 * Returns 0 in case of success, -1 in case of an
12309 * API error.
12310 */
12311static int
12312xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12313 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012314
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012315{
12316 int i;
12317
12318 if (ctxt == NULL)
12319 return (-1);
12320 /*
12321 * WARNING: Change the start value if adding to the
12322 * xmlSchemaParseOption.
12323 */
12324 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12325 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012326 return (-1);
12327 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012328 }
12329 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012330 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012331}
12332
12333/**
12334 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012335 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012336 *
12337 * Returns the option combination of the parser context.
12338 */
12339static int
12340xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012341
12342{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012343 if (ctxt == NULL)
12344 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012345 else
12346 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012347}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012348#endif
12349
Daniel Veillard4255d502002-04-16 15:50:10 +000012350/**
12351 * xmlSchemaNewParserCtxt:
12352 * @URL: the location of the schema
12353 *
12354 * Create an XML Schemas parse context for that file/resource expected
12355 * to contain an XML Schemas file.
12356 *
12357 * Returns the parser context or NULL in case of error
12358 */
12359xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012360xmlSchemaNewParserCtxt(const char *URL)
12361{
Daniel Veillard4255d502002-04-16 15:50:10 +000012362 xmlSchemaParserCtxtPtr ret;
12363
12364 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012365 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012366
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012367 ret = xmlSchemaParserCtxtCreate();
12368 if (ret == NULL)
12369 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012370 ret->dict = xmlDictCreate();
12371 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012372 return (ret);
12373}
12374
12375/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012376 * xmlSchemaNewMemParserCtxt:
12377 * @buffer: a pointer to a char array containing the schemas
12378 * @size: the size of the array
12379 *
12380 * Create an XML Schemas parse context for that memory buffer expected
12381 * to contain an XML Schemas file.
12382 *
12383 * Returns the parser context or NULL in case of error
12384 */
12385xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012386xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12387{
Daniel Veillard6045c902002-10-09 21:13:59 +000012388 xmlSchemaParserCtxtPtr ret;
12389
12390 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012391 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012392 ret = xmlSchemaParserCtxtCreate();
12393 if (ret == NULL)
12394 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012395 ret->buffer = buffer;
12396 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012397 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012398 return (ret);
12399}
12400
12401/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012402 * xmlSchemaNewDocParserCtxt:
12403 * @doc: a preparsed document tree
12404 *
12405 * Create an XML Schemas parse context for that document.
12406 * NB. The document may be modified during the parsing process.
12407 *
12408 * Returns the parser context or NULL in case of error
12409 */
12410xmlSchemaParserCtxtPtr
12411xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12412{
12413 xmlSchemaParserCtxtPtr ret;
12414
12415 if (doc == NULL)
12416 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012417 ret = xmlSchemaParserCtxtCreate();
12418 if (ret == NULL)
12419 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012420 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012421 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012422 /* The application has responsibility for the document */
12423 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012424
12425 return (ret);
12426}
12427
12428/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012429 * xmlSchemaFreeParserCtxt:
12430 * @ctxt: the schema parser context
12431 *
12432 * Free the resources associated to the schema parser context
12433 */
12434void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012435xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12436{
Daniel Veillard4255d502002-04-16 15:50:10 +000012437 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012438 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012439 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012440 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012441 if (ctxt->vctxt != NULL) {
12442 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12443 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012444 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12445 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12446 ctxt->constructor = NULL;
12447 ctxt->ownsConstructor = 0;
12448 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012449 if (ctxt->attrProhibs != NULL)
12450 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012451 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012452 xmlFree(ctxt);
12453}
12454
12455/************************************************************************
12456 * *
12457 * Building the content models *
12458 * *
12459 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012460
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012461static void
12462xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012463 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012464{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012465 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012466 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012467 xmlSchemaSubstGroupPtr substGroup;
12468 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012469
12470 elemDecl = (xmlSchemaElementPtr) particle->children;
12471 /*
12472 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012473 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012474 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012475 if (end == NULL)
12476 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012477 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012478 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012479 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012480 XML_SCHEMAP_INTERNAL,
12481 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12482 "declaration is marked having a subst. group but none "
12483 "available.\n", elemDecl->name, NULL);
12484 return;
12485 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012486 if (counter >= 0) {
12487 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012488 * NOTE that we put the declaration in, even if it's abstract.
12489 * However, an error will be raised during *validation* if an element
12490 * information item shall be validated against an abstract element
12491 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012492 */
12493 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12494 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12495 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12496 /*
12497 * Add subst. group members.
12498 */
12499 for (i = 0; i < substGroup->members->nbItems; i++) {
12500 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12501 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12502 member->name, member->targetNamespace, member);
12503 }
12504 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012505 /*
12506 * NOTE that we put the declaration in, even if it's abstract,
12507 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012508 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012509 xmlAutomataNewTransition2(pctxt->am,
12510 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012511 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12512 /*
12513 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012514 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012515 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012516 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012517 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12518 member->name, member->targetNamespace,
12519 1, 1, member);
12520 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012521 }
12522 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012523 xmlAutomataStatePtr hop;
12524 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12525 UNBOUNDED : particle->maxOccurs - 1;
12526 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12527
12528 counter =
12529 xmlAutomataNewCounter(pctxt->am, minOccurs,
12530 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012531 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012532
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012533 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012534 xmlAutomataNewTransition2(pctxt->am,
12535 start, NULL,
12536 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012537 hop);
12538 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012539 * Add subst. group members.
12540 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012541 for (i = 0; i < substGroup->members->nbItems; i++) {
12542 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12543 xmlAutomataNewEpsilon(pctxt->am,
12544 xmlAutomataNewTransition2(pctxt->am,
12545 start, NULL,
12546 member->name, member->targetNamespace, member),
12547 hop);
12548 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012549 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12550 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12551 }
12552 if (particle->minOccurs == 0)
12553 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012554 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012555}
12556
12557static void
12558xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12559 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012560{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012561 if (((xmlSchemaElementPtr) particle->children)->flags &
12562 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012563 /*
12564 * Substitution groups.
12565 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012566 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012567 } else {
12568 xmlSchemaElementPtr elemDecl;
12569 xmlAutomataStatePtr start;
12570
12571 elemDecl = (xmlSchemaElementPtr) particle->children;
12572
12573 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012574 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012575 if (particle->maxOccurs == 1) {
12576 start = ctxt->state;
12577 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012578 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12579 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12580 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012581 /* Special case. */
12582 start = ctxt->state;
12583 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12584 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012585 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012586 } else {
12587 int counter;
12588 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12589 UNBOUNDED : particle->maxOccurs - 1;
12590 int minOccurs = particle->minOccurs < 1 ?
12591 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012592
12593 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012594 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12595 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12596 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12597 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12598 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12599 NULL, counter);
12600 }
12601 if (particle->minOccurs == 0)
12602 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12603 }
12604}
12605
Daniel Veillard4255d502002-04-16 15:50:10 +000012606/**
12607 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012608 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012609 * @particle: the particle component
12610 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012611 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012612 * Create the automaton for the {content type} of a complex type.
12613 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012614 */
12615static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012616xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012617 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012618{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012619 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012620 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012621 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012622 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012623 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012624 /*
12625 * Just return in this case. A missing "term" of the particle
12626 * might arise due to an invalid "term" component.
12627 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012628 return;
12629 }
12630
12631 switch (particle->children->type) {
12632 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012633 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012634 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012635 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012636
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012637 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012638
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012639 start = pctxt->state;
12640 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012641
12642 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012643 if (wild->any == 1) {
12644 /*
12645 * We need to add both transitions:
12646 *
12647 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012648 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012649 pctxt->state =
12650 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012651 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012652 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012653 /*
12654 * 2. the {"*"} for elements in no namespace.
12655 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012656 pctxt->state =
12657 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012658 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012659 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012660
12661 } else if (wild->nsSet != NULL) {
12662 ns = wild->nsSet;
12663 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012664 pctxt->state = start;
12665 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12666 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12667 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012668 ns = ns->next;
12669 } while (ns != NULL);
12670
12671 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012672 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12673 start, end, BAD_CAST "*", wild->negNsSet->value,
12674 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012675 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012676 } else {
12677 int counter;
12678 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012679 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012680 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012681 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012682 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012683
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012684 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12685 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012686 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012687 pctxt->state =
12688 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012689 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012690 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12691 pctxt->state =
12692 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012693 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012694 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012695 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012696 ns = wild->nsSet;
12697 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012698 pctxt->state =
12699 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012700 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012701 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012702 ns = ns->next;
12703 } while (ns != NULL);
12704
12705 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012707 start, hop, BAD_CAST "*", wild->negNsSet->value,
12708 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012709 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012710 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12711 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012712 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012713 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012714 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012715 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012716 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012717 break;
12718 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012719 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012720 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012721 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012722 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012723 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012724
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012725 /*
12726 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012727 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012728 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012729 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12730 sub = particle->children->children;
12731 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012732 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012733 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012734 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012735 }
12736 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012737 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012738
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012739 if (particle->maxOccurs >= UNBOUNDED) {
12740 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012741 xmlAutomataStatePtr tmp;
12742 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012743
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012744 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012745 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012746 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012747
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012748 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012749 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012750
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012751 sub = particle->children->children;
12752 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012753 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012754 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012755 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012756 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012757 tmp = pctxt->state;
12758 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012759 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012760 pctxt->state =
12761 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012762 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012763
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012764 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012765 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012766 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012767 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012768
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012769 sub = particle->children->children;
12770 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012771 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012772 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012773 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012774 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012775 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012776 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012777 /*
12778 * epsilon needed to block previous trans from
12779 * being allowed to enter back from another
12780 * construct
12781 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012782 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12783 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012784 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012785 xmlAutomataNewEpsilon(pctxt->am,
12786 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012787 }
12788 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012789 } else if ((particle->maxOccurs > 1)
12790 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012791 xmlAutomataStatePtr tmp;
12792 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012793
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012794 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012795 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012796 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012797
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012798 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012799 particle->minOccurs - 1,
12800 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012801
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012802 sub = particle->children->children;
12803 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012804 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012805 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012806 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012807 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012808 tmp = pctxt->state;
12809 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012810 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012811 pctxt->state =
12812 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012813 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012814 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012815 xmlAutomataNewEpsilon(pctxt->am,
12816 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012817 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012818 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012819 sub = particle->children->children;
12820 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012821 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012822 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012823 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012824 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012825 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012826 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12827 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012828 }
12829 }
12830 }
12831 break;
12832 }
12833 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012834 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012835 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012836
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012837 start = pctxt->state;
12838 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012839
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012840 /*
12841 * iterate over the subtypes and remerge the end with an
12842 * epsilon transition
12843 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012844 if (particle->maxOccurs == 1) {
12845 sub = particle->children->children;
12846 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012847 pctxt->state = start;
12848 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012849 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012850 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012851 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012852 }
12853 } else {
12854 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012855 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012856 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12857 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012858 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012859 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012860
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012861 /*
12862 * use a counter to keep track of the number of transtions
12863 * which went through the choice.
12864 */
12865 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012866 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12867 hop = xmlAutomataNewState(pctxt->am);
12868 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012869
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012870 sub = particle->children->children;
12871 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012872 pctxt->state = base;
12873 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012874 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012875 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012876 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012877 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012878 xmlAutomataNewEpsilon(pctxt->am, start, base);
12879 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12880 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012881 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012882 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012883 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012884 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012885 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012886 break;
12887 }
12888 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012889 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012890 xmlSchemaParticlePtr sub;
12891 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012892 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012893
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012894 sub = (xmlSchemaParticlePtr) particle->children->children;
12895 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012896 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012897 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012898 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012899 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012900
12901 elemDecl = (xmlSchemaElementPtr) sub->children;
12902 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012903 PERROR_INT("xmlSchemaBuildAContentModel",
12904 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012905 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012906 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012907 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012908 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012909 * {particles} of the group must be 0 or 1; this is
12910 * already ensured during the parse of the content of
12911 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012912 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012913 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12914 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012915
Daniel Veillarda980bef2005-07-18 21:34:03 +000012916 /*
12917 * This is an abstract group, we need to share
12918 * the same counter for all the element transitions
12919 * derived from the group
12920 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012921 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012922 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012923 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12924 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012925 } else {
12926 if ((sub->minOccurs == 1) &&
12927 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012928 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12929 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012930 elemDecl->name,
12931 elemDecl->targetNamespace,
12932 1, 1, elemDecl);
12933 } else if ((sub->minOccurs == 0) &&
12934 (sub->maxOccurs == 1)) {
12935
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012936 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12937 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012938 elemDecl->name,
12939 elemDecl->targetNamespace,
12940 0,
12941 1,
12942 elemDecl);
12943 }
12944 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012945 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012946 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012947 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012948 pctxt->state =
12949 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012950 break;
12951 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012952 case XML_SCHEMA_TYPE_GROUP:
12953 /*
12954 * If we hit a model group definition, then this means that
12955 * it was empty, thus was not substituted for the containing
12956 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012957 * TODO: But the group should be substituted and not occur at
12958 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012959 */
12960 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012961 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012962 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12963 "xmlSchemaBuildAContentModel",
12964 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012965 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012966 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012967 }
12968}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012969
Daniel Veillard4255d502002-04-16 15:50:10 +000012970/**
12971 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012972 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012973 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012974 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012975 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012976 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012977 */
12978static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012979xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012980 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012981{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012982 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
12983 (type->contModel != NULL) ||
12984 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
12985 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012986 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012987
12988#ifdef DEBUG_CONTENT
12989 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012990 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012991#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012992 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000012993 ctxt->am = xmlNewAutomata();
12994 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012995 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012996 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012997 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012998 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000012999 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013000 /*
13001 * Build the automaton.
13002 */
13003 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013004 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013005 type->contModel = xmlAutomataCompile(ctxt->am);
13006 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013007 xmlSchemaPCustomErr(ctxt,
13008 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013009 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013010 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013011 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013012 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013013 XML_SCHEMAP_NOT_DETERMINISTIC,
13014 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013015 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013016 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013017 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013018#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013019 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013020 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013021 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013022#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013023 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013024 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013025 xmlFreeAutomata(ctxt->am);
13026 ctxt->am = NULL;
13027}
13028
13029/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013030 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013031 * @elem: the schema element context
13032 * @ctxt: the schema parser context
13033 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013034 * Resolves the references of an element declaration
13035 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013036 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013037 */
13038static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013039xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13040 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013041{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013042 if ((ctxt == NULL) || (elemDecl == NULL) ||
13043 ((elemDecl != NULL) &&
13044 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013045 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013046 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013047
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013048 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013049 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013050
13051 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013052 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013053 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013054 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013055 elemDecl->namedTypeNs);
13056 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013057 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013058 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013059 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013060 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013061 XML_SCHEMA_TYPE_BASIC, "type definition");
13062 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013063 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013064 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013065 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013066 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013067
Daniel Veillardc0826a72004-08-10 14:17:33 +000013068 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013069 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013070 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013071 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013072 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13073 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013074 if (substHead == NULL) {
13075 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013076 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013077 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013078 "substitutionGroup", elemDecl->substGroup,
13079 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013080 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013081 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013082 /*
13083 * Set the "substitution group affiliation".
13084 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013085 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013086 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013087 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013088 * The type definitions is set to:
13089 * SPEC "...the {type definition} of the element
13090 * declaration ·resolved· to by the ·actual value·
13091 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013092 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013093 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013094 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013095 }
13096 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013097 /*
13098 * SPEC "The definition of anyType serves as the default type definition
13099 * for element declarations whose XML representation does not specify one."
13100 */
13101 if ((elemDecl->subtypes == NULL) &&
13102 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013103 (elemDecl->substGroup == NULL))
13104 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013105}
13106
13107/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013108 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013109 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013110 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013111 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013112 * Checks and builds the "member type definitions" property of the union
13113 * simple type. This handles part (1), part (2) is done in
13114 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13115 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013116 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013117 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013118static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013119xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13120 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013121{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013122
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013123 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013124 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013125
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013126 /*
13127 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13128 * define the explicit members as the type definitions ·resolved·
13129 * to by the items in the ·actual value· of the memberTypes [attribute],
13130 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013131 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013132 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013133 /*
13134 * Resolve references.
13135 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013136 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013137 lastLink = NULL;
13138 while (link != NULL) {
13139 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013140
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013141 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13142 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13143
13144 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013145 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013146 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013147 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013148 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13149 /*
13150 * Remove the member type link.
13151 */
13152 if (lastLink == NULL)
13153 type->memberTypes = link->next;
13154 else
13155 lastLink->next = link->next;
13156 newLink = link;
13157 link = link->next;
13158 xmlFree(newLink);
13159 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013160 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013161 lastLink = link;
13162 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013163 }
13164 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013165 /*
13166 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013167 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013168 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013169 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013170 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13171 if (link == NULL) {
13172 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13173 return (-1);
13174 }
13175 link->type = memberType;
13176 link->next = NULL;
13177 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013178 type->memberTypes = link;
13179 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013180 lastLink->next = link;
13181 lastLink = link;
13182 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013183 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013184 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013185}
13186
Daniel Veillard4255d502002-04-16 15:50:10 +000013187/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013188 * xmlSchemaIsDerivedFromBuiltInType:
13189 * @ctxt: the schema parser context
13190 * @type: the type definition
13191 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013192 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013193 *
13194 * Returns 1 if the type has the given value type, or
13195 * is derived from such a type.
13196 */
William M. Brack803812b2004-06-03 02:11:24 +000013197static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013198xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013199{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013200 if (type == NULL)
13201 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013202 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013203 return (0);
13204 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13205 if (type->builtInType == valType)
13206 return(1);
13207 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13208 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13209 return (0);
13210 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13211 } else
13212 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013213
13214 return (0);
13215}
13216
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013217#if 0
13218/**
13219 * xmlSchemaIsDerivedFromBuiltInType:
13220 * @ctxt: the schema parser context
13221 * @type: the type definition
13222 * @valType: the value type
13223 *
13224 *
13225 * Returns 1 if the type has the given value type, or
13226 * is derived from such a type.
13227 */
13228static int
13229xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13230{
13231 if (type == NULL)
13232 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013233 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013234 return (0);
13235 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13236 if (type->builtInType == valType)
13237 return(1);
13238 return (0);
13239 } else
13240 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13241
13242 return (0);
13243}
13244#endif
13245
13246static xmlSchemaTypePtr
13247xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13248{
13249 if (type == NULL)
13250 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013251 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013252 return (NULL);
13253 if (type->type == XML_SCHEMA_TYPE_BASIC)
13254 return(type);
13255 else
13256 return(xmlSchemaQueryBuiltInType(type->subtypes));
13257
13258 return (NULL);
13259}
13260
Daniel Veillard3646d642004-06-02 19:19:14 +000013261/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013262 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013263 * @type: the simpleType definition
13264 *
13265 * Returns the primitive type of the given type or
13266 * NULL in case of error.
13267 */
13268static xmlSchemaTypePtr
13269xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13270{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013271
Daniel Veillard01fa6152004-06-29 17:04:39 +000013272 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013273 /*
13274 * Note that anySimpleType is actually not a primitive type
13275 * but we need that here.
13276 */
13277 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13278 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013279 return (type);
13280 type = type->baseType;
13281 }
13282
13283 return (NULL);
13284}
13285
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013286#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013287/**
13288 * xmlSchemaGetBuiltInTypeAncestor:
13289 * @type: the simpleType definition
13290 *
13291 * Returns the primitive type of the given type or
13292 * NULL in case of error.
13293 */
13294static xmlSchemaTypePtr
13295xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13296{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013297 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013298 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013299 while (type != NULL) {
13300 if (type->type == XML_SCHEMA_TYPE_BASIC)
13301 return (type);
13302 type = type->baseType;
13303 }
13304
13305 return (NULL);
13306}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013307#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013308
Daniel Veillard01fa6152004-06-29 17:04:39 +000013309/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013310 * xmlSchemaCloneWildcardNsConstraints:
13311 * @ctxt: the schema parser context
13312 * @dest: the destination wildcard
13313 * @source: the source wildcard
13314 *
13315 * Clones the namespace constraints of source
13316 * and assignes them to dest.
13317 * Returns -1 on internal error, 0 otherwise.
13318 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013319static int
13320xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013321 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013322 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013323{
13324 xmlSchemaWildcardNsPtr cur, tmp, last;
13325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013326 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013327 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013328 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013329 cur = source->nsSet;
13330 last = NULL;
13331 while (cur != NULL) {
13332 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13333 if (tmp == NULL)
13334 return(-1);
13335 tmp->value = cur->value;
13336 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013337 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013338 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013339 last->next = tmp;
13340 last = tmp;
13341 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013342 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013343 if (dest->negNsSet != NULL)
13344 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013345 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013346 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13347 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013348 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013349 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013350 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013351 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013352 return(0);
13353}
13354
Daniel Veillard50355f02004-06-08 17:52:16 +000013355/**
13356 * xmlSchemaUnionWildcards:
13357 * @ctxt: the schema parser context
13358 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013359 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013360 *
13361 * Unions the namespace constraints of the given wildcards.
13362 * @completeWild will hold the resulting union.
13363 * Returns a positive error code on failure, -1 in case of an
13364 * internal error, 0 otherwise.
13365 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013366static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013367xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013368 xmlSchemaWildcardPtr completeWild,
13369 xmlSchemaWildcardPtr curWild)
13370{
13371 xmlSchemaWildcardNsPtr cur, curB, tmp;
13372
13373 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013374 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013375 * value.
13376 */
13377 if ((completeWild->any == curWild->any) &&
13378 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13379 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013380
Daniel Veillard3646d642004-06-02 19:19:14 +000013381 if ((completeWild->negNsSet == NULL) ||
13382 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013383
Daniel Veillard3646d642004-06-02 19:19:14 +000013384 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013385 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013386
13387 /*
13388 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013389 */
13390 cur = completeWild->nsSet;
13391 while (cur != NULL) {
13392 found = 0;
13393 curB = curWild->nsSet;
13394 while (curB != NULL) {
13395 if (cur->value == curB->value) {
13396 found = 1;
13397 break;
13398 }
13399 curB = curB->next;
13400 }
13401 if (!found)
13402 break;
13403 cur = cur->next;
13404 }
13405 if (found)
13406 return(0);
13407 } else
13408 return(0);
13409 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013410 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013411 /*
13412 * 2 If either O1 or O2 is any, then any must be the value
13413 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013414 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013415 if (completeWild->any == 0) {
13416 completeWild->any = 1;
13417 if (completeWild->nsSet != NULL) {
13418 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13419 completeWild->nsSet = NULL;
13420 }
13421 if (completeWild->negNsSet != NULL) {
13422 xmlFree(completeWild->negNsSet);
13423 completeWild->negNsSet = NULL;
13424 }
13425 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013426 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013427 }
13428 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013429 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013430 * then the union of those sets must be the value.
13431 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013432 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013433 int found;
13434 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013435
Daniel Veillard3646d642004-06-02 19:19:14 +000013436 cur = curWild->nsSet;
13437 start = completeWild->nsSet;
13438 while (cur != NULL) {
13439 found = 0;
13440 curB = start;
13441 while (curB != NULL) {
13442 if (cur->value == curB->value) {
13443 found = 1;
13444 break;
13445 }
13446 curB = curB->next;
13447 }
13448 if (!found) {
13449 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013450 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013451 return (-1);
13452 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013453 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013454 completeWild->nsSet = tmp;
13455 }
13456 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013457 }
13458
Daniel Veillard3646d642004-06-02 19:19:14 +000013459 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013460 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013461 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013462 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013463 * or ·absent·), then a pair of not and ·absent· must be the value.
13464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013465 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013466 (curWild->negNsSet != NULL) &&
13467 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13468 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013469
13470 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013471 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013472 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013473 * 5.
13474 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013475 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013476 (completeWild->negNsSet->value != NULL) &&
13477 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013478 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013479 (curWild->negNsSet->value != NULL) &&
13480 (completeWild->nsSet != NULL))) {
13481
13482 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483
Daniel Veillard3646d642004-06-02 19:19:14 +000013484 if (completeWild->nsSet != NULL) {
13485 cur = completeWild->nsSet;
13486 curB = curWild->negNsSet;
13487 } else {
13488 cur = curWild->nsSet;
13489 curB = completeWild->negNsSet;
13490 }
13491 nsFound = 0;
13492 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013493 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013494 absentFound = 1;
13495 else if (cur->value == curB->value)
13496 nsFound = 1;
13497 if (nsFound && absentFound)
13498 break;
13499 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013500 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013501
13502 if (nsFound && absentFound) {
13503 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013504 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013505 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013506 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013507 completeWild->any = 1;
13508 if (completeWild->nsSet != NULL) {
13509 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13510 completeWild->nsSet = NULL;
13511 }
13512 if (completeWild->negNsSet != NULL) {
13513 xmlFree(completeWild->negNsSet);
13514 completeWild->negNsSet = NULL;
13515 }
13516 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013517 /*
13518 * 5.2 If the set S includes the negated namespace name
13519 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013520 * be the value.
13521 */
13522 if (completeWild->nsSet != NULL) {
13523 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13524 completeWild->nsSet = NULL;
13525 }
13526 if (completeWild->negNsSet == NULL) {
13527 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13528 if (completeWild->negNsSet == NULL)
13529 return (-1);
13530 }
13531 completeWild->negNsSet->value = NULL;
13532 } else if ((!nsFound) && absentFound) {
13533 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013534 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013535 * namespace name, then the union is not expressible.
13536 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013537 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013538 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013539 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013540 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013541 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013542 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013543 /*
13544 * 5.4 If the set S does not include either the negated namespace
13545 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013546 * and a namespace name must be the value.
13547 */
13548 if (completeWild->negNsSet == NULL) {
13549 if (completeWild->nsSet != NULL) {
13550 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13551 completeWild->nsSet = NULL;
13552 }
13553 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13554 if (completeWild->negNsSet == NULL)
13555 return (-1);
13556 completeWild->negNsSet->value = curWild->negNsSet->value;
13557 }
13558 }
13559 return (0);
13560 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013561 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013562 * 6.
13563 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013564 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013565 (completeWild->negNsSet->value == NULL) &&
13566 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013567 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013568 (curWild->negNsSet->value == NULL) &&
13569 (completeWild->nsSet != NULL))) {
13570
13571 if (completeWild->nsSet != NULL) {
13572 cur = completeWild->nsSet;
13573 } else {
13574 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013575 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013576 while (cur != NULL) {
13577 if (cur->value == NULL) {
13578 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013579 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013580 * value.
13581 */
13582 completeWild->any = 1;
13583 if (completeWild->nsSet != NULL) {
13584 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13585 completeWild->nsSet = NULL;
13586 }
13587 if (completeWild->negNsSet != NULL) {
13588 xmlFree(completeWild->negNsSet);
13589 completeWild->negNsSet = NULL;
13590 }
13591 return (0);
13592 }
13593 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013594 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013595 if (completeWild->negNsSet == NULL) {
13596 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013597 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013598 * and ·absent· must be the value.
13599 */
13600 if (completeWild->nsSet != NULL) {
13601 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13602 completeWild->nsSet = NULL;
13603 }
13604 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13605 if (completeWild->negNsSet == NULL)
13606 return (-1);
13607 completeWild->negNsSet->value = NULL;
13608 }
13609 return (0);
13610 }
13611 return (0);
13612
13613}
13614
Daniel Veillard50355f02004-06-08 17:52:16 +000013615/**
13616 * xmlSchemaIntersectWildcards:
13617 * @ctxt: the schema parser context
13618 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013619 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013620 *
13621 * Intersects the namespace constraints of the given wildcards.
13622 * @completeWild will hold the resulting intersection.
13623 * Returns a positive error code on failure, -1 in case of an
13624 * internal error, 0 otherwise.
13625 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013626static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013627xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013628 xmlSchemaWildcardPtr completeWild,
13629 xmlSchemaWildcardPtr curWild)
13630{
William M. Brack803812b2004-06-03 02:11:24 +000013631 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013632
13633 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013634 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013635 * value.
13636 */
13637 if ((completeWild->any == curWild->any) &&
13638 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13639 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013640
Daniel Veillard3646d642004-06-02 19:19:14 +000013641 if ((completeWild->negNsSet == NULL) ||
13642 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013643
Daniel Veillard3646d642004-06-02 19:19:14 +000013644 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013645 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013646
13647 /*
13648 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013649 */
13650 cur = completeWild->nsSet;
13651 while (cur != NULL) {
13652 found = 0;
13653 curB = curWild->nsSet;
13654 while (curB != NULL) {
13655 if (cur->value == curB->value) {
13656 found = 1;
13657 break;
13658 }
13659 curB = curB->next;
13660 }
13661 if (!found)
13662 break;
13663 cur = cur->next;
13664 }
13665 if (found)
13666 return(0);
13667 } else
13668 return(0);
13669 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013670 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013671 /*
13672 * 2 If either O1 or O2 is any, then the other must be the value.
13673 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013674 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013675 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013676 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013677 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013678 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013679 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013680 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13681 * name or ·absent·) and the other is a set of (namespace names or
13682 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013683 * the set, minus ·absent· if it was in the set, must be the value.
13684 */
13685 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13686 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13687 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013688
Daniel Veillard3646d642004-06-02 19:19:14 +000013689 if (completeWild->nsSet == NULL) {
13690 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013691 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013692 return(-1);
13693 } else
13694 neg = curWild->negNsSet->value;
13695 /*
13696 * Remove absent and negated.
13697 */
13698 prev = NULL;
13699 cur = completeWild->nsSet;
13700 while (cur != NULL) {
13701 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013702 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013703 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013704 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013705 prev->next = cur->next;
13706 xmlFree(cur);
13707 break;
13708 }
13709 prev = cur;
13710 cur = cur->next;
13711 }
13712 if (neg != NULL) {
13713 prev = NULL;
13714 cur = completeWild->nsSet;
13715 while (cur != NULL) {
13716 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013717 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013718 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013719 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013720 prev->next = cur->next;
13721 xmlFree(cur);
13722 break;
13723 }
13724 prev = cur;
13725 cur = cur->next;
13726 }
13727 }
13728
13729 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013730 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013731 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013732 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013733 * then the intersection of those sets must be the value.
13734 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013735 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013736 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013737
Daniel Veillard3646d642004-06-02 19:19:14 +000013738 cur = completeWild->nsSet;
13739 prev = NULL;
13740 while (cur != NULL) {
13741 found = 0;
13742 curB = curWild->nsSet;
13743 while (curB != NULL) {
13744 if (cur->value == curB->value) {
13745 found = 1;
13746 break;
13747 }
13748 curB = curB->next;
13749 }
13750 if (!found) {
13751 if (prev == NULL)
13752 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013753 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013754 prev->next = cur->next;
13755 tmp = cur->next;
13756 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013757 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013758 continue;
13759 }
13760 prev = cur;
13761 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013762 }
13763
Daniel Veillard3646d642004-06-02 19:19:14 +000013764 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013765 }
13766 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013767 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013768 */
13769 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013770 (curWild->negNsSet != NULL) &&
13771 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013772 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013773 (curWild->negNsSet->value != NULL)) {
13774
13775 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013776 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013777 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013778 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013779 }
13780 /*
13781 * 6 If the one is a negation of a namespace name and the other
13782 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013783 * of a namespace name must be the value.
13784 */
13785 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13786 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013787 (completeWild->negNsSet->value == NULL)) {
13788 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013789 }
13790 return(0);
13791}
13792
Daniel Veillard50355f02004-06-08 17:52:16 +000013793/**
13794 * xmlSchemaIsWildcardNsConstraintSubset:
13795 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013796 * @sub: the first wildcard
13797 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013798 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013799 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13800 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013801 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013802 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013803 */
13804static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013805xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13806 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013807{
Daniel Veillard50355f02004-06-08 17:52:16 +000013808 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013809 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013810 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013811 if (super->any)
13812 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013813 /*
13814 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13815 * 2.2 super must be a pair of not and the same value.
13816 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013817 if ((sub->negNsSet != NULL) &&
13818 (super->negNsSet != NULL) &&
13819 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013820 return (0);
13821 /*
13822 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013823 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013824 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013825 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013826 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013827 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013828 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013829 xmlSchemaWildcardNsPtr cur, curB;
13830 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013831
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013832 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013833 while (cur != NULL) {
13834 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013835 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013836 while (curB != NULL) {
13837 if (cur->value == curB->value) {
13838 found = 1;
13839 break;
13840 }
13841 curB = curB->next;
13842 }
13843 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013844 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013845 cur = cur->next;
13846 }
13847 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013848 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013849 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013850 xmlSchemaWildcardNsPtr cur;
13851 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013852 * 3.2.2 super must be a pair of not and a namespace name or
13853 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013854 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013855 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013856 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013857 if (cur->value == super->negNsSet->value)
13858 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013859 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013860 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013861 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013862 }
13863 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013864 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013865}
13866
Daniel Veillard50355f02004-06-08 17:52:16 +000013867static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013868xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013869 int *fixed,
13870 const xmlChar **value,
13871 xmlSchemaValPtr *val)
13872{
13873 *fixed = 0;
13874 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013875 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013876 *val = NULL;
13877
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013878 if (attruse->defValue != NULL) {
13879 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013880 if (val != NULL)
13881 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013882 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013883 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013884 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013885 } else if ((attruse->attrDecl != NULL) &&
13886 (attruse->attrDecl->defValue != NULL)) {
13887 *value = attruse->attrDecl->defValue;
13888 if (val != NULL)
13889 *val = attruse->attrDecl->defVal;
13890 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013891 *fixed = 1;
13892 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013893 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013894 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013895}
Daniel Veillard3646d642004-06-02 19:19:14 +000013896/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013897 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013898 * @wild: the wildcard
13899 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013900 *
13901 * Validation Rule: Wildcard allows Namespace Name
13902 * (cvc-wildcard-namespace)
13903 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013904 * Returns 0 if the given namespace matches the wildcard,
13905 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013906 */
13907static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013908xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13909 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013910{
13911 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013912 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013913
13914 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013915 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013916 else if (wild->nsSet != NULL) {
13917 xmlSchemaWildcardNsPtr cur;
13918
13919 cur = wild->nsSet;
13920 while (cur != NULL) {
13921 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013922 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013923 cur = cur->next;
13924 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013925 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013926 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013927 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013928
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013929 return(1);
13930}
13931
13932#define XML_SCHEMA_ACTION_DERIVE 0
13933#define XML_SCHEMA_ACTION_REDEFINE 1
13934
13935#define WXS_ACTION_STR(a) \
13936((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13937
13938/*
13939* Schema Component Constraint:
13940* Derivation Valid (Restriction, Complex)
13941* derivation-ok-restriction (2) - (4)
13942*
13943* ATTENTION:
13944* In XML Schema 1.1 this will be:
13945* Validation Rule:
13946* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13947*
13948*/
13949static int
13950xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13951 int action,
13952 xmlSchemaBasicItemPtr item,
13953 xmlSchemaBasicItemPtr baseItem,
13954 xmlSchemaItemListPtr uses,
13955 xmlSchemaItemListPtr baseUses,
13956 xmlSchemaWildcardPtr wild,
13957 xmlSchemaWildcardPtr baseWild)
13958{
13959 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013960 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013961 const xmlChar *bEffValue;
13962 int effFixed;
13963
13964 if (uses != NULL) {
13965 for (i = 0; i < uses->nbItems; i++) {
13966 cur = uses->items[i];
13967 found = 0;
13968 if (baseUses == NULL)
13969 goto not_found;
13970 for (j = 0; j < baseUses->nbItems; j++) {
13971 bcur = baseUses->items[j];
13972 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13973 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13974 (WXS_ATTRUSE_DECL_TNS(cur) ==
13975 WXS_ATTRUSE_DECL_TNS(bcur)))
13976 {
13977 /*
13978 * (2.1) "If there is an attribute use in the {attribute
13979 * uses} of the {base type definition} (call this B) whose
13980 * {attribute declaration} has the same {name} and {target
13981 * namespace}, then all of the following must be true:"
13982 */
13983 found = 1;
13984
13985 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
13986 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
13987 {
13988 xmlChar *str = NULL;
13989 /*
13990 * (2.1.1) "one of the following must be true:"
13991 * (2.1.1.1) "B's {required} is false."
13992 * (2.1.1.2) "R's {required} is true."
13993 */
13994 xmlSchemaPAttrUseErr4(pctxt,
13995 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
13996 WXS_ITEM_NODE(item), item, cur,
13997 "The 'optional' attribute use is inconsistent "
13998 "with the corresponding 'required' attribute use of "
13999 "the %s %s",
14000 WXS_ACTION_STR(action),
14001 xmlSchemaGetComponentDesignation(&str, baseItem),
14002 NULL, NULL);
14003 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014004 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014005 } else if (xmlSchemaCheckCOSSTDerivedOK(pctxt,
14006 WXS_ATTRUSE_TYPEDEF(cur),
14007 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14008 {
14009 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14010
14011 /*
14012 * SPEC (2.1.2) "R's {attribute declaration}'s
14013 * {type definition} must be validly derived from
14014 * B's {type definition} given the empty set as
14015 * defined in Type Derivation OK (Simple) (§3.14.6)."
14016 */
14017 xmlSchemaPAttrUseErr4(pctxt,
14018 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14019 WXS_ITEM_NODE(item), item, cur,
14020 "The attribute declaration's %s "
14021 "is not validly derived from "
14022 "the corresponding %s of the "
14023 "attribute declaration in the %s %s",
14024 xmlSchemaGetComponentDesignation(&strA,
14025 WXS_ATTRUSE_TYPEDEF(cur)),
14026 xmlSchemaGetComponentDesignation(&strB,
14027 WXS_ATTRUSE_TYPEDEF(bcur)),
14028 WXS_ACTION_STR(action),
14029 xmlSchemaGetComponentDesignation(&strC, baseItem));
14030 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14031 FREE_AND_NULL(strA);
14032 FREE_AND_NULL(strB);
14033 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014034 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014035 } else {
14036 /*
14037 * 2.1.3 [Definition:] Let the effective value
14038 * constraint of an attribute use be its {value
14039 * constraint}, if present, otherwise its {attribute
14040 * declaration}'s {value constraint} .
14041 */
14042 xmlSchemaGetEffectiveValueConstraint(bcur,
14043 &effFixed, &bEffValue, NULL);
14044 /*
14045 * 2.1.3 ... one of the following must be true
14046 *
14047 * 2.1.3.1 B's ·effective value constraint· is
14048 * ·absent· or default.
14049 */
14050 if ((bEffValue != NULL) &&
14051 (effFixed == 1)) {
14052 const xmlChar *rEffValue = NULL;
14053
14054 xmlSchemaGetEffectiveValueConstraint(bcur,
14055 &effFixed, &rEffValue, NULL);
14056 /*
14057 * 2.1.3.2 R's ·effective value constraint· is
14058 * fixed with the same string as B's.
14059 * MAYBE TODO: Compare the computed values.
14060 * Hmm, it says "same string" so
14061 * string-equality might really be sufficient.
14062 */
14063 if ((effFixed == 0) ||
14064 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14065 {
14066 xmlChar *str = NULL;
14067
14068 xmlSchemaPAttrUseErr4(pctxt,
14069 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14070 WXS_ITEM_NODE(item), item, cur,
14071 "The effective value constraint of the "
14072 "attribute use is inconsistent with "
14073 "its correspondent in the %s %s",
14074 WXS_ACTION_STR(action),
14075 xmlSchemaGetComponentDesignation(&str,
14076 baseItem),
14077 NULL, NULL);
14078 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014079 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014080 }
14081 }
14082 }
14083 break;
14084 }
14085 }
14086not_found:
14087 if (!found) {
14088 /*
14089 * (2.2) "otherwise the {base type definition} must have an
14090 * {attribute wildcard} and the {target namespace} of the
14091 * R's {attribute declaration} must be ·valid· with respect
14092 * to that wildcard, as defined in Wildcard allows Namespace
14093 * Name (§3.10.4)."
14094 */
14095 if ((baseWild == NULL) ||
14096 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14097 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14098 {
14099 xmlChar *str = NULL;
14100
14101 xmlSchemaPAttrUseErr4(pctxt,
14102 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14103 WXS_ITEM_NODE(item), item, cur,
14104 "Neither a matching attribute use, "
14105 "nor a matching wildcard exists in the %s %s",
14106 WXS_ACTION_STR(action),
14107 xmlSchemaGetComponentDesignation(&str, baseItem),
14108 NULL, NULL);
14109 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014110 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014111 }
14112 }
14113 }
14114 }
14115 /*
14116 * SPEC derivation-ok-restriction (3):
14117 * (3) "For each attribute use in the {attribute uses} of the {base type
14118 * definition} whose {required} is true, there must be an attribute
14119 * use with an {attribute declaration} with the same {name} and
14120 * {target namespace} as its {attribute declaration} in the {attribute
14121 * uses} of the complex type definition itself whose {required} is true.
14122 */
14123 if (baseUses != NULL) {
14124 for (j = 0; j < baseUses->nbItems; j++) {
14125 bcur = baseUses->items[j];
14126 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14127 continue;
14128 found = 0;
14129 if (uses != NULL) {
14130 for (i = 0; i < uses->nbItems; i++) {
14131 cur = uses->items[i];
14132 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14133 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14134 (WXS_ATTRUSE_DECL_TNS(cur) ==
14135 WXS_ATTRUSE_DECL_TNS(bcur))) {
14136 found = 1;
14137 break;
14138 }
14139 }
14140 }
14141 if (!found) {
14142 xmlChar *strA = NULL, *strB = NULL;
14143
14144 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14145 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14146 NULL, item,
14147 "A matching attribute use for the "
14148 "'required' %s of the %s %s is missing",
14149 xmlSchemaGetComponentDesignation(&strA, bcur),
14150 WXS_ACTION_STR(action),
14151 xmlSchemaGetComponentDesignation(&strB, baseItem),
14152 NULL);
14153 FREE_AND_NULL(strA);
14154 FREE_AND_NULL(strB);
14155 }
14156 }
14157 }
14158 /*
14159 * derivation-ok-restriction (4)
14160 */
14161 if (wild != NULL) {
14162 /*
14163 * (4) "If there is an {attribute wildcard}, all of the
14164 * following must be true:"
14165 */
14166 if (baseWild == NULL) {
14167 xmlChar *str = NULL;
14168
14169 /*
14170 * (4.1) "The {base type definition} must also have one."
14171 */
14172 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14173 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14174 NULL, item,
14175 "The %s has an attribute wildcard, "
14176 "but the %s %s '%s' does not have one",
14177 WXS_ITEM_TYPE_NAME(item),
14178 WXS_ACTION_STR(action),
14179 WXS_ITEM_TYPE_NAME(baseItem),
14180 xmlSchemaGetComponentQName(&str, baseItem));
14181 FREE_AND_NULL(str);
14182 return(pctxt->err);
14183 } else if ((baseWild->any == 0) &&
14184 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14185 {
14186 xmlChar *str = NULL;
14187 /*
14188 * (4.2) "The complex type definition's {attribute wildcard}'s
14189 * {namespace constraint} must be a subset of the {base type
14190 * definition}'s {attribute wildcard}'s {namespace constraint},
14191 * as defined by Wildcard Subset (§3.10.6)."
14192 */
14193 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14194 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14195 NULL, item,
14196 "The attribute wildcard is not a valid "
14197 "subset of the wildcard in the %s %s '%s'",
14198 WXS_ACTION_STR(action),
14199 WXS_ITEM_TYPE_NAME(baseItem),
14200 xmlSchemaGetComponentQName(&str, baseItem),
14201 NULL);
14202 FREE_AND_NULL(str);
14203 return(pctxt->err);
14204 }
14205 /* 4.3 Unless the {base type definition} is the ·ur-type
14206 * definition·, the complex type definition's {attribute
14207 * wildcard}'s {process contents} must be identical to or
14208 * stronger than the {base type definition}'s {attribute
14209 * wildcard}'s {process contents}, where strict is stronger
14210 * than lax is stronger than skip.
14211 */
14212 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14213 (wild->processContents < baseWild->processContents)) {
14214 xmlChar *str = NULL;
14215 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14216 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14217 NULL, baseItem,
14218 "The {process contents} of the attribute wildcard is "
14219 "weaker than the one in the %s %s '%s'",
14220 WXS_ACTION_STR(action),
14221 WXS_ITEM_TYPE_NAME(baseItem),
14222 xmlSchemaGetComponentQName(&str, baseItem),
14223 NULL);
14224 FREE_AND_NULL(str)
14225 return(pctxt->err);
14226 }
14227 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014228 return(0);
14229}
14230
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014231
14232static int
14233xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14234 xmlSchemaBasicItemPtr item,
14235 xmlSchemaWildcardPtr *completeWild,
14236 xmlSchemaItemListPtr list,
14237 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014238/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014239 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014240 * @ctxt: the schema parser context
14241 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014242 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014243 *
14244 * Builds the wildcard and the attribute uses on the given complex type.
14245 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014246 *
14247 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14248 * strings, so recheck this if we start to hardcode some schemata, since
14249 * they might not be in the same dict.
14250 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014251 */
14252static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014253xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014254 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014255{
14256 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014257 xmlSchemaAttributeUsePtr use;
14258 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014259
Daniel Veillard01fa6152004-06-29 17:04:39 +000014260 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014261 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014262 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014263 return (-1);
14264 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014265 baseType = type->baseType;
14266 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14267 if (xmlSchemaTypeFixup(baseType, pctxt) == -1)
14268 return(-1);
14269
14270 uses = type->attrUses;
14271 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014272 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014273 * Expand attribute group references. And build the 'complete'
14274 * wildcard, i.e. intersect multiple wildcards.
14275 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014276 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014277 if (uses != NULL) {
14278 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014279 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014280 * This one will transfer all attr. prohibitions
14281 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014282 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014283 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14284 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14285 pctxt->attrProhibs) == -1)
14286 {
14287 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14288 "failed to expand attributes");
14289 }
14290 if (pctxt->attrProhibs->nbItems != 0)
14291 prohibs = pctxt->attrProhibs;
14292 } else {
14293 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14294 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14295 NULL) == -1)
14296 {
14297 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14298 "failed to expand attributes");
14299 }
14300 }
14301 }
14302 /*
14303 * Inherit the attribute uses of the base type.
14304 */
14305 if (baseUses != NULL) {
14306 int i, j;
14307 xmlSchemaAttributeUseProhibPtr pro;
14308
14309 if (WXS_IS_RESTRICTION(type)) {
14310 int usesCount;
14311 xmlSchemaAttributeUsePtr tmp;
14312
14313 if (uses != NULL)
14314 usesCount = uses->nbItems;
14315 else
14316 usesCount = 0;
14317
14318 /* Restriction. */
14319 for (i = 0; i < baseUses->nbItems; i++) {
14320 use = baseUses->items[i];
14321 if (prohibs) {
14322 /*
14323 * Filter out prohibited uses.
14324 */
14325 for (j = 0; j < prohibs->nbItems; j++) {
14326 pro = prohibs->items[j];
14327 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14328 (WXS_ATTRUSE_DECL_TNS(use) ==
14329 pro->targetNamespace))
14330 {
14331 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014332 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014333 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014334 }
14335 if (usesCount) {
14336 /*
14337 * Filter out existing uses.
14338 */
14339 for (j = 0; j < usesCount; j++) {
14340 tmp = uses->items[j];
14341 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14342 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14343 (WXS_ATTRUSE_DECL_TNS(use) ==
14344 WXS_ATTRUSE_DECL_TNS(tmp)))
14345 {
14346 goto inherit_next;
14347 }
14348 }
14349 }
14350 if (uses == NULL) {
14351 type->attrUses = xmlSchemaItemListCreate();
14352 if (type->attrUses == NULL)
14353 goto exit_failure;
14354 uses = type->attrUses;
14355 }
14356 xmlSchemaItemListAddSize(uses, 2, use);
14357inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014358 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014359 } else {
14360 /* Extension. */
14361 for (i = 0; i < baseUses->nbItems; i++) {
14362 use = baseUses->items[i];
14363 if (uses == NULL) {
14364 type->attrUses = xmlSchemaItemListCreate();
14365 if (type->attrUses == NULL)
14366 goto exit_failure;
14367 uses = type->attrUses;
14368 }
14369 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14370 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014371 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014372 }
14373 /*
14374 * Shrink attr. uses.
14375 */
14376 if (uses) {
14377 if (uses->nbItems == 0) {
14378 xmlSchemaItemListFree(uses);
14379 type->attrUses = NULL;
14380 }
14381 /*
14382 * TODO: We could shrink the size of the array
14383 * to fit the actual number of items.
14384 */
14385 }
14386 /*
14387 * Compute the complete wildcard.
14388 */
14389 if (WXS_IS_EXTENSION(type)) {
14390 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014391 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014392 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14393 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014394 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014395 if (type->attributeWildcard != NULL) {
14396 /*
14397 * Union the complete wildcard with the base wildcard.
14398 * SPEC {attribute wildcard}
14399 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14400 * and {annotation} are those of the ·complete wildcard·,
14401 * and whose {namespace constraint} is the intensional union
14402 * of the {namespace constraint} of the ·complete wildcard·
14403 * and of the ·base wildcard·, as defined in Attribute
14404 * Wildcard Union (§3.10.6)."
14405 */
14406 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14407 baseType->attributeWildcard) == -1)
14408 goto exit_failure;
14409 } else {
14410 /*
14411 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14412 * then the ·base wildcard·."
14413 */
14414 type->attributeWildcard = baseType->attributeWildcard;
14415 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014416 } else {
14417 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014418 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14419 * ·complete wildcard"
14420 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014421 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014422 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014423 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014424 /*
14425 * SPEC {attribute wildcard}
14426 * (3.1) "If the <restriction> alternative is chosen, then the
14427 * ·complete wildcard·;"
14428 * NOOP
14429 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014430 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014431
Daniel Veillard3646d642004-06-02 19:19:14 +000014432 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014433
14434exit_failure:
14435 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014436}
14437
14438/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014439 * xmlSchemaTypeFinalContains:
14440 * @schema: the schema
14441 * @type: the type definition
14442 * @final: the final
14443 *
14444 * Evaluates if a type definition contains the given "final".
14445 * This does take "finalDefault" into account as well.
14446 *
14447 * Returns 1 if the type does containt the given "final",
14448 * 0 otherwise.
14449 */
14450static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014451xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014452{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014453 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014454 return (0);
14455 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014456 return (1);
14457 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014458 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014459}
14460
14461/**
14462 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14463 * @type: the Union Simple Type
14464 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014465 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014466 * returns NULL otherwise.
14467 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014468static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014469xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14470{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014471 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014472 if (type->memberTypes != NULL)
14473 return (type->memberTypes);
14474 else
14475 type = type->baseType;
14476 }
14477 return (NULL);
14478}
14479
14480/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014481 * xmlSchemaGetParticleTotalRangeMin:
14482 * @particle: the particle
14483 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014484 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014485 * (all and sequence) + (choice)
14486 *
14487 * Returns the minimun Effective Total Range.
14488 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014489static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014490xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014491{
14492 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014493 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014494 return (0);
14495 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014496 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014497 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014498 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014499
14500 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014501 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014502 while (part != NULL) {
14503 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14504 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014505 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014506 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014507 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014508 if (cur == 0)
14509 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014510 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014511 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014512 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014513 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014514 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014515 } else {
14516 /* <all> and <sequence> */
14517 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014518 xmlSchemaParticlePtr part =
14519 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014520
14521 if (part == NULL)
14522 return (0);
14523 do {
14524 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14525 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014526 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014527 else
14528 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014529 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014530 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014531 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014532 }
14533}
14534
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014535/**
14536 * xmlSchemaGetParticleTotalRangeMax:
14537 * @particle: the particle
14538 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014539 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014540 * (all and sequence) + (choice)
14541 *
14542 * Returns the maximum Effective Total Range.
14543 */
14544static int
14545xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14546{
14547 if ((particle->children == NULL) ||
14548 (particle->children->children == NULL))
14549 return (0);
14550 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14551 int max = -1, cur;
14552 xmlSchemaParticlePtr part =
14553 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014554
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014555 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14556 if (part->children == NULL)
14557 continue;
14558 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14559 (part->children->type == XML_SCHEMA_TYPE_ANY))
14560 cur = part->maxOccurs;
14561 else
14562 cur = xmlSchemaGetParticleTotalRangeMax(part);
14563 if (cur == UNBOUNDED)
14564 return (UNBOUNDED);
14565 if ((max < cur) || (max == -1))
14566 max = cur;
14567 }
14568 /* TODO: Handle overflows? */
14569 return (particle->maxOccurs * max);
14570 } else {
14571 /* <all> and <sequence> */
14572 int sum = 0, cur;
14573 xmlSchemaParticlePtr part =
14574 (xmlSchemaParticlePtr) particle->children->children;
14575
14576 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14577 if (part->children == NULL)
14578 continue;
14579 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14580 (part->children->type == XML_SCHEMA_TYPE_ANY))
14581 cur = part->maxOccurs;
14582 else
14583 cur = xmlSchemaGetParticleTotalRangeMax(part);
14584 if (cur == UNBOUNDED)
14585 return (UNBOUNDED);
14586 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14587 return (UNBOUNDED);
14588 sum += cur;
14589 }
14590 /* TODO: Handle overflows? */
14591 return (particle->maxOccurs * sum);
14592 }
14593}
14594
14595/**
14596 * xmlSchemaIsParticleEmptiable:
14597 * @particle: the particle
14598 *
14599 * Schema Component Constraint: Particle Emptiable
14600 * Checks whether the given particle is emptiable.
14601 *
14602 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014603 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014604static int
14605xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14606{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014607 /*
14608 * SPEC (1) "Its {min occurs} is 0."
14609 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014610 if ((particle == NULL) || (particle->minOccurs == 0) ||
14611 (particle->children == NULL))
14612 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014613 /*
14614 * SPEC (2) "Its {term} is a group and the minimum part of the
14615 * effective total range of that group, [...] is 0."
14616 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014617 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014618 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014619 return (1);
14620 }
14621 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014622}
14623
14624/**
14625 * xmlSchemaCheckCOSSTDerivedOK:
14626 * @type: the derived simple type definition
14627 * @baseType: the base type definition
14628 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014629 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014630 * Type Derivation OK (Simple) (cos-st-derived-OK)
14631 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014632 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014633 * derived from @baseType.
14634 *
14635 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014636 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014637static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014638xmlSchemaCheckCOSSTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
14639 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014640 xmlSchemaTypePtr baseType,
14641 int subset)
14642{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014643 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014644 * 1 They are the same type definition.
14645 * TODO: The identy check might have to be more complex than this.
14646 */
14647 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014648 return (0);
14649 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014650 * 2.1 restriction is not in the subset, or in the {final}
14651 * of its own {base type definition};
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014652 * TODO: Revise this, it looks strange.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014653 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014654 if (WXS_IS_TYPE_NOT_FIXED(type))
14655 if (xmlSchemaTypeFixup(type, ctxt) == -1)
14656 return(-1);
14657 if (WXS_IS_TYPE_NOT_FIXED(baseType))
14658 if (xmlSchemaTypeFixup(baseType, ctxt) == -1)
14659 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014660 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014661 (xmlSchemaTypeFinalContains(type->baseType,
14662 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14663 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014664 }
14665 /* 2.2 */
14666 if (type->baseType == baseType) {
14667 /*
14668 * 2.2.1 D's ·base type definition· is B.
14669 */
14670 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014671 }
14672 /*
14673 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14674 * and is validly derived from B given the subset, as defined by this
14675 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014676 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014677 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
14678 (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014679 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014680 return (0);
14681 }
14682 /*
14683 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014684 * definition·.
14685 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014686 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14687 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014688 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014689 }
14690 /*
14691 * 2.2.4 B's {variety} is union and D is validly derived from a type
14692 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014693 * defined by this constraint.
14694 *
14695 * NOTE: This seems not to involve built-in types, since there is no
14696 * built-in Union Simple Type.
14697 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014698 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014699 xmlSchemaTypeLinkPtr cur;
14700
14701 cur = baseType->memberTypes;
14702 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014703 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
14704 if (xmlSchemaTypeFixup(cur->type, ctxt) == -1)
14705 return(-1);
14706 if (xmlSchemaCheckCOSSTDerivedOK(ctxt,
14707 type, cur->type, subset) == 0)
14708 {
14709 /*
14710 * It just has to be validly derived from at least one
14711 * member-type.
14712 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014713 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014714 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014715 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014716 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014717 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014718 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14719}
14720
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014721/**
14722 * xmlSchemaCheckTypeDefCircularInternal:
14723 * @pctxt: the schema parser context
14724 * @ctxtType: the type definition
14725 * @ancestor: an ancestor of @ctxtType
14726 *
14727 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014728 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014729 *
14730 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14731 * circular, 0 otherwise.
14732 */
14733static int
14734xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14735 xmlSchemaTypePtr ctxtType,
14736 xmlSchemaTypePtr ancestor)
14737{
14738 int ret;
14739
14740 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14741 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014742
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014743 if (ctxtType == ancestor) {
14744 xmlSchemaPCustomErr(pctxt,
14745 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014746 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014747 "The definition is circular", NULL);
14748 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14749 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014750 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14751 /*
14752 * Avoid inifinite recursion on circular types not yet checked.
14753 */
14754 return (0);
14755 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014756 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14757 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14758 ancestor->baseType);
14759 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14760 return (ret);
14761}
14762
14763/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014764 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014765 * @item: the complex/simple type definition
14766 * @ctxt: the parser context
14767 * @name: the name
14768 *
14769 * Checks for circular type definitions.
14770 */
14771static void
14772xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014773 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014774{
14775 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014776 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14777 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014778 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014779 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14780 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014781}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014782
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014783/*
14784* Simple Type Definition Representation OK (src-simple-type) 4
14785*
14786* "4 Circular union type definition is disallowed. That is, if the
14787* <union> alternative is chosen, there must not be any entries in the
14788* memberTypes [attribute] at any depth which resolve to the component
14789* corresponding to the <simpleType>."
14790*
14791* Note that this should work on the *representation* of a component,
14792* thus assumes any union types in the member types not being yet
14793* substituted. At this stage we need the variety of the types
14794* to be already computed.
14795*/
14796static int
14797xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14798 xmlSchemaTypePtr ctxType,
14799 xmlSchemaTypeLinkPtr members)
14800{
14801 xmlSchemaTypeLinkPtr member;
14802 xmlSchemaTypePtr memberType;
14803
14804 member = members;
14805 while (member != NULL) {
14806 memberType = member->type;
14807 while ((memberType != NULL) &&
14808 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14809 if (memberType == ctxType) {
14810 xmlSchemaPCustomErr(pctxt,
14811 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014812 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014813 "The union type definition is circular", NULL);
14814 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14815 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014816 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014817 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14818 {
14819 int res;
14820 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14821 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14822 ctxType,
14823 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14824 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14825 if (res != 0)
14826 return(res);
14827 }
14828 memberType = memberType->baseType;
14829 }
14830 member = member->next;
14831 }
14832 return(0);
14833}
14834
14835static int
14836xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14837 xmlSchemaTypePtr type)
14838{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014839 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014840 return(0);
14841 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14842 type->memberTypes));
14843}
14844
Daniel Veillard01fa6152004-06-29 17:04:39 +000014845/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014846 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014847 * @item: the complex/simple type definition
14848 * @ctxt: the parser context
14849 * @name: the name
14850 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014851 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014852 */
14853static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014854xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014855 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014856{
14857 if (typeDef == NULL)
14858 return;
14859
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014860 /*
14861 * Resolve the base type.
14862 */
14863 if (typeDef->baseType == NULL) {
14864 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14865 typeDef->base, typeDef->baseNs);
14866 if (typeDef->baseType == NULL) {
14867 xmlSchemaPResCompAttrErr(ctxt,
14868 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014869 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014870 "base", typeDef->base, typeDef->baseNs,
14871 XML_SCHEMA_TYPE_SIMPLE, NULL);
14872 return;
14873 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014874 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014875 if (WXS_IS_SIMPLE(typeDef)) {
14876 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014877 /*
14878 * Resolve the memberTypes.
14879 */
14880 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14881 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014882 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014883 /*
14884 * Resolve the itemType.
14885 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014886 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14887
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014888 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014889 typeDef->base, typeDef->baseNs);
14890
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014891 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014892 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14893 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014894 typeDef->subtypes = NULL;
14895 xmlSchemaPResCompAttrErr(ctxt,
14896 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014897 WXS_BASIC_CAST typeDef, typeDef->node,
14898 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014899 XML_SCHEMA_TYPE_SIMPLE, NULL);
14900 }
14901 }
14902 return;
14903 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014904 }
14905 /*
14906 * The ball of letters below means, that if we have a particle
14907 * which has a QName-helper component as its {term}, we want
14908 * to resolve it...
14909 */
14910 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14911 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14912 XML_SCHEMA_TYPE_PARTICLE) &&
14913 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14914 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14915 XML_SCHEMA_EXTRA_QNAMEREF))
14916 {
14917 xmlSchemaQNameRefPtr ref =
14918 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14919 xmlSchemaModelGroupDefPtr groupDef;
14920
14921 /*
14922 * URGENT TODO: Test this.
14923 */
14924 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14925 /*
14926 * Resolve the MG definition reference.
14927 */
14928 groupDef =
14929 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14930 ref->itemType, ref->name, ref->targetNamespace);
14931 if (groupDef == NULL) {
14932 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14933 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14934 "ref", ref->name, ref->targetNamespace, ref->itemType,
14935 NULL);
14936 /* Remove the particle. */
14937 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14938 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14939 /* Remove the particle. */
14940 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14941 else {
14942 /*
14943 * Assign the MG definition's {model group} to the
14944 * particle's {term}.
14945 */
14946 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14947
14948 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14949 /*
14950 * SPEC cos-all-limited (1.2)
14951 * "1.2 the {term} property of a particle with
14952 * {max occurs}=1 which is part of a pair which constitutes
14953 * the {content type} of a complex type definition."
14954 */
14955 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14956 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14957 /* TODO: error code */
14958 XML_SCHEMAP_COS_ALL_LIMITED,
14959 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14960 "The particle's {max occurs} must be 1, since the "
14961 "reference resolves to an 'all' model group",
14962 NULL, NULL);
14963 }
14964 }
14965 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014966 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014967}
14968
14969
14970
14971/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014972 * xmlSchemaCheckSTPropsCorrect:
14973 * @ctxt: the schema parser context
14974 * @type: the simple type definition
14975 *
14976 * Checks st-props-correct.
14977 *
14978 * Returns 0 if the properties are correct,
14979 * if not, a positive error code and -1 on internal
14980 * errors.
14981 */
14982static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014983xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014984 xmlSchemaTypePtr type)
14985{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014986 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000014987 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000014988
Daniel Veillardc0826a72004-08-10 14:17:33 +000014989 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014990 /*
14991 * Schema Component Constraint: Simple Type Definition Properties Correct
14992 *
14993 * NOTE: This is somehow redundant, since we actually built a simple type
14994 * to have all the needed information; this acts as an self test.
14995 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014996 /* Base type: If the datatype has been ·derived· by ·restriction·
14997 * then the Simple Type Definition component from which it is ·derived·,
14998 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000014999 */
15000 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015001 /*
15002 * TODO: Think about: "modulo the impact of Missing
15003 * Sub-components (§5.3)."
15004 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015005 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015006 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015007 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015008 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015009 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015010
Daniel Veillard01fa6152004-06-29 17:04:39 +000015011 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015012 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015013 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015014 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015015 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015016 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015017 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015018 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015019 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15020 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015021 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015022 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015023 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015024 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015025 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015026 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015027 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015028 "the simple ur-type definition as base type, not '%s'",
15029 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015030 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015031 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15032 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015033 /*
15034 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015035 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015036 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15037 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015038 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015039 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015040 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015041 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015042 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15043 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015044 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015045
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015046 /*
15047 * 3 The {final} of the {base type definition} must not contain restriction.
15048 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015049 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015050 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15051 xmlSchemaPCustomErr(ctxt,
15052 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015053 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015054 "The 'final' of its base type '%s' must not contain "
15055 "'restriction'",
15056 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015057 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015058 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015059 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015060
15061 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015062 * 2 All simple type definitions must be derived ultimately from the ·simple
15063 * ur-type definition (so· circular definitions are disallowed). That is, it
15064 * must be possible to reach a built-in primitive datatype or the ·simple
15065 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015066 *
15067 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015068 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015069 return (0);
15070}
15071
15072/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015073 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015074 * @ctxt: the schema parser context
15075 * @type: the simple type definition
15076 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015077 * Schema Component Constraint:
15078 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15079
15080 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015081 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015082 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015083 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015084 * a positive error code otherwise.
15085 */
15086static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015087xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015088 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015089{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015090 xmlChar *str = NULL;
15091
Daniel Veillard01fa6152004-06-29 17:04:39 +000015092 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015093 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15094 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015095 return (-1);
15096 }
15097
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015098 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015099 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015100 /*
15101 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015102 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015103 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015104 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015105 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015106 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015107 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015108 "The base type '%s' is not an atomic simple type",
15109 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015110 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015111 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15112 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015113 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015114 * restriction.
15115 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015116 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015117 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015118 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015119 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015120 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015121 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015122 "The final of its base type '%s' must not contain 'restriction'",
15123 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015124 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015125 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15126 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015127
15128 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015129 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015130 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015131 * Primitive datatypes.
15132 */
15133 if (type->facets != NULL) {
15134 xmlSchemaFacetPtr facet;
15135 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015136
Daniel Veillard01fa6152004-06-29 17:04:39 +000015137 primitive = xmlSchemaGetPrimitiveType(type);
15138 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015139 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15140 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015141 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015142 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015143 facet = type->facets;
15144 do {
15145 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015146 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015147 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015148 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015149 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015150 }
15151 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015152 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015153 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015154 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015155 }
15156 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015157 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15158 * of the {base type definition} (call this BF),then the DF's {value}
15159 * must be a valid restriction of BF's {value} as defined in
15160 * [XML Schemas: Datatypes]."
15161 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015162 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015163 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015164 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015165 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015166 xmlSchemaTypePtr itemType = NULL;
15167
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015168 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015169 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015170 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15171 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015172 return (-1);
15173 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015174 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015175 xmlSchemaTypeFixup(itemType, pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015176 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015177 * 2.1 The {item type definition} must have a {variety} of atomic or
15178 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015179 * must be atomic).
15180 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015181 if ((! WXS_IS_ATOMIC(itemType)) &&
15182 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015183 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015184 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015185 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015186 "The item type '%s' does not have a variety of atomic or union",
15187 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015188 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015189 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015190 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015191 xmlSchemaTypeLinkPtr member;
15192
15193 member = itemType->memberTypes;
15194 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015195 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015196 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015197 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015198 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015199 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015200 "member type '%s' of this item type is not atomic",
15201 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015202 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015203 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15204 }
15205 member = member->next;
15206 }
15207 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015208
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015209 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015210 xmlSchemaFacetPtr facet;
15211 /*
15212 * This is the case if we have: <simpleType><list ..
15213 */
15214 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015215 * 2.3.1
15216 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015217 * contain list.
15218 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015219 if (xmlSchemaTypeFinalContains(itemType,
15220 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15221 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015222 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015223 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015224 "The final of its item type '%s' must not contain 'list'",
15225 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015226 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015227 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15228 }
15229 /*
15230 * 2.3.1.2 The {facets} must only contain the whiteSpace
15231 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015232 * OPTIMIZE TODO: the S4S already disallows any facet
15233 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015234 */
15235 if (type->facets != NULL) {
15236 facet = type->facets;
15237 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015238 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015239 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015240 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015241 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015242 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15243 }
15244 facet = facet->next;
15245 } while (facet != NULL);
15246 }
15247 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015248 * MAYBE TODO: (Hmm, not really) Datatypes states:
15249 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15250 * whose ·lexical space· allows space (such as string or anyURI)or
15251 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015252 * ·lexical space· allows space.
15253 */
15254 } else {
15255 /*
15256 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015257 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015258 */
15259 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015260 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015261 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15262 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015263 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015264 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015265 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015266 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015267 "The base type '%s' must be a list type",
15268 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015269 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015270 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15271 }
15272 /*
15273 * 2.3.2.2 The {final} of the {base type definition} must not
15274 * contain restriction.
15275 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015276 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015277 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015278 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015279 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015280 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015281 "The 'final' of the base type '%s' must not contain 'restriction'",
15282 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015283 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015284 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15285 }
15286 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015288 * from the {base type definition}'s {item type definition} given
15289 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15290 */
15291 {
15292 xmlSchemaTypePtr baseItemType;
15293
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015294 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015295 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015296 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15297 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015298 return (-1);
15299 }
15300 if ((itemType != baseItemType) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015301 (xmlSchemaCheckCOSSTDerivedOK(pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015302 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015303 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015304 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015305 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015306 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015307 "The item type '%s' is not validly derived from "
15308 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015309 xmlSchemaGetComponentQName(&str, itemType),
15310 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15311 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015312
15313 FREE_AND_NULL(str)
15314 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015315 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015316 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15317 }
15318 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015319
Daniel Veillard01fa6152004-06-29 17:04:39 +000015320 if (type->facets != NULL) {
15321 xmlSchemaFacetPtr facet;
15322 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015323 /*
15324 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015325 * and enumeration facet components are allowed among the {facets}.
15326 */
15327 facet = type->facets;
15328 do {
15329 switch (facet->type) {
15330 case XML_SCHEMA_FACET_LENGTH:
15331 case XML_SCHEMA_FACET_MINLENGTH:
15332 case XML_SCHEMA_FACET_MAXLENGTH:
15333 case XML_SCHEMA_FACET_WHITESPACE:
15334 /*
15335 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015336 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015337 */
15338 case XML_SCHEMA_FACET_PATTERN:
15339 case XML_SCHEMA_FACET_ENUMERATION:
15340 break;
15341 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015342 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015343 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015344 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015345 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015346 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015347 * invalid facets.
15348 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015349 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015350 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015351 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015352 facet = facet->next;
15353 } while (facet != NULL);
15354 if (ok == 0)
15355 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15356 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015357 * SPEC (2.3.2.5) (same as 1.3.2)
15358 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015359 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015360 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015361 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015362 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015363 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015364 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015365 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015366 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015367 * atomic or list.
15368 */
15369 xmlSchemaTypeLinkPtr member;
15370
15371 member = type->memberTypes;
15372 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015373 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015374 xmlSchemaTypeFixup(member->type, pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015375
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015376 if ((! WXS_IS_ATOMIC(member->type)) &&
15377 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015378 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015379 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015380 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015381 "The member type '%s' is neither an atomic, nor a list type",
15382 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015383 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015384 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15385 }
15386 member = member->next;
15387 }
15388 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015389 * 3.3.1 If the {base type definition} is the ·simple ur-type
15390 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015391 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015392 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015393 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015394 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015395 * {final} which does not contain union.
15396 */
15397 member = type->memberTypes;
15398 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015399 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015400 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015401 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015402 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015403 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015404 "The 'final' of member type '%s' contains 'union'",
15405 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015406 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015407 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15408 }
15409 member = member->next;
15410 }
15411 /*
15412 * 3.3.1.2 The {facets} must be empty.
15413 */
15414 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015415 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015416 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015417 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015418 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015419 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15420 }
15421 } else {
15422 /*
15423 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015424 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015425 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015426 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015427 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015428 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015429 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015430 "The base type '%s' is not a union type",
15431 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015432 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015433 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15434 }
15435 /*
15436 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15437 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015438 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015439 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015440 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015441 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015442 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015443 "The 'final' of its base type '%s' must not contain 'restriction'",
15444 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015445 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015446 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15447 }
15448 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015449 * 3.3.2.3 The {member type definitions}, in order, must be validly
15450 * derived from the corresponding type definitions in the {base
15451 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 * as defined in Type Derivation OK (Simple) (§3.14.6).
15453 */
15454 {
15455 xmlSchemaTypeLinkPtr baseMember;
15456
15457 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015458 * OPTIMIZE: if the type is restricting, it has no local defined
15459 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015460 * thus a check for equality can be skipped.
15461 */
15462 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015463 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015464 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015465 * types of it's base type. This check seems not necessary with
15466 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015467 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015468 */
15469 if (type->memberTypes != NULL) {
15470 member = type->memberTypes;
15471 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015472 if ((member == NULL) && (baseMember != NULL)) {
15473 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15474 "different number of member types in base");
15475 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015476 while (member != NULL) {
15477 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015478 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15479 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015480 }
15481 if ((member->type != baseMember->type) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015482 (xmlSchemaCheckCOSSTDerivedOK(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015483 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015484 xmlChar *strBMT = NULL, *strBT = NULL;
15485
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015486 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015487 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015488 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015489 "The member type %s is not validly "
15490 "derived from its corresponding member "
15491 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015492 xmlSchemaGetComponentQName(&str, member->type),
15493 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15494 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015495 FREE_AND_NULL(str)
15496 FREE_AND_NULL(strBMT)
15497 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015498 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015499 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015500 member = member->next;
15501 baseMember = baseMember->next;
15502 }
15503 }
15504 }
15505 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015506 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015507 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015508 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015509 if (type->facets != NULL) {
15510 xmlSchemaFacetPtr facet;
15511 int ok = 1;
15512
15513 facet = type->facets;
15514 do {
15515 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15516 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015518 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015519 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015520 ok = 0;
15521 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015522 facet = facet->next;
15523 } while (facet != NULL);
15524 if (ok == 0)
15525 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015526
Daniel Veillard01fa6152004-06-29 17:04:39 +000015527 }
15528 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015529 * SPEC (3.3.2.5) (same as 1.3.2)
15530 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015531 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015532 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015533 */
15534 }
15535 }
15536
15537 return (0);
15538}
15539
15540/**
15541 * xmlSchemaCheckSRCSimpleType:
15542 * @ctxt: the schema parser context
15543 * @type: the simple type definition
15544 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015545 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015546 *
15547 * Returns 0 if the constraints are satisfied,
15548 * if not a positive error code and -1 on internal
15549 * errors.
15550 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015551#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015552static int
15553xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15554 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015555{
15556 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015557 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015558 * must satisfy the conditions set out in Constraints on Simple Type
15559 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015560 */
15561 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015562 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015563 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015564 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015565 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015566 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015567 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015568 /*
15569 *
15570 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015571 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015572 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015573 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015574 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015575 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015576 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015577 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015578 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015579 /*
15580 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015581 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015582 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015583 return (0);
15584}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015585#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015586
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015587static int
15588xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15589{
15590 if (ctxt->vctxt == NULL) {
15591 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15592 if (ctxt->vctxt == NULL) {
15593 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015594 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015595 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015596 "failed to create a temp. validation context.\n",
15597 NULL, NULL);
15598 return (-1);
15599 }
15600 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015601 xmlSchemaSetValidErrors(ctxt->vctxt,
15602 ctxt->error, ctxt->warning, ctxt->userData);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015603 }
15604 return (0);
15605}
15606
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015607static int
15608xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15609 xmlNodePtr node,
15610 xmlSchemaTypePtr type,
15611 const xmlChar *value,
15612 xmlSchemaValPtr *retVal,
15613 int fireErrors,
15614 int normalize,
15615 int isNormalized);
15616
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015617/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015618 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015619 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015620 * @type: the simple type definition
15621 * @value: the default value
15622 * @node: an optional node (the holder of the value)
15623 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015624 * Schema Component Constraint: Element Default Valid (Immediate)
15625 * (cos-valid-default)
15626 * This will be used by the parser only. For the validator there's
15627 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015628 *
15629 * Returns 0 if the constraints are satisfied,
15630 * if not, a positive error code and -1 on internal
15631 * errors.
15632 */
15633static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015634xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15635 xmlNodePtr node,
15636 xmlSchemaTypePtr type,
15637 const xmlChar *value,
15638 xmlSchemaValPtr *val)
15639{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015640 int ret = 0;
15641
15642 /*
15643 * cos-valid-default:
15644 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015645 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015646 * definition the appropriate case among the following must be true:
15647 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015648 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015649 /*
15650 * Complex type.
15651 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015652 * SPEC (2.1) "its {content type} must be a simple type definition
15653 * or mixed."
15654 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015655 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015656 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015657 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015658 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15659 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015660 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015661 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015662 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015663 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015664 "For a string to be a valid default, the type definition "
15665 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015666 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015667 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15668 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015669 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015670 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015671 * 1 If the type definition is a simple type definition, then the string
15672 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015673 * Valid (§3.14.4).
15674 *
15675 * AND
15676 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015677 * 2.2.1 If the {content type} is a simple type definition, then the
15678 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015679 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015680 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015681 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015682 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015683 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015684 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015685 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015686 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015687 else
15688 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015689
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015690 if (ret < 0) {
15691 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15692 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015693 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015694
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015695 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015696}
15697
15698/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015699 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015700 * @ctxt: the schema parser context
15701 * @type: the complex type definition
15702 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015703 *.(4.6) Constraints on Complex Type Definition Schema Components
15704 * Schema Component Constraint:
15705 * Complex Type Definition Properties Correct (ct-props-correct)
15706 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015707 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015708 * Returns 0 if the constraints are satisfied, a positive
15709 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015710 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015711static int
15712xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15713 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015714{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015715 /*
15716 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15717 *
15718 * SPEC (1) "The values of the properties of a complex type definition must
15719 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015720 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015721 * Sub-components (§5.3)."
15722 */
15723 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015724 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015725 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015726 /*
15727 * SPEC (2) "If the {base type definition} is a simple type definition,
15728 * the {derivation method} must be extension."
15729 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015730 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015731 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015732 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015733 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015734 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015735 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015736 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015737 /*
15738 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15739 * definition·. That is, it must be possible to reach the ·ur-type
15740 * definition by repeatedly following the {base type definition}."
15741 *
15742 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015743 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015744 /*
15745 * NOTE that (4) and (5) need the following:
15746 * - attribute uses need to be already inherited (apply attr. prohibitions)
15747 * - attribute group references need to be expanded already
15748 * - simple types need to be typefixed already
15749 */
15750 if (type->attrUses &&
15751 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15752 {
15753 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15754 xmlSchemaAttributeUsePtr use, tmp;
15755 int i, j, hasId = 0;
15756
15757 for (i = uses->nbItems -1; i >= 0; i--) {
15758 use = uses->items[i];
15759
15760 /*
15761 * SPEC ct-props-correct
15762 * (4) "Two distinct attribute declarations in the
15763 * {attribute uses} must not have identical {name}s and
15764 * {target namespace}s."
15765 */
15766 if (i > 0) {
15767 for (j = i -1; j >= 0; j--) {
15768 tmp = uses->items[j];
15769 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15770 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15771 (WXS_ATTRUSE_DECL_TNS(use) ==
15772 WXS_ATTRUSE_DECL_TNS(tmp)))
15773 {
15774 xmlChar *str = NULL;
15775
15776 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15777 XML_SCHEMAP_AG_PROPS_CORRECT,
15778 NULL, WXS_BASIC_CAST type,
15779 "Duplicate %s",
15780 xmlSchemaGetComponentDesignation(&str, use),
15781 NULL);
15782 FREE_AND_NULL(str);
15783 /*
15784 * Remove the duplicate.
15785 */
15786 if (xmlSchemaItemListRemove(uses, i) == -1)
15787 goto exit_failure;
15788 goto next_use;
15789 }
15790 }
15791 }
15792 /*
15793 * SPEC ct-props-correct
15794 * (5) "Two distinct attribute declarations in the
15795 * {attribute uses} must not have {type definition}s which
15796 * are or are derived from ID."
15797 */
15798 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15799 if (xmlSchemaIsDerivedFromBuiltInType(
15800 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15801 {
15802 if (hasId) {
15803 xmlChar *str = NULL;
15804
15805 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15806 XML_SCHEMAP_AG_PROPS_CORRECT,
15807 NULL, WXS_BASIC_CAST type,
15808 "There must not exist more than one attribute "
15809 "declaration of type 'xs:ID' "
15810 "(or derived from 'xs:ID'). The %s violates this "
15811 "constraint",
15812 xmlSchemaGetComponentDesignation(&str, use),
15813 NULL);
15814 FREE_AND_NULL(str);
15815 if (xmlSchemaItemListRemove(uses, i) == -1)
15816 goto exit_failure;
15817 }
15818
15819 hasId = 1;
15820 }
15821 }
15822next_use: {}
15823 }
15824 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015825 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015826exit_failure:
15827 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015828}
15829
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015830static int
15831xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15832 xmlSchemaTypePtr typeB)
15833{
15834 /*
15835 * TODO: This should implement component-identity
15836 * in the future.
15837 */
15838 if ((typeA == NULL) || (typeB == NULL))
15839 return (0);
15840 return (typeA == typeB);
15841}
15842
15843/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015844 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015845 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015846 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015847 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015848 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015849 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015850 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015851 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15852 *
15853 * STATUS: completed
15854 *
15855 * Returns 0 if the constraints are satisfied, or 1
15856 * if not.
15857 */
15858static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015859xmlSchemaCheckCOSCTDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15860 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015861 xmlSchemaTypePtr baseType,
15862 int set)
15863{
15864 int equal = xmlSchemaAreEqualTypes(type, baseType);
15865 /* TODO: Error codes. */
15866 /*
15867 * SPEC "For a complex type definition (call it D, for derived)
15868 * to be validly derived from a type definition (call this
15869 * B, for base) given a subset of {extension, restriction}
15870 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015871 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015872 if (! equal) {
15873 /*
15874 * SPEC (1) "If B and D are not the same type definition, then the
15875 * {derivation method} of D must not be in the subset."
15876 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015877 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15878 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015879 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015880 } else {
15881 /*
15882 * SPEC (2.1) "B and D must be the same type definition."
15883 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015884 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015885 }
15886 /*
15887 * SPEC (2.2) "B must be D's {base type definition}."
15888 */
15889 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015890 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015891 /*
15892 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15893 * definition·."
15894 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015895 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015896 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015897
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015898 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015899 /*
15900 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15901 * must be validly derived from B given the subset as defined by this
15902 * constraint."
15903 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015904 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015905 baseType, set));
15906 } else {
15907 /*
15908 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15909 * must be validly derived from B given the subset as defined in Type
15910 * Derivation OK (Simple) (§3.14.6).
15911 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015912 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type->baseType,
15913 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015914 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015915}
15916
15917/**
15918 * xmlSchemaCheckCOSDerivedOK:
15919 * @type: the derived simple type definition
15920 * @baseType: the base type definition
15921 *
15922 * Calls:
15923 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015924 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015925 * Checks wheter @type can be validly derived from @baseType.
15926 *
15927 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015928 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015929static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015930xmlSchemaCheckCOSDerivedOK(xmlSchemaParserCtxtPtr ctxt,
15931 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015932 xmlSchemaTypePtr baseType,
15933 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015934{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015935 if (WXS_IS_SIMPLE(type))
15936 return (xmlSchemaCheckCOSSTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015937 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015938 return (xmlSchemaCheckCOSCTDerivedOK(ctxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015939}
15940
William M. Brack2f2a6632004-08-20 23:09:47 +000015941/**
15942 * xmlSchemaCheckCOSCTExtends:
15943 * @ctxt: the schema parser context
15944 * @type: the complex type definition
15945 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015946 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015947 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015948 * Derivation Valid (Extension) (cos-ct-extends)
15949 *
15950 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015951 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015952 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015953 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015954 *
15955 * Returns 0 if the constraints are satisfied, a positive
15956 * error code if not and -1 if an internal error occured.
15957 */
15958static int
15959xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15960 xmlSchemaTypePtr type)
15961{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015962 xmlSchemaTypePtr base = type->baseType;
15963 /*
15964 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15965 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015966 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015967 /*
15968 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015969 * then all of the following must be true:"
15970 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015971 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015972 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015973 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015974 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000015975 */
15976 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
15977 xmlSchemaPCustomErr(ctxt,
15978 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015979 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000015980 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015981 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000015982 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
15983 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015984
15985 /*
15986 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
15987 * since they are automatically satisfied through the
15988 * inheriting mechanism.
15989 * Note that even if redefining components, the inheriting mechanism
15990 * is used.
15991 */
15992#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000015993 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015994 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015995 * uses}
15996 * of the complex type definition itself, that is, for every attribute
15997 * use in the {attribute uses} of the {base type definition}, there
15998 * must be an attribute use in the {attribute uses} of the complex
15999 * type definition itself whose {attribute declaration} has the same
16000 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016001 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016003 if (base->attrUses != NULL) {
16004 int i, j, found;
16005 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016006
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016007 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16008 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16009 found = 0;
16010 if (type->attrUses != NULL) {
16011 use = (WXS_LIST_CAST type->attrUses)->items[j];
16012 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16013 {
16014 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16015 WXS_ATTRUSE_DECL_NAME(buse)) &&
16016 (WXS_ATTRUSE_DECL_TNS(use) ==
16017 WXS_ATTRUSE_DECL_TNS(buse)) &&
16018 (WXS_ATTRUSE_TYPEDEF(use) ==
16019 WXS_ATTRUSE_TYPEDEF(buse))
16020 {
16021 found = 1;
16022 break;
16023 }
16024 }
16025 }
16026 if (! found) {
16027 xmlChar *str = NULL;
16028
16029 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16030 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16031 NULL, WXS_BASIC_CAST type,
16032 /*
16033 * TODO: The report does not indicate that also the
16034 * type needs to be the same.
16035 */
16036 "This type is missing a matching correspondent "
16037 "for its {base type}'s %s in its {attribute uses}",
16038 xmlSchemaGetComponentDesignation(&str,
16039 buse->children),
16040 NULL);
16041 FREE_AND_NULL(str)
16042 }
16043 }
16044 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016045 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016046 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16047 * definition must also have one, and the base type definition's
16048 * {attribute wildcard}'s {namespace constraint} must be a subset
16049 * of the complex type definition's {attribute wildcard}'s {namespace
16050 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016051 */
16052
16053 /*
16054 * MAYBE TODO: Enable if ever needed. But this will be needed only
16055 * if created the type via a schema construction API.
16056 */
16057 if (base->attributeWildcard != NULL) {
16058 if (type->attributeWilcard == NULL) {
16059 xmlChar *str = NULL;
16060
16061 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16062 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16063 NULL, type,
16064 "The base %s has an attribute wildcard, "
16065 "but this type is missing an attribute wildcard",
16066 xmlSchemaGetComponentDesignation(&str, base));
16067 FREE_AND_NULL(str)
16068
16069 } else if (xmlSchemaCheckCOSNSSubset(
16070 base->attributeWildcard, type->attributeWildcard))
16071 {
16072 xmlChar *str = NULL;
16073
16074 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16075 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16076 NULL, type,
16077 "The attribute wildcard is not a valid "
16078 "superset of the one in the base %s",
16079 xmlSchemaGetComponentDesignation(&str, base));
16080 FREE_AND_NULL(str)
16081 }
16082 }
16083#endif
16084 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016085 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016086 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016087 if ((type->contentTypeDef != NULL) &&
16088 (type->contentTypeDef == base->contentTypeDef)) {
16089 /*
16090 * SPEC (1.4.1) "The {content type} of the {base type definition}
16091 * and the {content type} of the complex type definition itself
16092 * must be the same simple type definition"
16093 * PASS
16094 */
16095 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16096 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16097 /*
16098 * SPEC (1.4.2) "The {content type} of both the {base type
16099 * definition} and the complex type definition itself must
16100 * be empty."
16101 * PASS
16102 */
16103 } else {
16104 /*
16105 * SPEC (1.4.3) "All of the following must be true:"
16106 */
16107 if (type->subtypes == NULL) {
16108 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016109 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016110 * definition itself must specify a particle.
16111 */
16112 xmlSchemaPCustomErr(ctxt,
16113 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016114 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016115 "The content type must specify a particle", NULL);
16116 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16117 }
16118 /*
16119 * SPEC (1.4.3.2) "One of the following must be true:"
16120 */
16121 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16122 /*
16123 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16124 * definition} must be empty.
16125 * PASS
16126 */
16127 } else {
16128 /*
16129 * SPEC (1.4.3.2.2) "All of the following must be true:"
16130 */
16131 if ((type->contentType != base->contentType) ||
16132 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16133 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16134 /*
16135 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16136 * or both must be element-only."
16137 */
16138 xmlSchemaPCustomErr(ctxt,
16139 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016140 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016141 "The content type of both, the type and its base "
16142 "type, must either 'mixed' or 'element-only'", NULL);
16143 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016144 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016145 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016146 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016147 * complex type definition must be a ·valid extension·
16148 * of the {base type definition}'s particle, as defined
16149 * in Particle Valid (Extension) (§3.9.6)."
16150 *
16151 * NOTE that we won't check "Particle Valid (Extension)",
16152 * since it is ensured by the derivation process in
16153 * xmlSchemaTypeFixup(). We need to implement this when heading
16154 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016155 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016156 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016157 }
16158 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016159 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016160 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016161 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016162 } else {
16163 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016164 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016165 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016166 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016167 if (type->contentTypeDef != base) {
16168 /*
16169 * SPEC (2.1) "The {content type} must be the same simple type
16170 * definition."
16171 */
16172 xmlSchemaPCustomErr(ctxt,
16173 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016174 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016175 "The content type must be the simple base type", NULL);
16176 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16177 }
16178 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16179 /*
16180 * SPEC (2.2) "The {final} of the {base type definition} must not
16181 * contain extension"
16182 * NOTE that this is the same as (1.1).
16183 */
16184 xmlSchemaPCustomErr(ctxt,
16185 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016186 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016187 "The 'final' of the base type definition "
16188 "contains 'extension'", NULL);
16189 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016190 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016191 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016192 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016193}
16194
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016195/**
16196 * xmlSchemaCheckDerivationOKRestriction:
16197 * @ctxt: the schema parser context
16198 * @type: the complex type definition
16199 *
16200 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016201 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016202 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16203 *
16204 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016205 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016206 * (5.4.2) ???
16207 *
16208 * ATTENTION:
16209 * In XML Schema 1.1 this will be:
16210 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016211 *
16212 * Returns 0 if the constraints are satisfied, a positive
16213 * error code if not and -1 if an internal error occured.
16214 */
16215static int
16216xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16217 xmlSchemaTypePtr type)
16218{
16219 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016220
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016221 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016222 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016223 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016224 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016225 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016226 if (! WXS_IS_COMPLEX(base)) {
16227 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16228 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16229 type->node, WXS_BASIC_CAST type,
16230 "The base type must be a complex type", NULL, NULL);
16231 return(ctxt->err);
16232 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016233 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16234 /*
16235 * SPEC (1) "The {base type definition} must be a complex type
16236 * definition whose {final} does not contain restriction."
16237 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016238 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16239 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16240 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016241 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016242 "contains 'restriction'", NULL, NULL);
16243 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016244 }
16245 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016246 * SPEC (2), (3) and (4)
16247 * Those are handled in a separate function, since the
16248 * same constraints are needed for redefinition of
16249 * attribute groups as well.
16250 */
16251 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16252 XML_SCHEMA_ACTION_DERIVE,
16253 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16254 type->attrUses, base->attrUses,
16255 type->attributeWildcard,
16256 base->attributeWildcard) == -1)
16257 {
16258 return(-1);
16259 }
16260 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016261 * SPEC (5) "One of the following must be true:"
16262 */
16263 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16264 /*
16265 * SPEC (5.1) "The {base type definition} must be the
16266 * ·ur-type definition·."
16267 * PASS
16268 */
16269 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16270 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16271 /*
16272 * SPEC (5.2.1) "The {content type} of the complex type definition
16273 * must be a simple type definition"
16274 *
16275 * SPEC (5.2.2) "One of the following must be true:"
16276 */
16277 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016278 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16279 {
16280 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016281 /*
16282 * SPEC (5.2.2.1) "The {content type} of the {base type
16283 * definition} must be a simple type definition from which
16284 * the {content type} is validly derived given the empty
16285 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016286 *
16287 * ATTENTION TODO: This seems not needed if the type implicitely
16288 * derived from the base type.
16289 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016290 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016291 err = xmlSchemaCheckCOSSTDerivedOK(ctxt, type->contentTypeDef,
16292 base->contentTypeDef, 0);
16293 if (err != 0) {
16294 xmlChar *strA = NULL, *strB = NULL;
16295
16296 if (err == -1)
16297 return(-1);
16298 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16299 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16300 NULL, WXS_BASIC_CAST type,
16301 "The {content type} %s is not validly derived from the "
16302 "base type's {content type} %s",
16303 xmlSchemaGetComponentDesignation(&strA,
16304 type->contentTypeDef),
16305 xmlSchemaGetComponentDesignation(&strB,
16306 base->contentTypeDef));
16307 FREE_AND_NULL(strA);
16308 FREE_AND_NULL(strB);
16309 return(ctxt->err);
16310 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016311 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16312 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016313 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016314 /*
16315 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16316 * and have a particle which is ·emptiable· as defined in
16317 * Particle Emptiable (§3.9.6)."
16318 * PASS
16319 */
16320 } else {
16321 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016322 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16323 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016324 "The content type of the base type must be either "
16325 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016326 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016327 }
16328 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16329 /*
16330 * SPEC (5.3.1) "The {content type} of the complex type itself must
16331 * be empty"
16332 */
16333 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16334 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016335 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016336 * definition} must also be empty."
16337 * PASS
16338 */
16339 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16340 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16341 xmlSchemaIsParticleEmptiable(
16342 (xmlSchemaParticlePtr) base->subtypes)) {
16343 /*
16344 * SPEC (5.3.2.2) "The {content type} of the {base type
16345 * definition} must be elementOnly or mixed and have a particle
16346 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16347 * PASS
16348 */
16349 } else {
16350 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016351 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16352 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016353 "The content type of the base type must be either "
16354 "empty or 'mixed' (or 'elements-only') and an emptiable "
16355 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016356 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016357 }
16358 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016359 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016360 /*
16361 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16362 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016363 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016364 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016365 /*
16366 * SPEC (5.4.1.2) "The {content type} of the complex type
16367 * definition itself and of the {base type definition} must be
16368 * mixed"
16369 */
16370 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016371 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16372 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016373 "If the content type is 'mixed', then the content type of the "
16374 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016375 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016376 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016377 /*
16378 * SPEC (5.4.2) "The particle of the complex type definition itself
16379 * must be a ·valid restriction· of the particle of the {content
16380 * type} of the {base type definition} as defined in Particle Valid
16381 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016382 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016383 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016384 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016385 } else {
16386 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016387 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16388 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016389 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016390 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016391 }
16392 return (0);
16393}
16394
16395/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016396 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016397 * @ctxt: the schema parser context
16398 * @type: the complex type definition
16399 *
16400 * (3.4.6) Constraints on Complex Type Definition Schema Components
16401 *
16402 * Returns 0 if the constraints are satisfied, a positive
16403 * error code if not and -1 if an internal error occured.
16404 */
16405static int
16406xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16407 xmlSchemaTypePtr type)
16408{
16409 int ret;
16410 /*
16411 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016412 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016413 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16414 if (ret != 0)
16415 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016416 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016417 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16418 else
16419 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16420 return (ret);
16421}
16422
16423/**
16424 * xmlSchemaCheckSRCCT:
16425 * @ctxt: the schema parser context
16426 * @type: the complex type definition
16427 *
16428 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016429 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016430 * Complex Type Definition Representation OK (src-ct)
16431 *
16432 * Returns 0 if the constraints are satisfied, a positive
16433 * error code if not and -1 if an internal error occured.
16434 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016435static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016436xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016437 xmlSchemaTypePtr type)
16438{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016439 xmlSchemaTypePtr base;
16440 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016441
16442 /*
16443 * TODO: Adjust the error codes here, as I used
16444 * XML_SCHEMAP_SRC_CT_1 only yet.
16445 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016446 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016447 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016448 /*
16449 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016450 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016451 * must be a complex type definition;
16452 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016453 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016454 xmlChar *str = NULL;
16455 xmlSchemaPCustomErr(ctxt,
16456 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016457 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016458 "If using <complexContent>, the base type is expected to be "
16459 "a complex type. The base type '%s' is a simple type",
16460 xmlSchemaFormatQName(&str, base->targetNamespace,
16461 base->name));
16462 FREE_AND_NULL(str)
16463 return (XML_SCHEMAP_SRC_CT_1);
16464 }
16465 } else {
16466 /*
16467 * SPEC
16468 * 2 If the <simpleContent> alternative is chosen, all of the
16469 * following must be true:
16470 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16471 * base [attribute] must be one of the following:
16472 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016473 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016474 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016475 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016476 /*
16477 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016478 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016479 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016480 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016481 xmlSchemaPCustomErr(ctxt,
16482 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016483 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016484 "If using <simpleContent> and <restriction>, the base "
16485 "type must be a complex type. The base type '%s' is "
16486 "a simple type",
16487 xmlSchemaFormatQName(&str, base->targetNamespace,
16488 base->name));
16489 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016490 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016491 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016492 } else {
16493 /* Base type is a complex type. */
16494 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16495 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16496 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016497 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016498 * simple type definition;
16499 * PASS
16500 */
16501 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016502 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016503 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016504 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016505 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016506 type->name);
16507 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016508 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016509 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016510 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016511
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016512 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016513 * 2.1.2 only if the <restriction> alternative is also
16514 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016515 * is mixed and a particle emptiable.
16516 */
16517 if (! xmlSchemaIsParticleEmptiable(
16518 (xmlSchemaParticlePtr) base->subtypes)) {
16519 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016520 } else
16521 /*
16522 * Attention: at this point the <simpleType> child is in
16523 * ->contentTypeDef (put there during parsing).
16524 */
16525 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016526 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016527 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016528 * 2.2 If clause 2.1.2 above is satisfied, then there
16529 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016530 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016531 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016532 /* TODO: Change error code to ..._SRC_CT_2_2. */
16533 xmlSchemaPCustomErr(ctxt,
16534 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016535 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016536 "A <simpleType> is expected among the children "
16537 "of <restriction>, if <simpleContent> is used and "
16538 "the base type '%s' is a complex type",
16539 xmlSchemaFormatQName(&str, base->targetNamespace,
16540 base->name));
16541 FREE_AND_NULL(str)
16542 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016543 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016545 ret = XML_SCHEMAP_SRC_CT_1;
16546 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016547 }
16548 if (ret > 0) {
16549 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016550 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016551 xmlSchemaPCustomErr(ctxt,
16552 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016553 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016554 "If <simpleContent> and <restriction> is used, the "
16555 "base type must be a simple type or a complex type with "
16556 "mixed content and particle emptiable. The base type "
16557 "'%s' is none of those",
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 } else {
16561 xmlSchemaPCustomErr(ctxt,
16562 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016563 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016564 "If <simpleContent> and <extension> is used, the "
16565 "base type must be a simple type. The base type '%s' "
16566 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016567 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016568 base->name));
16569 }
16570 FREE_AND_NULL(str)
16571 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016572 }
16573 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016574 * SPEC (3) "The corresponding complex type definition component must
16575 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016576 * Definition Schema Components (§3.4.6);"
16577 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016578 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016579 /*
16580 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016581 * above for {attribute wildcard} is satisfied, the intensional
16582 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016583 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016584 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016585 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016586 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016587}
William M. Brack2f2a6632004-08-20 23:09:47 +000016588
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016589#ifdef ENABLE_PARTICLE_RESTRICTION
16590/**
16591 * xmlSchemaCheckParticleRangeOK:
16592 * @ctxt: the schema parser context
16593 * @type: the complex type definition
16594 *
16595 * (3.9.6) Constraints on Particle Schema Components
16596 * Schema Component Constraint:
16597 * Occurrence Range OK (range-ok)
16598 *
16599 * STATUS: complete
16600 *
16601 * Returns 0 if the constraints are satisfied, a positive
16602 * error code if not and -1 if an internal error occured.
16603 */
16604static int
16605xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16606 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016607{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016608 if (rmin < bmin)
16609 return (1);
16610 if ((bmax != UNBOUNDED) &&
16611 (rmax > bmax))
16612 return (1);
16613 return (0);
16614}
16615
16616/**
16617 * xmlSchemaCheckRCaseNameAndTypeOK:
16618 * @ctxt: the schema parser context
16619 * @r: the restricting element declaration particle
16620 * @b: the base element declaration particle
16621 *
16622 * (3.9.6) Constraints on Particle Schema Components
16623 * Schema Component Constraint:
16624 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16625 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016626 *
16627 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016628 * MISSING (3.2.3)
16629 * CLARIFY: (3.2.2)
16630 *
16631 * Returns 0 if the constraints are satisfied, a positive
16632 * error code if not and -1 if an internal error occured.
16633 */
16634static int
16635xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16636 xmlSchemaParticlePtr r,
16637 xmlSchemaParticlePtr b)
16638{
16639 xmlSchemaElementPtr elemR, elemB;
16640
16641 /* TODO: Error codes (rcase-NameAndTypeOK). */
16642 elemR = (xmlSchemaElementPtr) r->children;
16643 elemB = (xmlSchemaElementPtr) b->children;
16644 /*
16645 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16646 * the same."
16647 */
16648 if ((elemR != elemB) &&
16649 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16650 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16651 return (1);
16652 /*
16653 * SPEC (2) "R's occurrence range is a valid restriction of B's
16654 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16655 */
16656 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16657 b->minOccurs, b->maxOccurs) != 0)
16658 return (1);
16659 /*
16660 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16661 * {scope} are global."
16662 */
16663 if (elemR == elemB)
16664 return (0);
16665 /*
16666 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16667 */
16668 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16669 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16670 return (1);
16671 /*
16672 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16673 * or is not fixed, or R's declaration's {value constraint} is fixed
16674 * with the same value."
16675 */
16676 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16677 ((elemR->value == NULL) ||
16678 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16679 /* TODO: Equality of the initial value or normalized or canonical? */
16680 (! xmlStrEqual(elemR->value, elemB->value))))
16681 return (1);
16682 /*
16683 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16684 * definitions} is a subset of B's declaration's {identity-constraint
16685 * definitions}, if any."
16686 */
16687 if (elemB->idcs != NULL) {
16688 /* TODO */
16689 }
16690 /*
16691 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16692 * superset of B's declaration's {disallowed substitutions}."
16693 */
16694 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16695 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16696 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16697 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16698 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16699 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16700 return (1);
16701 /*
16702 * SPEC (3.2.5) "R's {type definition} is validly derived given
16703 * {extension, list, union} from B's {type definition}"
16704 *
16705 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16706 * set, if the corresponding constraints handle "restriction" and
16707 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016708 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016709 */
16710 {
16711 int set = 0;
16712
16713 set |= SUBSET_EXTENSION;
16714 set |= SUBSET_LIST;
16715 set |= SUBSET_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016716 if (xmlSchemaCheckCOSDerivedOK(elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016717 elemB->subtypes, set) != 0)
16718 return (1);
16719 }
16720 return (0);
16721}
16722
16723/**
16724 * xmlSchemaCheckRCaseNSCompat:
16725 * @ctxt: the schema parser context
16726 * @r: the restricting element declaration particle
16727 * @b: the base wildcard particle
16728 *
16729 * (3.9.6) Constraints on Particle Schema Components
16730 * Schema Component Constraint:
16731 * Particle Derivation OK (Elt:Any -- NSCompat)
16732 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016733 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016734 * STATUS: complete
16735 *
16736 * Returns 0 if the constraints are satisfied, a positive
16737 * error code if not and -1 if an internal error occured.
16738 */
16739static int
16740xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16741 xmlSchemaParticlePtr r,
16742 xmlSchemaParticlePtr b)
16743{
16744 /* TODO:Error codes (rcase-NSCompat). */
16745 /*
16746 * SPEC "For an element declaration particle to be a ·valid restriction·
16747 * of a wildcard particle all of the following must be true:"
16748 *
16749 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16750 * with respect to the wildcard's {namespace constraint} as defined by
16751 * Wildcard allows Namespace Name (§3.10.4)."
16752 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016753 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016754 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16755 return (1);
16756 /*
16757 * SPEC (2) "R's occurrence range is a valid restriction of B's
16758 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16759 */
16760 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16761 b->minOccurs, b->maxOccurs) != 0)
16762 return (1);
16763
16764 return (0);
16765}
16766
16767/**
16768 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16769 * @ctxt: the schema parser context
16770 * @r: the restricting element declaration particle
16771 * @b: the base model group particle
16772 *
16773 * (3.9.6) Constraints on Particle Schema Components
16774 * Schema Component Constraint:
16775 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16776 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016777 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016778 * STATUS: TODO
16779 *
16780 * Returns 0 if the constraints are satisfied, a positive
16781 * error code if not and -1 if an internal error occured.
16782 */
16783static int
16784xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16785 xmlSchemaParticlePtr r,
16786 xmlSchemaParticlePtr b)
16787{
16788 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16789 TODO
16790 return (0);
16791}
16792
16793/**
16794 * xmlSchemaCheckRCaseNSSubset:
16795 * @ctxt: the schema parser context
16796 * @r: the restricting wildcard particle
16797 * @b: the base wildcard particle
16798 *
16799 * (3.9.6) Constraints on Particle Schema Components
16800 * Schema Component Constraint:
16801 * Particle Derivation OK (Any:Any -- NSSubset)
16802 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016803 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016804 * STATUS: complete
16805 *
16806 * Returns 0 if the constraints are satisfied, a positive
16807 * error code if not and -1 if an internal error occured.
16808 */
16809static int
16810xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16811 xmlSchemaParticlePtr r,
16812 xmlSchemaParticlePtr b,
16813 int isAnyTypeBase)
16814{
16815 /* TODO: Error codes (rcase-NSSubset). */
16816 /*
16817 * SPEC (1) "R's occurrence range is a valid restriction of B's
16818 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16819 */
16820 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16821 b->minOccurs, b->maxOccurs))
16822 return (1);
16823 /*
16824 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16825 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16826 */
16827 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16828 (xmlSchemaWildcardPtr) b->children))
16829 return (1);
16830 /*
16831 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16832 * definition·, R's {process contents} must be identical to or stronger
16833 * than B's {process contents}, where strict is stronger than lax is
16834 * stronger than skip."
16835 */
16836 if (! isAnyTypeBase) {
16837 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16838 ((xmlSchemaWildcardPtr) b->children)->processContents)
16839 return (1);
16840 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016841
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016842 return (0);
16843}
16844
16845/**
16846 * xmlSchemaCheckCOSParticleRestrict:
16847 * @ctxt: the schema parser context
16848 * @type: the complex type definition
16849 *
16850 * (3.9.6) Constraints on Particle Schema Components
16851 * Schema Component Constraint:
16852 * Particle Valid (Restriction) (cos-particle-restrict)
16853 *
16854 * STATUS: TODO
16855 *
16856 * Returns 0 if the constraints are satisfied, a positive
16857 * error code if not and -1 if an internal error occured.
16858 */
16859static int
16860xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16861 xmlSchemaParticlePtr r,
16862 xmlSchemaParticlePtr b)
16863{
16864 int ret = 0;
16865
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016866 /*part = WXS_TYPE_PARTICLE(type);
16867 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016868 */
16869
16870 TODO
16871
16872 /*
16873 * SPEC (1) "They are the same particle."
16874 */
16875 if (r == b)
16876 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016877
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016878
16879 return (0);
16880}
16881
16882/**
16883 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16884 * @ctxt: the schema parser context
16885 * @r: the model group particle
16886 * @b: the base wildcard particle
16887 *
16888 * (3.9.6) Constraints on Particle Schema Components
16889 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016890 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016891 * NSRecurseCheckCardinality)
16892 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016893 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016894 * STATUS: TODO: subst-groups
16895 *
16896 * Returns 0 if the constraints are satisfied, a positive
16897 * error code if not and -1 if an internal error occured.
16898 */
16899static int
16900xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16901 xmlSchemaParticlePtr r,
16902 xmlSchemaParticlePtr b)
16903{
16904 xmlSchemaParticlePtr part;
16905 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16906 if ((r->children == NULL) || (r->children->children == NULL))
16907 return (-1);
16908 /*
16909 * SPEC "For a group particle to be a ·valid restriction· of a
16910 * wildcard particle..."
16911 *
16912 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016913 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016914 * Particle Valid (Restriction) (§3.9.6)."
16915 */
16916 part = (xmlSchemaParticlePtr) r->children->children;
16917 do {
16918 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16919 return (1);
16920 part = (xmlSchemaParticlePtr) part->next;
16921 } while (part != NULL);
16922 /*
16923 * SPEC (2) "The effective total range of the group [...] is a
16924 * valid restriction of B's occurrence range as defined by
16925 * Occurrence Range OK (§3.9.6)."
16926 */
16927 if (xmlSchemaCheckParticleRangeOK(
16928 xmlSchemaGetParticleTotalRangeMin(r),
16929 xmlSchemaGetParticleTotalRangeMax(r),
16930 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016931 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016932 return (0);
16933}
16934
16935/**
16936 * xmlSchemaCheckRCaseRecurse:
16937 * @ctxt: the schema parser context
16938 * @r: the <all> or <sequence> model group particle
16939 * @b: the base <all> or <sequence> model group particle
16940 *
16941 * (3.9.6) Constraints on Particle Schema Components
16942 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016943 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016944 Recurse)
16945 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016946 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016947 * STATUS: ?
16948 * TODO: subst-groups
16949 *
16950 * Returns 0 if the constraints are satisfied, a positive
16951 * error code if not and -1 if an internal error occured.
16952 */
16953static int
16954xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16955 xmlSchemaParticlePtr r,
16956 xmlSchemaParticlePtr b)
16957{
16958 /* xmlSchemaParticlePtr part; */
16959 /* TODO: Error codes (rcase-Recurse). */
16960 if ((r->children == NULL) || (b->children == NULL) ||
16961 (r->children->type != b->children->type))
16962 return (-1);
16963 /*
16964 * SPEC "For an all or sequence group particle to be a ·valid
16965 * restriction· of another group particle with the same {compositor}..."
16966 *
16967 * SPEC (1) "R's occurrence range is a valid restriction of B's
16968 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16969 */
16970 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16971 b->minOccurs, b->maxOccurs))
16972 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016973
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016974
16975 return (0);
16976}
16977
16978#endif
16979
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016980#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
16981 xmlSchemaPCustomErrExt(pctxt, \
16982 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016983 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016984 "It is an error for both '%s' and '%s' to be specified on the "\
16985 "same type definition", \
16986 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
16987 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
16988
16989#define FACET_RESTR_ERR(fac1, msg) \
16990 xmlSchemaPCustomErr(pctxt, \
16991 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016992 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016993 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016994
16995#define FACET_RESTR_FIXED_ERR(fac) \
16996 xmlSchemaPCustomErr(pctxt, \
16997 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016998 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016999 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017000 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017001
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017002static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017003xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17004 xmlSchemaFacetPtr facet1,
17005 xmlSchemaFacetPtr facet2,
17006 int lessGreater,
17007 int orEqual,
17008 int ofBase)
17009{
17010 xmlChar *msg = NULL;
17011
17012 msg = xmlStrdup(BAD_CAST "'");
17013 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17014 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17015 if (lessGreater == 0)
17016 msg = xmlStrcat(msg, BAD_CAST " equal to");
17017 if (lessGreater == 1)
17018 msg = xmlStrcat(msg, BAD_CAST " greater than");
17019 else
17020 msg = xmlStrcat(msg, BAD_CAST " less than");
17021
17022 if (orEqual)
17023 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17024 msg = xmlStrcat(msg, BAD_CAST " '");
17025 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17026 if (ofBase)
17027 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17028 else
17029 msg = xmlStrcat(msg, BAD_CAST "'");
17030
17031 xmlSchemaPCustomErr(pctxt,
17032 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017033 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017034 (const char *) msg, NULL);
17035
17036 if (msg != NULL)
17037 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017038}
17039
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017040/*
17041* xmlSchemaDeriveAndValidateFacets:
17042*
17043* Schema Component Constraint: Simple Type Restriction (Facets)
17044* (st-restrict-facets)
17045*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017046static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017047xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17048 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017049{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017050 xmlSchemaTypePtr base = type->baseType;
17051 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017052 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017053 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17054 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17055 fmininc = NULL, fmaxinc = NULL,
17056 fminexc = NULL, fmaxexc = NULL,
17057 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17058 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17059 bfmininc = NULL, bfmaxinc = NULL,
17060 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017061 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017062
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017063 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017064 * SPEC st-restrict-facets 1:
17065 * "The {variety} of R is the same as that of B."
17066 */
17067 /*
17068 * SPEC st-restrict-facets 2:
17069 * "If {variety} is atomic, the {primitive type definition}
17070 * of R is the same as that of B."
17071 *
17072 * NOTE: we leave 1 & 2 out for now, since this will be
17073 * satisfied by the derivation process.
17074 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17075 */
17076 /*
17077 * SPEC st-restrict-facets 3:
17078 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017079 * of B, eliminating duplicates. To eliminate duplicates,
17080 * when a facet of the same kind occurs in both S and the
17081 * {facets} of B, the one in the {facets} of B is not
17082 * included, with the exception of enumeration and pattern
17083 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017084 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017085 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017086
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017087 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17088 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017089
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017090 last = type->facetSet;
17091 if (last != NULL)
17092 while (last->next != NULL)
17093 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017094
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017095 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17096 facet = cur->facet;
17097 switch (facet->type) {
17098 case XML_SCHEMA_FACET_LENGTH:
17099 flength = facet; break;
17100 case XML_SCHEMA_FACET_MINLENGTH:
17101 fminlen = facet; break;
17102 case XML_SCHEMA_FACET_MININCLUSIVE:
17103 fmininc = facet; break;
17104 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17105 fminexc = facet; break;
17106 case XML_SCHEMA_FACET_MAXLENGTH:
17107 fmaxlen = facet; break;
17108 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17109 fmaxinc = facet; break;
17110 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17111 fmaxexc = facet; break;
17112 case XML_SCHEMA_FACET_TOTALDIGITS:
17113 ftotdig = facet; break;
17114 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17115 ffracdig = facet; break;
17116 default:
17117 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017118 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017119 }
17120 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17121 facet = cur->facet;
17122 switch (facet->type) {
17123 case XML_SCHEMA_FACET_LENGTH:
17124 bflength = facet; break;
17125 case XML_SCHEMA_FACET_MINLENGTH:
17126 bfminlen = facet; break;
17127 case XML_SCHEMA_FACET_MININCLUSIVE:
17128 bfmininc = facet; break;
17129 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17130 bfminexc = facet; break;
17131 case XML_SCHEMA_FACET_MAXLENGTH:
17132 bfmaxlen = facet; break;
17133 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17134 bfmaxinc = facet; break;
17135 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17136 bfmaxexc = facet; break;
17137 case XML_SCHEMA_FACET_TOTALDIGITS:
17138 bftotdig = facet; break;
17139 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17140 bffracdig = facet; break;
17141 default:
17142 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017143 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017144 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017145 /*
17146 * length and minLength or maxLength (2.2) + (3.2)
17147 */
17148 if (flength && (fminlen || fmaxlen)) {
17149 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17150 "either of 'minLength' or 'maxLength' to be specified on "
17151 "the same type definition")
17152 }
17153 /*
17154 * Mutual exclusions in the same derivation step.
17155 */
17156 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017157 /*
17158 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017159 */
17160 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17161 }
17162 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017163 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017164 * SCC "minInclusive and minExclusive"
17165 */
17166 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017167 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017168
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017169 if (flength && bflength) {
17170 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017171 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017172 * The values have to be equal.
17173 */
17174 res = xmlSchemaCompareValues(flength->val, bflength->val);
17175 if (res == -2)
17176 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017177 if (res != 0)
17178 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17179 if ((res != 0) && (bflength->fixed)) {
17180 FACET_RESTR_FIXED_ERR(flength)
17181 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017182
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017183 }
17184 if (fminlen && bfminlen) {
17185 /*
17186 * SCC "minLength valid restriction"
17187 * minLength >= BASE minLength
17188 */
17189 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17190 if (res == -2)
17191 goto internal_error;
17192 if (res == -1)
17193 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17194 if ((res != 0) && (bfminlen->fixed)) {
17195 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017196 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017197 }
17198 if (fmaxlen && bfmaxlen) {
17199 /*
17200 * SCC "maxLength valid restriction"
17201 * maxLength <= BASE minLength
17202 */
17203 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17204 if (res == -2)
17205 goto internal_error;
17206 if (res == 1)
17207 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17208 if ((res != 0) && (bfmaxlen->fixed)) {
17209 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017210 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017211 }
17212 /*
17213 * SCC "length and minLength or maxLength"
17214 */
17215 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017216 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017217 if (flength) {
17218 if (! fminlen)
17219 flength = bflength;
17220 if (fminlen) {
17221 /* (1.1) length >= minLength */
17222 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17223 if (res == -2)
17224 goto internal_error;
17225 if (res == -1)
17226 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17227 }
17228 if (! fmaxlen)
17229 fmaxlen = bfmaxlen;
17230 if (fmaxlen) {
17231 /* (2.1) length <= maxLength */
17232 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17233 if (res == -2)
17234 goto internal_error;
17235 if (res == 1)
17236 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17237 }
17238 }
17239 if (fmaxinc) {
17240 /*
17241 * "maxInclusive"
17242 */
17243 if (fmininc) {
17244 /* SCC "maxInclusive >= minInclusive" */
17245 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17246 if (res == -2)
17247 goto internal_error;
17248 if (res == -1) {
17249 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17250 }
17251 }
17252 /*
17253 * SCC "maxInclusive valid restriction"
17254 */
17255 if (bfmaxinc) {
17256 /* maxInclusive <= BASE maxInclusive */
17257 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17258 if (res == -2)
17259 goto internal_error;
17260 if (res == 1)
17261 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17262 if ((res != 0) && (bfmaxinc->fixed)) {
17263 FACET_RESTR_FIXED_ERR(fmaxinc)
17264 }
17265 }
17266 if (bfmaxexc) {
17267 /* maxInclusive < BASE maxExclusive */
17268 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17269 if (res == -2)
17270 goto internal_error;
17271 if (res != -1) {
17272 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17273 }
17274 }
17275 if (bfmininc) {
17276 /* maxInclusive >= BASE minInclusive */
17277 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17278 if (res == -2)
17279 goto internal_error;
17280 if (res == -1) {
17281 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17282 }
17283 }
17284 if (bfminexc) {
17285 /* maxInclusive > BASE minExclusive */
17286 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17287 if (res == -2)
17288 goto internal_error;
17289 if (res != 1) {
17290 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17291 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017292 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017293 }
17294 if (fmaxexc) {
17295 /*
17296 * "maxExclusive >= minExclusive"
17297 */
17298 if (fminexc) {
17299 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17300 if (res == -2)
17301 goto internal_error;
17302 if (res == -1) {
17303 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17304 }
17305 }
17306 /*
17307 * "maxExclusive valid restriction"
17308 */
17309 if (bfmaxexc) {
17310 /* maxExclusive <= BASE maxExclusive */
17311 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17312 if (res == -2)
17313 goto internal_error;
17314 if (res == 1) {
17315 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17316 }
17317 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017318 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017319 }
17320 }
17321 if (bfmaxinc) {
17322 /* maxExclusive <= BASE maxInclusive */
17323 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17324 if (res == -2)
17325 goto internal_error;
17326 if (res == 1) {
17327 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17328 }
17329 }
17330 if (bfmininc) {
17331 /* maxExclusive > BASE minInclusive */
17332 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17333 if (res == -2)
17334 goto internal_error;
17335 if (res != 1) {
17336 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17337 }
17338 }
17339 if (bfminexc) {
17340 /* maxExclusive > BASE minExclusive */
17341 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17342 if (res == -2)
17343 goto internal_error;
17344 if (res != 1) {
17345 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17346 }
17347 }
17348 }
17349 if (fminexc) {
17350 /*
17351 * "minExclusive < maxInclusive"
17352 */
17353 if (fmaxinc) {
17354 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17355 if (res == -2)
17356 goto internal_error;
17357 if (res != -1) {
17358 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17359 }
17360 }
17361 /*
17362 * "minExclusive valid restriction"
17363 */
17364 if (bfminexc) {
17365 /* minExclusive >= BASE minExclusive */
17366 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17367 if (res == -2)
17368 goto internal_error;
17369 if (res == -1) {
17370 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17371 }
17372 if ((res != 0) && (bfminexc->fixed)) {
17373 FACET_RESTR_FIXED_ERR(fminexc)
17374 }
17375 }
17376 if (bfmaxinc) {
17377 /* minExclusive <= BASE maxInclusive */
17378 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17379 if (res == -2)
17380 goto internal_error;
17381 if (res == 1) {
17382 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17383 }
17384 }
17385 if (bfmininc) {
17386 /* minExclusive >= BASE minInclusive */
17387 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17388 if (res == -2)
17389 goto internal_error;
17390 if (res == -1) {
17391 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17392 }
17393 }
17394 if (bfmaxexc) {
17395 /* minExclusive < BASE maxExclusive */
17396 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17397 if (res == -2)
17398 goto internal_error;
17399 if (res != -1) {
17400 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17401 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017402 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017403 }
17404 if (fmininc) {
17405 /*
17406 * "minInclusive < maxExclusive"
17407 */
17408 if (fmaxexc) {
17409 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17410 if (res == -2)
17411 goto internal_error;
17412 if (res != -1) {
17413 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17414 }
17415 }
17416 /*
17417 * "minExclusive valid restriction"
17418 */
17419 if (bfmininc) {
17420 /* minInclusive >= BASE minInclusive */
17421 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17422 if (res == -2)
17423 goto internal_error;
17424 if (res == -1) {
17425 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17426 }
17427 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017428 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017429 }
17430 }
17431 if (bfmaxinc) {
17432 /* minInclusive <= BASE maxInclusive */
17433 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17434 if (res == -2)
17435 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017436 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017437 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17438 }
17439 }
17440 if (bfminexc) {
17441 /* minInclusive > BASE minExclusive */
17442 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17443 if (res == -2)
17444 goto internal_error;
17445 if (res != 1)
17446 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17447 }
17448 if (bfmaxexc) {
17449 /* minInclusive < BASE maxExclusive */
17450 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17451 if (res == -2)
17452 goto internal_error;
17453 if (res != -1)
17454 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17455 }
17456 }
17457 if (ftotdig && bftotdig) {
17458 /*
17459 * SCC " totalDigits valid restriction"
17460 * totalDigits <= BASE totalDigits
17461 */
17462 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17463 if (res == -2)
17464 goto internal_error;
17465 if (res == 1)
17466 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17467 -1, 1, 1);
17468 if ((res != 0) && (bftotdig->fixed)) {
17469 FACET_RESTR_FIXED_ERR(ftotdig)
17470 }
17471 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017472 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017473 /*
17474 * SCC "fractionDigits valid restriction"
17475 * fractionDigits <= BASE fractionDigits
17476 */
17477 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17478 if (res == -2)
17479 goto internal_error;
17480 if (res == 1)
17481 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17482 -1, 1, 1);
17483 if ((res != 0) && (bffracdig->fixed)) {
17484 FACET_RESTR_FIXED_ERR(ffracdig)
17485 }
17486 }
17487 /*
17488 * SCC "fractionDigits less than or equal to totalDigits"
17489 */
17490 if (! ftotdig)
17491 ftotdig = bftotdig;
17492 if (! ffracdig)
17493 ffracdig = bffracdig;
17494 if (ftotdig && ffracdig) {
17495 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17496 if (res == -2)
17497 goto internal_error;
17498 if (res == 1)
17499 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17500 -1, 1, 0);
17501 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017502 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017503 * *Enumerations* won' be added here, since only the first set
17504 * of enumerations in the ancestor-or-self axis is used
17505 * for validation, plus we need to use the base type of those
17506 * enumerations for whitespace.
17507 *
17508 * *Patterns*: won't be add here, since they are ORed at
17509 * type level and ANDed at ancestor level. This will
17510 * happed during validation by walking the base axis
17511 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017512 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017513 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17514 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017515 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017516 * Special handling of enumerations and patterns.
17517 * TODO: hmm, they should not appear in the set, so remove this.
17518 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017519 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017520 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017521 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017522 /*
17523 * Search for a duplicate facet in the current type.
17524 */
17525 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017526 /* err = 0; */
17527 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017528 while (link != NULL) {
17529 facet = link->facet;
17530 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017531 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017532 case XML_SCHEMA_FACET_WHITESPACE:
17533 /*
17534 * The whitespace must be stronger.
17535 */
17536 if (facet->whitespace < bfacet->whitespace) {
17537 FACET_RESTR_ERR(flength,
17538 "The 'whitespace' value has to be equal to "
17539 "or stronger than the 'whitespace' value of "
17540 "the base type")
17541 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017542 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017543 (facet->whitespace != bfacet->whitespace)) {
17544 FACET_RESTR_FIXED_ERR(facet)
17545 }
17546 break;
17547 default:
17548 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017549 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017550 /* Duplicate found. */
17551 break;
17552 }
17553 link = link->next;
17554 }
17555 /*
17556 * If no duplicate was found: add the base types's facet
17557 * to the set.
17558 */
17559 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017560 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017561 xmlMalloc(sizeof(xmlSchemaFacetLink));
17562 if (link == NULL) {
17563 xmlSchemaPErrMemory(pctxt,
17564 "deriving facets, creating a facet link", NULL);
17565 return (-1);
17566 }
17567 link->facet = cur->facet;
17568 link->next = NULL;
17569 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017570 type->facetSet = link;
17571 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 last->next = link;
17573 last = link;
17574 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017575
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017576 }
17577
17578 return (0);
17579internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017580 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17581 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017582 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017583}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017584
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017585static int
17586xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17587 xmlSchemaTypePtr type)
17588{
17589 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17590 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017591 * The actual value is then formed by replacing any union type
17592 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017593 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017594 *
17595 * TODO: There's a bug entry at
17596 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17597 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017598 */
17599 link = type->memberTypes;
17600 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017601
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017602 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017603 xmlSchemaTypeFixup(link->type, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017604
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017605 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017606 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017607 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017608 link->type = subLink->type;
17609 if (subLink->next != NULL) {
17610 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017611 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017612 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017613 while (subLink != NULL) {
17614 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017615 xmlMalloc(sizeof(xmlSchemaTypeLink));
17616 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017617 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017618 NULL);
17619 return (-1);
17620 }
17621 newLink->type = subLink->type;
17622 prevLink->next = newLink;
17623 prevLink = newLink;
17624 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017625
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017626 subLink = subLink->next;
17627 }
17628 }
17629 }
17630 }
17631 link = link->next;
17632 }
17633 return (0);
17634}
17635
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017636static void
17637xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17638{
17639 int has = 0, needVal = 0, normVal = 0;
17640
17641 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17642 if (has) {
17643 needVal = (type->baseType->flags &
17644 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17645 normVal = (type->baseType->flags &
17646 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17647 }
17648 if (type->facets != NULL) {
17649 xmlSchemaFacetPtr fac;
17650
17651 for (fac = type->facets; fac != NULL; fac = fac->next) {
17652 switch (fac->type) {
17653 case XML_SCHEMA_FACET_WHITESPACE:
17654 break;
17655 case XML_SCHEMA_FACET_PATTERN:
17656 normVal = 1;
17657 has = 1;
17658 break;
17659 case XML_SCHEMA_FACET_ENUMERATION:
17660 needVal = 1;
17661 normVal = 1;
17662 has = 1;
17663 break;
17664 default:
17665 has = 1;
17666 break;
17667 }
17668 }
17669 }
17670 if (normVal)
17671 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17672 if (needVal)
17673 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17674 if (has)
17675 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17676
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017677 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017678 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17679 /*
17680 * OPTIMIZE VAL TODO: Some facets need a computed value.
17681 */
17682 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17683 (prim->builtInType != XML_SCHEMAS_STRING)) {
17684 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17685 }
17686 }
17687}
17688
17689static int
17690xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17691{
17692
17693
17694 /*
17695 * Evaluate the whitespace-facet value.
17696 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017697 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017698 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17699 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017700 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017701 return (0);
17702
17703 if (type->facetSet != NULL) {
17704 xmlSchemaFacetLinkPtr lin;
17705
17706 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17707 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17708 switch (lin->facet->whitespace) {
17709 case XML_SCHEMAS_FACET_PRESERVE:
17710 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17711 break;
17712 case XML_SCHEMAS_FACET_REPLACE:
17713 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17714 break;
17715 case XML_SCHEMAS_FACET_COLLAPSE:
17716 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17717 break;
17718 default:
17719 return (-1);
17720 }
17721 return (0);
17722 }
17723 }
17724 }
17725 /*
17726 * For all ·atomic· datatypes other than string (and types ·derived·
17727 * by ·restriction· from it) the value of whiteSpace is fixed to
17728 * collapse
17729 */
17730 {
17731 xmlSchemaTypePtr anc;
17732
17733 for (anc = type->baseType; anc != NULL &&
17734 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17735 anc = anc->baseType) {
17736
17737 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17738 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17739 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17740
17741 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17742 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17743 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17744
17745 } else
17746 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17747 break;
17748 }
17749 }
17750 return (0);
17751 }
17752 return (0);
17753}
17754
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017755static int
17756xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17757 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017758{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017759 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17760 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017761 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017762 return(0);
17763 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017764
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017765 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017766 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017767 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017768 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017769 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017770 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017771 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017772 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017773 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017774 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017775 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017776 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017777 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017778 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017779 * Corresponds to <simpleType><union>...
17780 */
17781 if (type->memberTypes == NULL) {
17782 /*
17783 * This one is really needed, so get out.
17784 */
17785 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17786 "union type has no member-types assigned");
17787 return(-1);
17788 }
17789 } else {
17790 /*
17791 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017792 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017793 if (type->baseType == NULL) {
17794 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17795 "type has no base-type assigned");
17796 return(-1);
17797 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017798 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017799 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17800 return(-1);
17801 /*
17802 * Variety
17803 * If the <restriction> alternative is chosen, then the
17804 * {variety} of the {base type definition}.
17805 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017806 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017807 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017808 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017809 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017810 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017811 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017812 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017813 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017814 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017815 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017816 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017817 * NOTE that we won't assign the memberTypes of the base,
17818 * since this will make trouble when freeing them; we will
17819 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017820 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017821 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017822 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017823 return(0);
17824}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017825
Daniel Veillard8651f532002-04-17 09:06:27 +000017826#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017827xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17828 xmlSchemaTypePtr type)
17829{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017830 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017831 xmlGenericError(xmlGenericErrorContext,
17832 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017833 type->node->doc->URL,
17834 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017835 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017836 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017837 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017838 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017839 switch (type->contentType) {
17840 case XML_SCHEMA_CONTENT_SIMPLE:
17841 xmlGenericError(xmlGenericErrorContext, "simple\n");
17842 break;
17843 case XML_SCHEMA_CONTENT_ELEMENTS:
17844 xmlGenericError(xmlGenericErrorContext, "elements\n");
17845 break;
17846 case XML_SCHEMA_CONTENT_UNKNOWN:
17847 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17848 break;
17849 case XML_SCHEMA_CONTENT_EMPTY:
17850 xmlGenericError(xmlGenericErrorContext, "empty\n");
17851 break;
17852 case XML_SCHEMA_CONTENT_MIXED:
17853 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017854 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017855 xmlGenericError(xmlGenericErrorContext,
17856 "mixed as emptiable particle\n");
17857 else
17858 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17859 break;
17860 /* Removed, since not used. */
17861 /*
17862 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17863 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17864 break;
17865 */
17866 case XML_SCHEMA_CONTENT_BASIC:
17867 xmlGenericError(xmlGenericErrorContext, "basic\n");
17868 break;
17869 default:
17870 xmlGenericError(xmlGenericErrorContext,
17871 "not registered !!!\n");
17872 break;
17873 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017874 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017875}
Daniel Veillard8651f532002-04-17 09:06:27 +000017876#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017877
17878/*
17879* 3.14.6 Constraints on Simple Type Definition Schema Components
17880*/
17881static int
17882xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17883 xmlSchemaTypePtr type)
17884{
17885 int res, olderrs = pctxt->nberrors;
17886
17887 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17888 return(-1);
17889
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017890 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017891 return(0);
17892
17893 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17894 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17895
17896 if (type->baseType == NULL) {
17897 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17898 "missing baseType");
17899 goto exit_failure;
17900 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017901 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017902 xmlSchemaTypeFixup(type->baseType, pctxt);
17903 /*
17904 * If a member type of a union is a union itself, we need to substitute
17905 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017906 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17907 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017908 */
17909 if ((type->memberTypes != NULL) &&
17910 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17911 return(-1);
17912 /*
17913 * SPEC src-simple-type 1
17914 * "The corresponding simple type definition, if any, must satisfy
17915 * the conditions set out in Constraints on Simple Type Definition
17916 * Schema Components (§3.14.6)."
17917 */
17918 /*
17919 * Schema Component Constraint: Simple Type Definition Properties Correct
17920 * (st-props-correct)
17921 */
17922 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17923 HFAILURE HERROR
17924 /*
17925 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17926 * (cos-st-restricts)
17927 */
17928 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17929 HFAILURE HERROR
17930 /*
17931 * TODO: Removed the error report, since it got annoying to get an
17932 * extra error report, if anything failed until now.
17933 * Enable this if needed.
17934 *
17935 * xmlSchemaPErr(ctxt, type->node,
17936 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17937 * "Simple type '%s' does not satisfy the constraints "
17938 * "on simple type definitions.\n",
17939 * type->name, NULL);
17940 */
17941 /*
17942 * Schema Component Constraint: Simple Type Restriction (Facets)
17943 * (st-restrict-facets)
17944 */
17945 res = xmlSchemaCheckFacetValues(type, pctxt);
17946 HFAILURE HERROR
17947 if ((type->facetSet != NULL) ||
17948 (type->baseType->facetSet != NULL)) {
17949 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17950 HFAILURE HERROR
17951 }
17952 /*
17953 * Whitespace value.
17954 */
17955 res = xmlSchemaTypeFixupWhitespace(type);
17956 HFAILURE HERROR
17957 xmlSchemaTypeFixupOptimFacets(type);
17958
17959exit_error:
17960#ifdef DEBUG_TYPE
17961 xmlSchemaDebugFixedType(pctxt, type);
17962#endif
17963 if (olderrs != pctxt->nberrors)
17964 return(pctxt->err);
17965 return(0);
17966
17967exit_failure:
17968#ifdef DEBUG_TYPE
17969 xmlSchemaDebugFixedType(pctxt, type);
17970#endif
17971 return(-1);
17972}
17973
17974static int
17975xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
17976 xmlSchemaTypePtr type)
17977{
17978 int res = 0, olderrs = pctxt->nberrors;
17979 xmlSchemaTypePtr baseType = type->baseType;
17980
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017981 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017982 return(0);
17983 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17984 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017985 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017986 "missing baseType");
17987 goto exit_failure;
17988 }
17989 /*
17990 * Fixup the base type.
17991 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017992 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017993 xmlSchemaTypeFixup(baseType, pctxt);
17994 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
17995 /*
17996 * Skip fixup if the base type is invalid.
17997 * TODO: Generate a warning!
17998 */
17999 return(0);
18000 }
18001 /*
18002 * This basically checks if the base type can be derived.
18003 */
18004 res = xmlSchemaCheckSRCCT(pctxt, type);
18005 HFAILURE HERROR
18006 /*
18007 * Fixup the content type.
18008 */
18009 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18010 /*
18011 * Corresponds to <complexType><simpleContent>...
18012 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018013 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018014 (baseType->contentTypeDef != NULL) &&
18015 (WXS_IS_RESTRICTION(type))) {
18016 xmlSchemaTypePtr contentBase, content;
18017#ifdef ENABLE_NAMED_LOCALS
18018 char buf[30];
18019 const xmlChar *tmpname;
18020#endif
18021 /*
18022 * SPEC (1) If <restriction> + base type is <complexType>,
18023 * "whose own {content type} is a simple type..."
18024 */
18025 if (type->contentTypeDef != NULL) {
18026 /*
18027 * SPEC (1.1) "the simple type definition corresponding to the
18028 * <simpleType> among the [children] of <restriction> if there
18029 * is one;"
18030 * Note that this "<simpleType> among the [children]" was put
18031 * into ->contentTypeDef during parsing.
18032 */
18033 contentBase = type->contentTypeDef;
18034 type->contentTypeDef = NULL;
18035 } else {
18036 /*
18037 * (1.2) "...otherwise (<restriction> has no <simpleType>
18038 * among its [children]), the simple type definition which
18039 * is the {content type} of the ... base type."
18040 */
18041 contentBase = baseType->contentTypeDef;
18042 }
18043 /*
18044 * SPEC
18045 * "... a simple type definition which restricts the simple
18046 * type definition identified in clause 1.1 or clause 1.2
18047 * with a set of facet components"
18048 *
18049 * Create the anonymous simple type, which will be the content
18050 * type of the complex type.
18051 */
18052#ifdef ENABLE_NAMED_LOCALS
18053 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18054 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018055 content = xmlSchemaAddType(pctxt, pctxt->schema,
18056 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018057 type->node, 0);
18058#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018059 content = xmlSchemaAddType(pctxt, pctxt->schema,
18060 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018061 type->node, 0);
18062#endif
18063 if (content == NULL)
18064 goto exit_failure;
18065 /*
18066 * We will use the same node as for the <complexType>
18067 * to have it somehow anchored in the schema doc.
18068 */
18069 content->type = XML_SCHEMA_TYPE_SIMPLE;
18070 content->baseType = contentBase;
18071 /*
18072 * Move the facets, previously anchored on the
18073 * complexType during parsing.
18074 */
18075 content->facets = type->facets;
18076 type->facets = NULL;
18077 content->facetSet = type->facetSet;
18078 type->facetSet = NULL;
18079
18080 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018081 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018082 xmlSchemaTypeFixup(contentBase, pctxt);
18083 /*
18084 * Fixup the newly created type. We don't need to check
18085 * for circularity here.
18086 */
18087 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18088 HFAILURE HERROR
18089 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18090 HFAILURE HERROR
18091
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018092 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018093 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18094 (WXS_IS_RESTRICTION(type))) {
18095 /*
18096 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18097 * an emptiable particle, then a simple type definition which
18098 * restricts the <restriction>'s <simpleType> child.
18099 */
18100 if ((type->contentTypeDef == NULL) ||
18101 (type->contentTypeDef->baseType == NULL)) {
18102 /*
18103 * TODO: Check if this ever happens.
18104 */
18105 xmlSchemaPCustomErr(pctxt,
18106 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018107 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018108 "Internal error: xmlSchemaTypeFixup, "
18109 "complex type '%s': the <simpleContent><restriction> "
18110 "is missing a <simpleType> child, but was not catched "
18111 "by xmlSchemaCheckSRCCT()", type->name);
18112 goto exit_failure;
18113 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018114 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018115 /*
18116 * SPEC (3) If <extension> + base is <complexType> with
18117 * <simpleType> content, "...then the {content type} of that
18118 * complex type definition"
18119 */
18120 if (baseType->contentTypeDef == NULL) {
18121 /*
18122 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18123 * should have catched this already.
18124 */
18125 xmlSchemaPCustomErr(pctxt,
18126 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018127 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018128 "Internal error: xmlSchemaTypeFixup, "
18129 "complex type '%s': the <extension>ed base type is "
18130 "a complex type with no simple content type",
18131 type->name);
18132 goto exit_failure;
18133 }
18134 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018135 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018136 /*
18137 * SPEC (4) <extension> + base is <simpleType>
18138 * "... then that simple type definition"
18139 */
18140 type->contentTypeDef = baseType;
18141 } else {
18142 /*
18143 * TODO: Check if this ever happens.
18144 */
18145 xmlSchemaPCustomErr(pctxt,
18146 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018147 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018148 "Internal error: xmlSchemaTypeFixup, "
18149 "complex type '%s' with <simpleContent>: unhandled "
18150 "derivation case", type->name);
18151 goto exit_failure;
18152 }
18153 } else {
18154 int dummySequence = 0;
18155 xmlSchemaParticlePtr particle =
18156 (xmlSchemaParticlePtr) type->subtypes;
18157 /*
18158 * Corresponds to <complexType><complexContent>...
18159 *
18160 * NOTE that the effective mixed was already set during parsing of
18161 * <complexType> and <complexContent>; its flag value is
18162 * XML_SCHEMAS_TYPE_MIXED.
18163 *
18164 * Compute the "effective content":
18165 * (2.1.1) + (2.1.2) + (2.1.3)
18166 */
18167 if ((particle == NULL) ||
18168 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18169 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18170 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18171 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18172 (particle->minOccurs == 0))) &&
18173 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18174 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18175 /*
18176 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18177 * a particle whose properties are as follows:..."
18178 *
18179 * Empty sequence model group with
18180 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18181 * NOTE that we sill assign it the <complexType> node to
18182 * somehow anchor it in the doc.
18183 */
18184 if ((particle == NULL) ||
18185 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18186 /*
18187 * Create the particle.
18188 */
18189 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18190 type->node, 1, 1);
18191 if (particle == NULL)
18192 goto exit_failure;
18193 /*
18194 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018195 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018196 particle->children = (xmlSchemaTreeItemPtr)
18197 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18198 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18199 if (particle->children == NULL)
18200 goto exit_failure;
18201
18202 type->subtypes = (xmlSchemaTypePtr) particle;
18203 }
18204 dummySequence = 1;
18205 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18206 } else {
18207 /*
18208 * SPEC (2.1.5) "otherwise empty"
18209 */
18210 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18211 }
18212 } else {
18213 /*
18214 * SPEC (2.2) "otherwise the particle corresponding to the
18215 * <all>, <choice>, <group> or <sequence> among the
18216 * [children]."
18217 */
18218 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18219 }
18220 /*
18221 * Compute the "content type".
18222 */
18223 if (WXS_IS_RESTRICTION(type)) {
18224 /*
18225 * SPEC (3.1) "If <restriction>..."
18226 * (3.1.1) + (3.1.2) */
18227 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18228 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18229 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18230 }
18231 } else {
18232 /*
18233 * SPEC (3.2) "If <extension>..."
18234 */
18235 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18236 /*
18237 * SPEC (3.2.1)
18238 */
18239 type->contentType = baseType->contentType;
18240 type->subtypes = baseType->subtypes;
18241 /*
18242 * NOTE that the effective mixed is ignored here.
18243 */
18244 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18245 /*
18246 * SPEC (3.2.2)
18247 */
18248 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18249 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18250 } else {
18251 /*
18252 * SPEC (3.2.3)
18253 */
18254 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18255 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18256 /*
18257 * "A model group whose {compositor} is sequence and whose
18258 * {particles} are..."
18259 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018260 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18261 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18262 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18263 XML_SCHEMA_TYPE_ALL))
18264 {
18265 /*
18266 * SPEC cos-all-limited (1)
18267 */
18268 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18269 /* TODO: error code */
18270 XML_SCHEMAP_COS_ALL_LIMITED,
18271 WXS_ITEM_NODE(type), NULL,
18272 "The type has an 'all' model group in its "
18273 "{content type} and thus cannot be derived from "
18274 "a non-empty type, since this would produce a "
18275 "'sequence' model group containing the 'all' "
18276 "model group; 'all' model groups are not "
18277 "allowed to appear inside other model groups",
18278 NULL, NULL);
18279
18280 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18281 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18282 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18283 XML_SCHEMA_TYPE_ALL))
18284 {
18285 /*
18286 * SPEC cos-all-limited (1)
18287 */
18288 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18289 /* TODO: error code */
18290 XML_SCHEMAP_COS_ALL_LIMITED,
18291 WXS_ITEM_NODE(type), NULL,
18292 "A type cannot be derived by extension from a type "
18293 "which has an 'all' model group in its "
18294 "{content type}, since this would produce a "
18295 "'sequence' model group containing the 'all' "
18296 "model group; 'all' model groups are not "
18297 "allowed to appear inside other model groups",
18298 NULL, NULL);
18299
18300 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018301 xmlSchemaTreeItemPtr effectiveContent =
18302 (xmlSchemaTreeItemPtr) type->subtypes;
18303 /*
18304 * Create the particle.
18305 */
18306 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18307 type->node, 1, 1);
18308 if (particle == NULL)
18309 goto exit_failure;
18310 /*
18311 * Create the "sequence" model group.
18312 */
18313 particle->children = (xmlSchemaTreeItemPtr)
18314 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18315 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18316 if (particle->children == NULL)
18317 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018318 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018319 /*
18320 * SPEC "the particle of the {content type} of
18321 * the ... base ..."
18322 * Create a duplicate of the base type's particle
18323 * and assign its "term" to it.
18324 */
18325 particle->children->children =
18326 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18327 pctxt->schema, type->node,
18328 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18329 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18330 if (particle->children->children == NULL)
18331 goto exit_failure;
18332 particle = (xmlSchemaParticlePtr)
18333 particle->children->children;
18334 particle->children =
18335 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18336 /*
18337 * SPEC "followed by the ·effective content·."
18338 */
18339 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018340 /*
18341 * This all will result in:
18342 * new-particle
18343 * --> new-sequence(
18344 * new-particle
18345 * --> base-model,
18346 * this-particle
18347 * --> this-model
18348 * )
18349 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018350 } else {
18351 /*
18352 * This is the case when there is already an empty
18353 * <sequence> with minOccurs==maxOccurs==1.
18354 * Just add the base types's content type.
18355 * NOTE that, although we miss to add an intermediate
18356 * <sequence>, this should produce no difference to
18357 * neither the regex compilation of the content model,
18358 * nor to the complex type contraints.
18359 */
18360 particle->children->children =
18361 (xmlSchemaTreeItemPtr) baseType->subtypes;
18362 }
18363 }
18364 }
18365 }
18366 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018367 * Now fixup attribute uses:
18368 * - expand attr. group references
18369 * - intersect attribute wildcards
18370 * - inherit attribute uses of the base type
18371 * - inherit or union attr. wildcards if extending
18372 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018373 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018374 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018375 HFAILURE HERROR
18376 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018377 * Apply the complex type component constraints; this will not
18378 * check attributes, since this is done in
18379 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018380 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018381 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018382 HFAILURE HERROR
18383
18384#ifdef DEBUG_TYPE
18385 xmlSchemaDebugFixedType(pctxt, type);
18386#endif
18387 if (olderrs != pctxt->nberrors)
18388 return(pctxt->err);
18389 else
18390 return(0);
18391
18392exit_error:
18393 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18394#ifdef DEBUG_TYPE
18395 xmlSchemaDebugFixedType(pctxt, type);
18396#endif
18397 return(pctxt->err);
18398
18399exit_failure:
18400 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18401#ifdef DEBUG_TYPE
18402 xmlSchemaDebugFixedType(pctxt, type);
18403#endif
18404 return(-1);
18405}
18406
18407
18408/**
18409 * xmlSchemaTypeFixup:
18410 * @typeDecl: the schema type definition
18411 * @ctxt: the schema parser context
18412 *
18413 * Fixes the content model of the type.
18414 * URGENT TODO: We need an int result!
18415 */
18416static int
18417xmlSchemaTypeFixup(xmlSchemaTypePtr type,
18418 xmlSchemaParserCtxtPtr pctxt)
18419{
18420 if (type == NULL)
18421 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018422 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018423 return(0);
18424 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
18425 return(xmlSchemaFixupComplexType(pctxt, type));
18426 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
18427 return(xmlSchemaFixupSimpleTypeStageTwo(pctxt, type));
18428 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018429}
18430
18431/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018432 * xmlSchemaCheckFacet:
18433 * @facet: the facet
18434 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018435 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018436 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018437 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018438 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018439 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018440 * Returns 0 if valid, a positive error code if not valid and
18441 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018442 */
18443int
18444xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018445 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018446 xmlSchemaParserCtxtPtr pctxt,
18447 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018448{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018449 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018450
Daniel Veillardce682bc2004-11-05 17:22:25 +000018451 if ((facet == NULL) || (typeDecl == NULL))
18452 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018453 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018454 * TODO: will the parser context be given if used from
18455 * the relaxNG module?
18456 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018457 if (pctxt == NULL)
18458 ctxtGiven = 0;
18459 else
18460 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018461
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018462 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018463 case XML_SCHEMA_FACET_MININCLUSIVE:
18464 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18465 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018466 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18467 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018468 /*
18469 * Okay we need to validate the value
18470 * at that point.
18471 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018472 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018473
18474 /* 4.3.5.5 Constraints on enumeration Schema Components
18475 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018476 * It is an ·error· if any member of {value} is not in the
18477 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018478 *
18479 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018480 * The value ·must· be in the
18481 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018482 */
18483 /*
18484 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018485 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018486 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018487 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018488 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018489 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018490 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018491 */
18492 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18493 base = typeDecl->baseType;
18494 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018495 PERROR_INT("xmlSchemaCheckFacet",
18496 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018497 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018498 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018499 } else
18500 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018501
18502 if (! ctxtGiven) {
18503 /*
18504 * A context is needed if called from RelaxNG.
18505 */
18506 pctxt = xmlSchemaNewParserCtxt("*");
18507 if (pctxt == NULL)
18508 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018509 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018510 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018511 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018512 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018513 * facet->node is just the node holding the facet
18514 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018515 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018516 */
18517 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018518 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018519 facet->value, &(facet->val), 1, 1, 0);
18520 if (ret != 0) {
18521 if (ret < 0) {
18522 /* No error message for RelaxNG. */
18523 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018524 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018525 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18526 "Internal error: xmlSchemaCheckFacet, "
18527 "failed to validate the value '%s' of the "
18528 "facet '%s' against the base type",
18529 facet->value, xmlSchemaFacetTypeToString(facet->type));
18530 }
18531 goto internal_error;
18532 }
18533 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18534 /* No error message for RelaxNG. */
18535 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018536 xmlChar *str = NULL;
18537
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018538 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018539 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018540 "The value '%s' of the facet does not validate "
18541 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018542 facet->value,
18543 xmlSchemaFormatQName(&str,
18544 base->targetNamespace, base->name));
18545 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018546 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018547 goto exit;
18548 } else if (facet->val == NULL) {
18549 if (ctxtGiven) {
18550 PERROR_INT("xmlSchemaCheckFacet",
18551 "value was not computed");
18552 }
18553 TODO
18554 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018555 break;
18556 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018557 case XML_SCHEMA_FACET_PATTERN:
18558 facet->regexp = xmlRegexpCompile(facet->value);
18559 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018560 ret = XML_SCHEMAP_REGEXP_INVALID;
18561 /* No error message for RelaxNG. */
18562 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018563 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018564 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018565 "The value '%s' of the facet 'pattern' is not a "
18566 "valid regular expression",
18567 facet->value, NULL);
18568 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018569 }
18570 break;
18571 case XML_SCHEMA_FACET_TOTALDIGITS:
18572 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18573 case XML_SCHEMA_FACET_LENGTH:
18574 case XML_SCHEMA_FACET_MAXLENGTH:
18575 case XML_SCHEMA_FACET_MINLENGTH:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018576 ret = xmlSchemaValidatePredefinedType(
18577 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18578 facet->value, &(facet->val));
18579 if (ret != 0) {
18580 if (ret < 0) {
18581 /* No error message for RelaxNG. */
18582 if (ctxtGiven) {
18583 PERROR_INT("xmlSchemaCheckFacet",
18584 "validating facet value");
18585 }
18586 goto internal_error;
18587 }
18588 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18589 /* No error message for RelaxNG. */
18590 if (ctxtGiven) {
18591 /* error code */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018592 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018593 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018594 "The value '%s' of the facet '%s' is not a valid "
18595 "'nonNegativeInteger'",
18596 facet->value,
18597 xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018598 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018599 }
18600 break;
18601 }
18602 case XML_SCHEMA_FACET_WHITESPACE:{
18603 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18604 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18605 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18606 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18607 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18608 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18609 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018610 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18611 /* No error message for RelaxNG. */
18612 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018613 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018614 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018615 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018616 "The value '%s' of the facet 'whitespace' is not "
18617 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018618 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018619 }
18620 }
18621 default:
18622 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018623 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018624exit:
18625 if ((! ctxtGiven) && (pctxt != NULL))
18626 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018627 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018628internal_error:
18629 if ((! ctxtGiven) && (pctxt != NULL))
18630 xmlSchemaFreeParserCtxt(pctxt);
18631 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018632}
18633
18634/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018635 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018636 * @typeDecl: the schema type definition
18637 * @ctxt: the schema parser context
18638 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018639 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018640 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018641static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018642xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018643 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018644{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018645 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018646 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018647 /*
18648 * NOTE: It is intended to use the facets list, instead
18649 * of facetSet.
18650 */
18651 if (typeDecl->facets != NULL) {
18652 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018653
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018654 /*
18655 * Temporarily assign the "schema" to the validation context
18656 * of the parser context. This is needed for NOTATION validation.
18657 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018658 if (pctxt->vctxt == NULL) {
18659 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18660 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018661 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018662 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018663 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018664 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18665 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018666 facet = facet->next;
18667 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018668 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018669 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018670 if (olderrs != pctxt->nberrors)
18671 return(pctxt->err);
18672 return(0);
18673exit_failure:
18674 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018675}
18676
18677/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018678 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018679 * @ctxtMGroup: the searched model group
18680 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018681 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018682 *
18683 * This one is intended to be used by
18684 * xmlSchemaCheckGroupDefCircular only.
18685 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018686 * Returns the particle with the circular model group definition reference,
18687 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018688 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018689static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018690xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018691 xmlSchemaTreeItemPtr particle)
18692{
18693 xmlSchemaTreeItemPtr circ = NULL;
18694 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018695 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018696
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018697 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018698 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018699 if (term == NULL)
18700 continue;
18701 switch (term->type) {
18702 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018703 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018704 if (gdef == groupDef)
18705 return (particle);
18706 /*
18707 * Mark this model group definition to avoid infinite
18708 * recursion on circular references not yet examined.
18709 */
18710 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18711 continue;
18712 if (gdef->children != NULL) {
18713 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18714 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18715 gdef->children->children);
18716 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18717 if (circ != NULL)
18718 return (circ);
18719 }
18720 break;
18721 case XML_SCHEMA_TYPE_SEQUENCE:
18722 case XML_SCHEMA_TYPE_CHOICE:
18723 case XML_SCHEMA_TYPE_ALL:
18724 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18725 if (circ != NULL)
18726 return (circ);
18727 break;
18728 default:
18729 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018730 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018731 }
18732 return (NULL);
18733}
18734
18735/**
18736 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018737 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018738 * @ctxt: the parser context
18739 * @name: the name
18740 *
18741 * Checks for circular references to model group definitions.
18742 */
18743static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018744xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018745 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018746{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018747 /*
18748 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018749 * 2 Circular groups are disallowed. That is, within the {particles}
18750 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018751 * is the group itself.
18752 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018753 if ((item == NULL) ||
18754 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18755 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018756 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018757 {
18758 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018759
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018760 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018761 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018762 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018763 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018764 * TODO: The error report is not adequate: this constraint
18765 * is defined for model groups but not definitions, but since
18766 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018767 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018768 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018769 */
18770 xmlSchemaPCustomErr(ctxt,
18771 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018772 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018773 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018774 "defined", xmlSchemaFormatQName(&str,
18775 item->targetNamespace, item->name));
18776 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018777 /*
18778 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018779 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018780 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018781 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018782 }
18783 }
18784}
18785
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018786/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018787 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018788 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018789 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018790 *
18791 * Assigns the model group of model group definitions to the "term"
18792 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018793 * In xmlSchemaResolveModelGroupParticleReferences the model group
18794 * definitions were assigned to the "term", since needed for the
18795 * circularity check.
18796 *
18797 * Schema Component Constraint:
18798 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018799 */
18800static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018801xmlSchemaModelGroupToModelGroupDefFixup(
18802 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18803 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018804{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018805 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18806
18807 while (particle != NULL) {
18808 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18809 ((WXS_PARTICLE_TERM(particle))->type !=
18810 XML_SCHEMA_TYPE_GROUP))
18811 {
18812 particle = WXS_PTC_CAST particle->next;
18813 continue;
18814 }
18815 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18816 /*
18817 * TODO: Remove the particle.
18818 */
18819 WXS_PARTICLE_TERM(particle) = NULL;
18820 particle = WXS_PTC_CAST particle->next;
18821 continue;
18822 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018823 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018824 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018825 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018826 WXS_PARTICLE_TERM(particle) =
18827 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18828
18829 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018830 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018831}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018832
18833/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018834 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018835 * @ctxtGr: the searched attribute group
18836 * @attr: the current attribute list to be processed
18837 *
18838 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018839 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018840 *
18841 * Returns the circular attribute grou reference, otherwise NULL.
18842 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018843static xmlSchemaQNameRefPtr
18844xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18845 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018846{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018847 xmlSchemaAttributeGroupPtr gr;
18848 xmlSchemaQNameRefPtr ref, circ;
18849 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018850 /*
18851 * We will search for an attribute group reference which
18852 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018853 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018854 for (i = 0; i < list->nbItems; i++) {
18855 ref = list->items[i];
18856 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18857 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18858 (ref->item != NULL))
18859 {
18860 gr = WXS_ATTR_GROUP_CAST ref->item;
18861 if (gr == ctxtGr)
18862 return(ref);
18863 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18864 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018865 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018866 * Mark as visited to avoid infinite recursion on
18867 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018868 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018869 if ((gr->attrUses) &&
18870 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18871 {
18872 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18873 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18874 (xmlSchemaItemListPtr) gr->attrUses);
18875 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18876 if (circ != NULL)
18877 return (circ);
18878 }
18879
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018880 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018881 }
18882 return (NULL);
18883}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018884
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018885/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018886 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018887 * attrGr: the attribute group definition
18888 * @ctxt: the parser context
18889 * @name: the name
18890 *
18891 * Checks for circular references of attribute groups.
18892 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018893static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018894xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018895 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018896{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018897 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018898 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018899 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018900 * 3 Circular group reference is disallowed outside <redefine>.
18901 * That is, unless this element information item's parent is
18902 * <redefine>, then among the [children], if any, there must
18903 * not be an <attributeGroup> with ref [attribute] which resolves
18904 * to the component corresponding to this <attributeGroup>. Indirect
18905 * circularity is also ruled out. That is, when QName resolution
18906 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18907 * any <attributeGroup>s with a ref [attribute] among the [children],
18908 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018909 * which resolves to the component corresponding to this <attributeGroup>.
18910 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018911 if (attrGr->attrUses == NULL)
18912 return(0);
18913 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18914 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018915 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018916 xmlSchemaQNameRefPtr circ;
18917
18918 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18919 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018920 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018921 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018922 /*
18923 * TODO: Report the referenced attr group as QName.
18924 */
18925 xmlSchemaPCustomErr(ctxt,
18926 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018927 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018928 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018929 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18930 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018931 /*
18932 * NOTE: We will cut the reference to avoid further
18933 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018934 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018935 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018936 circ->item = NULL;
18937 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018938 }
18939 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018940 return(0);
18941}
18942
18943static int
18944xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18945 xmlSchemaAttributeGroupPtr attrGr);
18946
18947/**
18948 * xmlSchemaExpandAttributeGroupRefs:
18949 * @pctxt: the parser context
18950 * @node: the node of the component holding the attribute uses
18951 * @completeWild: the intersected wildcard to be returned
18952 * @list: the attribute uses
18953 *
18954 * Substitutes contained attribute group references
18955 * for their attribute uses. Wilcards are intersected.
18956 * Attribute use prohibitions are removed from the list
18957 * and returned via the @prohibs list.
18958 * Pointlessness of attr. prohibs, if a matching attr. decl
18959 * is existent a well, are checked.
18960 */
18961static int
18962xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
18963 xmlSchemaBasicItemPtr item,
18964 xmlSchemaWildcardPtr *completeWild,
18965 xmlSchemaItemListPtr list,
18966 xmlSchemaItemListPtr prohibs)
18967{
18968 xmlSchemaAttributeGroupPtr gr;
18969 xmlSchemaAttributeUsePtr use;
18970 xmlSchemaItemListPtr sublist;
18971 int i, j;
18972 int created = (*completeWild == NULL) ? 0 : 1;
18973
18974 if (prohibs)
18975 prohibs->nbItems = 0;
18976
18977 for (i = 0; i < list->nbItems; i++) {
18978 use = list->items[i];
18979
18980 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
18981 if (prohibs == NULL) {
18982 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
18983 "unexpected attr prohibition found");
18984 return(-1);
18985 }
18986 /*
18987 * Remove from attribute uses.
18988 */
18989 if (xmlSchemaItemListRemove(list, i) == -1)
18990 return(-1);
18991 i--;
18992 /*
18993 * Note that duplicate prohibitions were already
18994 * handled at parsing time.
18995 */
18996 /*
18997 * Add to list of prohibitions.
18998 */
18999 xmlSchemaItemListAddSize(prohibs, 2, use);
19000 continue;
19001 }
19002 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19003 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19004 {
19005 if ((WXS_QNAME_CAST use)->item == NULL)
19006 return(-1);
19007 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19008 /*
19009 * Expand the referenced attr. group.
19010 * TODO: remove this, this is done in a previous step, so
19011 * already done here.
19012 */
19013 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19014 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19015 return(-1);
19016 }
19017 /*
19018 * Build the 'complete' wildcard; i.e. intersect multiple
19019 * wildcards.
19020 */
19021 if (gr->attributeWildcard != NULL) {
19022 if (*completeWild == NULL) {
19023 *completeWild = gr->attributeWildcard;
19024 } else {
19025 if (! created) {
19026 xmlSchemaWildcardPtr tmpWild;
19027
19028 /*
19029 * Copy the first encountered wildcard as context,
19030 * except for the annotation.
19031 *
19032 * Although the complete wildcard might not correspond
19033 * to any node in the schema, we will anchor it on
19034 * the node of the owner component.
19035 */
19036 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19037 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19038 WXS_ITEM_NODE(item));
19039 if (tmpWild == NULL)
19040 return(-1);
19041 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19042 tmpWild, *completeWild) == -1)
19043 return (-1);
19044 tmpWild->processContents = (*completeWild)->processContents;
19045 *completeWild = tmpWild;
19046 created = 1;
19047 }
19048
19049 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19050 gr->attributeWildcard) == -1)
19051 return(-1);
19052 }
19053 }
19054 /*
19055 * Just remove the reference if the referenced group does not
19056 * contain any attribute uses.
19057 */
19058 if (gr->attrUses == NULL) {
19059 if (xmlSchemaItemListRemove(list, i) == -1)
19060 return(-1);
19061 i--;
19062 continue;
19063 }
19064 /*
19065 * Add the attribute uses.
19066 */
19067 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19068 if (sublist->nbItems != 0) {
19069 list->items[i] = sublist->items[0];
19070 if (sublist->nbItems != 1) {
19071 for (j = 1; j < sublist->nbItems; j++) {
19072 i++;
19073 if (xmlSchemaItemListInsert(list,
19074 sublist->items[j], i) == -1)
19075 return(-1);
19076 }
19077 }
19078 }
19079 }
19080
19081 }
19082 /*
19083 * Handle pointless prohibitions of declared attributes.
19084 */
19085 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19086 xmlSchemaAttributeUseProhibPtr prohib;
19087
19088 for (i = prohibs->nbItems -1; i >= 0; i--) {
19089 prohib = prohibs->items[i];
19090 for (j = 0; j < list->nbItems; j++) {
19091 use = list->items[j];
19092
19093 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19094 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19095 {
19096 xmlChar *str = NULL;
19097
19098 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19099 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19100 prohib->node, NULL,
19101 "Skipping pointless attribute use prohibition "
19102 "'%s', since a corresponding attribute use "
19103 "exists already in the type definition",
19104 xmlSchemaFormatQName(&str,
19105 prohib->targetNamespace, prohib->name),
19106 NULL, NULL);
19107 FREE_AND_NULL(str);
19108 /*
19109 * Remove the prohibition.
19110 */
19111 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19112 return(-1);
19113 break;
19114 }
19115 }
19116 }
19117 }
19118 return(0);
19119}
19120
19121/**
19122 * xmlSchemaAttributeGroupExpandRefs:
19123 * @pctxt: the parser context
19124 * @attrGr: the attribute group definition
19125 *
19126 * Computation of:
19127 * {attribute uses} property
19128 * {attribute wildcard} property
19129 *
19130 * Substitutes contained attribute group references
19131 * for their attribute uses. Wilcards are intersected.
19132 */
19133static int
19134xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19135 xmlSchemaAttributeGroupPtr attrGr)
19136{
19137 if ((attrGr->attrUses == NULL) ||
19138 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19139 return(0);
19140
19141 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19142 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19143 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19144 return(-1);
19145 return(0);
19146}
19147
19148/**
19149 * xmlSchemaAttributeGroupExpandRefs:
19150 * @pctxt: the parser context
19151 * @attrGr: the attribute group definition
19152 *
19153 * Substitutes contained attribute group references
19154 * for their attribute uses. Wilcards are intersected.
19155 *
19156 * Schema Component Constraint:
19157 * Attribute Group Definition Properties Correct (ag-props-correct)
19158 */
19159static int
19160xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19161 xmlSchemaAttributeGroupPtr attrGr)
19162{
19163 /*
19164 * SPEC ag-props-correct
19165 * (1) "The values of the properties of an attribute group definition
19166 * must be as described in the property tableau in The Attribute
19167 * Group Definition Schema Component (§3.6.1), modulo the impact of
19168 * Missing Sub-components (§5.3);"
19169 */
19170
19171 if ((attrGr->attrUses != NULL) &&
19172 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19173 {
19174 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19175 xmlSchemaAttributeUsePtr use, tmp;
19176 int i, j, hasId = 0;
19177
19178 for (i = uses->nbItems -1; i >= 0; i--) {
19179 use = uses->items[i];
19180 /*
19181 * SPEC ag-props-correct
19182 * (2) "Two distinct members of the {attribute uses} must not have
19183 * {attribute declaration}s both of whose {name}s match and whose
19184 * {target namespace}s are identical."
19185 */
19186 if (i > 0) {
19187 for (j = i -1; j >= 0; j--) {
19188 tmp = uses->items[j];
19189 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19190 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19191 (WXS_ATTRUSE_DECL_TNS(use) ==
19192 WXS_ATTRUSE_DECL_TNS(tmp)))
19193 {
19194 xmlChar *str = NULL;
19195
19196 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19197 XML_SCHEMAP_AG_PROPS_CORRECT,
19198 attrGr->node, WXS_BASIC_CAST attrGr,
19199 "Duplicate %s",
19200 xmlSchemaGetComponentDesignation(&str, use),
19201 NULL);
19202 FREE_AND_NULL(str);
19203 /*
19204 * Remove the duplicate.
19205 */
19206 if (xmlSchemaItemListRemove(uses, i) == -1)
19207 return(-1);
19208 goto next_use;
19209 }
19210 }
19211 }
19212 /*
19213 * SPEC ag-props-correct
19214 * (3) "Two distinct members of the {attribute uses} must not have
19215 * {attribute declaration}s both of whose {type definition}s are or
19216 * are derived from ID."
19217 * TODO: Does 'derived' include member-types of unions?
19218 */
19219 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19220 if (xmlSchemaIsDerivedFromBuiltInType(
19221 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19222 {
19223 if (hasId) {
19224 xmlChar *str = NULL;
19225
19226 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19227 XML_SCHEMAP_AG_PROPS_CORRECT,
19228 attrGr->node, WXS_BASIC_CAST attrGr,
19229 "There must not exist more than one attribute "
19230 "declaration of type 'xs:ID' "
19231 "(or derived from 'xs:ID'). The %s violates this "
19232 "constraint",
19233 xmlSchemaGetComponentDesignation(&str, use),
19234 NULL);
19235 FREE_AND_NULL(str);
19236 if (xmlSchemaItemListRemove(uses, i) == -1)
19237 return(-1);
19238 }
19239 hasId = 1;
19240 }
19241 }
19242next_use: {}
19243 }
19244 }
19245 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019246}
19247
19248/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019249 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019250 * @attrgrpDecl: the schema attribute definition
19251 * @ctxt: the schema parser context
19252 * @name: the attribute name
19253 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019254 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019255 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019256static int
19257xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19258 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019259{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019260 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019261
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019262 if (ref->item != NULL)
19263 return(0);
19264 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19265 ref->name,
19266 ref->targetNamespace);
19267 if (group == NULL) {
19268 xmlSchemaPResCompAttrErr(ctxt,
19269 XML_SCHEMAP_SRC_RESOLVE,
19270 NULL, ref->node,
19271 "ref", ref->name, ref->targetNamespace,
19272 ref->itemType, NULL);
19273 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019274 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019275 ref->item = WXS_BASIC_CAST group;
19276 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019277}
19278
19279/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019280 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019281 * @item: an schema attribute declaration/use
19282 * @ctxt: a schema parser context
19283 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019284 *
19285 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019286 * Schema Component Constraint:
19287 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019288 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019289 * Validates the value constraints of an attribute declaration/use.
19290 * NOTE that this needs the simle type definitions to be already
19291 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019292 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019293static int
19294xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19295 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019296{
19297
19298 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019299 * SPEC a-props-correct (1)
19300 * "The values of the properties of an attribute declaration must
19301 * be as described in the property tableau in The Attribute
19302 * Declaration Schema Component (§3.2.1), modulo the impact of
19303 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019304 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019305
19306 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19307 return(0);
19308
19309 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019310 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019311
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019312 /*
19313 * SPEC a-props-correct (3)
19314 * "If the {type definition} is or is derived from ID then there
19315 * must not be a {value constraint}."
19316 */
19317 if (xmlSchemaIsDerivedFromBuiltInType(
19318 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19319 {
19320 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19321 XML_SCHEMAP_A_PROPS_CORRECT_3,
19322 NULL, WXS_BASIC_CAST attr,
19323 "Value constraints are not allowed if the type definition "
19324 "is or is derived from xs:ID",
19325 NULL, NULL);
19326 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019327 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019328 /*
19329 * SPEC a-props-correct (2)
19330 * "if there is a {value constraint}, the canonical lexical
19331 * representation of its value must be ·valid· with respect
19332 * to the {type definition} as defined in String Valid (§3.14.4)."
19333 * TODO: Don't care about the *cononical* stuff here, this requirement
19334 * will be removed in WXS 1.1 anyway.
19335 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019336 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019337 attr->node, WXS_ATTR_TYPEDEF(attr),
19338 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019339 1, 1, 0);
19340 if (ret != 0) {
19341 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019342 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019343 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019345 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019346 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019347 XML_SCHEMAP_A_PROPS_CORRECT_2,
19348 NULL, WXS_BASIC_CAST attr,
19349 "The value of the value constraint is not valid",
19350 NULL, NULL);
19351 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019352 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019353 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019354
19355 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019356}
19357
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019358static xmlSchemaElementPtr
19359xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19360 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019361{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019362 xmlSchemaElementPtr ret;
19363
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019364 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019365 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019366 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019367 return (ancestor);
19368
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019369 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019370 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019371 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019372 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019373 WXS_SUBST_HEAD(ancestor));
19374 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019375
19376 return (ret);
19377}
19378
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019379/**
19380 * xmlSchemaCheckElemPropsCorrect:
19381 * @ctxt: a schema parser context
19382 * @decl: the element declaration
19383 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019384 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019385 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019386 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019387 *
19388 * STATUS:
19389 * missing: (6)
19390 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019391static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019392xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19393 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019394{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019395 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019396 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019397 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019398 * SPEC (1) "The values of the properties of an element declaration
19399 * must be as described in the property tableau in The Element
19400 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19401 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019402 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019403 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19404 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019405
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019406 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019407 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019408 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019409 * affiliation}, then {scope} must be global."
19410 */
19411 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19412 xmlSchemaPCustomErr(pctxt,
19413 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019414 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019415 "Only global element declarations can have a "
19416 "substitution group affiliation", NULL);
19417 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019418 }
19419 /*
19420 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19421 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019422 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019423 * property."
19424 */
19425 if (head == elemDecl)
19426 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019427 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019428 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19429 else
19430 circ = NULL;
19431 if (circ != NULL) {
19432 xmlChar *strA = NULL, *strB = NULL;
19433
19434 xmlSchemaPCustomErrExt(pctxt,
19435 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019436 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019437 "The element declaration '%s' defines a circular "
19438 "substitution group to element declaration '%s'",
19439 xmlSchemaGetComponentQName(&strA, circ),
19440 xmlSchemaGetComponentQName(&strB, head),
19441 NULL);
19442 FREE_AND_NULL(strA)
19443 FREE_AND_NULL(strB)
19444 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19445 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019446 /*
19447 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019448 * the {type definition}
19449 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019450 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019451 * of the {substitution group exclusions} of the {substitution group
19452 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19453 * (if the {type definition} is complex) or as defined in
19454 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019455 * simple)."
19456 *
19457 * NOTE: {substitution group exclusions} means the values of the
19458 * attribute "final".
19459 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019460
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019461 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019462 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019463
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019464 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19465 set |= SUBSET_EXTENSION;
19466 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19467 set |= SUBSET_RESTRICTION;
19468
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019469 if (xmlSchemaCheckCOSDerivedOK(pctxt, typeDef,
19470 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019471 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19472
19473 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019474 xmlSchemaPCustomErrExt(pctxt,
19475 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019476 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019477 "The type definition '%s' was "
19478 "either rejected by the substitution group "
19479 "affiliation '%s', or not validly derived from its type "
19480 "definition '%s'",
19481 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019482 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019483 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019484 FREE_AND_NULL(strA)
19485 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019486 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019487 }
19488 }
19489 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019490 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019491 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019492 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019493 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019494 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019495 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019496 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019497 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019498 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019499 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019500 (WXS_IS_COMPLEX(typeDef) &&
19501 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019502 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19503 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019504
19505 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19506 xmlSchemaPCustomErr(pctxt,
19507 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019508 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019509 "The type definition (or type definition's content type) is or "
19510 "is derived from ID; value constraints are not allowed in "
19511 "conjunction with such a type definition", NULL);
19512 } else if (elemDecl->value != NULL) {
19513 int vcret;
19514 xmlNodePtr node = NULL;
19515
19516 /*
19517 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19518 * representation of its value must be ·valid· with respect to the
19519 * {type definition} as defined in Element Default Valid (Immediate)
19520 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019521 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019522 if (typeDef == NULL) {
19523 xmlSchemaPErr(pctxt, elemDecl->node,
19524 XML_SCHEMAP_INTERNAL,
19525 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19526 "type is missing... skipping validation of "
19527 "the value constraint", NULL, NULL);
19528 return (-1);
19529 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019530 if (elemDecl->node != NULL) {
19531 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19532 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19533 BAD_CAST "fixed");
19534 else
19535 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19536 BAD_CAST "default");
19537 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019538 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19539 typeDef, elemDecl->value, &(elemDecl->defVal));
19540 if (vcret != 0) {
19541 if (vcret < 0) {
19542 PERROR_INT("xmlSchemaElemCheckValConstr",
19543 "failed to validate the value constraint of an "
19544 "element declaration");
19545 return (-1);
19546 }
19547 return (vcret);
19548 }
19549 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019550
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019551 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019552}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019553
19554/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019555 * xmlSchemaCheckElemSubstGroup:
19556 * @ctxt: a schema parser context
19557 * @decl: the element declaration
19558 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019559 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019560 * Schema Component Constraint:
19561 * Substitution Group (cos-equiv-class)
19562 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019563 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019564 * a list will be built for each subst. group head, holding all direct
19565 * referents to this head.
19566 * NOTE that this function needs:
19567 * 1. circular subst. groups to be checked beforehand
19568 * 2. the declaration's type to be derived from the head's type
19569 *
19570 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019571 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019572 */
19573static void
19574xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19575 xmlSchemaElementPtr elemDecl)
19576{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019577 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019578 /* SPEC (1) "Its {abstract} is false." */
19579 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19580 return;
19581 {
19582 xmlSchemaElementPtr head;
19583 xmlSchemaTypePtr headType, type;
19584 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019585 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019586 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19587 * {disallowed substitutions} as the blocking constraint, as defined in
19588 * Substitution Group OK (Transitive) (§3.3.6)."
19589 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019590 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19591 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019592 set = 0;
19593 methSet = 0;
19594 /*
19595 * The blocking constraints.
19596 */
19597 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19598 continue;
19599 headType = head->subtypes;
19600 type = elemDecl->subtypes;
19601 if (headType == type)
19602 goto add_member;
19603 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19604 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19605 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19606 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19607 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019608 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019609 * "The set of all {derivation method}s involved in the
19610 * derivation of D's {type definition} from C's {type definition}
19611 * does not intersect with the union of the blocking constraint,
19612 * C's {prohibited substitutions} (if C is complex, otherwise the
19613 * empty set) and the {prohibited substitutions} (respectively the
19614 * empty set) of any intermediate {type definition}s in the
19615 * derivation of D's {type definition} from C's {type definition}."
19616 */
19617 /*
19618 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19619 * subst.head axis, the methSet does not need to be computed for
19620 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019621 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019622 /*
19623 * The set of all {derivation method}s involved in the derivation
19624 */
19625 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019626 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019627 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19628 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019629
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019630 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019631 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19632 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19633
19634 type = type->baseType;
19635 }
19636 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019637 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019638 * the head's type.
19639 */
19640 type = elemDecl->subtypes->baseType;
19641 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019642 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019643 if ((type->flags &
19644 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19645 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19646 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19647 if ((type->flags &
19648 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19649 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19650 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19651 } else
19652 break;
19653 if (type == headType)
19654 break;
19655 type = type->baseType;
19656 }
19657 if ((set != 0) &&
19658 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19659 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19660 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19661 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19662 continue;
19663 }
19664add_member:
19665 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19666 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19667 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19668 }
19669 }
19670}
19671
19672/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019673 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019674 * @item: an schema element declaration/particle
19675 * @ctxt: a schema parser context
19676 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019677 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019678 * Validates the value constraints of an element declaration.
19679 *
19680 * Fixes finish doing the computations on the element declarations.
19681 */
19682static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019683xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019684 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019685{
19686 if (elemDecl == NULL)
19687 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019688 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19689 return;
19690 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019691 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19692 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019693}
19694
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019695/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019696 * xmlSchemaResolveModelGroupParticleReferences:
19697 * @particle: a particle component
19698 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019699 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019700 * Resolves references of a model group's {particles} to
19701 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019702 */
19703static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019704xmlSchemaResolveModelGroupParticleReferences(
19705 xmlSchemaParserCtxtPtr ctxt,
19706 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019707{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019708 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19709 xmlSchemaQNameRefPtr ref;
19710 xmlSchemaBasicItemPtr refItem;
19711
19712 /*
19713 * URGENT TODO: Test this.
19714 */
19715 while (particle != NULL) {
19716 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19717 ((WXS_PARTICLE_TERM(particle))->type !=
19718 XML_SCHEMA_EXTRA_QNAMEREF))
19719 {
19720 goto next_particle;
19721 }
19722 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019723 /*
19724 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019725 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019726 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019727 particle->children = NULL;
19728
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019729 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19730 ref->itemType, ref->name, ref->targetNamespace);
19731 if (refItem == NULL) {
19732 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019733 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019734 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019735 /* TODO: remove the particle. */
19736 goto next_particle;
19737 }
19738 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19739 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19740 /* TODO: remove the particle. */
19741 goto next_particle;
19742 /*
19743 * NOTE that we will assign the model group definition
19744 * itself to the "term" of the particle. This will ease
19745 * the check for circular model group definitions. After
19746 * that the "term" will be assigned the model group of the
19747 * model group definition.
19748 */
19749 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19750 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019751 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019752 * SPEC cos-all-limited (1)
19753 * SPEC cos-all-limited (1.2)
19754 * "It appears only as the value of one or both of the
19755 * following properties:"
19756 * (1.1) "the {model group} property of a model group
19757 * definition."
19758 * (1.2) "the {term} property of a particle [... of] the "
19759 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019760 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019761 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19762 /* TODO: error code */
19763 XML_SCHEMAP_COS_ALL_LIMITED,
19764 WXS_ITEM_NODE(particle), NULL,
19765 "A model group definition is referenced, but "
19766 "it contains an 'all' model group, which "
19767 "cannot be contained by model groups",
19768 NULL, NULL);
19769 /* TODO: remove the particle. */
19770 goto next_particle;
19771 }
19772 particle->children = (xmlSchemaTreeItemPtr) refItem;
19773 } else {
19774 /*
19775 * TODO: Are referenced element declarations the only
19776 * other components we expect here?
19777 */
19778 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019779 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019780next_particle:
19781 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019782 }
19783}
19784
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019785static int
19786xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19787 xmlSchemaValPtr y)
19788{
19789 xmlSchemaTypePtr tx, ty, ptx, pty;
19790 int ret;
19791
19792 while (x != NULL) {
19793 /* Same types. */
19794 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19795 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19796 ptx = xmlSchemaGetPrimitiveType(tx);
19797 pty = xmlSchemaGetPrimitiveType(ty);
19798 /*
19799 * (1) if a datatype T' is ·derived· by ·restriction· from an
19800 * atomic datatype T then the ·value space· of T' is a subset of
19801 * the ·value space· of T. */
19802 /*
19803 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19804 * from a common atomic ancestor T then the ·value space·s of T'
19805 * and T'' may overlap.
19806 */
19807 if (ptx != pty)
19808 return(0);
19809 /*
19810 * We assume computed values to be normalized, so do a fast
19811 * string comparison for string based types.
19812 */
19813 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019814 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019815 if (! xmlStrEqual(
19816 xmlSchemaValueGetAsString(x),
19817 xmlSchemaValueGetAsString(y)))
19818 return (0);
19819 } else {
19820 ret = xmlSchemaCompareValuesWhtsp(
19821 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19822 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19823 if (ret == -2)
19824 return(-1);
19825 if (ret != 0)
19826 return(0);
19827 }
19828 /*
19829 * Lists.
19830 */
19831 x = xmlSchemaValueGetNext(x);
19832 if (x != NULL) {
19833 y = xmlSchemaValueGetNext(y);
19834 if (y == NULL)
19835 return (0);
19836 } else if (xmlSchemaValueGetNext(y) != NULL)
19837 return (0);
19838 else
19839 return (1);
19840 }
19841 return (0);
19842}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019843
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019844/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019845 * xmlSchemaResolveAttrUseReferences:
19846 * @item: an attribute use
19847 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019848 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019849 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019850 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019851static int
19852xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19853 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019854{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019855 if ((ctxt == NULL) || (ause == NULL))
19856 return(-1);
19857 if ((ause->attrDecl == NULL) ||
19858 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19859 return(0);
19860
19861 {
19862 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19863
19864 /*
19865 * TODO: Evaluate, what errors could occur if the declaration is not
19866 * found.
19867 */
19868 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19869 ref->name, ref->targetNamespace);
19870 if (ause->attrDecl == NULL) {
19871 xmlSchemaPResCompAttrErr(ctxt,
19872 XML_SCHEMAP_SRC_RESOLVE,
19873 WXS_BASIC_CAST ause, ause->node,
19874 "ref", ref->name, ref->targetNamespace,
19875 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19876 return(ctxt->err);;
19877 }
19878 }
19879 return(0);
19880}
19881
19882/**
19883 * xmlSchemaCheckAttrUsePropsCorrect:
19884 * @ctxt: a parser context
19885 * @use: an attribute use
19886 *
19887 * Schema Component Constraint:
19888 * Attribute Use Correct (au-props-correct)
19889 *
19890 */
19891static int
19892xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19893 xmlSchemaAttributeUsePtr use)
19894{
19895 if ((ctxt == NULL) || (use == NULL))
19896 return(-1);
19897 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19898 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19899 return(0);
19900
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019901 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019902 * SPEC au-props-correct (1)
19903 * "The values of the properties of an attribute use must be as
19904 * described in the property tableau in The Attribute Use Schema
19905 * Component (§3.5.1), modulo the impact of Missing
19906 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019907 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019908
19909 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19910 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19911 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19912 {
19913 xmlSchemaPCustomErr(ctxt,
19914 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19915 WXS_BASIC_CAST use, NULL,
19916 "The attribute declaration has a 'fixed' value constraint "
19917 ", thus the attribute use must also have a 'fixed' value "
19918 "constraint",
19919 NULL);
19920 return(ctxt->err);
19921 }
19922 /*
19923 * Compute and check the value constraint's value.
19924 */
19925 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19926 int ret;
19927 /*
19928 * TODO: The spec seems to be missing a check of the
19929 * value constraint of the attribute use. We will do it here.
19930 */
19931 /*
19932 * SPEC a-props-correct (3)
19933 */
19934 if (xmlSchemaIsDerivedFromBuiltInType(
19935 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19936 {
19937 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19938 XML_SCHEMAP_AU_PROPS_CORRECT,
19939 NULL, WXS_BASIC_CAST use,
19940 "Value constraints are not allowed if the type definition "
19941 "is or is derived from xs:ID",
19942 NULL, NULL);
19943 return(ctxt->err);
19944 }
19945
19946 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19947 use->node, WXS_ATTRUSE_TYPEDEF(use),
19948 use->defValue, &(use->defVal),
19949 1, 1, 0);
19950 if (ret != 0) {
19951 if (ret < 0) {
19952 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19953 "calling xmlSchemaVCheckCVCSimpleType()");
19954 return(-1);
19955 }
19956 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19957 XML_SCHEMAP_AU_PROPS_CORRECT,
19958 NULL, WXS_BASIC_CAST use,
19959 "The value of the value constraint is not valid",
19960 NULL, NULL);
19961 return(ctxt->err);
19962 }
19963 }
19964 /*
19965 * SPEC au-props-correct (2)
19966 * "If the {attribute declaration} has a fixed
19967 * {value constraint}, then if the attribute use itself has a
19968 * {value constraint}, it must also be fixed and its value must match
19969 * that of the {attribute declaration}'s {value constraint}."
19970 */
19971 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
19972 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19973 {
19974 if (! xmlSchemaAreValuesEqual(use->defVal,
19975 (WXS_ATTRUSE_DECL(use))->defVal))
19976 {
19977 xmlSchemaPCustomErr(ctxt,
19978 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19979 WXS_BASIC_CAST use, NULL,
19980 "The 'fixed' value constraint of the attribute use "
19981 "must match the attribute declaration's value "
19982 "constraint '%s'",
19983 (WXS_ATTRUSE_DECL(use))->defValue);
19984 }
19985 return(ctxt->err);
19986 }
19987 return(0);
19988}
19989
19990
19991
19992
19993/**
19994 * xmlSchemaResolveAttrTypeReferences:
19995 * @item: an attribute declaration
19996 * @ctxt: a parser context
19997 *
19998 * Resolves the referenced type definition component.
19999 */
20000static int
20001xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20002 xmlSchemaParserCtxtPtr ctxt)
20003{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020004 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020005 * The simple type definition corresponding to the <simpleType> element
20006 * information item in the [children], if present, otherwise the simple
20007 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020008 * [attribute], if present, otherwise the ·simple ur-type definition·.
20009 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020010 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020011 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020012 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20013 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020014 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020015 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020016 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020017
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020018 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20019 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020020 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020021 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020022 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020023 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020024 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020025 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020026 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020027 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020028 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020029
Daniel Veillard3646d642004-06-02 19:19:14 +000020030 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020031 /*
20032 * The type defaults to the xs:anySimpleType.
20033 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020034 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20035 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020036 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020037}
20038
20039/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020040 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020041 * @idc: the identity-constraint definition
20042 * @ctxt: the schema parser context
20043 * @name: the attribute name
20044 *
20045 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020046 * Schema Component Constraint:
20047 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020048 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020049static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020050xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020051 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020052{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020053 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020054 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020055 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020056 idc->ref->item = (xmlSchemaBasicItemPtr)
20057 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20058 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020059 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020060 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020061 * TODO: It is actually not an error to fail to resolve
20062 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020063 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020064 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020065 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020066 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020067 "refer", idc->ref->name,
20068 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020069 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020070 return(pctxt->err);
20071 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20072 /*
20073 * SPEC c-props-correct (1)
20074 */
20075 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20076 XML_SCHEMAP_C_PROPS_CORRECT,
20077 NULL, WXS_BASIC_CAST idc,
20078 "The keyref references a keyref",
20079 NULL, NULL);
20080 idc->ref->item = NULL;
20081 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020082 } else {
20083 if (idc->nbFields !=
20084 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20085 xmlChar *str = NULL;
20086 xmlSchemaIDCPtr refer;
20087
20088 refer = (xmlSchemaIDCPtr) idc->ref->item;
20089 /*
20090 * SPEC c-props-correct(2)
20091 * "If the {identity-constraint category} is keyref,
20092 * the cardinality of the {fields} must equal that of
20093 * the {fields} of the {referenced key}.
20094 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020095 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020096 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020097 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020098 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020099 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020100 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020101 refer->name),
20102 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020103 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020104 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020105 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020106 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020107 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020108 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020109}
20110
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020111static int
20112xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20113 xmlSchemaParserCtxtPtr pctxt)
20114{
20115 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20116 prohib->targetNamespace) == NULL) {
20117
20118 xmlSchemaPResCompAttrErr(pctxt,
20119 XML_SCHEMAP_SRC_RESOLVE,
20120 NULL, prohib->node,
20121 "ref", prohib->name, prohib->targetNamespace,
20122 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20123 return(XML_SCHEMAP_SRC_RESOLVE);
20124 }
20125 return(0);
20126}
20127
20128#define WXS_REDEFINED_TYPE(c) \
20129(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20130
20131#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20132(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20133
20134#define WXS_REDEFINED_ATTR_GROUP(c) \
20135(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20136
20137static int
20138xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20139{
20140 int err = 0;
20141 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20142 xmlSchemaBasicItemPtr prev, item;
20143 int wasRedefined;
20144
20145 if (redef == NULL)
20146 return(0);
20147
20148 do {
20149 item = redef->item;
20150 /*
20151 * First try to locate the redefined component in the
20152 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020153 * NOTE: According to this schema bug entry:
20154 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20155 * it's not clear if the referenced component needs to originate
20156 * from the <redefine>d schema _document_ or the schema; the latter
20157 * would include all imported and included sub-schemas of the
20158 * <redefine>d schema. Currenlty we latter approach is used.
20159 * SUPPLEMENT: It seems that the WG moves towards the latter
20160 * approach, so we are doing it right.
20161 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020162 */
20163 prev = xmlSchemaFindRedefCompInGraph(
20164 redef->targetBucket, item->type,
20165 redef->refName, redef->refTargetNs);
20166 if (prev == NULL) {
20167 xmlChar *str = NULL;
20168 xmlNodePtr node;
20169
20170 /*
20171 * SPEC src-redefine:
20172 * (6.2.1) "The ·actual value· of its own name attribute plus
20173 * target namespace must successfully ·resolve· to a model
20174 * group definition in I."
20175 * (7.2.1) "The ·actual value· of its own name attribute plus
20176 * target namespace must successfully ·resolve· to an attribute
20177 * group definition in I."
20178
20179 *
20180 * Note that, if we are redefining with the use of references
20181 * to components, the spec assumes the src-resolve to be used;
20182 * but this won't assure that we search only *inside* the
20183 * redefined schema.
20184 */
20185 if (redef->reference)
20186 node = WXS_ITEM_NODE(redef->reference);
20187 else
20188 node = WXS_ITEM_NODE(item);
20189 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20190 /*
20191 * TODO: error code.
20192 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20193 * reference kind.
20194 */
20195 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
20196 "The %s to be redefined could not be found in "
20197 "the redefined schema",
20198 WXS_ITEM_TYPE_NAME(item),
20199 xmlSchemaFormatQName(&str, redef->refTargetNs,
20200 redef->refName));
20201 FREE_AND_NULL(str);
20202 err = pctxt->err;
20203 redef = redef->next;
20204 continue;
20205 }
20206 /*
20207 * TODO: Obtaining and setting the redefinition state is really
20208 * clumsy.
20209 */
20210 wasRedefined = 0;
20211 switch (item->type) {
20212 case XML_SCHEMA_TYPE_COMPLEX:
20213 case XML_SCHEMA_TYPE_SIMPLE:
20214 if ((WXS_TYPE_CAST prev)->flags &
20215 XML_SCHEMAS_TYPE_REDEFINED)
20216 {
20217 wasRedefined = 1;
20218 break;
20219 }
20220 /* Mark it as redefined. */
20221 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20222 /*
20223 * Assign the redefined type to the
20224 * base type of the redefining type.
20225 * TODO: How
20226 */
20227 ((xmlSchemaTypePtr) item)->baseType =
20228 (xmlSchemaTypePtr) prev;
20229 break;
20230 case XML_SCHEMA_TYPE_GROUP:
20231 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20232 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20233 {
20234 wasRedefined = 1;
20235 break;
20236 }
20237 /* Mark it as redefined. */
20238 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20239 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20240 if (redef->reference != NULL) {
20241 /*
20242 * Overwrite the QName-reference with the
20243 * referenced model group def.
20244 */
20245 (WXS_PTC_CAST redef->reference)->children =
20246 WXS_TREE_CAST prev;
20247 }
20248 redef->target = prev;
20249 break;
20250 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20251 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20252 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20253 {
20254 wasRedefined = 1;
20255 break;
20256 }
20257 (WXS_ATTR_GROUP_CAST prev)->flags |=
20258 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20259 if (redef->reference != NULL) {
20260 /*
20261 * Assign the redefined attribute group to the
20262 * QName-reference component.
20263 * This is the easy case, since we will just
20264 * expand the redefined group.
20265 */
20266 (WXS_QNAME_CAST redef->reference)->item = prev;
20267 redef->target = NULL;
20268 } else {
20269 /*
20270 * This is the complicated case: we need
20271 * to apply src-redefine (7.2.2) at a later
20272 * stage, i.e. when attribute group references
20273 * have beed expanded and simple types have
20274 * beed fixed.
20275 */
20276 redef->target = prev;
20277 }
20278 break;
20279 default:
20280 PERROR_INT("xmlSchemaResolveRedefReferences",
20281 "Unexpected redefined component type");
20282 return(-1);
20283 }
20284 if (wasRedefined) {
20285 xmlChar *str = NULL;
20286 xmlNodePtr node;
20287
20288 if (redef->reference)
20289 node = WXS_ITEM_NODE(redef->reference);
20290 else
20291 node = WXS_ITEM_NODE(redef->item);
20292
20293 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20294 /* TODO: error code. */
20295 XML_SCHEMAP_SRC_REDEFINE,
20296 node, NULL,
20297 "The referenced %s was already redefined. Multiple "
20298 "redefinition of the same component is not supported",
20299 xmlSchemaGetComponentDesignation(&str, prev),
20300 NULL);
20301 FREE_AND_NULL(str)
20302 err = pctxt->err;
20303 redef = redef->next;
20304 continue;
20305 }
20306 redef = redef->next;
20307 } while (redef != NULL);
20308
20309 return(err);
20310}
20311
20312static int
20313xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20314{
20315 int err = 0;
20316 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20317 xmlSchemaBasicItemPtr item;
20318
20319 if (redef == NULL)
20320 return(0);
20321
20322 do {
20323 if (redef->target == NULL) {
20324 redef = redef->next;
20325 continue;
20326 }
20327 item = redef->item;
20328
20329 switch (item->type) {
20330 case XML_SCHEMA_TYPE_SIMPLE:
20331 case XML_SCHEMA_TYPE_COMPLEX:
20332 /*
20333 * Since the spec wants the {name} of the redefined
20334 * type to be 'absent', we'll NULL it.
20335 */
20336 (WXS_TYPE_CAST redef->target)->name = NULL;
20337
20338 /*
20339 * TODO: Seems like there's nothing more to do. The normal
20340 * inheritance mechanism is used. But not 100% sure.
20341 */
20342 break;
20343 case XML_SCHEMA_TYPE_GROUP:
20344 /*
20345 * URGENT TODO:
20346 * SPEC src-redefine:
20347 * (6.2.2) "The {model group} of the model group definition
20348 * which corresponds to it per XML Representation of Model
20349 * Group Definition Schema Components (§3.7.2) must be a
20350 * ·valid restriction· of the {model group} of that model
20351 * group definition in I, as defined in Particle Valid
20352 * (Restriction) (§3.9.6)."
20353 */
20354 break;
20355 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20356 /*
20357 * SPEC src-redefine:
20358 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20359 * the attribute group definition which corresponds to it
20360 * per XML Representation of Attribute Group Definition Schema
20361 * Components (§3.6.2) must be ·valid restrictions· of the
20362 * {attribute uses} and {attribute wildcard} of that attribute
20363 * group definition in I, as defined in clause 2, clause 3 and
20364 * clause 4 of Derivation Valid (Restriction, Complex)
20365 * (§3.4.6) (where references to the base type definition are
20366 * understood as references to the attribute group definition
20367 * in I)."
20368 */
20369 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20370 XML_SCHEMA_ACTION_REDEFINE,
20371 item, redef->target,
20372 (WXS_ATTR_GROUP_CAST item)->attrUses,
20373 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20374 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20375 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20376 if (err == -1)
20377 return(-1);
20378 break;
20379 default:
20380 break;
20381 }
20382 redef = redef->next;
20383 } while (redef != NULL);
20384 return(0);
20385}
20386
20387
20388static int
20389xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20390 xmlSchemaBucketPtr bucket)
20391{
20392 xmlSchemaBasicItemPtr item;
20393 int err;
20394 xmlHashTablePtr *table;
20395 const xmlChar *name;
20396 int i;
20397
20398#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20399 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20400 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20401 else \
20402 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20403
20404 /*
20405 * Add global components to the schema's hash tables.
20406 * This is the place where duplicate components will be
20407 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020408 * TODO: I think normally we should support imports of the
20409 * same namespace from multiple locations. We don't do currently,
20410 * but if we do then according to:
20411 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20412 * we would need, if imported directly, to import redefined
20413 * components as well to be able to catch clashing components.
20414 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020415 */
20416 if (bucket == NULL)
20417 return(-1);
20418 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20419 return(0);
20420 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20421
20422 for (i = 0; i < bucket->globals->nbItems; i++) {
20423 item = bucket->globals->items[i];
20424 table = NULL;
20425 switch (item->type) {
20426 case XML_SCHEMA_TYPE_COMPLEX:
20427 case XML_SCHEMA_TYPE_SIMPLE:
20428 if (WXS_REDEFINED_TYPE(item))
20429 continue;
20430 name = (WXS_TYPE_CAST item)->name;
20431 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20432 break;
20433 case XML_SCHEMA_TYPE_ELEMENT:
20434 name = (WXS_ELEM_CAST item)->name;
20435 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20436 break;
20437 case XML_SCHEMA_TYPE_ATTRIBUTE:
20438 name = (WXS_ATTR_CAST item)->name;
20439 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20440 break;
20441 case XML_SCHEMA_TYPE_GROUP:
20442 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20443 continue;
20444 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20445 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20446 break;
20447 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20448 if (WXS_REDEFINED_ATTR_GROUP(item))
20449 continue;
20450 name = (WXS_ATTR_GROUP_CAST item)->name;
20451 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20452 break;
20453 case XML_SCHEMA_TYPE_IDC_KEY:
20454 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20455 case XML_SCHEMA_TYPE_IDC_KEYREF:
20456 name = (WXS_IDC_CAST item)->name;
20457 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20458 break;
20459 case XML_SCHEMA_TYPE_NOTATION:
20460 name = ((xmlSchemaNotationPtr) item)->name;
20461 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20462 break;
20463 default:
20464 PERROR_INT("xmlSchemaAddComponents",
20465 "Unexpected global component type");
20466 continue;
20467 }
20468 if (*table == NULL) {
20469 *table = xmlHashCreateDict(10, pctxt->dict);
20470 if (*table == NULL) {
20471 PERROR_INT("xmlSchemaAddComponents",
20472 "failed to create a component hash table");
20473 return(-1);
20474 }
20475 }
20476 err = xmlHashAddEntry(*table, name, item);
20477 if (err != 0) {
20478 xmlChar *str = NULL;
20479
20480 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20481 XML_SCHEMAP_REDEFINED_TYPE,
20482 WXS_ITEM_NODE(item),
20483 WXS_BASIC_CAST item,
20484 "A global %s '%s' does already exist",
20485 WXS_ITEM_TYPE_NAME(item),
20486 xmlSchemaGetComponentQName(&str, item));
20487 FREE_AND_NULL(str);
20488 }
20489 }
20490 /*
20491 * Process imported/included schemas.
20492 */
20493 if (bucket->relations != NULL) {
20494 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20495 do {
20496 if ((rel->bucket != NULL) &&
20497 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20498 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20499 return(-1);
20500 }
20501 rel = rel->next;
20502 } while (rel != NULL);
20503 }
20504 return(0);
20505}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020506
20507static int
20508xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20509{
20510 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20511 xmlSchemaTreeItemPtr item, *items;
20512 int nbItems, i;
20513
20514#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20515
20516 if ((con->pending == NULL) ||
20517 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020518 return(0);
20519
20520 /* TODO:
20521 * SPEC (src-redefine):
20522 * (6.2) "If it has no such self-reference, then all of the
20523 * following must be true:"
20524
20525 * (6.2.2) The {model group} of the model group definition which
20526 * corresponds to it per XML Representation of Model Group
20527 * Definition Schema Components (§3.7.2) must be a ·valid
20528 * restriction· of the {model group} of that model group definition
20529 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20530 */
20531 xmlSchemaCheckSRCRedefineFirst(pctxt);
20532
20533 /*
20534 * Add global components to the schemata's hash tables.
20535 */
20536 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020537
20538 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020539 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20540 nbItems = con->pending->nbItems;
20541 /*
20542 * Now that we have parsed *all* the schema document(s) and converted
20543 * them to schema components, we can resolve references, apply component
20544 * constraints, create the FSA from the content model, etc.
20545 */
20546 /*
20547 * Resolve references of..
20548 *
20549 * 1. element declarations:
20550 * - the type definition
20551 * - the substitution group affiliation
20552 * 2. simple/complex types:
20553 * - the base type definition
20554 * - the memberTypes of union types
20555 * - the itemType of list types
20556 * 3. attributes declarations and attribute uses:
20557 * - the type definition
20558 * - if an attribute use, then the attribute declaration
20559 * 4. attribute group references:
20560 * - the attribute group definition
20561 * 5. particles:
20562 * - the term of the particle (e.g. a model group)
20563 * 6. IDC key-references:
20564 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020565 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020566 */
20567 for (i = 0; i < nbItems; i++) {
20568 item = items[i];
20569 switch (item->type) {
20570 case XML_SCHEMA_TYPE_ELEMENT:
20571 xmlSchemaResolveElementReferences(
20572 (xmlSchemaElementPtr) item, pctxt);
20573 FIXHFAILURE;
20574 break;
20575 case XML_SCHEMA_TYPE_COMPLEX:
20576 case XML_SCHEMA_TYPE_SIMPLE:
20577 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020578 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020579 FIXHFAILURE;
20580 break;
20581 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020582 xmlSchemaResolveAttrTypeReferences(
20583 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020584 FIXHFAILURE;
20585 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020586 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20587 xmlSchemaResolveAttrUseReferences(
20588 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020589 FIXHFAILURE;
20590 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020591 case XML_SCHEMA_EXTRA_QNAMEREF:
20592 if ((WXS_QNAME_CAST item)->itemType ==
20593 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20594 {
20595 xmlSchemaResolveAttrGroupReferences(
20596 WXS_QNAME_CAST item, pctxt);
20597 }
20598 FIXHFAILURE;
20599 break;
20600 case XML_SCHEMA_TYPE_SEQUENCE:
20601 case XML_SCHEMA_TYPE_CHOICE:
20602 case XML_SCHEMA_TYPE_ALL:
20603 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20604 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020605 FIXHFAILURE;
20606 break;
20607 case XML_SCHEMA_TYPE_IDC_KEY:
20608 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20609 case XML_SCHEMA_TYPE_IDC_KEYREF:
20610 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020611 (xmlSchemaIDCPtr) item, pctxt);
20612 FIXHFAILURE;
20613 break;
20614 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20615 /*
20616 * Handle attribue prohibition which had a
20617 * "ref" attribute.
20618 */
20619 xmlSchemaResolveAttrUseProhibReferences(
20620 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020621 FIXHFAILURE;
20622 break;
20623 default:
20624 break;
20625 }
20626 }
20627 if (pctxt->nberrors != 0)
20628 goto exit_error;
20629
20630 /*
20631 * Now that all references are resolved we
20632 * can check for circularity of...
20633 * 1. the base axis of type definitions
20634 * 2. nested model group definitions
20635 * 3. nested attribute group definitions
20636 * TODO: check for circual substitution groups.
20637 */
20638 for (i = 0; i < nbItems; i++) {
20639 item = items[i];
20640 /*
20641 * Let's better stop on the first error here.
20642 */
20643 switch (item->type) {
20644 case XML_SCHEMA_TYPE_COMPLEX:
20645 case XML_SCHEMA_TYPE_SIMPLE:
20646 xmlSchemaCheckTypeDefCircular(
20647 (xmlSchemaTypePtr) item, pctxt);
20648 FIXHFAILURE;
20649 if (pctxt->nberrors != 0)
20650 goto exit_error;
20651 break;
20652 case XML_SCHEMA_TYPE_GROUP:
20653 xmlSchemaCheckGroupDefCircular(
20654 (xmlSchemaModelGroupDefPtr) item, pctxt);
20655 FIXHFAILURE;
20656 if (pctxt->nberrors != 0)
20657 goto exit_error;
20658 break;
20659 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20660 xmlSchemaCheckAttrGroupCircular(
20661 (xmlSchemaAttributeGroupPtr) item, pctxt);
20662 FIXHFAILURE;
20663 if (pctxt->nberrors != 0)
20664 goto exit_error;
20665 break;
20666 default:
20667 break;
20668 }
20669 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020670 if (pctxt->nberrors != 0)
20671 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020672 /*
20673 * Model group definition references:
20674 * Such a reference is reflected by a particle at the component
20675 * level. Until now the 'term' of such particles pointed
20676 * to the model group definition; this was done, in order to
20677 * ease circularity checks. Now we need to set the 'term' of
20678 * such particles to the model group of the model group definition.
20679 */
20680 for (i = 0; i < nbItems; i++) {
20681 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020682 switch (item->type) {
20683 case XML_SCHEMA_TYPE_SEQUENCE:
20684 case XML_SCHEMA_TYPE_CHOICE:
20685 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20686 WXS_MODEL_GROUP_CAST item);
20687 break;
20688 default:
20689 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020690 }
20691 }
20692 if (pctxt->nberrors != 0)
20693 goto exit_error;
20694 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020695 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020696 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020697 for (i = 0; i < nbItems; i++) {
20698 item = items[i];
20699 switch (item->type) {
20700 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20701 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20702 WXS_ATTR_GROUP_HAS_REFS(item))
20703 {
20704 xmlSchemaAttributeGroupExpandRefs(pctxt,
20705 WXS_ATTR_GROUP_CAST item);
20706 FIXHFAILURE;
20707 }
20708 break;
20709 default:
20710 break;
20711 }
20712 }
20713 if (pctxt->nberrors != 0)
20714 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020715 /*
20716 * First compute the variety of simple types. This is needed as
20717 * a seperate step, since otherwise we won't be able to detect
20718 * circular union types in all cases.
20719 */
20720 for (i = 0; i < nbItems; i++) {
20721 item = items[i];
20722 switch (item->type) {
20723 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020724 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020725 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20726 (xmlSchemaTypePtr) item);
20727 FIXHFAILURE;
20728 }
20729 break;
20730 default:
20731 break;
20732 }
20733 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020734 if (pctxt->nberrors != 0)
20735 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020736 /*
20737 * Detect circular union types. Note that this needs the variety to
20738 * be already computed.
20739 */
20740 for (i = 0; i < nbItems; i++) {
20741 item = items[i];
20742 switch (item->type) {
20743 case XML_SCHEMA_TYPE_SIMPLE:
20744 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20745 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20746 (xmlSchemaTypePtr) item);
20747 FIXHFAILURE;
20748 }
20749 break;
20750 default:
20751 break;
20752 }
20753 }
20754 if (pctxt->nberrors != 0)
20755 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020756
20757 /*
20758 * Do the complete type fixup for simple types.
20759 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020760 for (i = 0; i < nbItems; i++) {
20761 item = items[i];
20762 switch (item->type) {
20763 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020764 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20765 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20766 FIXHFAILURE;
20767 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020768 break;
20769 default:
20770 break;
20771 }
20772 }
20773 if (pctxt->nberrors != 0)
20774 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020775 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020776 * At this point we need all simple types to be builded and checked.
20777 */
20778 /*
20779 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020780 */
20781 for (i = 0; i < nbItems; i++) {
20782 item = items[i];
20783 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020784 case XML_SCHEMA_TYPE_ATTRIBUTE:
20785 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20786 FIXHFAILURE;
20787 break;
20788 default:
20789 break;
20790 }
20791 }
20792 if (pctxt->nberrors != 0)
20793 goto exit_error;
20794 /*
20795 * Apply constraints for attribute uses.
20796 */
20797 for (i = 0; i < nbItems; i++) {
20798 item = items[i];
20799 switch (item->type) {
20800 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20801 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20802 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20803 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020804 FIXHFAILURE;
20805 }
20806 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020807 default:
20808 break;
20809 }
20810 }
20811 if (pctxt->nberrors != 0)
20812 goto exit_error;
20813
20814 /*
20815 * Apply constraints for attribute group definitions.
20816 */
20817 for (i = 0; i < nbItems; i++) {
20818 item = items[i];
20819 switch (item->type) {
20820 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20821 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20822 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20823 {
20824 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20825 FIXHFAILURE;
20826 }
20827 break;
20828 default:
20829 break;
20830 }
20831 }
20832 if (pctxt->nberrors != 0)
20833 goto exit_error;
20834
20835 /*
20836 * Apply constraints for redefinitions.
20837 */
20838 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20839 xmlSchemaCheckSRCRedefineSecond(pctxt);
20840 if (pctxt->nberrors != 0)
20841 goto exit_error;
20842
20843 /*
20844 * Fixup complex types.
20845 */
20846 for (i = 0; i < nbItems; i++) {
20847 item = con->pending->items[i];
20848 switch (item->type) {
20849 case XML_SCHEMA_TYPE_COMPLEX:
20850 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20851 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20852 FIXHFAILURE;
20853 }
20854 break;
20855 default:
20856 break;
20857 }
20858 }
20859 if (pctxt->nberrors != 0)
20860 goto exit_error;
20861
20862 /*
20863 * The list could have changed, since xmlSchemaFixupComplexType()
20864 * will create particles and model groups in some cases.
20865 */
20866 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20867 nbItems = con->pending->nbItems;
20868
20869 /*
20870 * At this point all complex types need to be builded and checked.
20871 */
20872 /*
20873 * Apply some constraints for element declarations.
20874 */
20875 for (i = 0; i < nbItems; i++) {
20876 item = items[i];
20877 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020878 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020879
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020880 if ((((xmlSchemaElementPtr) item)->flags &
20881 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20882 xmlSchemaCheckElementDeclComponent(
20883 (xmlSchemaElementPtr) item, pctxt);
20884 FIXHFAILURE;
20885 }
20886 break;
20887 default:
20888 break;
20889 }
20890 }
20891 if (pctxt->nberrors != 0)
20892 goto exit_error;
20893 /*
20894 * Finally we can build the automaton from the content model of
20895 * complex types.
20896 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020897
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020898 for (i = 0; i < nbItems; i++) {
20899 item = items[i];
20900 switch (item->type) {
20901 case XML_SCHEMA_TYPE_COMPLEX:
20902 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020903 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020904 break;
20905 default:
20906 break;
20907 }
20908 }
20909 if (pctxt->nberrors != 0)
20910 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020911 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020912 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020913 */
20914 con->pending->nbItems = 0;
20915 return(0);
20916exit_error:
20917 con->pending->nbItems = 0;
20918 return(pctxt->err);
20919exit_failure:
20920 con->pending->nbItems = 0;
20921 return(-1);
20922}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020923/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020924 * xmlSchemaParse:
20925 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020926 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020927 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020928 * XML Shema struture which can be used to validate instances.
20929 * *WARNING* this interface is highly subject to change
20930 *
20931 * Returns the internal XML Schema structure built from the resource or
20932 * NULL in case of error
20933 */
20934xmlSchemaPtr
20935xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20936{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020937 xmlSchemaPtr schema = NULL;
20938 xmlSchemaBucketPtr bucket = NULL;
20939 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020940
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020941 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020942 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020943 * the API; i.e. not automatically by the validated instance document.
20944 */
20945
Daniel Veillard4255d502002-04-16 15:50:10 +000020946 xmlSchemaInitTypes();
20947
Daniel Veillard6045c902002-10-09 21:13:59 +000020948 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020949 return (NULL);
20950
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020951 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020952 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020953 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020954 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020955
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020956 /* Create the *main* schema. */
20957 schema = xmlSchemaNewSchema(ctxt);
20958 if (schema == NULL)
20959 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000020960 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020961 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020962 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020963 if (ctxt->constructor == NULL) {
20964 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
20965 if (ctxt->constructor == NULL)
20966 return(NULL);
20967 /* Take ownership of the constructor to be able to free it. */
20968 ctxt->ownsConstructor = 1;
20969 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020970 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020971 /*
20972 * Locate and add the schema document.
20973 */
20974 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
20975 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
20976 NULL, NULL, &bucket);
20977 if (res == -1)
20978 goto exit_failure;
20979 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000020980 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020981
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020982 if (bucket == NULL) {
20983 /* TODO: Error code, actually we failed to *locate* the schema. */
20984 if (ctxt->URL)
20985 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20986 NULL, NULL,
20987 "Failed to locate the main schema resource at '%s'",
20988 ctxt->URL, NULL);
20989 else
20990 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
20991 NULL, NULL,
20992 "Failed to locate the main schema resource",
20993 NULL, NULL);
20994 goto exit;
20995 }
20996 /* Set the main schema bucket. */
20997 ctxt->constructor->bucket = bucket;
20998 ctxt->targetNamespace = bucket->targetNamespace;
20999 schema->targetNamespace = bucket->targetNamespace;
21000
21001 /* Then do the parsing for good. */
21002 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
21003 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021004 if (ctxt->nberrors != 0)
21005 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021006
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021007 schema->doc = bucket->doc;
21008 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021009
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021010 ctxt->schema = schema;
21011
21012 if (xmlSchemaFixupComponents(ctxt) == -1)
21013 goto exit_failure;
21014
21015 /*
21016 * TODO: This is not nice, since we cannot distinguish from the
21017 * result if there was an internal error or not.
21018 */
21019exit:
21020 if (ctxt->nberrors != 0) {
21021 if (schema) {
21022 xmlSchemaFree(schema);
21023 schema = NULL;
21024 }
21025 if (ctxt->constructor) {
21026 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21027 ctxt->constructor = NULL;
21028 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021029 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021030 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021031 ctxt->schema = NULL;
21032 return(schema);
21033exit_failure:
21034 /*
21035 * Quite verbose, but should catch internal errors, which were
21036 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021037 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021038 if (schema) {
21039 xmlSchemaFree(schema);
21040 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021041 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021042 if (ctxt->constructor) {
21043 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21044 ctxt->constructor = NULL;
21045 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021046 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021047 PERROR_INT2("xmlSchemaParse",
21048 "An internal error occured");
21049 ctxt->schema = NULL;
21050 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021051}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021052
Daniel Veillard4255d502002-04-16 15:50:10 +000021053/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021054 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021055 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021056 * @err: the error callback
21057 * @warn: the warning callback
21058 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021059 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021060 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021061 */
21062void
21063xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021064 xmlSchemaValidityErrorFunc err,
21065 xmlSchemaValidityWarningFunc warn, void *ctx)
21066{
Daniel Veillard4255d502002-04-16 15:50:10 +000021067 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021068 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021069 ctxt->error = err;
21070 ctxt->warning = warn;
21071 ctxt->userData = ctx;
21072}
21073
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021074/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021075 * xmlSchemaGetParserErrors:
21076 * @ctxt: a XMl-Schema parser context
21077 * @err: the error callback result
21078 * @warn: the warning callback result
21079 * @ctx: contextual data for the callbacks result
21080 *
21081 * Get the callback information used to handle errors for a parser context
21082 *
21083 * Returns -1 in case of failure, 0 otherwise
21084 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021085int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021086xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
21087 xmlSchemaValidityErrorFunc * err,
21088 xmlSchemaValidityWarningFunc * warn, void **ctx)
21089{
21090 if (ctxt == NULL)
21091 return(-1);
21092 if (err != NULL)
21093 *err = ctxt->error;
21094 if (warn != NULL)
21095 *warn = ctxt->warning;
21096 if (ctx != NULL)
21097 *ctx = ctxt->userData;
21098 return(0);
21099}
21100
21101/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021102 * xmlSchemaFacetTypeToString:
21103 * @type: the facet type
21104 *
21105 * Convert the xmlSchemaTypeType to a char string.
21106 *
21107 * Returns the char string representation of the facet type if the
21108 * type is a facet and an "Internal Error" string otherwise.
21109 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021110static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021111xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21112{
21113 switch (type) {
21114 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021115 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021116 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021117 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021118 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021119 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021120 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021121 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021122 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021123 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021124 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021125 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021126 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021127 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021128 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021129 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021130 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021131 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021132 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021133 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021134 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021135 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021136 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021137 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021138 default:
21139 break;
21140 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021141 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021142}
21143
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021144static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021145xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21146{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021147 /*
21148 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021149 * from xsd:string.
21150 */
21151 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021152 /*
21153 * Note that we assume a whitespace of preserve for anySimpleType.
21154 */
21155 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21156 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21157 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021158 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021159 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021160 else {
21161 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021162 * For all ·atomic· datatypes other than string (and types ·derived·
21163 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021164 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021165 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021166 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021167 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021168 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021169 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021170 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021171 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021172 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021173 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021174 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021175 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021176 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021177 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21178 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21179 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21180 return (XML_SCHEMA_WHITESPACE_REPLACE);
21181 else
21182 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021183 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021184 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021185}
21186
Daniel Veillard4255d502002-04-16 15:50:10 +000021187/************************************************************************
21188 * *
21189 * Simple type validation *
21190 * *
21191 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021192
Daniel Veillard4255d502002-04-16 15:50:10 +000021193
21194/************************************************************************
21195 * *
21196 * DOM Validation code *
21197 * *
21198 ************************************************************************/
21199
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021200/**
21201 * xmlSchemaAssembleByLocation:
21202 * @pctxt: a schema parser context
21203 * @vctxt: a schema validation context
21204 * @schema: the existing schema
21205 * @node: the node that fired the assembling
21206 * @nsName: the namespace name of the new schema
21207 * @location: the location of the schema
21208 *
21209 * Expands an existing schema by an additional schema.
21210 *
21211 * Returns 0 if the new schema is correct, a positive error code
21212 * number otherwise and -1 in case of an internal or API error.
21213 */
21214static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021215xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021216 xmlSchemaPtr schema,
21217 xmlNodePtr node,
21218 const xmlChar *nsName,
21219 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021220{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021221 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021222 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021223 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021224
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021225 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021226 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021227
21228 if (vctxt->pctxt == NULL) {
21229 VERROR_INT("xmlSchemaAssembleByLocation",
21230 "no parser context available");
21231 return(-1);
21232 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021233 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021234 if (pctxt->constructor == NULL) {
21235 PERROR_INT("xmlSchemaAssembleByLocation",
21236 "no constructor");
21237 return(-1);
21238 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021239 /*
21240 * Acquire the schema document.
21241 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021242 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21243 location, node);
21244 /*
21245 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21246 * the process will automatically change this to
21247 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21248 */
21249 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21250 location, NULL, NULL, 0, node, NULL, nsName,
21251 &bucket);
21252 if (ret != 0)
21253 return(ret);
21254 if (bucket == NULL) {
21255 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021256 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021257 return(-1);
21258 }
21259 /*
21260 * The first located schema will be handled as if all other
21261 * schemas imported by XSI were imported by this first schema.
21262 */
21263 if ((bucket != NULL) &&
21264 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21265 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21266 /*
21267 * TODO: Is this handled like an import? I.e. is it not an error
21268 * if the schema cannot be located?
21269 */
21270 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21271 return(0);
21272 /*
21273 * We will reuse the parser context for every schema imported
21274 * directly via XSI. So reset the context.
21275 */
21276 pctxt->nberrors = 0;
21277 pctxt->err = 0;
21278 pctxt->doc = bucket->doc;
21279
21280 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21281 if (ret == -1) {
21282 pctxt->doc = NULL;
21283 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021284 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021285 /* Paranoid error channelling. */
21286 if ((ret == 0) && (pctxt->nberrors != 0))
21287 ret = pctxt->err;
21288 if (pctxt->nberrors == 0) {
21289 /*
21290 * Only bother to fixup pending components, if there was
21291 * no error yet.
21292 */
21293 xmlSchemaFixupComponents(pctxt);
21294 vctxt->nberrors += pctxt->nberrors;
21295 } else {
21296 /* Add to validation error sum. */
21297 vctxt->nberrors += pctxt->nberrors;
21298 }
21299 pctxt->doc = NULL;
21300 return(ret);
21301exit_failure:
21302 pctxt->doc = NULL;
21303 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021304}
21305
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021306static xmlSchemaAttrInfoPtr
21307xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21308 int metaType)
21309{
21310 if (vctxt->nbAttrInfos == 0)
21311 return (NULL);
21312 {
21313 int i;
21314 xmlSchemaAttrInfoPtr iattr;
21315
21316 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21317 iattr = vctxt->attrInfos[i];
21318 if (iattr->metaType == metaType)
21319 return (iattr);
21320 }
21321
21322 }
21323 return (NULL);
21324}
21325
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021326/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021327 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021328 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021329 *
21330 * Expands an existing schema by an additional schema using
21331 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21332 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21333 * must be set to 1.
21334 *
21335 * Returns 0 if the new schema is correct, a positive error code
21336 * number otherwise and -1 in case of an internal or API error.
21337 */
21338static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021339xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021340{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021341 const xmlChar *cur, *end;
21342 const xmlChar *nsname = NULL, *location;
21343 int count = 0;
21344 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021345 xmlSchemaAttrInfoPtr iattr;
21346
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021347 /*
21348 * Parse the value; we will assume an even number of values
21349 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021350 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021351 * URGENT TODO: !! This needs to work for both
21352 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21353 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021354 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021355 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21356 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21357 if (iattr == NULL)
21358 xmlSchemaGetMetaAttrInfo(vctxt,
21359 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21360 if (iattr == NULL)
21361 return (0);
21362 cur = iattr->value;
21363 do {
21364 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021365 /*
21366 * Get the namespace name.
21367 */
21368 while (IS_BLANK_CH(*cur))
21369 cur++;
21370 end = cur;
21371 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21372 end++;
21373 if (end == cur)
21374 break;
21375 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021376 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021377 cur = end;
21378 }
21379 /*
21380 * Get the URI.
21381 */
21382 while (IS_BLANK_CH(*cur))
21383 cur++;
21384 end = cur;
21385 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21386 end++;
21387 if (end == cur)
21388 break;
21389 count++;
21390 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021391 cur = end;
21392 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21393 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021394 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021395 VERROR_INT("xmlSchemaAssembleByXSI",
21396 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021397 return (-1);
21398 }
21399 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021400 return (ret);
21401}
21402
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021403static const xmlChar *
21404xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21405 const xmlChar *prefix)
21406{
21407 if (vctxt->sax != NULL) {
21408 int i, j;
21409 xmlSchemaNodeInfoPtr inode;
21410
21411 for (i = vctxt->depth; i >= 0; i--) {
21412 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21413 inode = vctxt->elemInfos[i];
21414 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21415 if (((prefix == NULL) &&
21416 (inode->nsBindings[j] == NULL)) ||
21417 ((prefix != NULL) && xmlStrEqual(prefix,
21418 inode->nsBindings[j]))) {
21419
21420 /*
21421 * Note that the namespace bindings are already
21422 * in a string dict.
21423 */
21424 return (inode->nsBindings[j+1]);
21425 }
21426 }
21427 }
21428 }
21429 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021430#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021431 } else if (vctxt->reader != NULL) {
21432 xmlChar *nsName;
21433
21434 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21435 if (nsName != NULL) {
21436 const xmlChar *ret;
21437
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021438 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21439 xmlFree(nsName);
21440 return (ret);
21441 } else
21442 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021443#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021444 } else {
21445 xmlNsPtr ns;
21446
21447 if ((vctxt->inode->node == NULL) ||
21448 (vctxt->inode->node->doc == NULL)) {
21449 VERROR_INT("xmlSchemaLookupNamespace",
21450 "no node or node's doc avaliable");
21451 return (NULL);
21452 }
21453 ns = xmlSearchNs(vctxt->inode->node->doc,
21454 vctxt->inode->node, prefix);
21455 if (ns != NULL)
21456 return (ns->href);
21457 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021458 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021459}
21460
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021461/*
21462* This one works on the schema of the validation context.
21463*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021464static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021465xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21466 xmlSchemaPtr schema,
21467 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021468 const xmlChar *value,
21469 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021470 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021471{
21472 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021473
21474 if (vctxt && (vctxt->schema == NULL)) {
21475 VERROR_INT("xmlSchemaValidateNotation",
21476 "a schema is needed on the validation context");
21477 return (-1);
21478 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021479 ret = xmlValidateQName(value, 1);
21480 if (ret != 0)
21481 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021482 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021483 xmlChar *localName = NULL;
21484 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021485
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021486 localName = xmlSplitQName2(value, &prefix);
21487 if (prefix != NULL) {
21488 const xmlChar *nsName = NULL;
21489
21490 if (vctxt != NULL)
21491 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21492 else if (node != NULL) {
21493 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21494 if (ns != NULL)
21495 nsName = ns->href;
21496 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021497 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021498 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021499 return (1);
21500 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021501 if (nsName == NULL) {
21502 xmlFree(prefix);
21503 xmlFree(localName);
21504 return (1);
21505 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021506 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021507 if (valNeeded && (val != NULL)) {
21508 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21509 BAD_CAST xmlStrdup(nsName));
21510 if (*val == NULL)
21511 ret = -1;
21512 }
21513 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021514 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021515 xmlFree(prefix);
21516 xmlFree(localName);
21517 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021518 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021519 if (valNeeded && (val != NULL)) {
21520 (*val) = xmlSchemaNewNOTATIONValue(
21521 BAD_CAST xmlStrdup(value), NULL);
21522 if (*val == NULL)
21523 ret = -1;
21524 }
21525 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021526 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021527 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021528 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021529 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021530}
21531
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021532static int
21533xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21534 const xmlChar* lname,
21535 const xmlChar* nsname)
21536{
21537 int i;
21538
21539 lname = xmlDictLookup(vctxt->dict, lname, -1);
21540 if (lname == NULL)
21541 return(-1);
21542 if (nsname != NULL) {
21543 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21544 if (nsname == NULL)
21545 return(-1);
21546 }
21547 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21548 if ((vctxt->nodeQNames->items [i] == lname) &&
21549 (vctxt->nodeQNames->items[i +1] == nsname))
21550 /* Already there */
21551 return(i);
21552 }
21553 /* Add new entry. */
21554 i = vctxt->nodeQNames->nbItems;
21555 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21556 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21557 return(i);
21558}
21559
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021560/************************************************************************
21561 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021562 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021563 * *
21564 ************************************************************************/
21565
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021566/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021567 * xmlSchemaAugmentIDC:
21568 * @idcDef: the IDC definition
21569 *
21570 * Creates an augmented IDC definition item.
21571 *
21572 * Returns the item, or NULL on internal errors.
21573 */
21574static void
21575xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21576 xmlSchemaValidCtxtPtr vctxt)
21577{
21578 xmlSchemaIDCAugPtr aidc;
21579
21580 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21581 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021582 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021583 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21584 NULL);
21585 return;
21586 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021587 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021588 aidc->def = idcDef;
21589 aidc->next = NULL;
21590 if (vctxt->aidcs == NULL)
21591 vctxt->aidcs = aidc;
21592 else {
21593 aidc->next = vctxt->aidcs;
21594 vctxt->aidcs = aidc;
21595 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021596 /*
21597 * Save if we have keyrefs at all.
21598 */
21599 if ((vctxt->hasKeyrefs == 0) &&
21600 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21601 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021602}
21603
21604/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021605 * xmlSchemaIDCNewBinding:
21606 * @idcDef: the IDC definition of this binding
21607 *
21608 * Creates a new IDC binding.
21609 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021610 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021611 */
21612static xmlSchemaPSVIIDCBindingPtr
21613xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21614{
21615 xmlSchemaPSVIIDCBindingPtr ret;
21616
21617 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21618 sizeof(xmlSchemaPSVIIDCBinding));
21619 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021620 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021621 "allocating a PSVI IDC binding item", NULL);
21622 return (NULL);
21623 }
21624 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21625 ret->definition = idcDef;
21626 return (ret);
21627}
21628
21629/**
21630 * xmlSchemaIDCStoreNodeTableItem:
21631 * @vctxt: the WXS validation context
21632 * @item: the IDC node table item
21633 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021634 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021635 * They are stored to avoid copying them if IDC node-tables are merged
21636 * with corresponding parent IDC node-tables (bubbling).
21637 *
21638 * Returns 0 if succeeded, -1 on internal errors.
21639 */
21640static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021641xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021642 xmlSchemaPSVIIDCNodePtr item)
21643{
21644 /*
21645 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021646 */
21647 if (vctxt->idcNodes == NULL) {
21648 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021649 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21650 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021651 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021652 "allocating the IDC node table item list", NULL);
21653 return (-1);
21654 }
21655 vctxt->sizeIdcNodes = 20;
21656 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21657 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021658 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21659 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021660 sizeof(xmlSchemaPSVIIDCNodePtr));
21661 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021662 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021663 "re-allocating the IDC node table item list", NULL);
21664 return (-1);
21665 }
21666 }
21667 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021668
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021669 return (0);
21670}
21671
21672/**
21673 * xmlSchemaIDCStoreKey:
21674 * @vctxt: the WXS validation context
21675 * @item: the IDC key
21676 *
21677 * The validation context is used to store an IDC key.
21678 *
21679 * Returns 0 if succeeded, -1 on internal errors.
21680 */
21681static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021682xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021683 xmlSchemaPSVIIDCKeyPtr key)
21684{
21685 /*
21686 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021687 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021688 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021689 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021690 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21691 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021692 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021693 "allocating the IDC key storage list", NULL);
21694 return (-1);
21695 }
21696 vctxt->sizeIdcKeys = 40;
21697 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21698 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021699 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21700 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021701 sizeof(xmlSchemaPSVIIDCKeyPtr));
21702 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021703 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021704 "re-allocating the IDC key storage list", NULL);
21705 return (-1);
21706 }
21707 }
21708 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021709
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021710 return (0);
21711}
21712
21713/**
21714 * xmlSchemaIDCAppendNodeTableItem:
21715 * @bind: the IDC binding
21716 * @ntItem: the node-table item
21717 *
21718 * Appends the IDC node-table item to the binding.
21719 *
21720 * Returns 0 on success and -1 on internal errors.
21721 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021722static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021723xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21724 xmlSchemaPSVIIDCNodePtr ntItem)
21725{
21726 if (bind->nodeTable == NULL) {
21727 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021728 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021729 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21730 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021731 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021732 "allocating an array of IDC node-table items", NULL);
21733 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021734 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021735 } else if (bind->sizeNodes <= bind->nbNodes) {
21736 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021737 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21738 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021739 sizeof(xmlSchemaPSVIIDCNodePtr));
21740 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021741 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021742 "re-allocating an array of IDC node-table items", NULL);
21743 return(-1);
21744 }
21745 }
21746 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021747 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021748}
21749
21750/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021751 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021752 * @vctxt: the WXS validation context
21753 * @matcher: the IDC matcher
21754 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021755 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021756 * of the given matcher. If none found, a new one is created
21757 * and added to the IDC table.
21758 *
21759 * Returns an IDC binding or NULL on internal errors.
21760 */
21761static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021762xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021763 xmlSchemaIDCMatcherPtr matcher)
21764{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021765 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021766
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021767 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021768
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021769 if (ielem->idcTable == NULL) {
21770 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21771 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021772 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021773 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021774 } else {
21775 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021776
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021777 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021778 do {
21779 if (bind->definition == matcher->aidc->def)
21780 return(bind);
21781 if (bind->next == NULL) {
21782 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21783 if (bind->next == NULL)
21784 return (NULL);
21785 return(bind->next);
21786 }
21787 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021788 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021789 }
21790 return (NULL);
21791}
21792
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021793static xmlSchemaItemListPtr
21794xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21795 xmlSchemaIDCMatcherPtr matcher)
21796{
21797 if (matcher->targets == NULL)
21798 matcher->targets = xmlSchemaItemListCreate();
21799 return(matcher->targets);
21800}
21801
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021802/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021803 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021804 * @key: the IDC key
21805 *
21806 * Frees an IDC key together with its compiled value.
21807 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021808static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021809xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21810{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021811 if (key->val != NULL)
21812 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021813 xmlFree(key);
21814}
21815
21816/**
21817 * xmlSchemaIDCFreeBinding:
21818 *
21819 * Frees an IDC binding. Note that the node table-items
21820 * are not freed.
21821 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021822static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021823xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21824{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021825 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021826 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021827 if (bind->dupls != NULL)
21828 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021829 xmlFree(bind);
21830}
21831
21832/**
21833 * xmlSchemaIDCFreeIDCTable:
21834 * @bind: the first IDC binding in the list
21835 *
21836 * Frees an IDC table, i.e. all the IDC bindings in the list.
21837 */
21838static void
21839xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21840{
21841 xmlSchemaPSVIIDCBindingPtr prev;
21842
21843 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021844 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021845 bind = bind->next;
21846 xmlSchemaIDCFreeBinding(prev);
21847 }
21848}
21849
21850/**
21851 * xmlSchemaIDCFreeMatcherList:
21852 * @matcher: the first IDC matcher in the list
21853 *
21854 * Frees a list of IDC matchers.
21855 */
21856static void
21857xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21858{
21859 xmlSchemaIDCMatcherPtr next;
21860
21861 while (matcher != NULL) {
21862 next = matcher->next;
21863 if (matcher->keySeqs != NULL) {
21864 int i;
21865 for (i = 0; i < matcher->sizeKeySeqs; i++)
21866 if (matcher->keySeqs[i] != NULL)
21867 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021868 xmlFree(matcher->keySeqs);
21869 }
21870 if (matcher->targets != NULL) {
21871 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21872 int i;
21873 xmlSchemaPSVIIDCNodePtr idcNode;
21874 /*
21875 * Node-table items for keyrefs are not stored globally
21876 * to the validation context, since they are not bubbled.
21877 * We need to free them here.
21878 */
21879 for (i = 0; i < matcher->targets->nbItems; i++) {
21880 idcNode =
21881 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21882 xmlFree(idcNode->keys);
21883 xmlFree(idcNode);
21884 }
21885 }
21886 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021887 }
21888 xmlFree(matcher);
21889 matcher = next;
21890 }
21891}
21892
21893/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021894 * xmlSchemaIDCAddStateObject:
21895 * @vctxt: the WXS validation context
21896 * @matcher: the IDC matcher
21897 * @sel: the XPath information
21898 * @parent: the parent "selector" state object if any
21899 * @type: "selector" or "field"
21900 *
21901 * Creates/reuses and activates state objects for the given
21902 * XPath information; if the XPath expression consists of unions,
21903 * multiple state objects are created for every unioned expression.
21904 *
21905 * Returns 0 on success and -1 on internal errors.
21906 */
21907static int
21908xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21909 xmlSchemaIDCMatcherPtr matcher,
21910 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021911 int type)
21912{
21913 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021914
21915 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021916 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021917 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021918 if (vctxt->xpathStatePool != NULL) {
21919 sto = vctxt->xpathStatePool;
21920 vctxt->xpathStatePool = sto->next;
21921 sto->next = NULL;
21922 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021923 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021924 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021925 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021926 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21927 if (sto == NULL) {
21928 xmlSchemaVErrMemory(NULL,
21929 "allocating an IDC state object", NULL);
21930 return (-1);
21931 }
21932 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
21933 }
21934 /*
21935 * Add to global list.
21936 */
21937 if (vctxt->xpathStates != NULL)
21938 sto->next = vctxt->xpathStates;
21939 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021940
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021941 /*
21942 * Free the old xpath validation context.
21943 */
21944 if (sto->xpathCtxt != NULL)
21945 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
21946
21947 /*
21948 * Create a new XPath (pattern) validation context.
21949 */
21950 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
21951 (xmlPatternPtr) sel->xpathComp);
21952 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021953 VERROR_INT("xmlSchemaIDCAddStateObject",
21954 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021955 return (-1);
21956 }
21957 sto->type = type;
21958 sto->depth = vctxt->depth;
21959 sto->matcher = matcher;
21960 sto->sel = sel;
21961 sto->nbHistory = 0;
21962
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021963#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021964 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
21965 sto->sel->xpath);
21966#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021967 return (0);
21968}
21969
21970/**
21971 * xmlSchemaXPathEvaluate:
21972 * @vctxt: the WXS validation context
21973 * @nodeType: the nodeType of the current node
21974 *
21975 * Evaluates all active XPath state objects.
21976 *
21977 * Returns the number of IC "field" state objects which resolved to
21978 * this node, 0 if none resolved and -1 on internal errors.
21979 */
21980static int
21981xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021982 xmlElementType nodeType)
21983{
21984 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021985 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021986
21987 if (vctxt->xpathStates == NULL)
21988 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021989
21990 if (nodeType == XML_ATTRIBUTE_NODE)
21991 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021992#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021993 {
21994 xmlChar *str = NULL;
21995 xmlGenericError(xmlGenericErrorContext,
21996 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021997 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
21998 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021999 FREE_AND_NULL(str)
22000 }
22001#endif
22002 /*
22003 * Process all active XPath state objects.
22004 */
22005 first = vctxt->xpathStates;
22006 sto = first;
22007 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022008#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022009 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022010 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22011 sto->matcher->aidc->def->name, sto->sel->xpath);
22012 else
22013 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22014 sto->matcher->aidc->def->name, sto->sel->xpath);
22015#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022016 if (nodeType == XML_ELEMENT_NODE)
22017 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022018 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022019 else
22020 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022021 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022022
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022023 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022024 VERROR_INT("xmlSchemaXPathEvaluate",
22025 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022026 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022027 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022028 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022029 goto next_sto;
22030 /*
22031 * Full match.
22032 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022033#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022034 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022035 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022036#endif
22037 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022038 * Register a match in the state object history.
22039 */
22040 if (sto->history == NULL) {
22041 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22042 if (sto->history == NULL) {
22043 xmlSchemaVErrMemory(NULL,
22044 "allocating the state object history", NULL);
22045 return(-1);
22046 }
22047 sto->sizeHistory = 10;
22048 } else if (sto->sizeHistory <= sto->nbHistory) {
22049 sto->sizeHistory *= 2;
22050 sto->history = (int *) xmlRealloc(sto->history,
22051 sto->sizeHistory * sizeof(int));
22052 if (sto->history == NULL) {
22053 xmlSchemaVErrMemory(NULL,
22054 "re-allocating the state object history", NULL);
22055 return(-1);
22056 }
22057 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022058 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022059
22060#ifdef DEBUG_IDC
22061 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22062 vctxt->depth);
22063#endif
22064
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022065 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22066 xmlSchemaIDCSelectPtr sel;
22067 /*
22068 * Activate state objects for the IDC fields of
22069 * the IDC selector.
22070 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022071#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022072 xmlGenericError(xmlGenericErrorContext, "IDC: "
22073 "activating field states\n");
22074#endif
22075 sel = sto->matcher->aidc->def->fields;
22076 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022077 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22078 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22079 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022080 sel = sel->next;
22081 }
22082 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22083 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022084 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022085 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022086#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022087 xmlGenericError(xmlGenericErrorContext,
22088 "IDC: key found\n");
22089#endif
22090 /*
22091 * Notify that the character value of this node is
22092 * needed.
22093 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022094 if (resolved == 0) {
22095 if ((vctxt->inode->flags &
22096 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22097 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22098 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022099 resolved++;
22100 }
22101next_sto:
22102 if (sto->next == NULL) {
22103 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022104 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022105 */
22106 head = first;
22107 sto = vctxt->xpathStates;
22108 } else
22109 sto = sto->next;
22110 }
22111 return (resolved);
22112}
22113
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022114static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022115xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022116 xmlChar **buf,
22117 xmlSchemaPSVIIDCKeyPtr *seq,
22118 int count)
22119{
22120 int i, res;
22121 const xmlChar *value = NULL;
22122
22123 *buf = xmlStrdup(BAD_CAST "[");
22124 for (i = 0; i < count; i++) {
22125 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022126 res = xmlSchemaGetCanonValueWhtsp(seq[i]->val, &value,
22127 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type));
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022128 if (res == 0)
22129 *buf = xmlStrcat(*buf, value);
22130 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022131 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22132 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022133 *buf = xmlStrcat(*buf, BAD_CAST "???");
22134 }
22135 if (i < count -1)
22136 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22137 else
22138 *buf = xmlStrcat(*buf, BAD_CAST "'");
22139 if (value != NULL) {
22140 xmlFree((xmlChar *) value);
22141 value = NULL;
22142 }
22143 }
22144 *buf = xmlStrcat(*buf, BAD_CAST "]");
22145
22146 return (BAD_CAST *buf);
22147}
22148
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022149/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022150 * xmlSchemaXPathPop:
22151 * @vctxt: the WXS validation context
22152 *
22153 * Pops all XPath states.
22154 *
22155 * Returns 0 on success and -1 on internal errors.
22156 */
22157static int
22158xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22159{
22160 xmlSchemaIDCStateObjPtr sto;
22161 int res;
22162
22163 if (vctxt->xpathStates == NULL)
22164 return(0);
22165 sto = vctxt->xpathStates;
22166 do {
22167 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22168 if (res == -1)
22169 return (-1);
22170 sto = sto->next;
22171 } while (sto != NULL);
22172 return(0);
22173}
22174
22175/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022176 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022177 * @vctxt: the WXS validation context
22178 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022179 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022180 *
22181 * Processes and pops the history items of the IDC state objects.
22182 * IDC key-sequences are validated/created on IDC bindings.
22183 *
22184 * Returns 0 on success and -1 on internal errors.
22185 */
22186static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022187xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022188 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022189{
22190 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022191 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022192 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022193 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022194
22195 if (vctxt->xpathStates == NULL)
22196 return (0);
22197 sto = vctxt->xpathStates;
22198
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022199#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022200 {
22201 xmlChar *str = NULL;
22202 xmlGenericError(xmlGenericErrorContext,
22203 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022204 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22205 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022206 FREE_AND_NULL(str)
22207 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022208#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022209 /*
22210 * Evaluate the state objects.
22211 */
22212 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022213 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22214 if (res == -1) {
22215 VERROR_INT("xmlSchemaXPathProcessHistory",
22216 "calling xmlStreamPop()");
22217 return (-1);
22218 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022219#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022220 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22221 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022222#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022223 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022224 goto deregister_check;
22225
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022226 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022227
22228 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022229 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022230 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022231 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022232 sto = sto->next;
22233 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022234 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022235 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022236 /*
22237 * NOTE: According to
22238 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22239 * ... the simple-content of complex types is also allowed.
22240 */
22241
22242 if (WXS_IS_COMPLEX(type)) {
22243 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22244 /*
22245 * Sanity check for complex types with simple content.
22246 */
22247 simpleType = type->contentTypeDef;
22248 if (simpleType == NULL) {
22249 VERROR_INT("xmlSchemaXPathProcessHistory",
22250 "field resolves to a CT with simple content "
22251 "but the CT is missing the ST definition");
22252 return (-1);
22253 }
22254 } else
22255 simpleType = NULL;
22256 } else
22257 simpleType = type;
22258 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022259 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022260
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022261 /*
22262 * Not qualified if the field resolves to a node of non
22263 * simple type.
22264 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022265 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022266 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022267 WXS_BASIC_CAST sto->matcher->aidc->def,
22268 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022269 "non-simple type",
22270 sto->sel->xpath,
22271 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22272 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022273 sto->nbHistory--;
22274 goto deregister_check;
22275 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022276
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022277 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022278 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022279 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022280 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022281 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022282 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022283 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022284 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022285 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022286 sto->nbHistory--;
22287 goto deregister_check;
22288 } else {
22289 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22290 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022291 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022292
22293 /*
22294 * The key will be anchored on the matcher's list of
22295 * key-sequences. The position in this list is determined
22296 * by the target node's depth relative to the matcher's
22297 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022298 *
22299 * Element Depth Pos List-entries
22300 * <scope> 0 NULL
22301 * <bar> 1 NULL
22302 * <target/> 2 2 target
22303 * <bar>
22304 * </scope>
22305 *
22306 * The size of the list is only dependant on the depth of
22307 * the tree.
22308 * An entry will be NULLed in selector_leave, i.e. when
22309 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022310 */
22311 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022312 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022313
22314 /*
22315 * Create/grow the array of key-sequences.
22316 */
22317 if (matcher->keySeqs == NULL) {
22318 if (pos > 9)
22319 matcher->sizeKeySeqs = pos * 2;
22320 else
22321 matcher->sizeKeySeqs = 10;
22322 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22323 xmlMalloc(matcher->sizeKeySeqs *
22324 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22325 if (matcher->keySeqs == NULL) {
22326 xmlSchemaVErrMemory(NULL,
22327 "allocating an array of key-sequences",
22328 NULL);
22329 return(-1);
22330 }
22331 memset(matcher->keySeqs, 0,
22332 matcher->sizeKeySeqs *
22333 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22334 } else if (pos >= matcher->sizeKeySeqs) {
22335 int i = matcher->sizeKeySeqs;
22336
22337 matcher->sizeKeySeqs *= 2;
22338 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22339 xmlRealloc(matcher->keySeqs,
22340 matcher->sizeKeySeqs *
22341 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022342 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022343 xmlSchemaVErrMemory(NULL,
22344 "reallocating an array of key-sequences",
22345 NULL);
22346 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022347 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022348 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022349 * The array needs to be NULLed.
22350 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022351 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022352 for (; i < matcher->sizeKeySeqs; i++)
22353 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022354 }
22355
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022356 /*
22357 * Get/create the key-sequence.
22358 */
22359 keySeq = matcher->keySeqs[pos];
22360 if (keySeq == NULL) {
22361 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022362 } else if (keySeq[idx] != NULL) {
22363 xmlChar *str = NULL;
22364 /*
22365 * cvc-identity-constraint:
22366 * 3 For each node in the ·target node set· all
22367 * of the {fields}, with that node as the context
22368 * node, evaluate to either an empty node-set or
22369 * a node-set with exactly one member, which must
22370 * have a simple type.
22371 *
22372 * The key was already set; report an error.
22373 */
22374 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22375 XML_SCHEMAV_CVC_IDC, NULL,
22376 WXS_BASIC_CAST matcher->aidc->def,
22377 "The XPath '%s' of a field of %s evaluates to a "
22378 "node-set with more than one member",
22379 sto->sel->xpath,
22380 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22381 FREE_AND_NULL(str);
22382 sto->nbHistory--;
22383 goto deregister_check;
22384 } else
22385 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022386
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022387create_sequence:
22388 /*
22389 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022390 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022391 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22392 matcher->aidc->def->nbFields *
22393 sizeof(xmlSchemaPSVIIDCKeyPtr));
22394 if (keySeq == NULL) {
22395 xmlSchemaVErrMemory(NULL,
22396 "allocating an IDC key-sequence", NULL);
22397 return(-1);
22398 }
22399 memset(keySeq, 0, matcher->aidc->def->nbFields *
22400 sizeof(xmlSchemaPSVIIDCKeyPtr));
22401 matcher->keySeqs[pos] = keySeq;
22402create_key:
22403 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022404 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022405 */
22406 if (key == NULL) {
22407 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22408 sizeof(xmlSchemaPSVIIDCKey));
22409 if (key == NULL) {
22410 xmlSchemaVErrMemory(NULL,
22411 "allocating a IDC key", NULL);
22412 xmlFree(keySeq);
22413 matcher->keySeqs[pos] = NULL;
22414 return(-1);
22415 }
22416 /*
22417 * Consume the compiled value.
22418 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022419 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022420 key->val = vctxt->inode->val;
22421 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022422 /*
22423 * Store the key in a global list.
22424 */
22425 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22426 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022427 return (-1);
22428 }
22429 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022430 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022431 }
22432 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022433
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022434 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022435 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022436 xmlSchemaPSVIIDCNodePtr ntItem;
22437 xmlSchemaIDCMatcherPtr matcher;
22438 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022439 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022440 int pos, i, j, nbKeys;
22441 /*
22442 * Here we have the following scenario:
22443 * An IDC 'selector' state object resolved to a target node,
22444 * during the time this target node was in the
22445 * ancestor-or-self axis, the 'field' state object(s) looked
22446 * out for matching nodes to create a key-sequence for this
22447 * target node. Now we are back to this target node and need
22448 * to put the key-sequence, together with the target node
22449 * itself, into the node-table of the corresponding IDC
22450 * binding.
22451 */
22452 matcher = sto->matcher;
22453 idc = matcher->aidc->def;
22454 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022455 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022456 /*
22457 * Check if the matcher has any key-sequences at all, plus
22458 * if it has a key-sequence for the current target node.
22459 */
22460 if ((matcher->keySeqs == NULL) ||
22461 (matcher->sizeKeySeqs <= pos)) {
22462 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22463 goto selector_key_error;
22464 else
22465 goto selector_leave;
22466 }
22467
22468 keySeq = &(matcher->keySeqs[pos]);
22469 if (*keySeq == NULL) {
22470 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22471 goto selector_key_error;
22472 else
22473 goto selector_leave;
22474 }
22475
22476 for (i = 0; i < nbKeys; i++) {
22477 if ((*keySeq)[i] == NULL) {
22478 /*
22479 * Not qualified, if not all fields did resolve.
22480 */
22481 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22482 /*
22483 * All fields of a "key" IDC must resolve.
22484 */
22485 goto selector_key_error;
22486 }
22487 goto selector_leave;
22488 }
22489 }
22490 /*
22491 * All fields did resolve.
22492 */
22493
22494 /*
22495 * 4.1 If the {identity-constraint category} is unique(/key),
22496 * then no two members of the ·qualified node set· have
22497 * ·key-sequences· whose members are pairwise equal, as
22498 * defined by Equal in [XML Schemas: Datatypes].
22499 *
22500 * Get the IDC binding from the matcher and check for
22501 * duplicate key-sequences.
22502 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022503#if 0
22504 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22505#endif
22506 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022507 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022508 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022509 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022510
22511 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022512 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022513 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022514 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022515 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022516 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022517 bkeySeq =
22518 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022519 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022520 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022521 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022522 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022523 if (res == -1) {
22524 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022525 } else if (res == 0) {
22526 /*
22527 * One of the keys differs, so the key-sequence
22528 * won't be equal; get out.
22529 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022530 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022531 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022532 }
22533 if (res == 1) {
22534 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022535 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022536 */
22537 break;
22538 }
22539 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022540 } while (i < targets->nbItems);
22541 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022542 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022543 /*
22544 * TODO: Try to report the key-sequence.
22545 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022546 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022547 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022548 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022549 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022550 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022551 (*keySeq), nbKeys),
22552 xmlSchemaGetIDCDesignation(&strB, idc));
22553 FREE_AND_NULL(str);
22554 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022555 goto selector_leave;
22556 }
22557 }
22558 /*
22559 * Add a node-table item to the IDC binding.
22560 */
22561 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22562 sizeof(xmlSchemaPSVIIDCNode));
22563 if (ntItem == NULL) {
22564 xmlSchemaVErrMemory(NULL,
22565 "allocating an IDC node-table item", NULL);
22566 xmlFree(*keySeq);
22567 *keySeq = NULL;
22568 return(-1);
22569 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022570 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022571
22572 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022573 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022574 */
22575 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22576 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22577 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022578 xmlFree(*keySeq);
22579 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022580 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022581 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022582 ntItem->nodeQNameID = -1;
22583 } else {
22584 /*
22585 * Save a cached QName for this node on the IDC node, to be
22586 * able to report it, even if the node is not saved.
22587 */
22588 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22589 vctxt->inode->localName, vctxt->inode->nsName);
22590 if (ntItem->nodeQNameID == -1) {
22591 xmlFree(ntItem);
22592 xmlFree(*keySeq);
22593 *keySeq = NULL;
22594 return (-1);
22595 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022596 }
22597 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022598 * Init the node-table item: Save the node, position and
22599 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022600 */
22601 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022602 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022603 ntItem->keys = *keySeq;
22604 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022605#if 0
22606 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22607#endif
22608 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022609 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22610 /*
22611 * Free the item, since keyref items won't be
22612 * put on a global list.
22613 */
22614 xmlFree(ntItem->keys);
22615 xmlFree(ntItem);
22616 }
22617 return (-1);
22618 }
22619
22620 goto selector_leave;
22621selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022622 {
22623 xmlChar *str = NULL;
22624 /*
22625 * 4.2.1 (KEY) The ·target node set· and the
22626 * ·qualified node set· are equal, that is, every
22627 * member of the ·target node set· is also a member
22628 * of the ·qualified node set· and vice versa.
22629 */
22630 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22631 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022632 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022633 "Not all fields of %s evaluate to a node",
22634 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22635 FREE_AND_NULL(str);
22636 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022637selector_leave:
22638 /*
22639 * Free the key-sequence if not added to the IDC table.
22640 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022641 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022642 xmlFree(*keySeq);
22643 *keySeq = NULL;
22644 }
22645 } /* if selector */
22646
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022647 sto->nbHistory--;
22648
22649deregister_check:
22650 /*
22651 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022652 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022653 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022654#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022655 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22656 sto->sel->xpath);
22657#endif
22658 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022659 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022660 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022661 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022662 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022663 nextsto = sto->next;
22664 /*
22665 * Unlink from the list of active XPath state objects.
22666 */
22667 vctxt->xpathStates = sto->next;
22668 sto->next = vctxt->xpathStatePool;
22669 /*
22670 * Link it to the pool of reusable state objects.
22671 */
22672 vctxt->xpathStatePool = sto;
22673 sto = nextsto;
22674 } else
22675 sto = sto->next;
22676 } /* while (sto != NULL) */
22677 return (0);
22678}
22679
22680/**
22681 * xmlSchemaIDCRegisterMatchers:
22682 * @vctxt: the WXS validation context
22683 * @elemDecl: the element declaration
22684 *
22685 * Creates helper objects to evaluate IDC selectors/fields
22686 * successively.
22687 *
22688 * Returns 0 if OK and -1 on internal errors.
22689 */
22690static int
22691xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22692 xmlSchemaElementPtr elemDecl)
22693{
22694 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22695 xmlSchemaIDCPtr idc, refIdc;
22696 xmlSchemaIDCAugPtr aidc;
22697
22698 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22699 if (idc == NULL)
22700 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022701
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022702#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022703 {
22704 xmlChar *str = NULL;
22705 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022706 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022707 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22708 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022709 FREE_AND_NULL(str)
22710 }
22711#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022712 if (vctxt->inode->idcMatchers != NULL) {
22713 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22714 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022715 return (-1);
22716 }
22717 do {
22718 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22719 /*
22720 * Since IDCs bubbles are expensive we need to know the
22721 * depth at which the bubbles should stop; this will be
22722 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022723 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022724 * be -1, indicating that no bubbles are needed.
22725 */
22726 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22727 if (refIdc != NULL) {
22728 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022729 * Remember that we have keyrefs on this node.
22730 */
22731 vctxt->inode->hasKeyrefs = 1;
22732 /*
22733 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022734 */
22735 aidc = vctxt->aidcs;
22736 while (aidc != NULL) {
22737 if (aidc->def == refIdc)
22738 break;
22739 aidc = aidc->next;
22740 }
22741 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022742 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022743 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022744 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022745 return (-1);
22746 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022747 if ((aidc->keyrefDepth == -1) ||
22748 (vctxt->depth < aidc->keyrefDepth))
22749 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022750 }
22751 }
22752 /*
22753 * Lookup the augmented IDC item for the IDC definition.
22754 */
22755 aidc = vctxt->aidcs;
22756 while (aidc != NULL) {
22757 if (aidc->def == idc)
22758 break;
22759 aidc = aidc->next;
22760 }
22761 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022762 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22763 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022764 return (-1);
22765 }
22766 /*
22767 * Create an IDC matcher for every IDC definition.
22768 */
22769 matcher = (xmlSchemaIDCMatcherPtr)
22770 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22771 if (matcher == NULL) {
22772 xmlSchemaVErrMemory(vctxt,
22773 "allocating an IDC matcher", NULL);
22774 return (-1);
22775 }
22776 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22777 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022778 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022779 else
22780 last->next = matcher;
22781 last = matcher;
22782
22783 matcher->type = IDC_MATCHER;
22784 matcher->depth = vctxt->depth;
22785 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022786 matcher->idcType = aidc->def->type;
22787#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022788 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22789#endif
22790 /*
22791 * Init the automaton state object.
22792 */
22793 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022794 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022795 return (-1);
22796
22797 idc = idc->next;
22798 } while (idc != NULL);
22799 return (0);
22800}
22801
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022802static int
22803xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22804 xmlSchemaNodeInfoPtr ielem)
22805{
22806 xmlSchemaPSVIIDCBindingPtr bind;
22807 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22808 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22809 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22810
22811 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22812 /* vctxt->createIDCNodeTables */
22813 while (matcher != NULL) {
22814 /*
22815 * Skip keyref IDCs and empty IDC target-lists.
22816 */
22817 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22818 WXS_ILIST_IS_EMPTY(matcher->targets))
22819 {
22820 matcher = matcher->next;
22821 continue;
22822 }
22823 /*
22824 * If we _want_ the IDC node-table to be created in any case
22825 * then do so. Otherwise create them only if keyrefs need them.
22826 */
22827 if ((! vctxt->createIDCNodeTables) &&
22828 ((matcher->aidc->keyrefDepth == -1) ||
22829 (matcher->aidc->keyrefDepth > vctxt->depth)))
22830 {
22831 matcher = matcher->next;
22832 continue;
22833 }
22834 /*
22835 * Get/create the IDC binding on this element for the IDC definition.
22836 */
22837 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22838
22839 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22840 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22841 nbDupls = bind->dupls->nbItems;
22842 } else {
22843 dupls = NULL;
22844 nbDupls = 0;
22845 }
22846 if (bind->nodeTable != NULL) {
22847 nbNodeTable = bind->nbNodes;
22848 } else {
22849 nbNodeTable = 0;
22850 }
22851
22852 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22853 /*
22854 * Transfer all IDC target-nodes to the IDC node-table.
22855 */
22856 bind->nodeTable =
22857 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22858 bind->sizeNodes = matcher->targets->sizeItems;
22859 bind->nbNodes = matcher->targets->nbItems;
22860
22861 matcher->targets->items = NULL;
22862 matcher->targets->sizeItems = 0;
22863 matcher->targets->nbItems = 0;
22864 } else {
22865 /*
22866 * Compare the key-sequences and add to the IDC node-table.
22867 */
22868 nbTargets = matcher->targets->nbItems;
22869 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22870 nbFields = matcher->aidc->def->nbFields;
22871 i = 0;
22872 do {
22873 keys = targets[i]->keys;
22874 if (nbDupls) {
22875 /*
22876 * Search in already found duplicates first.
22877 */
22878 j = 0;
22879 do {
22880 if (nbFields == 1) {
22881 res = xmlSchemaAreValuesEqual(keys[0]->val,
22882 dupls[j]->keys[0]->val);
22883 if (res == -1)
22884 goto internal_error;
22885 if (res == 1) {
22886 /*
22887 * Equal key-sequence.
22888 */
22889 goto next_target;
22890 }
22891 } else {
22892 res = 0;
22893 ntkeys = dupls[j]->keys;
22894 for (k = 0; k < nbFields; k++) {
22895 res = xmlSchemaAreValuesEqual(keys[k]->val,
22896 ntkeys[k]->val);
22897 if (res == -1)
22898 goto internal_error;
22899 if (res == 0) {
22900 /*
22901 * One of the keys differs.
22902 */
22903 break;
22904 }
22905 }
22906 if (res == 1) {
22907 /*
22908 * Equal key-sequence found.
22909 */
22910 goto next_target;
22911 }
22912 }
22913 j++;
22914 } while (j < nbDupls);
22915 }
22916 if (nbNodeTable) {
22917 j = 0;
22918 do {
22919 if (nbFields == 1) {
22920 res = xmlSchemaAreValuesEqual(keys[0]->val,
22921 bind->nodeTable[j]->keys[0]->val);
22922 if (res == -1)
22923 goto internal_error;
22924 if (res == 0) {
22925 /*
22926 * The key-sequence differs.
22927 */
22928 goto next_node_table_entry;
22929 }
22930 } else {
22931 res = 0;
22932 ntkeys = bind->nodeTable[j]->keys;
22933 for (k = 0; k < nbFields; k++) {
22934 res = xmlSchemaAreValuesEqual(keys[k]->val,
22935 ntkeys[k]->val);
22936 if (res == -1)
22937 goto internal_error;
22938 if (res == 0) {
22939 /*
22940 * One of the keys differs.
22941 */
22942 goto next_node_table_entry;
22943 }
22944 }
22945 }
22946 /*
22947 * Add the duplicate to the list of duplicates.
22948 */
22949 if (bind->dupls == NULL) {
22950 bind->dupls = xmlSchemaItemListCreate();
22951 if (bind->dupls == NULL)
22952 goto internal_error;
22953 }
22954 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
22955 goto internal_error;
22956 /*
22957 * Remove the duplicate entry from the IDC node-table.
22958 */
22959 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
22960 bind->nbNodes--;
22961
22962 goto next_target;
22963
22964next_node_table_entry:
22965 j++;
22966 } while (j < nbNodeTable);
22967 }
22968 /*
22969 * If everything is fine, then add the IDC target-node to
22970 * the IDC node-table.
22971 */
22972 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
22973 goto internal_error;
22974
22975next_target:
22976 i++;
22977 } while (i < nbTargets);
22978 }
22979 matcher = matcher->next;
22980 }
22981 return(0);
22982
22983internal_error:
22984 return(-1);
22985}
22986
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022987/**
22988 * xmlSchemaBubbleIDCNodeTables:
22989 * @depth: the current tree depth
22990 *
22991 * Merges IDC bindings of an element at @depth into the corresponding IDC
22992 * bindings of its parent element. If a duplicate note-table entry is found,
22993 * both, the parent node-table entry and child entry are discarded from the
22994 * node-table of the parent.
22995 *
22996 * Returns 0 if OK and -1 on internal errors.
22997 */
22998static int
22999xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23000{
23001 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023002 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23003 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023004 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023005 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023006
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023007 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023008 if (bind == NULL) {
23009 /* Fine, no table, no bubbles. */
23010 return (0);
23011 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023012
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023013 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23014 /*
23015 * Walk all bindings; create new or add to existing bindings.
23016 * Remove duplicate key-sequences.
23017 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023018 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023019
23020 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23021 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023022 /*
23023 * Check if the key/unique IDC table needs to be bubbled.
23024 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023025 if (! vctxt->createIDCNodeTables) {
23026 aidc = vctxt->aidcs;
23027 do {
23028 if (aidc->def == bind->definition) {
23029 if ((aidc->keyrefDepth == -1) ||
23030 (aidc->keyrefDepth >= vctxt->depth)) {
23031 goto next_binding;
23032 }
23033 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023034 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023035 aidc = aidc->next;
23036 } while (aidc != NULL);
23037 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023038
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023039 if (parTable != NULL)
23040 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023041 /*
23042 * Search a matching parent binding for the
23043 * IDC definition.
23044 */
23045 while (parBind != NULL) {
23046 if (parBind->definition == bind->definition)
23047 break;
23048 parBind = parBind->next;
23049 }
23050
23051 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023052 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023053 * Compare every node-table entry of the child node,
23054 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023055 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023056 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023057
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023058 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23059 oldDupls = parBind->dupls->nbItems;
23060 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23061 } else {
23062 dupls = NULL;
23063 oldDupls = 0;
23064 }
23065
23066 parNodes = parBind->nodeTable;
23067 nbFields = bind->definition->nbFields;
23068
23069 for (i = 0; i < bind->nbNodes; i++) {
23070 node = bind->nodeTable[i];
23071 if (node == NULL)
23072 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023073 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023074 * ...with every key-sequence of the parent node, already
23075 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023076 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023077 if (oldDupls) {
23078 j = 0;
23079 while (j < oldDupls) {
23080 if (nbFields == 1) {
23081 ret = xmlSchemaAreValuesEqual(
23082 node->keys[0]->val,
23083 dupls[j]->keys[0]->val);
23084 if (ret == -1)
23085 goto internal_error;
23086 if (ret == 0) {
23087 j++;
23088 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023089 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023090 } else {
23091 parNode = dupls[j];
23092 for (k = 0; k < nbFields; k++) {
23093 ret = xmlSchemaAreValuesEqual(
23094 node->keys[k]->val,
23095 parNode->keys[k]->val);
23096 if (ret == -1)
23097 goto internal_error;
23098 if (ret == 0)
23099 break;
23100 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023101 }
23102 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023103 /* Duplicate found. */
23104 break;
23105 j++;
23106 }
23107 if (j != oldDupls) {
23108 /* Duplicate found. Skip this entry. */
23109 continue;
23110 }
23111 }
23112 /*
23113 * ... and with every key-sequence of the parent node.
23114 */
23115 if (oldNum) {
23116 j = 0;
23117 while (j < oldNum) {
23118 if (nbFields == 1) {
23119 ret = xmlSchemaAreValuesEqual(
23120 node->keys[0]->val,
23121 parNodes[j]->keys[0]->val);
23122 if (ret == -1)
23123 goto internal_error;
23124 if (ret == 0) {
23125 j++;
23126 continue;
23127 }
23128 } else {
23129 parNode = parNodes[j];
23130 for (k = 0; k < nbFields; k++) {
23131 ret = xmlSchemaAreValuesEqual(
23132 node->keys[k]->val,
23133 parNode->keys[k]->val);
23134 if (ret == -1)
23135 goto internal_error;
23136 if (ret == 0)
23137 break;
23138 }
23139 }
23140 if (ret == 1)
23141 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023142 break;
23143 j++;
23144 }
23145 if (j != oldNum) {
23146 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023147 * Handle duplicates. Move the duplicate in
23148 * the parent's node-table to the list of
23149 * duplicates.
23150 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023151 oldNum--;
23152 parBind->nbNodes--;
23153 /*
23154 * Move last old item to pos of duplicate.
23155 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023156 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023157
23158 if (parBind->nbNodes != oldNum) {
23159 /*
23160 * If new items exist, move last new item to
23161 * last of old items.
23162 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023163 parNodes[oldNum] =
23164 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023165 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023166 if (parBind->dupls == NULL) {
23167 parBind->dupls = xmlSchemaItemListCreate();
23168 if (parBind->dupls == NULL)
23169 goto internal_error;
23170 }
23171 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023172 } else {
23173 /*
23174 * Add the node-table entry (node and key-sequence) of
23175 * the child node to the node table of the parent node.
23176 */
23177 if (parBind->nodeTable == NULL) {
23178 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023179 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023180 if (parBind->nodeTable == NULL) {
23181 xmlSchemaVErrMemory(NULL,
23182 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023183 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023184 }
23185 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023186 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023187 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023188 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23189 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23190 sizeof(xmlSchemaPSVIIDCNodePtr));
23191 if (parBind->nodeTable == NULL) {
23192 xmlSchemaVErrMemory(NULL,
23193 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023194 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023195 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023196 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023197 /*
23198 * Append the new node-table entry to the 'new node-table
23199 * entries' section.
23200 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023201 parBind->nodeTable[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023202 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023203
23204 }
23205
23206 }
23207 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023208 /*
23209 * No binding for the IDC was found: create a new one and
23210 * copy all node-tables.
23211 */
23212 parBind = xmlSchemaIDCNewBinding(bind->definition);
23213 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023214 goto internal_error;
23215
23216 /*
23217 * TODO: Hmm, how to optimize the initial number of
23218 * allocated entries?
23219 */
23220 if (bind->nbNodes != 0) {
23221 /*
23222 * Add all IDC node-table entries.
23223 */
23224 if (! vctxt->psviExposeIDCNodeTables) {
23225 /*
23226 * Just move the entries.
23227 * NOTE: this is quite save here, since
23228 * all the keyref lookups have already been
23229 * performed.
23230 */
23231 parBind->nodeTable = bind->nodeTable;
23232 bind->nodeTable = NULL;
23233 parBind->sizeNodes = bind->sizeNodes;
23234 bind->sizeNodes = 0;
23235 parBind->nbNodes = bind->nbNodes;
23236 bind->nbNodes = 0;
23237 } else {
23238 /*
23239 * Copy the entries.
23240 */
23241 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23242 xmlMalloc(bind->nbNodes *
23243 sizeof(xmlSchemaPSVIIDCNodePtr));
23244 if (parBind->nodeTable == NULL) {
23245 xmlSchemaVErrMemory(NULL,
23246 "allocating an array of IDC node-table "
23247 "items", NULL);
23248 xmlSchemaIDCFreeBinding(parBind);
23249 goto internal_error;
23250 }
23251 parBind->sizeNodes = bind->nbNodes;
23252 parBind->nbNodes = bind->nbNodes;
23253 memcpy(parBind->nodeTable, bind->nodeTable,
23254 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23255 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023256 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023257 if (bind->dupls) {
23258 /*
23259 * Move the duplicates.
23260 */
23261 if (parBind->dupls != NULL)
23262 xmlSchemaItemListFree(parBind->dupls);
23263 parBind->dupls = bind->dupls;
23264 bind->dupls = NULL;
23265 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023266 if (*parTable == NULL)
23267 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023268 else {
23269 parBind->next = *parTable;
23270 *parTable = parBind;
23271 }
23272 }
23273
23274next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023275 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023276 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023277 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023278
23279internal_error:
23280 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023281}
23282
23283/**
23284 * xmlSchemaCheckCVCIDCKeyRef:
23285 * @vctxt: the WXS validation context
23286 * @elemDecl: the element declaration
23287 *
23288 * Check the cvc-idc-keyref constraints.
23289 */
23290static int
23291xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23292{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023293 xmlSchemaIDCMatcherPtr matcher;
23294 xmlSchemaPSVIIDCBindingPtr bind;
23295
23296 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023297 /*
23298 * Find a keyref.
23299 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023300 while (matcher != NULL) {
23301 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23302 matcher->targets &&
23303 matcher->targets->nbItems)
23304 {
23305 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023306 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023307 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023308
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023309 nbFields = matcher->aidc->def->nbFields;
23310
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023311 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023312 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023313 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023314 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023315 while (bind != NULL) {
23316 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023317 bind->definition)
23318 break;
23319 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023320 }
23321 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023322 /*
23323 * Search for a matching key-sequences.
23324 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023325 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023326 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023327 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023328 if (bind != NULL) {
23329 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023330 for (j = 0; j < bind->nbNodes; j++) {
23331 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023332 for (k = 0; k < nbFields; k++) {
23333 res = xmlSchemaAreValuesEqual(keys[k]->val,
23334 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023335 if (res == 0)
23336 break;
23337 else if (res == -1) {
23338 return (-1);
23339 }
23340 }
23341 if (res == 1) {
23342 /*
23343 * Match found.
23344 */
23345 break;
23346 }
23347 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023348 if ((res == 0) && hasDupls) {
23349 /*
23350 * Search in duplicates
23351 */
23352 for (j = 0; j < bind->dupls->nbItems; j++) {
23353 keys = ((xmlSchemaPSVIIDCNodePtr)
23354 bind->dupls->items[j])->keys;
23355 for (k = 0; k < nbFields; k++) {
23356 res = xmlSchemaAreValuesEqual(keys[k]->val,
23357 refKeys[k]->val);
23358 if (res == 0)
23359 break;
23360 else if (res == -1) {
23361 return (-1);
23362 }
23363 }
23364 if (res == 1) {
23365 /*
23366 * Match in duplicates found.
23367 */
23368 xmlChar *str = NULL, *strB = NULL;
23369 xmlSchemaKeyrefErr(vctxt,
23370 XML_SCHEMAV_CVC_IDC, refNode,
23371 (xmlSchemaTypePtr) matcher->aidc->def,
23372 "More than one match found for "
23373 "key-sequence %s of keyref '%s'",
23374 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23375 refNode->keys, nbFields),
23376 xmlSchemaGetComponentQName(&strB,
23377 matcher->aidc->def));
23378 FREE_AND_NULL(str);
23379 FREE_AND_NULL(strB);
23380 break;
23381 }
23382 }
23383 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023384 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023385
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023386 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023387 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023388 xmlSchemaKeyrefErr(vctxt,
23389 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023390 (xmlSchemaTypePtr) matcher->aidc->def,
23391 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023392 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023393 refNode->keys, nbFields),
23394 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023395 FREE_AND_NULL(str);
23396 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023397 }
23398 }
23399 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023400 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023401 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023402 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023403 return (0);
23404}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023405
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023406/************************************************************************
23407 * *
23408 * XML Reader validation code *
23409 * *
23410 ************************************************************************/
23411
23412static xmlSchemaAttrInfoPtr
23413xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023414{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023415 xmlSchemaAttrInfoPtr iattr;
23416 /*
23417 * Grow/create list of attribute infos.
23418 */
23419 if (vctxt->attrInfos == NULL) {
23420 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23421 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23422 vctxt->sizeAttrInfos = 1;
23423 if (vctxt->attrInfos == NULL) {
23424 xmlSchemaVErrMemory(vctxt,
23425 "allocating attribute info list", NULL);
23426 return (NULL);
23427 }
23428 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23429 vctxt->sizeAttrInfos++;
23430 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23431 xmlRealloc(vctxt->attrInfos,
23432 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23433 if (vctxt->attrInfos == NULL) {
23434 xmlSchemaVErrMemory(vctxt,
23435 "re-allocating attribute info list", NULL);
23436 return (NULL);
23437 }
23438 } else {
23439 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23440 if (iattr->localName != NULL) {
23441 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23442 "attr info not cleared");
23443 return (NULL);
23444 }
23445 iattr->nodeType = XML_ATTRIBUTE_NODE;
23446 return (iattr);
23447 }
23448 /*
23449 * Create an attribute info.
23450 */
23451 iattr = (xmlSchemaAttrInfoPtr)
23452 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23453 if (iattr == NULL) {
23454 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23455 return (NULL);
23456 }
23457 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23458 iattr->nodeType = XML_ATTRIBUTE_NODE;
23459 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23460
23461 return (iattr);
23462}
23463
23464static int
23465xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23466 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023467 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023468 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023469 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023470 int ownedNames,
23471 xmlChar *value,
23472 int ownedValue)
23473{
23474 xmlSchemaAttrInfoPtr attr;
23475
23476 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23477 if (attr == NULL) {
23478 VERROR_INT("xmlSchemaPushAttribute",
23479 "calling xmlSchemaGetFreshAttrInfo()");
23480 return (-1);
23481 }
23482 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023483 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023484 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23485 attr->localName = localName;
23486 attr->nsName = nsName;
23487 if (ownedNames)
23488 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23489 /*
23490 * Evaluate if it's an XSI attribute.
23491 */
23492 if (nsName != NULL) {
23493 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23494 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23495 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23496 }
23497 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23498 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23499 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23500 }
23501 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23502 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23503 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23504 }
23505 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23506 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23507 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23508 }
23509 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23510 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23511 }
23512 }
23513 attr->value = value;
23514 if (ownedValue)
23515 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23516 if (attr->metaType != 0)
23517 attr->state = XML_SCHEMAS_ATTR_META;
23518 return (0);
23519}
23520
23521static void
23522xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23523{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023524 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023525 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023526 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23527 FREE_AND_NULL(ielem->localName);
23528 FREE_AND_NULL(ielem->nsName);
23529 } else {
23530 ielem->localName = NULL;
23531 ielem->nsName = NULL;
23532 }
23533 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23534 FREE_AND_NULL(ielem->value);
23535 } else {
23536 ielem->value = NULL;
23537 }
23538 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023539 /*
23540 * PSVI TODO: Be careful not to free it when the value is
23541 * exposed via PSVI.
23542 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023543 xmlSchemaFreeValue(ielem->val);
23544 ielem->val = NULL;
23545 }
23546 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023547 /*
23548 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23549 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023550 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23551 ielem->idcMatchers = NULL;
23552 }
23553 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023554 /*
23555 * OPTIMIZE TODO: Use a pool of IDC tables??.
23556 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023557 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23558 ielem->idcTable = NULL;
23559 }
23560 if (ielem->regexCtxt != NULL) {
23561 xmlRegFreeExecCtxt(ielem->regexCtxt);
23562 ielem->regexCtxt = NULL;
23563 }
23564 if (ielem->nsBindings != NULL) {
23565 xmlFree((xmlChar **)ielem->nsBindings);
23566 ielem->nsBindings = NULL;
23567 ielem->nbNsBindings = 0;
23568 ielem->sizeNsBindings = 0;
23569 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023570}
23571
23572/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023573 * xmlSchemaGetFreshElemInfo:
23574 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023575 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023576 * Creates/reuses and initializes the element info item for
23577 * the currect tree depth.
23578 *
23579 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023580 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023581static xmlSchemaNodeInfoPtr
23582xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023583{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023584 xmlSchemaNodeInfoPtr info = NULL;
23585
23586 if (vctxt->depth > vctxt->sizeElemInfos) {
23587 VERROR_INT("xmlSchemaGetFreshElemInfo",
23588 "inconsistent depth encountered");
23589 return (NULL);
23590 }
23591 if (vctxt->elemInfos == NULL) {
23592 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23593 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23594 if (vctxt->elemInfos == NULL) {
23595 xmlSchemaVErrMemory(vctxt,
23596 "allocating the element info array", NULL);
23597 return (NULL);
23598 }
23599 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23600 vctxt->sizeElemInfos = 10;
23601 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23602 int i = vctxt->sizeElemInfos;
23603
23604 vctxt->sizeElemInfos *= 2;
23605 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23606 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23607 sizeof(xmlSchemaNodeInfoPtr));
23608 if (vctxt->elemInfos == NULL) {
23609 xmlSchemaVErrMemory(vctxt,
23610 "re-allocating the element info array", NULL);
23611 return (NULL);
23612 }
23613 /*
23614 * We need the new memory to be NULLed.
23615 * TODO: Use memset instead?
23616 */
23617 for (; i < vctxt->sizeElemInfos; i++)
23618 vctxt->elemInfos[i] = NULL;
23619 } else
23620 info = vctxt->elemInfos[vctxt->depth];
23621
23622 if (info == NULL) {
23623 info = (xmlSchemaNodeInfoPtr)
23624 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23625 if (info == NULL) {
23626 xmlSchemaVErrMemory(vctxt,
23627 "allocating an element info", NULL);
23628 return (NULL);
23629 }
23630 vctxt->elemInfos[vctxt->depth] = info;
23631 } else {
23632 if (info->localName != NULL) {
23633 VERROR_INT("xmlSchemaGetFreshElemInfo",
23634 "elem info has not been cleared");
23635 return (NULL);
23636 }
23637 }
23638 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23639 info->nodeType = XML_ELEMENT_NODE;
23640 info->depth = vctxt->depth;
23641
23642 return (info);
23643}
23644
23645#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23646#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23647#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23648
23649static int
23650xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23651 xmlNodePtr node,
23652 xmlSchemaTypePtr type,
23653 xmlSchemaValType valType,
23654 const xmlChar * value,
23655 xmlSchemaValPtr val,
23656 unsigned long length,
23657 int fireErrors)
23658{
23659 int ret, error = 0;
23660
23661 xmlSchemaTypePtr tmpType;
23662 xmlSchemaFacetLinkPtr facetLink;
23663 xmlSchemaFacetPtr facet;
23664 unsigned long len = 0;
23665 xmlSchemaWhitespaceValueType ws;
23666
23667 /*
23668 * In Libxml2, derived built-in types have currently no explicit facets.
23669 */
23670 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023671 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023672
23673 /*
23674 * NOTE: Do not jump away, if the facetSet of the given type is
23675 * empty: until now, "pattern" and "enumeration" facets of the
23676 * *base types* need to be checked as well.
23677 */
23678 if (type->facetSet == NULL)
23679 goto pattern_and_enum;
23680
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023681 if (! WXS_IS_ATOMIC(type)) {
23682 if (WXS_IS_LIST(type))
23683 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023684 else
23685 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023686 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023687 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023688 * Whitespace handling is only of importance for string-based
23689 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023690 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023691 tmpType = xmlSchemaGetPrimitiveType(type);
23692 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023693 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023694 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23695 } else
23696 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23697 /*
23698 * If the value was not computed (for string or
23699 * anySimpleType based types), then use the provided
23700 * type.
23701 */
23702 if (val == NULL)
23703 valType = valType;
23704 else
23705 valType = xmlSchemaGetValType(val);
23706
23707 ret = 0;
23708 for (facetLink = type->facetSet; facetLink != NULL;
23709 facetLink = facetLink->next) {
23710 /*
23711 * Skip the pattern "whiteSpace": it is used to
23712 * format the character content beforehand.
23713 */
23714 switch (facetLink->facet->type) {
23715 case XML_SCHEMA_FACET_WHITESPACE:
23716 case XML_SCHEMA_FACET_PATTERN:
23717 case XML_SCHEMA_FACET_ENUMERATION:
23718 continue;
23719 case XML_SCHEMA_FACET_LENGTH:
23720 case XML_SCHEMA_FACET_MINLENGTH:
23721 case XML_SCHEMA_FACET_MAXLENGTH:
23722 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23723 valType, value, val, &len, ws);
23724 break;
23725 default:
23726 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23727 valType, value, val, ws);
23728 break;
23729 }
23730 if (ret < 0) {
23731 AERROR_INT("xmlSchemaValidateFacets",
23732 "validating against a atomic type facet");
23733 return (-1);
23734 } else if (ret > 0) {
23735 if (fireErrors)
23736 xmlSchemaFacetErr(actxt, ret, node,
23737 value, len, type, facetLink->facet, NULL, NULL, NULL);
23738 else
23739 return (ret);
23740 if (error == 0)
23741 error = ret;
23742 }
23743 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023744 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023745
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023746WXS_IS_LIST:
23747 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023748 goto pattern_and_enum;
23749 /*
23750 * "length", "minLength" and "maxLength" of list types.
23751 */
23752 ret = 0;
23753 for (facetLink = type->facetSet; facetLink != NULL;
23754 facetLink = facetLink->next) {
23755
23756 switch (facetLink->facet->type) {
23757 case XML_SCHEMA_FACET_LENGTH:
23758 case XML_SCHEMA_FACET_MINLENGTH:
23759 case XML_SCHEMA_FACET_MAXLENGTH:
23760 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23761 value, length, NULL);
23762 break;
23763 default:
23764 continue;
23765 }
23766 if (ret < 0) {
23767 AERROR_INT("xmlSchemaValidateFacets",
23768 "validating against a list type facet");
23769 return (-1);
23770 } else if (ret > 0) {
23771 if (fireErrors)
23772 xmlSchemaFacetErr(actxt, ret, node,
23773 value, length, type, facetLink->facet, NULL, NULL, NULL);
23774 else
23775 return (ret);
23776 if (error == 0)
23777 error = ret;
23778 }
23779 ret = 0;
23780 }
23781
23782pattern_and_enum:
23783 if (error >= 0) {
23784 int found = 0;
23785 /*
23786 * Process enumerations. Facet values are in the value space
23787 * of the defining type's base type. This seems to be a bug in the
23788 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23789 * Only the first set of enumerations in the ancestor-or-self axis
23790 * is used for validation.
23791 */
23792 ret = 0;
23793 tmpType = type;
23794 do {
23795 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23796 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23797 continue;
23798 found = 1;
23799 ret = xmlSchemaAreValuesEqual(facet->val, val);
23800 if (ret == 1)
23801 break;
23802 else if (ret < 0) {
23803 AERROR_INT("xmlSchemaValidateFacets",
23804 "validating against an enumeration facet");
23805 return (-1);
23806 }
23807 }
23808 if (ret != 0)
23809 break;
23810 tmpType = tmpType->baseType;
23811 } while ((tmpType != NULL) &&
23812 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23813 if (found && (ret == 0)) {
23814 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23815 if (fireErrors) {
23816 xmlSchemaFacetErr(actxt, ret, node,
23817 value, 0, type, NULL, NULL, NULL, NULL);
23818 } else
23819 return (ret);
23820 if (error == 0)
23821 error = ret;
23822 }
23823 }
23824
23825 if (error >= 0) {
23826 int found;
23827 /*
23828 * Process patters. Pattern facets are ORed at type level
23829 * and ANDed if derived. Walk the base type axis.
23830 */
23831 tmpType = type;
23832 facet = NULL;
23833 do {
23834 found = 0;
23835 for (facetLink = tmpType->facetSet; facetLink != NULL;
23836 facetLink = facetLink->next) {
23837 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23838 continue;
23839 found = 1;
23840 /*
23841 * NOTE that for patterns, @value needs to be the
23842 * normalized vaule.
23843 */
23844 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23845 if (ret == 1)
23846 break;
23847 else if (ret < 0) {
23848 AERROR_INT("xmlSchemaValidateFacets",
23849 "validating against a pattern facet");
23850 return (-1);
23851 } else {
23852 /*
23853 * Save the last non-validating facet.
23854 */
23855 facet = facetLink->facet;
23856 }
23857 }
23858 if (found && (ret != 1)) {
23859 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23860 if (fireErrors) {
23861 xmlSchemaFacetErr(actxt, ret, node,
23862 value, 0, type, facet, NULL, NULL, NULL);
23863 } else
23864 return (ret);
23865 if (error == 0)
23866 error = ret;
23867 break;
23868 }
23869 tmpType = tmpType->baseType;
23870 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23871 }
23872
23873 return (error);
23874}
23875
23876static xmlChar *
23877xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23878 const xmlChar *value)
23879{
23880 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23881 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23882 return (xmlSchemaCollapseString(value));
23883 case XML_SCHEMA_WHITESPACE_REPLACE:
23884 return (xmlSchemaWhiteSpaceReplace(value));
23885 default:
23886 return (NULL);
23887 }
23888}
23889
23890static int
23891xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23892 const xmlChar *value,
23893 xmlSchemaValPtr *val,
23894 int valNeeded)
23895{
23896 int ret;
23897 const xmlChar *nsName;
23898 xmlChar *local, *prefix = NULL;
23899
23900 ret = xmlValidateQName(value, 1);
23901 if (ret != 0) {
23902 if (ret == -1) {
23903 VERROR_INT("xmlSchemaValidateQName",
23904 "calling xmlValidateQName()");
23905 return (-1);
23906 }
23907 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23908 }
23909 /*
23910 * NOTE: xmlSplitQName2 will always return a duplicated
23911 * strings.
23912 */
23913 local = xmlSplitQName2(value, &prefix);
23914 if (local == NULL)
23915 local = xmlStrdup(value);
23916 /*
23917 * OPTIMIZE TODO: Use flags for:
23918 * - is there any namespace binding?
23919 * - is there a default namespace?
23920 */
23921 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23922
23923 if (prefix != NULL) {
23924 xmlFree(prefix);
23925 /*
23926 * A namespace must be found if the prefix is
23927 * NOT NULL.
23928 */
23929 if (nsName == NULL) {
23930 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023931 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023932 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023933 "The QName value '%s' has no "
23934 "corresponding namespace declaration in "
23935 "scope", value, NULL);
23936 if (local != NULL)
23937 xmlFree(local);
23938 return (ret);
23939 }
23940 }
23941 if (valNeeded && val) {
23942 if (nsName != NULL)
23943 *val = xmlSchemaNewQNameValue(
23944 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
23945 else
23946 *val = xmlSchemaNewQNameValue(NULL,
23947 BAD_CAST local);
23948 } else
23949 xmlFree(local);
23950 return (0);
23951}
23952
23953/*
23954* cvc-simple-type
23955*/
23956static int
23957xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
23958 xmlNodePtr node,
23959 xmlSchemaTypePtr type,
23960 const xmlChar *value,
23961 xmlSchemaValPtr *retVal,
23962 int fireErrors,
23963 int normalize,
23964 int isNormalized)
23965{
23966 int ret = 0, valNeeded = (retVal) ? 1 : 0;
23967 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023968 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023969 xmlChar *normValue = NULL;
23970
23971#define NORMALIZE(atype) \
23972 if ((! isNormalized) && \
23973 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
23974 normValue = xmlSchemaNormalizeValue(atype, value); \
23975 if (normValue != NULL) \
23976 value = normValue; \
23977 isNormalized = 1; \
23978 }
23979
23980 if ((retVal != NULL) && (*retVal != NULL)) {
23981 xmlSchemaFreeValue(*retVal);
23982 *retVal = NULL;
23983 }
23984 /*
23985 * 3.14.4 Simple Type Definition Validation Rules
23986 * Validation Rule: String Valid
23987 */
23988 /*
23989 * 1 It is schema-valid with respect to that definition as defined
23990 * by Datatype Valid in [XML Schemas: Datatypes].
23991 */
23992 /*
23993 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
23994 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
23995 * the string must be a ·declared entity name·.
23996 */
23997 /*
23998 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
23999 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24000 * then every whitespace-delimited substring of the string must be a ·declared
24001 * entity name·.
24002 */
24003 /*
24004 * 2.3 otherwise no further condition applies.
24005 */
24006 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24007 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024008 if (value == NULL)
24009 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024010 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024011 xmlSchemaTypePtr biType; /* The built-in type. */
24012 /*
24013 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24014 * a literal in the ·lexical space· of {base type definition}"
24015 */
24016 /*
24017 * Whitespace-normalize.
24018 */
24019 NORMALIZE(type);
24020 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24021 /*
24022 * Get the built-in type.
24023 */
24024 biType = type->baseType;
24025 while ((biType != NULL) &&
24026 (biType->type != XML_SCHEMA_TYPE_BASIC))
24027 biType = biType->baseType;
24028
24029 if (biType == NULL) {
24030 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24031 "could not get the built-in type");
24032 goto internal_error;
24033 }
24034 } else
24035 biType = type;
24036 /*
24037 * NOTATIONs need to be processed here, since they need
24038 * to lookup in the hashtable of NOTATION declarations of the schema.
24039 */
24040 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24041 switch (biType->builtInType) {
24042 case XML_SCHEMAS_NOTATION:
24043 ret = xmlSchemaValidateNotation(
24044 (xmlSchemaValidCtxtPtr) actxt,
24045 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24046 NULL, value, &val, valNeeded);
24047 break;
24048 case XML_SCHEMAS_QNAME:
24049 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24050 value, &val, valNeeded);
24051 break;
24052 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024053 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024054 if (valNeeded)
24055 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24056 value, &val, NULL);
24057 else
24058 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24059 value, NULL, NULL);
24060 break;
24061 }
24062 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24063 switch (biType->builtInType) {
24064 case XML_SCHEMAS_NOTATION:
24065 ret = xmlSchemaValidateNotation(NULL,
24066 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24067 value, &val, valNeeded);
24068 break;
24069 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024070 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024071 if (valNeeded)
24072 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24073 value, &val, node);
24074 else
24075 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24076 value, NULL, node);
24077 break;
24078 }
24079 } else {
24080 /*
24081 * Validation via a public API is not implemented yet.
24082 */
24083 TODO
24084 goto internal_error;
24085 }
24086 if (ret != 0) {
24087 if (ret < 0) {
24088 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24089 "validating against a built-in type");
24090 goto internal_error;
24091 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024092 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024093 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24094 else
24095 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24096 }
24097 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24098 /*
24099 * Check facets.
24100 */
24101 ret = xmlSchemaValidateFacets(actxt, node, type,
24102 (xmlSchemaValType) biType->builtInType, value, val,
24103 0, fireErrors);
24104 if (ret != 0) {
24105 if (ret < 0) {
24106 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24107 "validating facets of atomic simple type");
24108 goto internal_error;
24109 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024110 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024111 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24112 else
24113 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24114 }
24115 }
24116 if (fireErrors && (ret > 0))
24117 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024118 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024119
24120 xmlSchemaTypePtr itemType;
24121 const xmlChar *cur, *end;
24122 xmlChar *tmpValue = NULL;
24123 unsigned long len = 0;
24124 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24125 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24126 * of white space separated tokens, each of which ·match·es a literal
24127 * in the ·lexical space· of {item type definition}
24128 */
24129 /*
24130 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24131 * the list type has an enum or pattern facet.
24132 */
24133 NORMALIZE(type);
24134 /*
24135 * VAL TODO: Optimize validation of empty values.
24136 * VAL TODO: We do not have computed values for lists.
24137 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024138 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024139 cur = value;
24140 do {
24141 while (IS_BLANK_CH(*cur))
24142 cur++;
24143 end = cur;
24144 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24145 end++;
24146 if (end == cur)
24147 break;
24148 tmpValue = xmlStrndup(cur, end - cur);
24149 len++;
24150
24151 if (valNeeded)
24152 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24153 tmpValue, &curVal, fireErrors, 0, 1);
24154 else
24155 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24156 tmpValue, NULL, fireErrors, 0, 1);
24157 FREE_AND_NULL(tmpValue);
24158 if (curVal != NULL) {
24159 /*
24160 * Add to list of computed values.
24161 */
24162 if (val == NULL)
24163 val = curVal;
24164 else
24165 xmlSchemaValueAppend(prevVal, curVal);
24166 prevVal = curVal;
24167 curVal = NULL;
24168 }
24169 if (ret != 0) {
24170 if (ret < 0) {
24171 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24172 "validating an item of list simple type");
24173 goto internal_error;
24174 }
24175 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24176 break;
24177 }
24178 cur = end;
24179 } while (*cur != 0);
24180 FREE_AND_NULL(tmpValue);
24181 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24182 /*
24183 * Apply facets (pattern, enumeration).
24184 */
24185 ret = xmlSchemaValidateFacets(actxt, node, type,
24186 XML_SCHEMAS_UNKNOWN, value, val,
24187 len, fireErrors);
24188 if (ret != 0) {
24189 if (ret < 0) {
24190 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24191 "validating facets of list simple type");
24192 goto internal_error;
24193 }
24194 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24195 }
24196 }
24197 if (fireErrors && (ret > 0)) {
24198 /*
24199 * Report the normalized value.
24200 */
24201 normalize = 1;
24202 NORMALIZE(type);
24203 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24204 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024205 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024206 xmlSchemaTypeLinkPtr memberLink;
24207 /*
24208 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24209 * not apply directly; however, the normalization behavior of ·union·
24210 * types is controlled by the value of whiteSpace on that one of the
24211 * ·memberTypes· against which the ·union· is successfully validated.
24212 *
24213 * This means that the value is normalized by the first validating
24214 * member type, then the facets of the union type are applied. This
24215 * needs changing of the value!
24216 */
24217
24218 /*
24219 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24220 * literal in the ·lexical space· of at least one member of
24221 * {member type definitions}
24222 */
24223 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24224 if (memberLink == NULL) {
24225 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24226 "union simple type has no member types");
24227 goto internal_error;
24228 }
24229 /*
24230 * Always normalize union type values, since we currently
24231 * cannot store the whitespace information with the value
24232 * itself; otherwise a later value-comparison would be
24233 * not possible.
24234 */
24235 while (memberLink != NULL) {
24236 if (valNeeded)
24237 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24238 memberLink->type, value, &val, 0, 1, 0);
24239 else
24240 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24241 memberLink->type, value, NULL, 0, 1, 0);
24242 if (ret <= 0)
24243 break;
24244 memberLink = memberLink->next;
24245 }
24246 if (ret != 0) {
24247 if (ret < 0) {
24248 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24249 "validating members of union simple type");
24250 goto internal_error;
24251 }
24252 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24253 }
24254 /*
24255 * Apply facets (pattern, enumeration).
24256 */
24257 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24258 /*
24259 * The normalization behavior of ·union· types is controlled by
24260 * the value of whiteSpace on that one of the ·memberTypes·
24261 * against which the ·union· is successfully validated.
24262 */
24263 NORMALIZE(memberLink->type);
24264 ret = xmlSchemaValidateFacets(actxt, node, type,
24265 XML_SCHEMAS_UNKNOWN, value, val,
24266 0, fireErrors);
24267 if (ret != 0) {
24268 if (ret < 0) {
24269 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24270 "validating facets of union simple type");
24271 goto internal_error;
24272 }
24273 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24274 }
24275 }
24276 if (fireErrors && (ret > 0))
24277 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24278 }
24279
24280 if (normValue != NULL)
24281 xmlFree(normValue);
24282 if (ret == 0) {
24283 if (retVal != NULL)
24284 *retVal = val;
24285 else if (val != NULL)
24286 xmlSchemaFreeValue(val);
24287 } else if (val != NULL)
24288 xmlSchemaFreeValue(val);
24289 return (ret);
24290internal_error:
24291 if (normValue != NULL)
24292 xmlFree(normValue);
24293 if (val != NULL)
24294 xmlSchemaFreeValue(val);
24295 return (-1);
24296}
24297
24298static int
24299xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24300 const xmlChar *value,
24301 const xmlChar **nsName,
24302 const xmlChar **localName)
24303{
24304 int ret = 0;
24305
24306 if ((nsName == NULL) || (localName == NULL))
24307 return (-1);
24308 *nsName = NULL;
24309 *localName = NULL;
24310
24311 ret = xmlValidateQName(value, 1);
24312 if (ret == -1)
24313 return (-1);
24314 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024315 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024316 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24317 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24318 return (1);
24319 }
24320 {
24321 xmlChar *local = NULL;
24322 xmlChar *prefix;
24323
24324 /*
24325 * NOTE: xmlSplitQName2 will return a duplicated
24326 * string.
24327 */
24328 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024329 if (local == NULL)
24330 *localName = xmlDictLookup(vctxt->dict, value, -1);
24331 else {
24332 *localName = xmlDictLookup(vctxt->dict, local, -1);
24333 xmlFree(local);
24334 }
24335
24336 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24337
24338 if (prefix != NULL) {
24339 xmlFree(prefix);
24340 /*
24341 * A namespace must be found if the prefix is NOT NULL.
24342 */
24343 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024344 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024345 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024346 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024347 "The QName value '%s' has no "
24348 "corresponding namespace declaration in scope",
24349 value, NULL);
24350 return (2);
24351 }
24352 }
24353 }
24354 return (0);
24355}
24356
24357static int
24358xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24359 xmlSchemaAttrInfoPtr iattr,
24360 xmlSchemaTypePtr *localType,
24361 xmlSchemaElementPtr elemDecl)
24362{
24363 int ret = 0;
24364 /*
24365 * cvc-elt (3.3.4) : (4)
24366 * AND
24367 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24368 * (1.2.1.2.1) - (1.2.1.2.4)
24369 * Handle 'xsi:type'.
24370 */
24371 if (localType == NULL)
24372 return (-1);
24373 *localType = NULL;
24374 if (iattr == NULL)
24375 return (0);
24376 else {
24377 const xmlChar *nsName = NULL, *local = NULL;
24378 /*
24379 * TODO: We should report a *warning* that the type was overriden
24380 * by the instance.
24381 */
24382 ACTIVATE_ATTRIBUTE(iattr);
24383 /*
24384 * (cvc-elt) (3.3.4) : (4.1)
24385 * (cvc-assess-elt) (1.2.1.2.2)
24386 */
24387 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24388 &nsName, &local);
24389 if (ret != 0) {
24390 if (ret < 0) {
24391 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24392 "calling xmlSchemaQNameExpand() to validate the "
24393 "attribute 'xsi:type'");
24394 goto internal_error;
24395 }
24396 goto exit;
24397 }
24398 /*
24399 * (cvc-elt) (3.3.4) : (4.2)
24400 * (cvc-assess-elt) (1.2.1.2.3)
24401 */
24402 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24403 if (*localType == NULL) {
24404 xmlChar *str = NULL;
24405
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024406 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024407 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024408 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024409 "The QName value '%s' of the xsi:type attribute does not "
24410 "resolve to a type definition",
24411 xmlSchemaFormatQName(&str, nsName, local), NULL);
24412 FREE_AND_NULL(str);
24413 ret = vctxt->err;
24414 goto exit;
24415 }
24416 if (elemDecl != NULL) {
24417 int set = 0;
24418
24419 /*
24420 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24421 * "The ·local type definition· must be validly
24422 * derived from the {type definition} given the union of
24423 * the {disallowed substitutions} and the {type definition}'s
24424 * {prohibited substitutions}, as defined in
24425 * Type Derivation OK (Complex) (§3.4.6)
24426 * (if it is a complex type definition),
24427 * or given {disallowed substitutions} as defined in Type
24428 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24429 * definition)."
24430 *
24431 * {disallowed substitutions}: the "block" on the element decl.
24432 * {prohibited substitutions}: the "block" on the type def.
24433 */
24434 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24435 (elemDecl->subtypes->flags &
24436 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24437 set |= SUBSET_EXTENSION;
24438
24439 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24440 (elemDecl->subtypes->flags &
24441 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24442 set |= SUBSET_RESTRICTION;
24443
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024444 if ((vctxt->pctxt == NULL) &&
24445 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24446 return (-1);
24447 if (xmlSchemaCheckCOSDerivedOK(vctxt->pctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024448 elemDecl->subtypes, set) != 0) {
24449 xmlChar *str = NULL;
24450
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024451 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024452 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24453 "The type definition '%s', specified by xsi:type, is "
24454 "blocked or not validly derived from the type definition "
24455 "of the element declaration",
24456 xmlSchemaFormatQName(&str,
24457 (*localType)->targetNamespace,
24458 (*localType)->name),
24459 NULL);
24460 FREE_AND_NULL(str);
24461 ret = vctxt->err;
24462 *localType = NULL;
24463 }
24464 }
24465 }
24466exit:
24467 ACTIVATE_ELEM;
24468 return (ret);
24469internal_error:
24470 ACTIVATE_ELEM;
24471 return (-1);
24472}
24473
24474static int
24475xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24476{
24477 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024478 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024479
24480 /*
24481 * cvc-elt (3.3.4) : 1
24482 */
24483 if (elemDecl == NULL) {
24484 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24485 "No matching declaration available");
24486 return (vctxt->err);
24487 }
24488 /*
24489 * cvc-elt (3.3.4) : 2
24490 */
24491 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24492 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24493 "The element declaration is abstract");
24494 return (vctxt->err);
24495 }
24496 if (actualType == NULL) {
24497 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24498 "The type definition is absent");
24499 return (XML_SCHEMAV_CVC_TYPE_1);
24500 }
24501 if (vctxt->nbAttrInfos != 0) {
24502 int ret;
24503 xmlSchemaAttrInfoPtr iattr;
24504 /*
24505 * cvc-elt (3.3.4) : 3
24506 * Handle 'xsi:nil'.
24507 */
24508 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24509 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24510 if (iattr) {
24511 ACTIVATE_ATTRIBUTE(iattr);
24512 /*
24513 * Validate the value.
24514 */
24515 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024516 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024517 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24518 iattr->value, &(iattr->val), 1, 0, 0);
24519 ACTIVATE_ELEM;
24520 if (ret < 0) {
24521 VERROR_INT("xmlSchemaValidateElemDecl",
24522 "calling xmlSchemaVCheckCVCSimpleType() to "
24523 "validate the attribute 'xsi:nil'");
24524 return (-1);
24525 }
24526 if (ret == 0) {
24527 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24528 /*
24529 * cvc-elt (3.3.4) : 3.1
24530 */
24531 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24532 "The element is not 'nillable'");
24533 /* Does not return an error on purpose. */
24534 } else {
24535 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24536 /*
24537 * cvc-elt (3.3.4) : 3.2.2
24538 */
24539 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24540 (elemDecl->value != NULL)) {
24541 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24542 "The element cannot be 'nilled' because "
24543 "there is a fixed value constraint defined "
24544 "for it");
24545 /* Does not return an error on purpose. */
24546 } else
24547 vctxt->inode->flags |=
24548 XML_SCHEMA_ELEM_INFO_NILLED;
24549 }
24550 }
24551 }
24552 }
24553 /*
24554 * cvc-elt (3.3.4) : 4
24555 * Handle 'xsi:type'.
24556 */
24557 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24558 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24559 if (iattr) {
24560 xmlSchemaTypePtr localType = NULL;
24561
24562 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24563 elemDecl);
24564 if (ret != 0) {
24565 if (ret == -1) {
24566 VERROR_INT("xmlSchemaValidateElemDecl",
24567 "calling xmlSchemaProcessXSIType() to "
24568 "process the attribute 'xsi:type'");
24569 return (-1);
24570 }
24571 /* Does not return an error on purpose. */
24572 }
24573 if (localType != NULL) {
24574 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24575 actualType = localType;
24576 }
24577 }
24578 }
24579 /*
24580 * IDC: Register identity-constraint XPath matchers.
24581 */
24582 if ((elemDecl->idcs != NULL) &&
24583 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24584 return (-1);
24585 /*
24586 * No actual type definition.
24587 */
24588 if (actualType == NULL) {
24589 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24590 "The type definition is absent");
24591 return (XML_SCHEMAV_CVC_TYPE_1);
24592 }
24593 /*
24594 * Remember the actual type definition.
24595 */
24596 vctxt->inode->typeDef = actualType;
24597
24598 return (0);
24599}
24600
24601static int
24602xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24603{
24604 xmlSchemaAttrInfoPtr iattr;
24605 int ret = 0, i;
24606
24607 /*
24608 * SPEC cvc-type (3.1.1)
24609 * "The attributes of must be empty, excepting those whose namespace
24610 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24611 * whose local name is one of type, nil, schemaLocation or
24612 * noNamespaceSchemaLocation."
24613 */
24614 if (vctxt->nbAttrInfos == 0)
24615 return (0);
24616 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24617 iattr = vctxt->attrInfos[i];
24618 if (! iattr->metaType) {
24619 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024620 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024621 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24622 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24623 }
24624 }
24625 ACTIVATE_ELEM
24626 return (ret);
24627}
24628
24629/*
24630* Cleanup currently used attribute infos.
24631*/
24632static void
24633xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24634{
24635 int i;
24636 xmlSchemaAttrInfoPtr attr;
24637
24638 if (vctxt->nbAttrInfos == 0)
24639 return;
24640 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24641 attr = vctxt->attrInfos[i];
24642 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24643 if (attr->localName != NULL)
24644 xmlFree((xmlChar *) attr->localName);
24645 if (attr->nsName != NULL)
24646 xmlFree((xmlChar *) attr->nsName);
24647 }
24648 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24649 if (attr->value != NULL)
24650 xmlFree((xmlChar *) attr->value);
24651 }
24652 if (attr->val != NULL) {
24653 xmlSchemaFreeValue(attr->val);
24654 attr->val = NULL;
24655 }
24656 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24657 }
24658 vctxt->nbAttrInfos = 0;
24659}
24660
24661/*
24662* 3.4.4 Complex Type Definition Validation Rules
24663* Element Locally Valid (Complex Type) (cvc-complex-type)
24664* 3.2.4 Attribute Declaration Validation Rules
24665* Validation Rule: Attribute Locally Valid (cvc-attribute)
24666* Attribute Locally Valid (Use) (cvc-au)
24667*
24668* Only "assessed" attribute information items will be visible to
24669* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24670*/
24671static int
24672xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24673{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024674 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24675 xmlSchemaItemListPtr attrUseList;
24676 xmlSchemaAttributeUsePtr attrUse = NULL;
24677 xmlSchemaAttributePtr attrDecl = NULL;
24678 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24679 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024680 int xpathRes = 0, res, wildIDs = 0, fixed;
24681
24682 /*
24683 * SPEC (cvc-attribute)
24684 * (1) "The declaration must not be ·absent· (see Missing
24685 * Sub-components (§5.3) for how this can fail to be
24686 * the case)."
24687 * (2) "Its {type definition} must not be absent."
24688 *
24689 * NOTE (1) + (2): This is not handled here, since we currently do not
24690 * allow validation against schemas which have missing sub-components.
24691 *
24692 * SPEC (cvc-complex-type)
24693 * (3) "For each attribute information item in the element information
24694 * item's [attributes] excepting those whose [namespace name] is
24695 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24696 * [local name] is one of type, nil, schemaLocation or
24697 * noNamespaceSchemaLocation, the appropriate case among the following
24698 * must be true:
24699 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024700 */
24701 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24702 /*
24703 * @nbAttrs is the number of attributes present in the instance.
24704 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024705 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024706 if (attrUseList != NULL)
24707 nbUses = attrUseList->nbItems;
24708 else
24709 nbUses = 0;
24710 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024711 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024712 attrUse = attrUseList->items[i];
24713 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24714 for (j = 0; j < nbAttrs; j++) {
24715 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024716 /*
24717 * SPEC (cvc-complex-type) (3)
24718 * Skip meta attributes.
24719 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024720 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024721 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024722 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024723 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024724 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024725 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024726 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024727 continue;
24728 found = 1;
24729 /*
24730 * SPEC (cvc-complex-type)
24731 * (3.1) "If there is among the {attribute uses} an attribute
24732 * use with an {attribute declaration} whose {name} matches
24733 * the attribute information item's [local name] and whose
24734 * {target namespace} is identical to the attribute information
24735 * item's [namespace name] (where an ·absent· {target namespace}
24736 * is taken to be identical to a [namespace name] with no value),
24737 * then the attribute information must be ·valid· with respect
24738 * to that attribute use as per Attribute Locally Valid (Use)
24739 * (§3.5.4). In this case the {attribute declaration} of that
24740 * attribute use is the ·context-determined declaration· for the
24741 * attribute information item with respect to Schema-Validity
24742 * Assessment (Attribute) (§3.2.4) and
24743 * Assessment Outcome (Attribute) (§3.2.5).
24744 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024745 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24746 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024747 /*
24748 * Context-determined declaration.
24749 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024750 iattr->decl = attrDecl;
24751 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024752 break;
24753 }
24754
24755 if (found)
24756 continue;
24757
24758 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24759 /*
24760 * Handle non-existent, required attributes.
24761 *
24762 * SPEC (cvc-complex-type)
24763 * (4) "The {attribute declaration} of each attribute use in
24764 * the {attribute uses} whose {required} is true matches one
24765 * of the attribute information items in the element information
24766 * item's [attributes] as per clause 3.1 above."
24767 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024768 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24769 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024770 VERROR_INT(
24771 "xmlSchemaVAttributesComplex",
24772 "calling xmlSchemaGetFreshAttrInfo()");
24773 return (-1);
24774 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024775 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24776 tmpiattr->use = attrUse;
24777 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024778 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24779 ((attrUse->defValue != NULL) ||
24780 (attrDecl->defValue != NULL))) {
24781 /*
24782 * Handle non-existent, optional, default/fixed attributes.
24783 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024784 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24785 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024786 VERROR_INT(
24787 "xmlSchemaVAttributesComplex",
24788 "calling xmlSchemaGetFreshAttrInfo()");
24789 return (-1);
24790 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024791 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24792 tmpiattr->use = attrUse;
24793 tmpiattr->decl = attrDecl;
24794 tmpiattr->typeDef = attrDecl->subtypes;
24795 tmpiattr->localName = attrDecl->name;
24796 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024797 }
24798 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024799
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024800 if (vctxt->nbAttrInfos == 0)
24801 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024802 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024803 /*
24804 * Validate against the wildcard.
24805 */
24806 if (type->attributeWildcard != NULL) {
24807 /*
24808 * SPEC (cvc-complex-type)
24809 * (3.2.1) "There must be an {attribute wildcard}."
24810 */
24811 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024812 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024813 /*
24814 * SPEC (cvc-complex-type) (3)
24815 * Skip meta attributes.
24816 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024817 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024818 continue;
24819 /*
24820 * SPEC (cvc-complex-type)
24821 * (3.2.2) "The attribute information item must be ·valid· with
24822 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24823 *
24824 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24825 * "... its [namespace name] must be ·valid· with respect to
24826 * the wildcard constraint, as defined in Wildcard allows
24827 * Namespace Name (§3.10.4)."
24828 */
24829 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024830 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024831 /*
24832 * Handle processContents.
24833 *
24834 * SPEC (cvc-wildcard):
24835 * processContents | context-determined declaration:
24836 * "strict" "mustFind"
24837 * "lax" "none"
24838 * "skip" "skip"
24839 */
24840 if (type->attributeWildcard->processContents ==
24841 XML_SCHEMAS_ANY_SKIP) {
24842 /*
24843 * context-determined declaration = "skip"
24844 *
24845 * SPEC PSVI Assessment Outcome (Attribute)
24846 * [validity] = "notKnown"
24847 * [validation attempted] = "none"
24848 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024849 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024850 continue;
24851 }
24852 /*
24853 * Find an attribute declaration.
24854 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024855 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24856 iattr->localName, iattr->nsName);
24857 if (iattr->decl != NULL) {
24858 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024859 /*
24860 * SPEC (cvc-complex-type)
24861 * (5) "Let [Definition:] the wild IDs be the set of
24862 * all attribute information item to which clause 3.2
24863 * applied and whose ·validation· resulted in a
24864 * ·context-determined declaration· of mustFind or no
24865 * ·context-determined declaration· at all, and whose
24866 * [local name] and [namespace name] resolve (as
24867 * defined by QName resolution (Instance) (§3.15.4)) to
24868 * an attribute declaration whose {type definition} is
24869 * or is derived from ID. Then all of the following
24870 * must be true:"
24871 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024872 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024873 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024874 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024875 /*
24876 * SPEC (5.1) "There must be no more than one
24877 * item in ·wild IDs·."
24878 */
24879 if (wildIDs != 0) {
24880 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024881 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024882 TODO
24883 continue;
24884 }
24885 wildIDs++;
24886 /*
24887 * SPEC (cvc-complex-type)
24888 * (5.2) "If ·wild IDs· is non-empty, there must not
24889 * be any attribute uses among the {attribute uses}
24890 * whose {attribute declaration}'s {type definition}
24891 * is or is derived from ID."
24892 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024893 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024894 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024895 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024896 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024897 /* URGENT VAL TODO: implement */
24898 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024899 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024900 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024901 }
24902 }
24903 }
24904 } else if (type->attributeWildcard->processContents ==
24905 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024906 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024907 /*
24908 * SPEC PSVI Assessment Outcome (Attribute)
24909 * [validity] = "notKnown"
24910 * [validation attempted] = "none"
24911 */
24912 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024913 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024914 }
24915 }
24916 }
24917 }
24918
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024919 if (vctxt->nbAttrInfos == 0)
24920 return (0);
24921
24922 /*
24923 * Validate values, create default attributes, evaluate IDCs.
24924 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024925 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024926 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024927 /*
24928 * VAL TODO: Note that we won't try to resolve IDCs to
24929 * "lax" and "skip" validated attributes. Check what to
24930 * do in this case.
24931 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024932 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
24933 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024934 continue;
24935 /*
24936 * VAL TODO: What to do if the type definition is missing?
24937 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024938 if (iattr->typeDef == NULL) {
24939 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024940 continue;
24941 }
24942
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024943 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000024944 fixed = 0;
24945 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024946
24947 if (vctxt->xpathStates != NULL) {
24948 /*
24949 * Evaluate IDCs.
24950 */
24951 xpathRes = xmlSchemaXPathEvaluate(vctxt,
24952 XML_ATTRIBUTE_NODE);
24953 if (xpathRes == -1) {
24954 VERROR_INT("xmlSchemaVAttributesComplex",
24955 "calling xmlSchemaXPathEvaluate()");
24956 goto internal_error;
24957 }
24958 }
24959
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024960 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024961 /*
24962 * Default/fixed attributes.
24963 */
24964 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024965 if (iattr->use->defValue != NULL) {
24966 iattr->value = (xmlChar *) iattr->use->defValue;
24967 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024968 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024969 iattr->value = (xmlChar *) iattr->decl->defValue;
24970 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024971 }
24972 /*
24973 * IDCs will consume the precomputed default value,
24974 * so we need to clone it.
24975 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024976 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024977 VERROR_INT("xmlSchemaVAttributesComplex",
24978 "default/fixed value on an attribute use was "
24979 "not precomputed");
24980 goto internal_error;
24981 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024982 iattr->val = xmlSchemaCopyValue(iattr->val);
24983 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024984 VERROR_INT("xmlSchemaVAttributesComplex",
24985 "calling xmlSchemaCopyValue()");
24986 goto internal_error;
24987 }
24988 }
24989 /*
24990 * PSVI: Add the default attribute to the current element.
24991 * VAL TODO: Should we use the *normalized* value? This currently
24992 * uses the *initial* value.
24993 */
24994 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024995 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024996 xmlChar *normValue;
24997 const xmlChar *value;
24998
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024999 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025000 /*
25001 * Normalize the value.
25002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025003 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25004 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025005 if (normValue != NULL)
25006 value = BAD_CAST normValue;
25007
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025008 if (iattr->nsName == NULL) {
25009 if (xmlNewProp(iattr->node->parent,
25010 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025011 VERROR_INT("xmlSchemaVAttributesComplex",
25012 "callling xmlNewProp()");
25013 if (normValue != NULL)
25014 xmlFree(normValue);
25015 goto internal_error;
25016 }
25017 } else {
25018 xmlNsPtr ns;
25019
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025020 ns = xmlSearchNsByHref(iattr->node->doc,
25021 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025022 if (ns == NULL) {
25023 xmlChar prefix[12];
25024 int counter = 0;
25025
25026 /*
25027 * Create a namespace declaration on the validation
25028 * root node if no namespace declaration is in scope.
25029 */
25030 do {
25031 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025032 ns = xmlSearchNs(iattr->node->doc,
25033 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025034 if (counter > 1000) {
25035 VERROR_INT(
25036 "xmlSchemaVAttributesComplex",
25037 "could not compute a ns prefix for a "
25038 "default/fixed attribute");
25039 if (normValue != NULL)
25040 xmlFree(normValue);
25041 goto internal_error;
25042 }
25043 } while (ns != NULL);
25044 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025045 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025046 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025047 /*
25048 * TODO:
25049 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25050 * If we have QNames: do we need to ensure there's a
25051 * prefix defined for the QName?
25052 */
25053 xmlNewNsProp(iattr->node->parent, ns,
25054 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025055 }
25056 if (normValue != NULL)
25057 xmlFree(normValue);
25058 }
25059 /*
25060 * Go directly to IDC evaluation.
25061 */
25062 goto eval_idcs;
25063 }
25064 /*
25065 * Validate the value.
25066 */
25067 if (vctxt->value != NULL) {
25068 /*
25069 * Free last computed value; just for safety reasons.
25070 */
25071 xmlSchemaFreeValue(vctxt->value);
25072 vctxt->value = NULL;
25073 }
25074 /*
25075 * Note that the attribute *use* can be unavailable, if
25076 * the attribute was a wild attribute.
25077 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025078 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25079 ((iattr->use != NULL) &&
25080 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025081 fixed = 1;
25082 else
25083 fixed = 0;
25084 /*
25085 * SPEC (cvc-attribute)
25086 * (3) "The item's ·normalized value· must be locally ·valid·
25087 * with respect to that {type definition} as per
25088 * String Valid (§3.14.4)."
25089 *
25090 * VAL TODO: Do we already have the
25091 * "normalized attribute value" here?
25092 */
25093 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025094 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025095 /*
25096 * Request a computed value.
25097 */
25098 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025099 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025100 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025101 1, 1, 0);
25102 } else {
25103 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025104 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025105 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025106 1, 0, 0);
25107 }
25108
25109 if (res != 0) {
25110 if (res == -1) {
25111 VERROR_INT("xmlSchemaVAttributesComplex",
25112 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25113 goto internal_error;
25114 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025115 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025116 /*
25117 * SPEC PSVI Assessment Outcome (Attribute)
25118 * [validity] = "invalid"
25119 */
25120 goto eval_idcs;
25121 }
25122
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025123 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025124 /*
25125 * SPEC Attribute Locally Valid (Use) (cvc-au)
25126 * "For an attribute information item to be·valid·
25127 * with respect to an attribute use its *normalized*
25128 * value· must match the *canonical* lexical
25129 * representation of the attribute use's {value
25130 * constraint}value, if it is present and fixed."
25131 *
25132 * VAL TODO: The requirement for the *canonical* value
25133 * will be removed in XML Schema 1.1.
25134 */
25135 /*
25136 * SPEC Attribute Locally Valid (cvc-attribute)
25137 * (4) "The item's *actual* value· must match the *value* of
25138 * the {value constraint}, if it is present and fixed."
25139 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025140 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025141 /* VAL TODO: A value was not precomputed. */
25142 TODO
25143 goto eval_idcs;
25144 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025145 if ((iattr->use != NULL) &&
25146 (iattr->use->defValue != NULL)) {
25147 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025148 /* VAL TODO: A default value was not precomputed. */
25149 TODO
25150 goto eval_idcs;
25151 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025152 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025153 /*
25154 if (xmlSchemaCompareValuesWhtsp(attr->val,
25155 (xmlSchemaWhitespaceValueType) ws,
25156 attr->use->defVal,
25157 (xmlSchemaWhitespaceValueType) ws) != 0) {
25158 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025159 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25160 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025161 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025162 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025163 /* VAL TODO: A default value was not precomputed. */
25164 TODO
25165 goto eval_idcs;
25166 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025167 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025168 /*
25169 if (xmlSchemaCompareValuesWhtsp(attr->val,
25170 (xmlSchemaWhitespaceValueType) ws,
25171 attrDecl->defVal,
25172 (xmlSchemaWhitespaceValueType) ws) != 0) {
25173 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025174 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25175 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025176 }
25177 /*
25178 * [validity] = "valid"
25179 */
25180 }
25181eval_idcs:
25182 /*
25183 * Evaluate IDCs.
25184 */
25185 if (xpathRes) {
25186 if (xmlSchemaXPathProcessHistory(vctxt,
25187 vctxt->depth +1) == -1) {
25188 VERROR_INT("xmlSchemaVAttributesComplex",
25189 "calling xmlSchemaXPathEvaluate()");
25190 goto internal_error;
25191 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025192 } else if (vctxt->xpathStates != NULL)
25193 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025194 }
25195
25196 /*
25197 * Report errors.
25198 */
25199 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025200 iattr = vctxt->attrInfos[i];
25201 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25202 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25203 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25204 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025205 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025206 ACTIVATE_ATTRIBUTE(iattr);
25207 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025208 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25209 xmlChar *str = NULL;
25210 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025211 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025212 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25213 "The attribute '%s' is required but missing",
25214 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025215 iattr->decl->targetNamespace,
25216 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025217 NULL);
25218 FREE_AND_NULL(str)
25219 break;
25220 }
25221 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25222 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25223 "The type definition is absent");
25224 break;
25225 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025226 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025227 XML_SCHEMAV_CVC_AU, NULL, NULL,
25228 "The value '%s' does not match the fixed "
25229 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025230 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025231 break;
25232 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25233 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25234 "No matching global attribute declaration available, but "
25235 "demanded by the strict wildcard");
25236 break;
25237 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025238 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025239 break;
25240 /*
25241 * MAYBE VAL TODO: One might report different error messages
25242 * for the following errors.
25243 */
25244 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025245 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025246 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025247 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025248 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025249 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025250 }
25251 break;
25252 default:
25253 break;
25254 }
25255 }
25256
25257 ACTIVATE_ELEM;
25258 return (0);
25259internal_error:
25260 ACTIVATE_ELEM;
25261 return (-1);
25262}
25263
25264static int
25265xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25266 int *skip)
25267{
25268 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25269 /*
25270 * The namespace of the element was already identified to be
25271 * matching the wildcard.
25272 */
25273 if ((skip == NULL) || (wild == NULL) ||
25274 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25275 VERROR_INT("xmlSchemaValidateElemWildcard",
25276 "bad arguments");
25277 return (-1);
25278 }
25279 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025280 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25281 /*
25282 * URGENT VAL TODO: Either we need to position the stream to the
25283 * next sibling, or walk the whole subtree.
25284 */
25285 *skip = 1;
25286 return (0);
25287 }
25288 {
25289 xmlSchemaElementPtr decl = NULL;
25290
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025291 decl = xmlSchemaGetElem(vctxt->schema,
25292 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025293 if (decl != NULL) {
25294 vctxt->inode->decl = decl;
25295 return (0);
25296 }
25297 }
25298 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25299 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025300 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025301 "No matching global element declaration available, but "
25302 "demanded by the strict wildcard");
25303 return (vctxt->err);
25304 }
25305 if (vctxt->nbAttrInfos != 0) {
25306 xmlSchemaAttrInfoPtr iattr;
25307 /*
25308 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25309 * (1.2.1.2.1) - (1.2.1.2.3 )
25310 *
25311 * Use the xsi:type attribute for the type definition.
25312 */
25313 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25314 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25315 if (iattr != NULL) {
25316 if (xmlSchemaProcessXSIType(vctxt, iattr,
25317 &(vctxt->inode->typeDef), NULL) == -1) {
25318 VERROR_INT("xmlSchemaValidateElemWildcard",
25319 "calling xmlSchemaProcessXSIType() to "
25320 "process the attribute 'xsi:nil'");
25321 return (-1);
25322 }
25323 /*
25324 * Don't return an error on purpose.
25325 */
25326 return (0);
25327 }
25328 }
25329 /*
25330 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25331 *
25332 * Fallback to "anyType".
25333 */
25334 vctxt->inode->typeDef =
25335 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25336 return (0);
25337}
25338
25339/*
25340* xmlSchemaCheckCOSValidDefault:
25341*
25342* This will be called if: not nilled, no content and a default/fixed
25343* value is provided.
25344*/
25345
25346static int
25347xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25348 const xmlChar *value,
25349 xmlSchemaValPtr *val)
25350{
25351 int ret = 0;
25352 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25353
25354 /*
25355 * cos-valid-default:
25356 * Schema Component Constraint: Element Default Valid (Immediate)
25357 * For a string to be a valid default with respect to a type
25358 * definition the appropriate case among the following must be true:
25359 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025360 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025361 /*
25362 * Complex type.
25363 *
25364 * SPEC (2.1) "its {content type} must be a simple type definition
25365 * or mixed."
25366 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25367 * type}'s particle must be ·emptiable· as defined by
25368 * Particle Emptiable (§3.9.6)."
25369 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025370 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25371 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25372 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025373 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25374 /* NOTE that this covers (2.2.2) as well. */
25375 VERROR(ret, NULL,
25376 "For a string to be a valid default, the type definition "
25377 "must be a simple type or a complex type with simple content "
25378 "or mixed content and a particle emptiable");
25379 return(ret);
25380 }
25381 }
25382 /*
25383 * 1 If the type definition is a simple type definition, then the string
25384 * must be ·valid· with respect to that definition as defined by String
25385 * Valid (§3.14.4).
25386 *
25387 * AND
25388 *
25389 * 2.2.1 If the {content type} is a simple type definition, then the
25390 * string must be ·valid· with respect to that simple type definition
25391 * as defined by String Valid (§3.14.4).
25392 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025393 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025394
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025395 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025396 NULL, inode->typeDef, value, val, 1, 1, 0);
25397
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025398 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025399
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025400 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025401 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25402 }
25403 if (ret < 0) {
25404 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25405 "calling xmlSchemaVCheckCVCSimpleType()");
25406 }
25407 return (ret);
25408}
25409
25410static void
25411xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25412 const xmlChar * name ATTRIBUTE_UNUSED,
25413 xmlSchemaElementPtr item,
25414 xmlSchemaNodeInfoPtr inode)
25415{
25416 inode->decl = item;
25417#ifdef DEBUG_CONTENT
25418 {
25419 xmlChar *str = NULL;
25420
25421 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25422 xmlGenericError(xmlGenericErrorContext,
25423 "AUTOMATON callback for '%s' [declaration]\n",
25424 xmlSchemaFormatQName(&str,
25425 inode->localName, inode->nsName));
25426 } else {
25427 xmlGenericError(xmlGenericErrorContext,
25428 "AUTOMATON callback for '%s' [wildcard]\n",
25429 xmlSchemaFormatQName(&str,
25430 inode->localName, inode->nsName));
25431
25432 }
25433 FREE_AND_NULL(str)
25434 }
25435#endif
25436}
25437
25438static int
25439xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025440{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025441 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25442 if (vctxt->inode == NULL) {
25443 VERROR_INT("xmlSchemaValidatorPushElem",
25444 "calling xmlSchemaGetFreshElemInfo()");
25445 return (-1);
25446 }
25447 vctxt->nbAttrInfos = 0;
25448 return (0);
25449}
25450
25451static int
25452xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25453 xmlSchemaNodeInfoPtr inode,
25454 xmlSchemaTypePtr type,
25455 const xmlChar *value)
25456{
25457 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25458 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025459 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025460 type, value, &(inode->val), 1, 1, 0));
25461 else
25462 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025463 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025464 type, value, NULL, 1, 0, 0));
25465}
25466
25467
25468
25469/*
25470* Process END of element.
25471*/
25472static int
25473xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25474{
25475 int ret = 0;
25476 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25477
25478 if (vctxt->nbAttrInfos != 0)
25479 xmlSchemaClearAttrInfos(vctxt);
25480 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25481 /*
25482 * This element was not expected;
25483 * we will not validate child elements of broken parents.
25484 * Skip validation of all content of the parent.
25485 */
25486 vctxt->skipDepth = vctxt->depth -1;
25487 goto end_elem;
25488 }
25489 if ((inode->typeDef == NULL) ||
25490 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25491 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025492 * 1. the type definition might be missing if the element was
25493 * error prone
25494 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025495 */
25496 goto end_elem;
25497 }
25498 /*
25499 * Check the content model.
25500 */
25501 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25502 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25503
25504 /*
25505 * Workaround for "anyType".
25506 */
25507 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25508 goto character_content;
25509
25510 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25511 xmlChar *values[10];
25512 int terminal, nbval = 10, nbneg;
25513
25514 if (inode->regexCtxt == NULL) {
25515 /*
25516 * Create the regex context.
25517 */
25518 inode->regexCtxt =
25519 xmlRegNewExecCtxt(inode->typeDef->contModel,
25520 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25521 vctxt);
25522 if (inode->regexCtxt == NULL) {
25523 VERROR_INT("xmlSchemaValidatorPopElem",
25524 "failed to create a regex context");
25525 goto internal_error;
25526 }
25527#ifdef DEBUG_AUTOMATA
25528 xmlGenericError(xmlGenericErrorContext,
25529 "AUTOMATON create on '%s'\n", inode->localName);
25530#endif
25531 }
25532 /*
25533 * Get hold of the still expected content, since a further
25534 * call to xmlRegExecPushString() will loose this information.
25535 */
25536 xmlRegExecNextValues(inode->regexCtxt,
25537 &nbval, &nbneg, &values[0], &terminal);
25538 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25539 if (ret <= 0) {
25540 /*
25541 * Still missing something.
25542 */
25543 ret = 1;
25544 inode->flags |=
25545 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025546 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025547 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25548 "Missing child element(s)",
25549 nbval, nbneg, values);
25550#ifdef DEBUG_AUTOMATA
25551 xmlGenericError(xmlGenericErrorContext,
25552 "AUTOMATON missing ERROR on '%s'\n",
25553 inode->localName);
25554#endif
25555 } else {
25556 /*
25557 * Content model is satisfied.
25558 */
25559 ret = 0;
25560#ifdef DEBUG_AUTOMATA
25561 xmlGenericError(xmlGenericErrorContext,
25562 "AUTOMATON succeeded on '%s'\n",
25563 inode->localName);
25564#endif
25565 }
25566
25567 }
25568 }
25569 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25570 goto end_elem;
25571
25572character_content:
25573
25574 if (vctxt->value != NULL) {
25575 xmlSchemaFreeValue(vctxt->value);
25576 vctxt->value = NULL;
25577 }
25578 /*
25579 * Check character content.
25580 */
25581 if (inode->decl == NULL) {
25582 /*
25583 * Speedup if no declaration exists.
25584 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025585 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025586 ret = xmlSchemaVCheckINodeDataType(vctxt,
25587 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025588 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025589 ret = xmlSchemaVCheckINodeDataType(vctxt,
25590 inode, inode->typeDef->contentTypeDef,
25591 inode->value);
25592 }
25593 if (ret < 0) {
25594 VERROR_INT("xmlSchemaValidatorPopElem",
25595 "calling xmlSchemaVCheckCVCSimpleType()");
25596 goto internal_error;
25597 }
25598 goto end_elem;
25599 }
25600 /*
25601 * cvc-elt (3.3.4) : 5
25602 * The appropriate case among the following must be true:
25603 */
25604 /*
25605 * cvc-elt (3.3.4) : 5.1
25606 * If the declaration has a {value constraint},
25607 * the item has neither element nor character [children] and
25608 * clause 3.2 has not applied, then all of the following must be true:
25609 */
25610 if ((inode->decl->value != NULL) &&
25611 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25612 (! INODE_NILLED(inode))) {
25613 /*
25614 * cvc-elt (3.3.4) : 5.1.1
25615 * If the ·actual type definition· is a ·local type definition·
25616 * then the canonical lexical representation of the {value constraint}
25617 * value must be a valid default for the ·actual type definition· as
25618 * defined in Element Default Valid (Immediate) (§3.3.6).
25619 */
25620 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025621 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025622 * NOTE: Although the *canonical* value is stated, it is not
25623 * relevant if canonical or not. Additionally XML Schema 1.1
25624 * will removed this requirement as well.
25625 */
25626 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25627
25628 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25629 inode->decl->value, &(inode->val));
25630 if (ret != 0) {
25631 if (ret < 0) {
25632 VERROR_INT("xmlSchemaValidatorPopElem",
25633 "calling xmlSchemaCheckCOSValidDefault()");
25634 goto internal_error;
25635 }
25636 goto end_elem;
25637 }
25638 /*
25639 * Stop here, to avoid redundant validation of the value
25640 * (see following).
25641 */
25642 goto default_psvi;
25643 }
25644 /*
25645 * cvc-elt (3.3.4) : 5.1.2
25646 * The element information item with the canonical lexical
25647 * representation of the {value constraint} value used as its
25648 * ·normalized value· must be ·valid· with respect to the
25649 * ·actual type definition· as defined by Element Locally Valid (Type)
25650 * (§3.3.4).
25651 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025652 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025653 ret = xmlSchemaVCheckINodeDataType(vctxt,
25654 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025655 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025656 ret = xmlSchemaVCheckINodeDataType(vctxt,
25657 inode, inode->typeDef->contentTypeDef,
25658 inode->decl->value);
25659 }
25660 if (ret != 0) {
25661 if (ret < 0) {
25662 VERROR_INT("xmlSchemaValidatorPopElem",
25663 "calling xmlSchemaVCheckCVCSimpleType()");
25664 goto internal_error;
25665 }
25666 goto end_elem;
25667 }
25668
25669default_psvi:
25670 /*
25671 * PSVI: Create a text node on the instance element.
25672 */
25673 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25674 (inode->node != NULL)) {
25675 xmlNodePtr textChild;
25676 xmlChar *normValue;
25677 /*
25678 * VAL TODO: Normalize the value.
25679 */
25680 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25681 inode->decl->value);
25682 if (normValue != NULL) {
25683 textChild = xmlNewText(BAD_CAST normValue);
25684 xmlFree(normValue);
25685 } else
25686 textChild = xmlNewText(inode->decl->value);
25687 if (textChild == NULL) {
25688 VERROR_INT("xmlSchemaValidatorPopElem",
25689 "calling xmlNewText()");
25690 goto internal_error;
25691 } else
25692 xmlAddChild(inode->node, textChild);
25693 }
25694
25695 } else if (! INODE_NILLED(inode)) {
25696 /*
25697 * 5.2.1 The element information item must be ·valid· with respect
25698 * to the ·actual type definition· as defined by Element Locally
25699 * Valid (Type) (§3.3.4).
25700 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025701 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025702 /*
25703 * SPEC (cvc-type) (3.1)
25704 * "If the type definition is a simple type definition, ..."
25705 * (3.1.3) "If clause 3.2 of Element Locally Valid
25706 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25707 * must be ·valid· with respect to the type definition as defined
25708 * by String Valid (§3.14.4).
25709 */
25710 ret = xmlSchemaVCheckINodeDataType(vctxt,
25711 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025712 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025713 /*
25714 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25715 * definition, then the element information item must be
25716 * ·valid· with respect to the type definition as per
25717 * Element Locally Valid (Complex Type) (§3.4.4);"
25718 *
25719 * SPEC (cvc-complex-type) (2.2)
25720 * "If the {content type} is a simple type definition, ...
25721 * the ·normalized value· of the element information item is
25722 * ·valid· with respect to that simple type definition as
25723 * defined by String Valid (§3.14.4)."
25724 */
25725 ret = xmlSchemaVCheckINodeDataType(vctxt,
25726 inode, inode->typeDef->contentTypeDef, inode->value);
25727 }
25728 if (ret != 0) {
25729 if (ret < 0) {
25730 VERROR_INT("xmlSchemaValidatorPopElem",
25731 "calling xmlSchemaVCheckCVCSimpleType()");
25732 goto internal_error;
25733 }
25734 goto end_elem;
25735 }
25736 /*
25737 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25738 * not applied, all of the following must be true:
25739 */
25740 if ((inode->decl->value != NULL) &&
25741 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25742
25743 /*
25744 * TODO: We will need a computed value, when comparison is
25745 * done on computed values.
25746 */
25747 /*
25748 * 5.2.2.1 The element information item must have no element
25749 * information item [children].
25750 */
25751 if (inode->flags &
25752 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25753 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25754 VERROR(ret, NULL,
25755 "The content must not containt element nodes since "
25756 "there is a fixed value constraint");
25757 goto end_elem;
25758 } else {
25759 /*
25760 * 5.2.2.2 The appropriate case among the following must
25761 * be true:
25762 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025763 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025764 /*
25765 * 5.2.2.2.1 If the {content type} of the ·actual type
25766 * definition· is mixed, then the *initial value* of the
25767 * item must match the canonical lexical representation
25768 * of the {value constraint} value.
25769 *
25770 * ... the *initial value* of an element information
25771 * item is the string composed of, in order, the
25772 * [character code] of each character information item in
25773 * the [children] of that element information item.
25774 */
25775 if (! xmlStrEqual(inode->value, inode->decl->value)){
25776 /*
25777 * VAL TODO: Report invalid & expected values as well.
25778 * VAL TODO: Implement the canonical stuff.
25779 */
25780 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025781 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025782 ret, NULL, NULL,
25783 "The initial value '%s' does not match the fixed "
25784 "value constraint '%s'",
25785 inode->value, inode->decl->value);
25786 goto end_elem;
25787 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025788 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025789 /*
25790 * 5.2.2.2.2 If the {content type} of the ·actual type
25791 * definition· is a simple type definition, then the
25792 * *actual value* of the item must match the canonical
25793 * lexical representation of the {value constraint} value.
25794 */
25795 /*
25796 * VAL TODO: *actual value* is the normalized value, impl.
25797 * this.
25798 * VAL TODO: Report invalid & expected values as well.
25799 * VAL TODO: Implement a comparison with the computed values.
25800 */
25801 if (! xmlStrEqual(inode->value,
25802 inode->decl->value)) {
25803 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025804 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025805 ret, NULL, NULL,
25806 "The actual value '%s' does not match the fixed "
25807 "value constraint '%s'",
25808 inode->value,
25809 inode->decl->value);
25810 goto end_elem;
25811 }
25812 }
25813 }
25814 }
25815 }
25816
25817end_elem:
25818 if (vctxt->depth < 0) {
25819 /* TODO: raise error? */
25820 return (0);
25821 }
25822 if (vctxt->depth == vctxt->skipDepth)
25823 vctxt->skipDepth = -1;
25824 /*
25825 * Evaluate the history of XPath state objects.
25826 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025827 if (inode->appliedXPath &&
25828 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025829 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025830 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025831 * MAYBE TODO:
25832 * SPEC (6) "The element information item must be ·valid· with
25833 * respect to each of the {identity-constraint definitions} as per
25834 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025835 */
25836 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025837 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25838 * need to be built in any case.
25839 * We will currently build IDC node-tables and bubble them only if
25840 * keyrefs do exist.
25841 */
25842
25843 /*
25844 * Add the current IDC target-nodes to the IDC node-tables.
25845 */
25846 if ((inode->idcMatchers != NULL) &&
25847 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25848 {
25849 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25850 goto internal_error;
25851 }
25852 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025853 * Validate IDC keyrefs.
25854 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025855 if (vctxt->inode->hasKeyrefs)
25856 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25857 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025858 /*
25859 * Merge/free the IDC table.
25860 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025861 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025862#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025863 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025864 inode->nsName,
25865 inode->localName,
25866 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025867#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025868 if ((vctxt->depth > 0) &&
25869 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25870 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025871 /*
25872 * Merge the IDC node table with the table of the parent node.
25873 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025874 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25875 goto internal_error;
25876 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025877 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025878 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025879 * Clear the current ielem.
25880 * VAL TODO: Don't free the PSVI IDC tables if they are
25881 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025882 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025883 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025884 /*
25885 * Skip further processing if we are on the validation root.
25886 */
25887 if (vctxt->depth == 0) {
25888 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025889 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025890 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025891 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025892 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025893 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025894 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025895 if (vctxt->aidcs != NULL) {
25896 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25897 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025898 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025899 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025900 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025901 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025902 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025903 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025904 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025905 }
25906 aidc = aidc->next;
25907 } while (aidc != NULL);
25908 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025909 vctxt->depth--;
25910 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025911 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025912 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025913 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25914 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025915 return (ret);
25916
25917internal_error:
25918 vctxt->err = -1;
25919 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000025920}
25921
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025922/*
25923* 3.4.4 Complex Type Definition Validation Rules
25924* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
25925*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000025926static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025927xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000025928{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025929 xmlSchemaNodeInfoPtr pielem;
25930 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000025931 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000025932
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025933 if (vctxt->depth <= 0) {
25934 VERROR_INT("xmlSchemaValidateChildElem",
25935 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025936 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025937 }
25938 pielem = vctxt->elemInfos[vctxt->depth -1];
25939 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
25940 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025941 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025942 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025943 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025944 if (INODE_NILLED(pielem)) {
25945 /*
25946 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
25947 */
25948 ACTIVATE_PARENT_ELEM;
25949 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
25950 VERROR(ret, NULL,
25951 "Neither character nor element content is allowed, "
25952 "because the element was 'nilled'");
25953 ACTIVATE_ELEM;
25954 goto unexpected_elem;
25955 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025956
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025957 ptype = pielem->typeDef;
25958
25959 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
25960 /*
25961 * Workaround for "anyType": we have currently no content model
25962 * assigned for "anyType", so handle it explicitely.
25963 * "anyType" has an unbounded, lax "any" wildcard.
25964 */
25965 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
25966 vctxt->inode->localName,
25967 vctxt->inode->nsName);
25968
25969 if (vctxt->inode->decl == NULL) {
25970 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025971 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025972 * Process "xsi:type".
25973 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025974 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025975 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25976 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25977 if (iattr != NULL) {
25978 ret = xmlSchemaProcessXSIType(vctxt, iattr,
25979 &(vctxt->inode->typeDef), NULL);
25980 if (ret != 0) {
25981 if (ret == -1) {
25982 VERROR_INT("xmlSchemaValidateChildElem",
25983 "calling xmlSchemaProcessXSIType() to "
25984 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025985 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000025986 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025987 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000025988 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025989 } else {
25990 /*
25991 * Fallback to "anyType".
25992 *
25993 * SPEC (cvc-assess-elt)
25994 * "If the item cannot be ·strictly assessed·, [...]
25995 * an element information item's schema validity may be laxly
25996 * assessed if its ·context-determined declaration· is not
25997 * skip by ·validating· with respect to the ·ur-type
25998 * definition· as per Element Locally Valid (Type) (§3.3.4)."
25999 */
26000 vctxt->inode->typeDef =
26001 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026002 }
26003 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026004 return (0);
26005 }
26006
26007 switch (ptype->contentType) {
26008 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026009 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026010 * SPEC (2.1) "If the {content type} is empty, then the
26011 * element information item has no character or element
26012 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026013 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026014 ACTIVATE_PARENT_ELEM
26015 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26016 VERROR(ret, NULL,
26017 "Element content is not allowed, "
26018 "because the content type is empty");
26019 ACTIVATE_ELEM
26020 goto unexpected_elem;
26021 break;
26022
26023 case XML_SCHEMA_CONTENT_MIXED:
26024 case XML_SCHEMA_CONTENT_ELEMENTS: {
26025 xmlRegExecCtxtPtr regexCtxt;
26026 xmlChar *values[10];
26027 int terminal, nbval = 10, nbneg;
26028
26029 /* VAL TODO: Optimized "anyType" validation.*/
26030
26031 if (ptype->contModel == NULL) {
26032 VERROR_INT("xmlSchemaValidateChildElem",
26033 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026034 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026035 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026036 /*
26037 * Safety belf for evaluation if the cont. model was already
26038 * examined to be invalid.
26039 */
26040 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26041 VERROR_INT("xmlSchemaValidateChildElem",
26042 "validating elem, but elem content is already invalid");
26043 return (-1);
26044 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026045
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026046 regexCtxt = pielem->regexCtxt;
26047 if (regexCtxt == NULL) {
26048 /*
26049 * Create the regex context.
26050 */
26051 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26052 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26053 vctxt);
26054 if (regexCtxt == NULL) {
26055 VERROR_INT("xmlSchemaValidateChildElem",
26056 "failed to create a regex context");
26057 return (-1);
26058 }
26059 pielem->regexCtxt = regexCtxt;
26060#ifdef DEBUG_AUTOMATA
26061 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26062 pielem->localName);
26063#endif
26064 }
26065
26066 /*
26067 * SPEC (2.4) "If the {content type} is element-only or mixed,
26068 * then the sequence of the element information item's
26069 * element information item [children], if any, taken in
26070 * order, is ·valid· with respect to the {content type}'s
26071 * particle, as defined in Element Sequence Locally Valid
26072 * (Particle) (§3.9.4)."
26073 */
26074 ret = xmlRegExecPushString2(regexCtxt,
26075 vctxt->inode->localName,
26076 vctxt->inode->nsName,
26077 vctxt->inode);
26078#ifdef DEBUG_AUTOMATA
26079 if (ret < 0)
26080 xmlGenericError(xmlGenericErrorContext,
26081 "AUTOMATON push ERROR for '%s' on '%s'\n",
26082 vctxt->inode->localName, pielem->localName);
26083 else
26084 xmlGenericError(xmlGenericErrorContext,
26085 "AUTOMATON push OK for '%s' on '%s'\n",
26086 vctxt->inode->localName, pielem->localName);
26087#endif
26088 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26089 VERROR_INT("xmlSchemaValidateChildElem",
26090 "calling xmlRegExecPushString2()");
26091 return (-1);
26092 }
26093 if (ret < 0) {
26094 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26095 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026096 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026097 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26098 "This element is not expected",
26099 nbval, nbneg, values);
26100 ret = vctxt->err;
26101 goto unexpected_elem;
26102 } else
26103 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026104 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026105 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026106 case XML_SCHEMA_CONTENT_SIMPLE:
26107 case XML_SCHEMA_CONTENT_BASIC:
26108 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026109 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026110 /*
26111 * SPEC (cvc-complex-type) (2.2)
26112 * "If the {content type} is a simple type definition, then
26113 * the element information item has no element information
26114 * item [children], ..."
26115 */
26116 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26117 VERROR(ret, NULL, "Element content is not allowed, "
26118 "because the content type is a simple type definition");
26119 } else {
26120 /*
26121 * SPEC (cvc-type) (3.1.2) "The element information item must
26122 * have no element information item [children]."
26123 */
26124 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26125 VERROR(ret, NULL, "Element content is not allowed, "
26126 "because the type definition is simple");
26127 }
26128 ACTIVATE_ELEM
26129 ret = vctxt->err;
26130 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026131 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026132
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026133 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026134 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026135 }
26136 return (ret);
26137unexpected_elem:
26138 /*
26139 * Pop this element and set the skipDepth to skip
26140 * all further content of the parent element.
26141 */
26142 vctxt->skipDepth = vctxt->depth;
26143 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26144 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26145 return (ret);
26146}
26147
26148#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26149#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26150#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26151
26152static int
26153xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26154 int nodeType, const xmlChar *value, int len,
26155 int mode, int *consumed)
26156{
26157 /*
26158 * Unfortunately we have to duplicate the text sometimes.
26159 * OPTIMIZE: Maybe we could skip it, if:
26160 * 1. content type is simple
26161 * 2. whitespace is "collapse"
26162 * 3. it consists of whitespace only
26163 *
26164 * Process character content.
26165 */
26166 if (consumed != NULL)
26167 *consumed = 0;
26168 if (INODE_NILLED(vctxt->inode)) {
26169 /*
26170 * SPEC cvc-elt (3.3.4 - 3.2.1)
26171 * "The element information item must have no character or
26172 * element information item [children]."
26173 */
26174 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26175 "Neither character nor element content is allowed "
26176 "because the element is 'nilled'");
26177 return (vctxt->err);
26178 }
26179 /*
26180 * SPEC (2.1) "If the {content type} is empty, then the
26181 * element information item has no character or element
26182 * information item [children]."
26183 */
26184 if (vctxt->inode->typeDef->contentType ==
26185 XML_SCHEMA_CONTENT_EMPTY) {
26186 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26187 "Character content is not allowed, "
26188 "because the content type is empty");
26189 return (vctxt->err);
26190 }
26191
26192 if (vctxt->inode->typeDef->contentType ==
26193 XML_SCHEMA_CONTENT_ELEMENTS) {
26194 if ((nodeType != XML_TEXT_NODE) ||
26195 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26196 /*
26197 * SPEC cvc-complex-type (2.3)
26198 * "If the {content type} is element-only, then the
26199 * element information item has no character information
26200 * item [children] other than those whose [character
26201 * code] is defined as a white space in [XML 1.0 (Second
26202 * Edition)]."
26203 */
26204 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26205 "Character content other than whitespace is not allowed "
26206 "because the content type is 'element-only'");
26207 return (vctxt->err);
26208 }
26209 return (0);
26210 }
26211
26212 if ((value == NULL) || (value[0] == 0))
26213 return (0);
26214 /*
26215 * Save the value.
26216 * NOTE that even if the content type is *mixed*, we need the
26217 * *initial value* for default/fixed value constraints.
26218 */
26219 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26220 ((vctxt->inode->decl == NULL) ||
26221 (vctxt->inode->decl->value == NULL)))
26222 return (0);
26223
26224 if (vctxt->inode->value == NULL) {
26225 /*
26226 * Set the value.
26227 */
26228 switch (mode) {
26229 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26230 /*
26231 * When working on a tree.
26232 */
26233 vctxt->inode->value = value;
26234 break;
26235 case XML_SCHEMA_PUSH_TEXT_CREATED:
26236 /*
26237 * When working with the reader.
26238 * The value will be freed by the element info.
26239 */
26240 vctxt->inode->value = value;
26241 if (consumed != NULL)
26242 *consumed = 1;
26243 vctxt->inode->flags |=
26244 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26245 break;
26246 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26247 /*
26248 * When working with SAX.
26249 * The value will be freed by the element info.
26250 */
26251 if (len != -1)
26252 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26253 else
26254 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26255 vctxt->inode->flags |=
26256 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26257 break;
26258 default:
26259 break;
26260 }
26261 } else {
26262 /*
26263 * Concat the value.
26264 */
26265 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026266 vctxt->inode->value = BAD_CAST xmlStrncat(
26267 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026268 } else {
26269 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026270 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026271 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26272 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026273 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026274
26275 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026276}
26277
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026278static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026279xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026280{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026281 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026282
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026283 if ((vctxt->skipDepth != -1) &&
26284 (vctxt->depth >= vctxt->skipDepth)) {
26285 VERROR_INT("xmlSchemaValidateElem",
26286 "in skip-state");
26287 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026288 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026289 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026290 /*
26291 * URGENT TODO: Better to fully stop validation
26292 * if there was an error during dynamic schema construction.
26293 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026294 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26295 goto internal_error;
26296 }
26297 if (vctxt->depth > 0) {
26298 /*
26299 * Validate this element against the content model
26300 * of the parent.
26301 */
26302 ret = xmlSchemaValidateChildElem(vctxt);
26303 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026304 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026305 VERROR_INT("xmlSchemaValidateElem",
26306 "calling xmlSchemaStreamValidateChildElement()");
26307 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026308 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026309 goto exit;
26310 }
26311 if (vctxt->depth == vctxt->skipDepth)
26312 goto exit;
26313 if ((vctxt->inode->decl == NULL) &&
26314 (vctxt->inode->typeDef == NULL)) {
26315 VERROR_INT("xmlSchemaValidateElem",
26316 "the child element was valid but neither the "
26317 "declaration nor the type was set");
26318 goto internal_error;
26319 }
26320 } else {
26321 /*
26322 * Get the declaration of the validation root.
26323 */
26324 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26325 vctxt->inode->localName,
26326 vctxt->inode->nsName);
26327 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026328 ret = XML_SCHEMAV_CVC_ELT_1;
26329 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026330 "No matching global declaration available "
26331 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026332 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026333 }
26334 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026335
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026336 if (vctxt->inode->decl == NULL)
26337 goto type_validation;
26338
26339 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26340 int skip;
26341 /*
26342 * Wildcards.
26343 */
26344 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26345 if (ret != 0) {
26346 if (ret < 0) {
26347 VERROR_INT("xmlSchemaValidateElem",
26348 "calling xmlSchemaValidateElemWildcard()");
26349 goto internal_error;
26350 }
26351 goto exit;
26352 }
26353 if (skip) {
26354 vctxt->skipDepth = vctxt->depth;
26355 goto exit;
26356 }
26357 /*
26358 * The declaration might be set by the wildcard validation,
26359 * when the processContents is "lax" or "strict".
26360 */
26361 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26362 /*
26363 * Clear the "decl" field to not confuse further processing.
26364 */
26365 vctxt->inode->decl = NULL;
26366 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026367 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026368 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026369 /*
26370 * Validate against the declaration.
26371 */
26372 ret = xmlSchemaValidateElemDecl(vctxt);
26373 if (ret != 0) {
26374 if (ret < 0) {
26375 VERROR_INT("xmlSchemaValidateElem",
26376 "calling xmlSchemaValidateElemDecl()");
26377 goto internal_error;
26378 }
26379 goto exit;
26380 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026381 /*
26382 * Validate against the type definition.
26383 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026384type_validation:
26385
26386 if (vctxt->inode->typeDef == NULL) {
26387 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26388 ret = XML_SCHEMAV_CVC_TYPE_1;
26389 VERROR(ret, NULL,
26390 "The type definition is absent");
26391 goto exit;
26392 }
26393 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26394 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26395 ret = XML_SCHEMAV_CVC_TYPE_2;
26396 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026397 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026398 goto exit;
26399 }
26400 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026401 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026402 * during validation against the declaration. This must be done
26403 * _before_ attribute validation.
26404 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026405 if (vctxt->xpathStates != NULL) {
26406 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026407 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026408 if (ret == -1) {
26409 VERROR_INT("xmlSchemaValidateElem",
26410 "calling xmlSchemaXPathEvaluate()");
26411 goto internal_error;
26412 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026413 }
26414 /*
26415 * Validate attributes.
26416 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026417 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026418 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026419 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026420
26421 ret = xmlSchemaVAttributesComplex(vctxt);
26422 }
26423 } else if (vctxt->nbAttrInfos != 0) {
26424
26425 ret = xmlSchemaVAttributesSimple(vctxt);
26426 }
26427 /*
26428 * Clear registered attributes.
26429 */
26430 if (vctxt->nbAttrInfos != 0)
26431 xmlSchemaClearAttrInfos(vctxt);
26432 if (ret == -1) {
26433 VERROR_INT("xmlSchemaValidateElem",
26434 "calling attributes validation");
26435 goto internal_error;
26436 }
26437 /*
26438 * Don't return an error if attributes are invalid on purpose.
26439 */
26440 ret = 0;
26441
26442exit:
26443 if (ret != 0)
26444 vctxt->skipDepth = vctxt->depth;
26445 return (ret);
26446internal_error:
26447 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026448}
26449
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026450#ifdef XML_SCHEMA_READER_ENABLED
26451static int
26452xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026453{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026454 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26455 int depth, nodeType, ret = 0, consumed;
26456 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026457
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026458 vctxt->depth = -1;
26459 ret = xmlTextReaderRead(vctxt->reader);
26460 /*
26461 * Move to the document element.
26462 */
26463 while (ret == 1) {
26464 nodeType = xmlTextReaderNodeType(vctxt->reader);
26465 if (nodeType == XML_ELEMENT_NODE)
26466 goto root_found;
26467 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026468 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026469 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026470
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026471root_found:
26472
26473 do {
26474 depth = xmlTextReaderDepth(vctxt->reader);
26475 nodeType = xmlTextReaderNodeType(vctxt->reader);
26476
26477 if (nodeType == XML_ELEMENT_NODE) {
26478
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026479 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026480 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26481 VERROR_INT("xmlSchemaVReaderWalk",
26482 "calling xmlSchemaValidatorPushElem()");
26483 goto internal_error;
26484 }
26485 ielem = vctxt->inode;
26486 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26487 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26488 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26489 /*
26490 * Is the element empty?
26491 */
26492 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26493 if (ret == -1) {
26494 VERROR_INT("xmlSchemaVReaderWalk",
26495 "calling xmlTextReaderIsEmptyElement()");
26496 goto internal_error;
26497 }
26498 if (ret) {
26499 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26500 }
26501 /*
26502 * Register attributes.
26503 */
26504 vctxt->nbAttrInfos = 0;
26505 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26506 if (ret == -1) {
26507 VERROR_INT("xmlSchemaVReaderWalk",
26508 "calling xmlTextReaderMoveToFirstAttribute()");
26509 goto internal_error;
26510 }
26511 if (ret == 1) {
26512 do {
26513 /*
26514 * VAL TODO: How do we know that the reader works on a
26515 * node tree, to be able to pass a node here?
26516 */
26517 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26518 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26519 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26520 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26521
26522 VERROR_INT("xmlSchemaVReaderWalk",
26523 "calling xmlSchemaValidatorPushAttribute()");
26524 goto internal_error;
26525 }
26526 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26527 if (ret == -1) {
26528 VERROR_INT("xmlSchemaVReaderWalk",
26529 "calling xmlTextReaderMoveToFirstAttribute()");
26530 goto internal_error;
26531 }
26532 } while (ret == 1);
26533 /*
26534 * Back to element position.
26535 */
26536 ret = xmlTextReaderMoveToElement(vctxt->reader);
26537 if (ret == -1) {
26538 VERROR_INT("xmlSchemaVReaderWalk",
26539 "calling xmlTextReaderMoveToElement()");
26540 goto internal_error;
26541 }
26542 }
26543 /*
26544 * Validate the element.
26545 */
26546 ret= xmlSchemaValidateElem(vctxt);
26547 if (ret != 0) {
26548 if (ret == -1) {
26549 VERROR_INT("xmlSchemaVReaderWalk",
26550 "calling xmlSchemaValidateElem()");
26551 goto internal_error;
26552 }
26553 goto exit;
26554 }
26555 if (vctxt->depth == vctxt->skipDepth) {
26556 int curDepth;
26557 /*
26558 * Skip all content.
26559 */
26560 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26561 ret = xmlTextReaderRead(vctxt->reader);
26562 curDepth = xmlTextReaderDepth(vctxt->reader);
26563 while ((ret == 1) && (curDepth != depth)) {
26564 ret = xmlTextReaderRead(vctxt->reader);
26565 curDepth = xmlTextReaderDepth(vctxt->reader);
26566 }
26567 if (ret < 0) {
26568 /*
26569 * VAL TODO: A reader error occured; what to do here?
26570 */
26571 ret = 1;
26572 goto exit;
26573 }
26574 }
26575 goto leave_elem;
26576 }
26577 /*
26578 * READER VAL TODO: Is an END_ELEM really never called
26579 * if the elem is empty?
26580 */
26581 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26582 goto leave_elem;
26583 } else if (nodeType == END_ELEM) {
26584 /*
26585 * Process END of element.
26586 */
26587leave_elem:
26588 ret = xmlSchemaValidatorPopElem(vctxt);
26589 if (ret != 0) {
26590 if (ret < 0) {
26591 VERROR_INT("xmlSchemaVReaderWalk",
26592 "calling xmlSchemaValidatorPopElem()");
26593 goto internal_error;
26594 }
26595 goto exit;
26596 }
26597 if (vctxt->depth >= 0)
26598 ielem = vctxt->inode;
26599 else
26600 ielem = NULL;
26601 } else if ((nodeType == XML_TEXT_NODE) ||
26602 (nodeType == XML_CDATA_SECTION_NODE) ||
26603 (nodeType == WHTSP) ||
26604 (nodeType == SIGN_WHTSP)) {
26605 /*
26606 * Process character content.
26607 */
26608 xmlChar *value;
26609
26610 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26611 nodeType = XML_TEXT_NODE;
26612
26613 value = xmlTextReaderValue(vctxt->reader);
26614 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26615 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26616 if (! consumed)
26617 xmlFree(value);
26618 if (ret == -1) {
26619 VERROR_INT("xmlSchemaVReaderWalk",
26620 "calling xmlSchemaVPushText()");
26621 goto internal_error;
26622 }
26623 } else if ((nodeType == XML_ENTITY_NODE) ||
26624 (nodeType == XML_ENTITY_REF_NODE)) {
26625 /*
26626 * VAL TODO: What to do with entities?
26627 */
26628 TODO
26629 }
26630 /*
26631 * Read next node.
26632 */
26633 ret = xmlTextReaderRead(vctxt->reader);
26634 } while (ret == 1);
26635
26636exit:
26637 return (ret);
26638internal_error:
26639 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026640}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026641#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026642
26643/************************************************************************
26644 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026645 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026646 * *
26647 ************************************************************************/
26648
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026649/*
26650* Process text content.
26651*/
26652static void
26653xmlSchemaSAXHandleText(void *ctx,
26654 const xmlChar * ch,
26655 int len)
26656{
26657 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26658
26659 if (vctxt->depth < 0)
26660 return;
26661 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26662 return;
26663 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26664 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26665 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26666 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26667 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26668 "calling xmlSchemaVPushText()");
26669 vctxt->err = -1;
26670 xmlStopParser(vctxt->parserCtxt);
26671 }
26672}
26673
26674/*
26675* Process CDATA content.
26676*/
26677static void
26678xmlSchemaSAXHandleCDataSection(void *ctx,
26679 const xmlChar * ch,
26680 int len)
26681{
26682 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26683
26684 if (vctxt->depth < 0)
26685 return;
26686 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26687 return;
26688 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26689 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26690 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26691 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26692 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26693 "calling xmlSchemaVPushText()");
26694 vctxt->err = -1;
26695 xmlStopParser(vctxt->parserCtxt);
26696 }
26697}
26698
26699static void
26700xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26701 const xmlChar * name ATTRIBUTE_UNUSED)
26702{
26703 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26704
26705 if (vctxt->depth < 0)
26706 return;
26707 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26708 return;
26709 /* SAX VAL TODO: What to do here? */
26710 TODO
26711}
26712
26713static void
26714xmlSchemaSAXHandleStartElementNs(void *ctx,
26715 const xmlChar * localname,
26716 const xmlChar * prefix ATTRIBUTE_UNUSED,
26717 const xmlChar * URI,
26718 int nb_namespaces,
26719 const xmlChar ** namespaces,
26720 int nb_attributes,
26721 int nb_defaulted ATTRIBUTE_UNUSED,
26722 const xmlChar ** attributes)
26723{
26724 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26725 int ret;
26726 xmlSchemaNodeInfoPtr ielem;
26727 int i, j;
26728
26729 /*
26730 * SAX VAL TODO: What to do with nb_defaulted?
26731 */
26732 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026733 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026734 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026735 vctxt->depth++;
26736 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026737 return;
26738 /*
26739 * Push the element.
26740 */
26741 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26742 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26743 "calling xmlSchemaValidatorPushElem()");
26744 goto internal_error;
26745 }
26746 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026747 /*
26748 * TODO: Is this OK?
26749 */
26750 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026751 ielem->localName = localname;
26752 ielem->nsName = URI;
26753 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26754 /*
26755 * Register namespaces on the elem info.
26756 */
26757 if (nb_namespaces != 0) {
26758 /*
26759 * Although the parser builds its own namespace list,
26760 * we have no access to it, so we'll use an own one.
26761 */
26762 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26763 /*
26764 * Store prefix and namespace name.
26765 */
26766 if (ielem->nsBindings == NULL) {
26767 ielem->nsBindings =
26768 (const xmlChar **) xmlMalloc(10 *
26769 sizeof(const xmlChar *));
26770 if (ielem->nsBindings == NULL) {
26771 xmlSchemaVErrMemory(vctxt,
26772 "allocating namespace bindings for SAX validation",
26773 NULL);
26774 goto internal_error;
26775 }
26776 ielem->nbNsBindings = 0;
26777 ielem->sizeNsBindings = 5;
26778 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26779 ielem->sizeNsBindings *= 2;
26780 ielem->nsBindings =
26781 (const xmlChar **) xmlRealloc(
26782 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026783 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026784 if (ielem->nsBindings == NULL) {
26785 xmlSchemaVErrMemory(vctxt,
26786 "re-allocating namespace bindings for SAX validation",
26787 NULL);
26788 goto internal_error;
26789 }
26790 }
26791
26792 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26793 if (namespaces[j+1][0] == 0) {
26794 /*
26795 * Handle xmlns="".
26796 */
26797 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26798 } else
26799 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26800 namespaces[j+1];
26801 ielem->nbNsBindings++;
26802 }
26803 }
26804 /*
26805 * Register attributes.
26806 * SAX VAL TODO: We are not adding namespace declaration
26807 * attributes yet.
26808 */
26809 if (nb_attributes != 0) {
26810 xmlChar *value;
26811
26812 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26813 /*
26814 * Duplicate the value.
26815 */
26816 value = xmlStrndup(attributes[j+3],
26817 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026818 /*
26819 * TODO: Set the node line.
26820 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026821 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026822 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026823 value, 1);
26824 if (ret == -1) {
26825 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26826 "calling xmlSchemaValidatorPushAttribute()");
26827 goto internal_error;
26828 }
26829 }
26830 }
26831 /*
26832 * Validate the element.
26833 */
26834 ret = xmlSchemaValidateElem(vctxt);
26835 if (ret != 0) {
26836 if (ret == -1) {
26837 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26838 "calling xmlSchemaValidateElem()");
26839 goto internal_error;
26840 }
26841 goto exit;
26842 }
26843
26844exit:
26845 return;
26846internal_error:
26847 vctxt->err = -1;
26848 xmlStopParser(vctxt->parserCtxt);
26849 return;
26850}
26851
26852static void
26853xmlSchemaSAXHandleEndElementNs(void *ctx,
26854 const xmlChar * localname ATTRIBUTE_UNUSED,
26855 const xmlChar * prefix ATTRIBUTE_UNUSED,
26856 const xmlChar * URI ATTRIBUTE_UNUSED)
26857{
26858 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26859 int res;
26860
26861 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026862 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026863 */
26864 if (vctxt->skipDepth != -1) {
26865 if (vctxt->depth > vctxt->skipDepth) {
26866 vctxt->depth--;
26867 return;
26868 } else
26869 vctxt->skipDepth = -1;
26870 }
26871 /*
26872 * SAX VAL TODO: Just a temporary check.
26873 */
26874 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26875 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26876 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26877 "elem pop mismatch");
26878 }
26879 res = xmlSchemaValidatorPopElem(vctxt);
26880 if (res != 0) {
26881 if (res < 0) {
26882 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26883 "calling xmlSchemaValidatorPopElem()");
26884 goto internal_error;
26885 }
26886 goto exit;
26887 }
26888exit:
26889 return;
26890internal_error:
26891 vctxt->err = -1;
26892 xmlStopParser(vctxt->parserCtxt);
26893 return;
26894}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026895
Daniel Veillard4255d502002-04-16 15:50:10 +000026896/************************************************************************
26897 * *
26898 * Validation interfaces *
26899 * *
26900 ************************************************************************/
26901
26902/**
26903 * xmlSchemaNewValidCtxt:
26904 * @schema: a precompiled XML Schemas
26905 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026906 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026907 *
26908 * Returns the validation context or NULL in case of error
26909 */
26910xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026911xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26912{
Daniel Veillard4255d502002-04-16 15:50:10 +000026913 xmlSchemaValidCtxtPtr ret;
26914
26915 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26916 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026917 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026918 return (NULL);
26919 }
26920 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026921 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026922 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000026923 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026924 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000026925 return (ret);
26926}
26927
26928/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026929 * xmlSchemaClearValidCtxt:
26930 * @ctxt: the schema validation context
26931 *
26932 * Free the resources associated to the schema validation context;
26933 * leaves some fields alive intended for reuse of the context.
26934 */
26935static void
26936xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
26937{
26938 if (vctxt == NULL)
26939 return;
26940
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026941 /*
26942 * TODO: Should we clear the flags?
26943 * Might be problematic if one reuses the context
26944 * and assumes that the options remain the same.
26945 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026946 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026947 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026948 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026949#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026950 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026951#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026952 vctxt->hasKeyrefs = 0;
26953
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026954 if (vctxt->value != NULL) {
26955 xmlSchemaFreeValue(vctxt->value);
26956 vctxt->value = NULL;
26957 }
26958 /*
26959 * Augmented IDC information.
26960 */
26961 if (vctxt->aidcs != NULL) {
26962 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
26963 do {
26964 next = cur->next;
26965 xmlFree(cur);
26966 cur = next;
26967 } while (cur != NULL);
26968 vctxt->aidcs = NULL;
26969 }
26970 if (vctxt->idcNodes != NULL) {
26971 int i;
26972 xmlSchemaPSVIIDCNodePtr item;
26973
26974 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026975 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026976 xmlFree(item->keys);
26977 xmlFree(item);
26978 }
26979 xmlFree(vctxt->idcNodes);
26980 vctxt->idcNodes = NULL;
26981 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026982 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026983 * Note that we won't delete the XPath state pool here.
26984 */
26985 if (vctxt->xpathStates != NULL) {
26986 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
26987 vctxt->xpathStates = NULL;
26988 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026989 /*
26990 * Attribute info.
26991 */
26992 if (vctxt->nbAttrInfos != 0) {
26993 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026994 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026995 /*
26996 * Element info.
26997 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026998 if (vctxt->elemInfos != NULL) {
26999 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027000 xmlSchemaNodeInfoPtr ei;
27001
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027002 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027003 ei = vctxt->elemInfos[i];
27004 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027005 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027006 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027007 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027008 }
27009 xmlSchemaItemListClear(vctxt->nodeQNames);
27010 /* Recreate the dict. */
27011 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027012 /*
27013 * TODO: Is is save to recreate it? Do we have a scenario
27014 * where the user provides the dict?
27015 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027016 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027017}
27018
27019/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027020 * xmlSchemaFreeValidCtxt:
27021 * @ctxt: the schema validation context
27022 *
27023 * Free the resources associated to the schema validation context
27024 */
27025void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027026xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27027{
Daniel Veillard4255d502002-04-16 15:50:10 +000027028 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027029 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027030 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027031 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027032 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027033 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027034 if (ctxt->idcNodes != NULL) {
27035 int i;
27036 xmlSchemaPSVIIDCNodePtr item;
27037
27038 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027039 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027040 xmlFree(item->keys);
27041 xmlFree(item);
27042 }
27043 xmlFree(ctxt->idcNodes);
27044 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027045 if (ctxt->idcKeys != NULL) {
27046 int i;
27047 for (i = 0; i < ctxt->nbIdcKeys; i++)
27048 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27049 xmlFree(ctxt->idcKeys);
27050 }
27051
27052 if (ctxt->xpathStates != NULL)
27053 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27054 if (ctxt->xpathStatePool != NULL)
27055 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27056
27057 /*
27058 * Augmented IDC information.
27059 */
27060 if (ctxt->aidcs != NULL) {
27061 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27062 do {
27063 next = cur->next;
27064 xmlFree(cur);
27065 cur = next;
27066 } while (cur != NULL);
27067 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027068 if (ctxt->attrInfos != NULL) {
27069 int i;
27070 xmlSchemaAttrInfoPtr attr;
27071
27072 /* Just a paranoid call to the cleanup. */
27073 if (ctxt->nbAttrInfos != 0)
27074 xmlSchemaClearAttrInfos(ctxt);
27075 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27076 attr = ctxt->attrInfos[i];
27077 xmlFree(attr);
27078 }
27079 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027080 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027081 if (ctxt->elemInfos != NULL) {
27082 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027083 xmlSchemaNodeInfoPtr ei;
27084
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027085 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027086 ei = ctxt->elemInfos[i];
27087 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027088 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027089 xmlSchemaClearElemInfo(ei);
27090 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027091 }
27092 xmlFree(ctxt->elemInfos);
27093 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027094 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027095 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027096 if (ctxt->dict != NULL)
27097 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027098 xmlFree(ctxt);
27099}
27100
27101/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027102 * xmlSchemaIsValid:
27103 * @ctxt: the schema validation context
27104 *
27105 * Check if any error was detected during validation.
27106 *
27107 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27108 * of internal error.
27109 */
27110int
27111xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27112{
27113 if (ctxt == NULL)
27114 return(-1);
27115 return(ctxt->err == 0);
27116}
27117
27118/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027119 * xmlSchemaSetValidErrors:
27120 * @ctxt: a schema validation context
27121 * @err: the error function
27122 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027123 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027124 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027125 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027126 */
27127void
27128xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027129 xmlSchemaValidityErrorFunc err,
27130 xmlSchemaValidityWarningFunc warn, void *ctx)
27131{
Daniel Veillard4255d502002-04-16 15:50:10 +000027132 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027133 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027134 ctxt->error = err;
27135 ctxt->warning = warn;
27136 ctxt->userData = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027137 if (ctxt->pctxt != NULL)
27138 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027139}
27140
27141/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027142 * xmlSchemaSetValidStructuredErrors:
27143 * @ctxt: a schema validation context
27144 * @serror: the structured error function
27145 * @ctx: the functions context
27146 *
27147 * Set the structured error callback
27148 */
27149void
27150xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
27151 xmlStructuredErrorFunc serror, void *ctx)
27152{
27153 if (ctxt == NULL)
27154 return;
27155 ctxt->serror = serror;
27156 ctxt->error = NULL;
27157 ctxt->warning = NULL;
27158 ctxt->userData = ctx;
27159}
27160
27161/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027162 * xmlSchemaGetValidErrors:
27163 * @ctxt: a XML-Schema validation context
27164 * @err: the error function result
27165 * @warn: the warning function result
27166 * @ctx: the functions context result
27167 *
27168 * Get the error and warning callback informations
27169 *
27170 * Returns -1 in case of error and 0 otherwise
27171 */
27172int
27173xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
27174 xmlSchemaValidityErrorFunc * err,
27175 xmlSchemaValidityWarningFunc * warn, void **ctx)
27176{
27177 if (ctxt == NULL)
27178 return (-1);
27179 if (err != NULL)
27180 *err = ctxt->error;
27181 if (warn != NULL)
27182 *warn = ctxt->warning;
27183 if (ctx != NULL)
27184 *ctx = ctxt->userData;
27185 return (0);
27186}
27187
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027188
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027189/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027190 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027191 * @ctxt: a schema validation context
27192 * @options: a combination of xmlSchemaValidOption
27193 *
27194 * Sets the options to be used during the validation.
27195 *
27196 * Returns 0 in case of success, -1 in case of an
27197 * API error.
27198 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027199int
27200xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27201 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027202
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027203{
27204 int i;
27205
27206 if (ctxt == NULL)
27207 return (-1);
27208 /*
27209 * WARNING: Change the start value if adding to the
27210 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027211 * TODO: Is there an other, more easy to maintain,
27212 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027213 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027214 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027215 if (options & 1<<i)
27216 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027217 }
27218 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027219 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027220}
27221
27222/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027223 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027224 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027225 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027226 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027227 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027228 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027229 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027230int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027231xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027232
27233{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027234 if (ctxt == NULL)
27235 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027236 else
27237 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027238}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027239
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027240static int
27241xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27242{
27243 xmlAttrPtr attr;
27244 int ret = 0;
27245 xmlSchemaNodeInfoPtr ielem = NULL;
27246 xmlNodePtr node, valRoot;
27247 const xmlChar *nsName;
27248
27249 /* DOC VAL TODO: Move this to the start function. */
27250 valRoot = xmlDocGetRootElement(vctxt->doc);
27251 if (valRoot == NULL) {
27252 /* VAL TODO: Error code? */
27253 VERROR(1, NULL, "The document has no document element");
27254 return (1);
27255 }
27256 vctxt->depth = -1;
27257 vctxt->validationRoot = valRoot;
27258 node = valRoot;
27259 while (node != NULL) {
27260 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27261 goto next_sibling;
27262 if (node->type == XML_ELEMENT_NODE) {
27263
27264 /*
27265 * Init the node-info.
27266 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027267 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027268 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27269 goto internal_error;
27270 ielem = vctxt->inode;
27271 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027272 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027273 ielem->localName = node->name;
27274 if (node->ns != NULL)
27275 ielem->nsName = node->ns->href;
27276 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27277 /*
27278 * Register attributes.
27279 * DOC VAL TODO: We do not register namespace declaration
27280 * attributes yet.
27281 */
27282 vctxt->nbAttrInfos = 0;
27283 if (node->properties != NULL) {
27284 attr = node->properties;
27285 do {
27286 if (attr->ns != NULL)
27287 nsName = attr->ns->href;
27288 else
27289 nsName = NULL;
27290 ret = xmlSchemaValidatorPushAttribute(vctxt,
27291 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027292 /*
27293 * Note that we give it the line number of the
27294 * parent element.
27295 */
27296 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027297 attr->name, nsName, 0,
27298 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27299 if (ret == -1) {
27300 VERROR_INT("xmlSchemaDocWalk",
27301 "calling xmlSchemaValidatorPushAttribute()");
27302 goto internal_error;
27303 }
27304 attr = attr->next;
27305 } while (attr);
27306 }
27307 /*
27308 * Validate the element.
27309 */
27310 ret = xmlSchemaValidateElem(vctxt);
27311 if (ret != 0) {
27312 if (ret == -1) {
27313 VERROR_INT("xmlSchemaDocWalk",
27314 "calling xmlSchemaValidateElem()");
27315 goto internal_error;
27316 }
27317 /*
27318 * Don't stop validation; just skip the content
27319 * of this element.
27320 */
27321 goto leave_node;
27322 }
27323 if ((vctxt->skipDepth != -1) &&
27324 (vctxt->depth >= vctxt->skipDepth))
27325 goto leave_node;
27326 } else if ((node->type == XML_TEXT_NODE) ||
27327 (node->type == XML_CDATA_SECTION_NODE)) {
27328 /*
27329 * Process character content.
27330 */
27331 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27332 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27333 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27334 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27335 if (ret < 0) {
27336 VERROR_INT("xmlSchemaVDocWalk",
27337 "calling xmlSchemaVPushText()");
27338 goto internal_error;
27339 }
27340 /*
27341 * DOC VAL TODO: Should we skip further validation of the
27342 * element content here?
27343 */
27344 } else if ((node->type == XML_ENTITY_NODE) ||
27345 (node->type == XML_ENTITY_REF_NODE)) {
27346 /*
27347 * DOC VAL TODO: What to do with entities?
27348 */
27349 TODO
27350 } else {
27351 goto leave_node;
27352 /*
27353 * DOC VAL TODO: XInclude nodes, etc.
27354 */
27355 }
27356 /*
27357 * Walk the doc.
27358 */
27359 if (node->children != NULL) {
27360 node = node->children;
27361 continue;
27362 }
27363leave_node:
27364 if (node->type == XML_ELEMENT_NODE) {
27365 /*
27366 * Leaving the scope of an element.
27367 */
27368 if (node != vctxt->inode->node) {
27369 VERROR_INT("xmlSchemaVDocWalk",
27370 "element position mismatch");
27371 goto internal_error;
27372 }
27373 ret = xmlSchemaValidatorPopElem(vctxt);
27374 if (ret != 0) {
27375 if (ret < 0) {
27376 VERROR_INT("xmlSchemaVDocWalk",
27377 "calling xmlSchemaValidatorPopElem()");
27378 goto internal_error;
27379 }
27380 }
27381 if (node == valRoot)
27382 goto exit;
27383 }
27384next_sibling:
27385 if (node->next != NULL)
27386 node = node->next;
27387 else {
27388 node = node->parent;
27389 goto leave_node;
27390 }
27391 }
27392
27393exit:
27394 return (ret);
27395internal_error:
27396 return (-1);
27397}
27398
27399static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027400xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027401 /*
27402 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027403 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027404 vctxt->err = 0;
27405 vctxt->nberrors = 0;
27406 vctxt->depth = -1;
27407 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027408 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027409 vctxt->hasKeyrefs = 0;
27410#ifdef ENABLE_IDC_NODE_TABLES
27411 vctxt->createIDCNodeTables = 1;
27412#else
27413 vctxt->createIDCNodeTables = 0;
27414#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027415 /*
27416 * Create a schema + parser if necessary.
27417 */
27418 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027419 xmlSchemaParserCtxtPtr pctxt;
27420
27421 vctxt->xsiAssemble = 1;
27422 /*
27423 * If not schema was given then we will create a schema
27424 * dynamically using XSI schema locations.
27425 *
27426 * Create the schema parser context.
27427 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027428 if ((vctxt->pctxt == NULL) &&
27429 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27430 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027431 pctxt = vctxt->pctxt;
27432 pctxt->xsiAssemble = 1;
27433 /*
27434 * Create the schema.
27435 */
27436 vctxt->schema = xmlSchemaNewSchema(pctxt);
27437 if (vctxt->schema == NULL)
27438 return (-1);
27439 /*
27440 * Create the schema construction context.
27441 */
27442 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27443 if (pctxt->constructor == NULL)
27444 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027445 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027446 /*
27447 * Take ownership of the constructor to be able to free it.
27448 */
27449 pctxt->ownsConstructor = 1;
27450 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027451 /*
27452 * Augment the IDC definitions.
27453 */
27454 if (vctxt->schema->idcDef != NULL) {
27455 xmlHashScan(vctxt->schema->idcDef,
27456 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27457 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027458 return(0);
27459}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027460
Daniel Veillardf10ae122005-07-10 19:03:16 +000027461static void
27462xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027463 if (vctxt->xsiAssemble) {
27464 if (vctxt->schema != NULL) {
27465 xmlSchemaFree(vctxt->schema);
27466 vctxt->schema = NULL;
27467 }
27468 }
27469 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027470}
27471
27472static int
27473xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27474{
27475 int ret = 0;
27476
27477 if (xmlSchemaPreRun(vctxt) < 0)
27478 return(-1);
27479
27480 if (vctxt->doc != NULL) {
27481 /*
27482 * Tree validation.
27483 */
27484 ret = xmlSchemaVDocWalk(vctxt);
27485#ifdef LIBXML_READER_ENABLED
27486 } else if (vctxt->reader != NULL) {
27487 /*
27488 * XML Reader validation.
27489 */
27490#ifdef XML_SCHEMA_READER_ENABLED
27491 ret = xmlSchemaVReaderWalk(vctxt);
27492#endif
27493#endif
27494 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27495 /*
27496 * SAX validation.
27497 */
27498 ret = xmlParseDocument(vctxt->parserCtxt);
27499 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027500 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027501 "no instance to validate");
27502 ret = -1;
27503 }
27504
27505 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027506 if (ret == 0)
27507 ret = vctxt->err;
27508 return (ret);
27509}
27510
27511/**
27512 * xmlSchemaValidateOneElement:
27513 * @ctxt: a schema validation context
27514 * @elem: an element node
27515 *
27516 * Validate a branch of a tree, starting with the given @elem.
27517 *
27518 * Returns 0 if the element and its subtree is valid, a positive error
27519 * code number otherwise and -1 in case of an internal or API error.
27520 */
27521int
27522xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27523{
27524 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27525 return (-1);
27526
27527 if (ctxt->schema == NULL)
27528 return (-1);
27529
27530 ctxt->doc = elem->doc;
27531 ctxt->node = elem;
27532 ctxt->validationRoot = elem;
27533 return(xmlSchemaVStart(ctxt));
27534}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027535
Daniel Veillard259f0df2004-08-18 09:13:18 +000027536/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027537 * xmlSchemaValidateDoc:
27538 * @ctxt: a schema validation context
27539 * @doc: a parsed document tree
27540 *
27541 * Validate a document tree in memory.
27542 *
27543 * Returns 0 if the document is schemas valid, a positive error code
27544 * number otherwise and -1 in case of internal or API error.
27545 */
27546int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027547xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27548{
Daniel Veillard4255d502002-04-16 15:50:10 +000027549 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027550 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027551
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027552 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027553 ctxt->node = xmlDocGetRootElement(doc);
27554 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027555 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027556 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27557 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027558 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027559 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027560 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027561 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027562 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027563}
27564
Daniel Veillardcdc82732005-07-08 15:04:06 +000027565
27566/************************************************************************
27567 * *
27568 * Function and data for SAX streaming API *
27569 * *
27570 ************************************************************************/
27571typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27572typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27573
27574struct _xmlSchemaSplitSAXData {
27575 xmlSAXHandlerPtr user_sax;
27576 void *user_data;
27577 xmlSchemaValidCtxtPtr ctxt;
27578 xmlSAXHandlerPtr schemas_sax;
27579};
27580
Daniel Veillard971771e2005-07-09 17:32:57 +000027581#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27582
27583struct _xmlSchemaSAXPlug {
27584 unsigned int magic;
27585
27586 /* the original callbacks informations */
27587 xmlSAXHandlerPtr *user_sax_ptr;
27588 xmlSAXHandlerPtr user_sax;
27589 void **user_data_ptr;
27590 void *user_data;
27591
27592 /* the block plugged back and validation informations */
27593 xmlSAXHandler schemas_sax;
27594 xmlSchemaValidCtxtPtr ctxt;
27595};
27596
Daniel Veillardcdc82732005-07-08 15:04:06 +000027597/* All those functions just bounces to the user provided SAX handlers */
27598static void
27599internalSubsetSplit(void *ctx, const xmlChar *name,
27600 const xmlChar *ExternalID, const xmlChar *SystemID)
27601{
Daniel Veillard971771e2005-07-09 17:32:57 +000027602 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027603 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27604 (ctxt->user_sax->internalSubset != NULL))
27605 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27606 SystemID);
27607}
27608
27609static int
27610isStandaloneSplit(void *ctx)
27611{
Daniel Veillard971771e2005-07-09 17:32:57 +000027612 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027613 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27614 (ctxt->user_sax->isStandalone != NULL))
27615 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27616 return(0);
27617}
27618
27619static int
27620hasInternalSubsetSplit(void *ctx)
27621{
Daniel Veillard971771e2005-07-09 17:32:57 +000027622 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027623 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27624 (ctxt->user_sax->hasInternalSubset != NULL))
27625 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27626 return(0);
27627}
27628
27629static int
27630hasExternalSubsetSplit(void *ctx)
27631{
Daniel Veillard971771e2005-07-09 17:32:57 +000027632 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027633 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27634 (ctxt->user_sax->hasExternalSubset != NULL))
27635 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27636 return(0);
27637}
27638
27639static void
27640externalSubsetSplit(void *ctx, const xmlChar *name,
27641 const xmlChar *ExternalID, const xmlChar *SystemID)
27642{
Daniel Veillard971771e2005-07-09 17:32:57 +000027643 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027644 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27645 (ctxt->user_sax->internalSubset != NULL))
27646 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27647 SystemID);
27648}
27649
27650static xmlParserInputPtr
27651resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27652{
Daniel Veillard971771e2005-07-09 17:32:57 +000027653 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027654 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27655 (ctxt->user_sax->resolveEntity != NULL))
27656 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27657 systemId));
27658 return(NULL);
27659}
27660
27661static xmlEntityPtr
27662getEntitySplit(void *ctx, const xmlChar *name)
27663{
Daniel Veillard971771e2005-07-09 17:32:57 +000027664 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027665 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27666 (ctxt->user_sax->getEntity != NULL))
27667 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27668 return(NULL);
27669}
27670
27671static xmlEntityPtr
27672getParameterEntitySplit(void *ctx, const xmlChar *name)
27673{
Daniel Veillard971771e2005-07-09 17:32:57 +000027674 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027675 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27676 (ctxt->user_sax->getParameterEntity != NULL))
27677 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27678 return(NULL);
27679}
27680
27681
27682static void
27683entityDeclSplit(void *ctx, const xmlChar *name, int type,
27684 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27685{
Daniel Veillard971771e2005-07-09 17:32:57 +000027686 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027687 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27688 (ctxt->user_sax->entityDecl != NULL))
27689 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27690 systemId, content);
27691}
27692
27693static void
27694attributeDeclSplit(void *ctx, const xmlChar * elem,
27695 const xmlChar * name, int type, int def,
27696 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27697{
Daniel Veillard971771e2005-07-09 17:32:57 +000027698 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027699 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27700 (ctxt->user_sax->attributeDecl != NULL)) {
27701 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27702 def, defaultValue, tree);
27703 } else {
27704 xmlFreeEnumeration(tree);
27705 }
27706}
27707
27708static void
27709elementDeclSplit(void *ctx, const xmlChar *name, int type,
27710 xmlElementContentPtr content)
27711{
Daniel Veillard971771e2005-07-09 17:32:57 +000027712 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027713 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27714 (ctxt->user_sax->elementDecl != NULL))
27715 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27716}
27717
27718static void
27719notationDeclSplit(void *ctx, const xmlChar *name,
27720 const xmlChar *publicId, const xmlChar *systemId)
27721{
Daniel Veillard971771e2005-07-09 17:32:57 +000027722 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027723 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27724 (ctxt->user_sax->notationDecl != NULL))
27725 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27726 systemId);
27727}
27728
27729static void
27730unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27731 const xmlChar *publicId, const xmlChar *systemId,
27732 const xmlChar *notationName)
27733{
Daniel Veillard971771e2005-07-09 17:32:57 +000027734 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027735 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27736 (ctxt->user_sax->unparsedEntityDecl != NULL))
27737 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27738 systemId, notationName);
27739}
27740
27741static void
27742setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
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->setDocumentLocator != NULL))
27747 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27748}
27749
27750static void
27751startDocumentSplit(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->startDocument != NULL))
27756 ctxt->user_sax->startDocument(ctxt->user_data);
27757}
27758
27759static void
27760endDocumentSplit(void *ctx)
27761{
Daniel Veillard971771e2005-07-09 17:32:57 +000027762 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027763 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27764 (ctxt->user_sax->endDocument != NULL))
27765 ctxt->user_sax->endDocument(ctxt->user_data);
27766}
27767
27768static void
27769processingInstructionSplit(void *ctx, const xmlChar *target,
27770 const xmlChar *data)
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->processingInstruction != NULL))
27775 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27776}
27777
27778static void
27779commentSplit(void *ctx, const xmlChar *value)
27780{
Daniel Veillard971771e2005-07-09 17:32:57 +000027781 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027782 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27783 (ctxt->user_sax->comment != NULL))
27784 ctxt->user_sax->comment(ctxt->user_data, value);
27785}
27786
27787/*
27788 * Varargs error callbacks to the user application, harder ...
27789 */
27790
Daniel Veillardffa3c742005-07-21 13:24:09 +000027791static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027792warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027793 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027794 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27795 (ctxt->user_sax->warning != NULL)) {
27796 TODO
27797 }
27798}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027799static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027800errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027801 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027802 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27803 (ctxt->user_sax->error != NULL)) {
27804 TODO
27805 }
27806}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027807static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027808fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027809 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027810 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27811 (ctxt->user_sax->fatalError != NULL)) {
27812 TODO
27813 }
27814}
27815
27816/*
27817 * Those are function where both the user handler and the schemas handler
27818 * need to be called.
27819 */
27820static void
27821charactersSplit(void *ctx, const xmlChar *ch, int len)
27822{
Daniel Veillard971771e2005-07-09 17:32:57 +000027823 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027824 if (ctxt == NULL)
27825 return;
27826 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27827 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27828 if (ctxt->ctxt != NULL)
27829 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27830}
27831
27832static void
27833ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27834{
Daniel Veillard971771e2005-07-09 17:32:57 +000027835 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027836 if (ctxt == NULL)
27837 return;
27838 if ((ctxt->user_sax != NULL) &&
27839 (ctxt->user_sax->ignorableWhitespace != NULL))
27840 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27841 if (ctxt->ctxt != NULL)
27842 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27843}
27844
27845static void
27846cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27847{
Daniel Veillard971771e2005-07-09 17:32:57 +000027848 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027849 if (ctxt == NULL)
27850 return;
27851 if ((ctxt->user_sax != NULL) &&
27852 (ctxt->user_sax->ignorableWhitespace != NULL))
27853 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27854 if (ctxt->ctxt != NULL)
27855 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27856}
27857
27858static void
27859referenceSplit(void *ctx, const xmlChar *name)
27860{
Daniel Veillard971771e2005-07-09 17:32:57 +000027861 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027862 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27863 (ctxt->user_sax->reference != NULL))
27864 ctxt->user_sax->reference(ctxt->user_data, name);
27865 if (ctxt->ctxt != NULL)
27866 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27867}
27868
27869static void
27870startElementNsSplit(void *ctx, const xmlChar * localname,
27871 const xmlChar * prefix, const xmlChar * URI,
27872 int nb_namespaces, const xmlChar ** namespaces,
27873 int nb_attributes, int nb_defaulted,
27874 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027875 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027876 if (ctxt == NULL)
27877 return;
27878 if ((ctxt->user_sax != NULL) &&
27879 (ctxt->user_sax->startElementNs != NULL))
27880 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27881 URI, nb_namespaces, namespaces,
27882 nb_attributes, nb_defaulted,
27883 attributes);
27884 if (ctxt->ctxt != NULL)
27885 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27886 URI, nb_namespaces, namespaces,
27887 nb_attributes, nb_defaulted,
27888 attributes);
27889}
27890
27891static void
27892endElementNsSplit(void *ctx, const xmlChar * localname,
27893 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027894 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027895 if (ctxt == NULL)
27896 return;
27897 if ((ctxt->user_sax != NULL) &&
27898 (ctxt->user_sax->endElementNs != NULL))
27899 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27900 if (ctxt->ctxt != NULL)
27901 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27902}
27903
Daniel Veillard4255d502002-04-16 15:50:10 +000027904/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027905 * xmlSchemaSAXPlug:
27906 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027907 * @sax: a pointer to the original xmlSAXHandlerPtr
27908 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027909 *
27910 * Plug a SAX based validation layer in a SAX parsing event flow.
27911 * The original @saxptr and @dataptr data are replaced by new pointers
27912 * but the calls to the original will be maintained.
27913 *
27914 * Returns a pointer to a data structure needed to unplug the validation layer
27915 * or NULL in case of errors.
27916 */
27917xmlSchemaSAXPlugPtr
27918xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
27919 xmlSAXHandlerPtr *sax, void **user_data)
27920{
27921 xmlSchemaSAXPlugPtr ret;
27922 xmlSAXHandlerPtr old_sax;
27923
27924 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
27925 return(NULL);
27926
27927 /*
27928 * We only allow to plug into SAX2 event streams
27929 */
27930 old_sax = *sax;
27931 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
27932 return(NULL);
27933 if ((old_sax != NULL) &&
27934 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
27935 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
27936 return(NULL);
27937
27938 /*
27939 * everything seems right allocate the local data needed for that layer
27940 */
27941 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
27942 if (ret == NULL) {
27943 return(NULL);
27944 }
27945 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
27946 ret->magic = XML_SAX_PLUG_MAGIC;
27947 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
27948 ret->ctxt = ctxt;
27949 ret->user_sax_ptr = sax;
27950 ret->user_sax = old_sax;
27951 if (old_sax == NULL) {
27952 /*
27953 * go direct, no need for the split block and functions.
27954 */
27955 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
27956 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
27957 /*
27958 * Note that we use the same text-function for both, to prevent
27959 * the parser from testing for ignorable whitespace.
27960 */
27961 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
27962 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
27963
27964 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
27965 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
27966
27967 ret->user_data = ctxt;
27968 *user_data = ctxt;
27969 } else {
27970 /*
27971 * for each callback unused by Schemas initialize it to the Split
27972 * routine only if non NULL in the user block, this can speed up
27973 * things at the SAX level.
27974 */
27975 if (old_sax->internalSubset != NULL)
27976 ret->schemas_sax.internalSubset = internalSubsetSplit;
27977 if (old_sax->isStandalone != NULL)
27978 ret->schemas_sax.isStandalone = isStandaloneSplit;
27979 if (old_sax->hasInternalSubset != NULL)
27980 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
27981 if (old_sax->hasExternalSubset != NULL)
27982 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
27983 if (old_sax->resolveEntity != NULL)
27984 ret->schemas_sax.resolveEntity = resolveEntitySplit;
27985 if (old_sax->getEntity != NULL)
27986 ret->schemas_sax.getEntity = getEntitySplit;
27987 if (old_sax->entityDecl != NULL)
27988 ret->schemas_sax.entityDecl = entityDeclSplit;
27989 if (old_sax->notationDecl != NULL)
27990 ret->schemas_sax.notationDecl = notationDeclSplit;
27991 if (old_sax->attributeDecl != NULL)
27992 ret->schemas_sax.attributeDecl = attributeDeclSplit;
27993 if (old_sax->elementDecl != NULL)
27994 ret->schemas_sax.elementDecl = elementDeclSplit;
27995 if (old_sax->unparsedEntityDecl != NULL)
27996 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
27997 if (old_sax->setDocumentLocator != NULL)
27998 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
27999 if (old_sax->startDocument != NULL)
28000 ret->schemas_sax.startDocument = startDocumentSplit;
28001 if (old_sax->endDocument != NULL)
28002 ret->schemas_sax.endDocument = endDocumentSplit;
28003 if (old_sax->processingInstruction != NULL)
28004 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28005 if (old_sax->comment != NULL)
28006 ret->schemas_sax.comment = commentSplit;
28007 if (old_sax->warning != NULL)
28008 ret->schemas_sax.warning = warningSplit;
28009 if (old_sax->error != NULL)
28010 ret->schemas_sax.error = errorSplit;
28011 if (old_sax->fatalError != NULL)
28012 ret->schemas_sax.fatalError = fatalErrorSplit;
28013 if (old_sax->getParameterEntity != NULL)
28014 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28015 if (old_sax->externalSubset != NULL)
28016 ret->schemas_sax.externalSubset = externalSubsetSplit;
28017
28018 /*
28019 * the 6 schemas callback have to go to the splitter functions
28020 * Note that we use the same text-function for ignorableWhitespace
28021 * if possible, to prevent the parser from testing for ignorable
28022 * whitespace.
28023 */
28024 ret->schemas_sax.characters = charactersSplit;
28025 if ((old_sax->ignorableWhitespace != NULL) &&
28026 (old_sax->ignorableWhitespace != old_sax->characters))
28027 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28028 else
28029 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28030 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28031 ret->schemas_sax.reference = referenceSplit;
28032 ret->schemas_sax.startElementNs = startElementNsSplit;
28033 ret->schemas_sax.endElementNs = endElementNsSplit;
28034
28035 ret->user_data_ptr = user_data;
28036 ret->user_data = *user_data;
28037 *user_data = ret;
28038 }
28039
28040 /*
28041 * plug the pointers back.
28042 */
28043 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028044 ctxt->sax = *sax;
28045 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28046 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028047 return(ret);
28048}
28049
28050/**
28051 * xmlSchemaSAXUnplug:
28052 * @plug: a data structure returned by xmlSchemaSAXPlug
28053 *
28054 * Unplug a SAX based validation layer in a SAX parsing event flow.
28055 * The original pointers used in the call are restored.
28056 *
28057 * Returns 0 in case of success and -1 in case of failure.
28058 */
28059int
28060xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28061{
28062 xmlSAXHandlerPtr *sax;
28063 void **user_data;
28064
28065 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28066 return(-1);
28067 plug->magic = 0;
28068
Daniel Veillardf10ae122005-07-10 19:03:16 +000028069 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028070 /* restore the data */
28071 sax = plug->user_sax_ptr;
28072 *sax = plug->user_sax;
28073 if (plug->user_sax != NULL) {
28074 user_data = plug->user_data_ptr;
28075 *user_data = plug->user_data;
28076 }
28077
28078 /* free and return */
28079 xmlFree(plug);
28080 return(0);
28081}
28082
28083/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028084 * xmlSchemaValidateStream:
28085 * @ctxt: a schema validation context
28086 * @input: the input to use for reading the data
28087 * @enc: an optional encoding information
28088 * @sax: a SAX handler for the resulting events
28089 * @user_data: the context to provide to the SAX handler.
28090 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028091 * Validate an input based on a flow of SAX event from the parser
28092 * and forward the events to the @sax handler with the provided @user_data
28093 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028094 *
28095 * Returns 0 if the document is schemas valid, a positive error code
28096 * number otherwise and -1 in case of internal or API error.
28097 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028098int
Daniel Veillard4255d502002-04-16 15:50:10 +000028099xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028100 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28101 xmlSAXHandlerPtr sax, void *user_data)
28102{
Daniel Veillard971771e2005-07-09 17:32:57 +000028103 xmlSchemaSAXPlugPtr plug = NULL;
28104 xmlSAXHandlerPtr old_sax = NULL;
28105 xmlParserCtxtPtr pctxt = NULL;
28106 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028107 int ret;
28108
Daniel Veillard4255d502002-04-16 15:50:10 +000028109 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028110 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028111
Daniel Veillardcdc82732005-07-08 15:04:06 +000028112 /*
28113 * prepare the parser
28114 */
28115 pctxt = xmlNewParserCtxt();
28116 if (pctxt == NULL)
28117 return (-1);
28118 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028119 pctxt->sax = sax;
28120 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028121#if 0
28122 if (options)
28123 xmlCtxtUseOptions(pctxt, options);
28124#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028125 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028126
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028127 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028128 if (inputStream == NULL) {
28129 ret = -1;
28130 goto done;
28131 }
28132 inputPush(pctxt, inputStream);
28133 ctxt->parserCtxt = pctxt;
28134 ctxt->input = input;
28135
28136 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028137 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028138 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028139 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28140 if (plug == NULL) {
28141 ret = -1;
28142 goto done;
28143 }
28144 ctxt->input = input;
28145 ctxt->enc = enc;
28146 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028147 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28148 ret = xmlSchemaVStart(ctxt);
28149
28150 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28151 ret = ctxt->parserCtxt->errNo;
28152 if (ret == 0)
28153 ret = 1;
28154 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028155
28156done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028157 ctxt->parserCtxt = NULL;
28158 ctxt->sax = NULL;
28159 ctxt->input = NULL;
28160 if (plug != NULL) {
28161 xmlSchemaSAXUnplug(plug);
28162 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028163 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028164 if (pctxt != NULL) {
28165 pctxt->sax = old_sax;
28166 xmlFreeParserCtxt(pctxt);
28167 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028168 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028169}
28170
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028171/**
28172 * xmlSchemaValidateFile:
28173 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028174 * @filename: the URI of the instance
28175 * @options: a future set of options, currently unused
28176 *
28177 * Do a schemas validation of the given resource, it will use the
28178 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028179 *
28180 * Returns 0 if the document is valid, a positive error code
28181 * number otherwise and -1 in case of an internal or API error.
28182 */
28183int
28184xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028185 const char * filename,
28186 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028187{
28188 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028189 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028190
28191 if ((ctxt == NULL) || (filename == NULL))
28192 return (-1);
28193
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028194 input = xmlParserInputBufferCreateFilename(filename,
28195 XML_CHAR_ENCODING_NONE);
28196 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028197 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028198 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28199 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028200 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028201}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028202
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028203#define bottom_xmlschemas
28204#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028205#endif /* LIBXML_SCHEMAS_ENABLED */