blob: d8824de706b49194be1d83d4f82d69b0c9232a23 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * schemas.c : implementation of the XML Schema handling and
3 * schema validity checking
4 *
5 * See Copyright for the status of this software.
6 *
7 * Daniel Veillard <veillard@redhat.com>
8 */
9
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010/*
Daniel Veillardb0f397e2003-12-23 23:30:53 +000011 * TODO:
12 * - when types are redefined in includes, check that all
13 * types in the redef list are equal
14 * -> need a type equality operation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015 * - if we don't intend to use the schema for schemas, we
Daniel Veillard01fa6152004-06-29 17:04:39 +000016 * need to validate all schema attributes (ref, type, name)
17 * against their types.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018 * - Eliminate item creation for: ??
19 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020 * URGENT TODO:
21 * - For xsi-driven schema acquisition, augment the IDCs after every
22 * acquisition episode (xmlSchemaAugmentIDC).
23 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000024 * NOTES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025 * - Elimated item creation for: <restriction>, <extension>,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026 * <simpleContent>, <complexContent>, <list>, <union>
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000028 * PROBLEMS:
29 * - http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0337.html
30 * IDC XPath expression and chameleon includes: the targetNamespace is changed, so
31 * XPath will have trouble to resolve to this namespace, since not known.
32 *
33 *
34 * CONSTRAINTS:
35 *
36 * Schema Component Constraint:
37 * All Group Limited (cos-all-limited)
38 * Status: complete
39 * (1.2)
40 * In xmlSchemaGroupDefReferenceTermFixup() and
41 * (2)
42 * In xmlSchemaParseModelGroup()
43 * TODO: Actually this should go to component-level checks,
44 * but is done here due to performance. Move it to an other layer
45 * is schema construction via an API is implemented.
Daniel Veillardb0f397e2003-12-23 23:30:53 +000046 */
Daniel Veillard4255d502002-04-16 15:50:10 +000047#define IN_LIBXML
48#include "libxml.h"
49
50#ifdef LIBXML_SCHEMAS_ENABLED
51
52#include <string.h>
53#include <libxml/xmlmemory.h>
54#include <libxml/parser.h>
55#include <libxml/parserInternals.h>
56#include <libxml/hash.h>
Daniel Veillard5a872412002-05-22 06:40:27 +000057#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000058#include <libxml/xmlschemas.h>
59#include <libxml/schemasInternals.h>
60#include <libxml/xmlschemastypes.h>
61#include <libxml/xmlautomata.h>
62#include <libxml/xmlregexp.h>
Daniel Veillardbe9c6322003-11-22 20:37:51 +000063#include <libxml/dict.h>
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000064#include <libxml/encoding.h>
65#include <libxml/xmlIO.h>
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000066#ifdef LIBXML_PATTERN_ENABLED
67#include <libxml/pattern.h>
68#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000069#ifdef LIBXML_READER_ENABLED
70#include <libxml/xmlreader.h>
71#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000072
Daniel Veillarda84c0b32003-06-02 16:58:46 +000073/* #define DEBUG 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000074
Daniel Veillard82bbbd42003-05-11 20:16:09 +000075/* #define DEBUG_CONTENT 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000076
Daniel Veillard82bbbd42003-05-11 20:16:09 +000077/* #define DEBUG_TYPE 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000078
Daniel Veillard118aed72002-09-24 14:13:13 +000079/* #define DEBUG_CONTENT_REGEXP 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000080
Daniel Veillard4255d502002-04-16 15:50:10 +000081/* #define DEBUG_AUTOMATA 1 */
82
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000083/* #define DEBUG_IDC */
84
85/* #define DEBUG_IDC_NODE_TABLE */
86
87#ifdef DEBUG_IDC
88 #ifndef DEBUG_IDC_NODE_TABLE
89 #define DEBUG_IDC_NODE_TABLE
90 #endif
91#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000092
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000093/* #define ENABLE_PARTICLE_RESTRICTION 1 */
94
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000095#define ENABLE_REDEFINE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000096
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000097/* #define ENABLE_NAMED_LOCALS */
98
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000099/* #define ENABLE_IDC_NODE_TABLES_TEST */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000100
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000101#define DUMP_CONTENT_MODEL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000102
103#ifdef LIBXML_READER_ENABLED
104/* #define XML_SCHEMA_READER_ENABLED */
105#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000106
Daniel Veillard4255d502002-04-16 15:50:10 +0000107#define UNBOUNDED (1 << 30)
108#define TODO \
109 xmlGenericError(xmlGenericErrorContext, \
110 "Unimplemented block at %s:%d\n", \
111 __FILE__, __LINE__);
112
William M. Brack2f2a6632004-08-20 23:09:47 +0000113#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +0000114
Daniel Veillard4255d502002-04-16 15:50:10 +0000115/*
116 * The XML Schemas namespaces
117 */
118static const xmlChar *xmlSchemaNs = (const xmlChar *)
119 "http://www.w3.org/2001/XMLSchema";
120
121static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
122 "http://www.w3.org/2001/XMLSchema-instance";
123
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000124static const xmlChar *xmlNamespaceNs = (const xmlChar *)
125 "http://www.w3.org/2000/xmlns/";
126
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000127/*
128* Come casting macros.
129*/
130#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +0000131#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
132#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000133#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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000581 void *errCtxt; /* user specific error context */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000582 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
583 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Kasimier T. Buchcikdfbc33a2006-01-03 10:51:59 +0000584 int err;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000585 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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000958 void *errCtxt; /* user specific data block */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000959 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,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001050 xmlSchemaAbstractCtxtPtr 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. Buchcikf326e772005-10-20 11:06:00 +00001076xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001077 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++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001879 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001880 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001881 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001882 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001883 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001884 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001885 error, XML_ERR_ERROR, NULL, 0,
1886 (const char *) str1, (const char *) str2, NULL, 0, 0,
1887 msg, str1, str2);
1888}
1889
1890/**
1891 * xmlSchemaPErr2:
1892 * @ctxt: the parsing context
1893 * @node: the context node
1894 * @node: the current child
1895 * @error: the error code
1896 * @msg: the error message
1897 * @str1: extra data
1898 * @str2: extra data
1899 *
1900 * Handle a parser error
1901 */
1902static void
1903xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1904 xmlNodePtr child, int error,
1905 const char *msg, const xmlChar * str1, const xmlChar * str2)
1906{
1907 if (child != NULL)
1908 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1909 else
1910 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1911}
1912
Daniel Veillard01fa6152004-06-29 17:04:39 +00001913
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001914/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001915 * xmlSchemaPErrExt:
1916 * @ctxt: the parsing context
1917 * @node: the context node
1918 * @error: the error code
1919 * @strData1: extra data
1920 * @strData2: extra data
1921 * @strData3: extra data
1922 * @msg: the message
1923 * @str1: extra parameter for the message display
1924 * @str2: extra parameter for the message display
1925 * @str3: extra parameter for the message display
1926 * @str4: extra parameter for the message display
1927 * @str5: extra parameter for the message display
1928 *
1929 * Handle a parser error
1930 */
1931static void
1932xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1933 const xmlChar * strData1, const xmlChar * strData2,
1934 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1935 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1936 const xmlChar * str5)
1937{
1938
1939 xmlGenericErrorFunc channel = NULL;
1940 xmlStructuredErrorFunc schannel = NULL;
1941 void *data = NULL;
1942
1943 if (ctxt != NULL) {
1944 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001945 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001946 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001947 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001948 schannel = ctxt->serror;
1949 }
1950 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1951 error, XML_ERR_ERROR, NULL, 0,
1952 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001953 (const char *) strData3, 0, 0, msg, str1, str2,
1954 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001955}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001956
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001957/************************************************************************
1958 * *
1959 * Allround error functions *
1960 * *
1961 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001962
1963/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001964 * xmlSchemaVTypeErrMemory:
1965 * @node: a context node
1966 * @extra: extra informations
1967 *
1968 * Handle an out of memory condition
1969 */
1970static void
1971xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1972 const char *extra, xmlNodePtr node)
1973{
1974 if (ctxt != NULL) {
1975 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001976 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001977 }
1978 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1979 extra);
1980}
1981
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001982static void
1983xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1984 const char *msg, const xmlChar *str)
1985{
1986 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1987 msg, (const char *) str);
1988}
1989
1990#define WXS_ERROR_TYPE_ERROR 1
1991#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001992/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001993 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001994 * @ctxt: the validation context
1995 * @node: the context node
1996 * @error: the error code
1997 * @msg: the error message
1998 * @str1: extra data
1999 * @str2: extra data
2000 * @str3: extra data
2001 *
2002 * Handle a validation error
2003 */
2004static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002005xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002006 xmlErrorLevel errorLevel,
2007 int error, xmlNodePtr node, int line, const char *msg,
2008 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002009 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002010{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002011 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002012 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002013 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002014
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002015 if (ctxt != NULL) {
2016 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2017 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002018 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002019 if (errorLevel != XML_ERR_WARNING) {
2020 vctxt->nberrors++;
2021 vctxt->err = error;
2022 channel = vctxt->error;
2023 } else {
2024 channel = vctxt->warning;
2025 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002026 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002027 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002028
2029 /*
2030 * Error node. If we specify a line number, then
2031 * do not channel any node to the error function.
2032 */
2033 if (line == 0) {
2034 if ((node == NULL) &&
2035 (vctxt->depth >= 0) &&
2036 (vctxt->inode != NULL)) {
2037 node = vctxt->inode->node;
2038 }
2039 /*
2040 * Get filename and line if no node-tree.
2041 */
2042 if ((node == NULL) &&
2043 (vctxt->parserCtxt != NULL) &&
2044 (vctxt->parserCtxt->input != NULL)) {
2045 file = vctxt->parserCtxt->input->filename;
2046 line = vctxt->parserCtxt->input->line;
2047 }
2048 } else {
2049 /*
2050 * Override the given node's (if any) position
2051 * and channel only the given line number.
2052 */
2053 node = NULL;
2054 /*
2055 * Get filename.
2056 */
2057 if (vctxt->doc != NULL)
2058 file = (const char *) vctxt->doc->URL;
2059 else if ((vctxt->parserCtxt != NULL) &&
2060 (vctxt->parserCtxt->input != NULL))
2061 file = vctxt->parserCtxt->input->filename;
2062 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002063 __xmlRaiseError(schannel, channel, data, ctxt,
2064 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002065 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002066 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002067 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002068
2069 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2070 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002071 if (errorLevel != XML_ERR_WARNING) {
2072 pctxt->nberrors++;
2073 pctxt->err = error;
2074 channel = pctxt->error;
2075 } else {
2076 channel = pctxt->warning;
2077 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002078 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002079 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002080 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002081 node, XML_FROM_SCHEMASP, error,
2082 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002083 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002084 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002085 } else {
2086 TODO
2087 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002088 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002089}
2090
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002091/**
2092 * xmlSchemaErr3:
2093 * @ctxt: the validation context
2094 * @node: the context node
2095 * @error: the error code
2096 * @msg: the error message
2097 * @str1: extra data
2098 * @str2: extra data
2099 * @str3: extra data
2100 *
2101 * Handle a validation error
2102 */
2103static void
2104xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2105 int error, xmlNodePtr node, const char *msg,
2106 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2107{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002108 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2109 msg, str1, str2, str3, NULL);
2110}
2111
2112static void
2113xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2114 int error, xmlNodePtr node, const char *msg,
2115 const xmlChar *str1, const xmlChar *str2,
2116 const xmlChar *str3, const xmlChar *str4)
2117{
2118 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2119 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002120}
2121
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002122static void
2123xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2124 int error, xmlNodePtr node, const char *msg,
2125 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002126{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002127 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002128}
2129
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002130static xmlChar *
2131xmlSchemaFormatNodeForError(xmlChar ** msg,
2132 xmlSchemaAbstractCtxtPtr actxt,
2133 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002134{
2135 xmlChar *str = NULL;
2136
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002137 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002138 if ((node != NULL) &&
2139 (node->type != XML_ELEMENT_NODE) &&
2140 (node->type != XML_ATTRIBUTE_NODE))
2141 {
2142 /*
2143 * Don't try to format other nodes than element and
2144 * attribute nodes.
2145 * Play save and return an empty string.
2146 */
2147 *msg = xmlStrdup(BAD_CAST "");
2148 return(*msg);
2149 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002150 if (node != NULL) {
2151 /*
2152 * Work on tree nodes.
2153 */
2154 if (node->type == XML_ATTRIBUTE_NODE) {
2155 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002156
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002157 *msg = xmlStrdup(BAD_CAST "Element '");
2158 if (elem->ns != NULL)
2159 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2160 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002161 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002162 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2163 NULL, elem->name));
2164 FREE_AND_NULL(str);
2165 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2166 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2167 } else {
2168 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002169 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002170 if (node->ns != NULL)
2171 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2172 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002173 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002174 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2175 NULL, node->name));
2176 FREE_AND_NULL(str);
2177 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2178 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2179 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2180 /*
2181 * Work on node infos.
2182 */
2183 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2184 xmlSchemaNodeInfoPtr ielem =
2185 vctxt->elemInfos[vctxt->depth];
2186
2187 *msg = xmlStrdup(BAD_CAST "Element '");
2188 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2189 ielem->nsName, ielem->localName));
2190 FREE_AND_NULL(str);
2191 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2192 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2193 } else {
2194 *msg = xmlStrdup(BAD_CAST "Element '");
2195 }
2196 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2197 vctxt->inode->nsName, vctxt->inode->localName));
2198 FREE_AND_NULL(str);
2199 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002200 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2201 /*
2202 * Hmm, no node while parsing?
2203 * Return an empty string, in case NULL will break something.
2204 */
2205 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002206 } else {
2207 TODO
2208 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002209 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002210 /*
2211 * VAL TODO: The output of the given schema component is currently
2212 * disabled.
2213 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002214#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002215 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2216 *msg = xmlStrcat(*msg, BAD_CAST " [");
2217 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2218 NULL, type, NULL, 0));
2219 FREE_AND_NULL(str)
2220 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002221 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002222#endif
2223 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002224}
2225
Daniel Veillardc0826a72004-08-10 14:17:33 +00002226static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002227xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002228 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002229 const char *message,
2230 const xmlChar *str1,
2231 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002232{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002233 xmlChar *msg = NULL;
2234
Daniel Veillard14b56432006-03-09 18:41:40 +00002235 if (actxt == NULL)
2236 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002237 msg = xmlStrdup(BAD_CAST "Internal error: ");
2238 msg = xmlStrcat(msg, BAD_CAST funcName);
2239 msg = xmlStrcat(msg, BAD_CAST ", ");
2240 msg = xmlStrcat(msg, BAD_CAST message);
2241 msg = xmlStrcat(msg, BAD_CAST ".\n");
2242
2243 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2244 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002245 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002248 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002249 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002250
2251 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002252}
2253
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002254static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002255xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2256 const char *funcName,
2257 const char *message)
2258{
2259 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2260}
2261
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002262#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002263static void
2264xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2265 const char *funcName,
2266 const char *message,
2267 const xmlChar *str1,
2268 const xmlChar *str2)
2269{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002270 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002271 str1, str2);
2272}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002273#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002274
2275static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002276xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2277 xmlParserErrors error,
2278 xmlNodePtr node,
2279 xmlSchemaBasicItemPtr item,
2280 const char *message,
2281 const xmlChar *str1, const xmlChar *str2,
2282 const xmlChar *str3, const xmlChar *str4)
2283{
2284 xmlChar *msg = NULL;
2285
2286 if ((node == NULL) && (item != NULL) &&
2287 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2288 node = WXS_ITEM_NODE(item);
2289 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2290 msg = xmlStrcat(msg, BAD_CAST ": ");
2291 } else
2292 xmlSchemaFormatNodeForError(&msg, actxt, node);
2293 msg = xmlStrcat(msg, (const xmlChar *) message);
2294 msg = xmlStrcat(msg, BAD_CAST ".\n");
2295 xmlSchemaErr4(actxt, error, node,
2296 (const char *) msg, str1, str2, str3, str4);
2297 FREE_AND_NULL(msg)
2298}
2299
2300static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002301xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2302 xmlParserErrors error,
2303 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002304 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002305 const char *message,
2306 const xmlChar *str1,
2307 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002308{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002309 xmlSchemaCustomErr4(actxt, error, node, item,
2310 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002311}
2312
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002313
2314
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002315static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002316xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2317 xmlParserErrors error,
2318 xmlNodePtr node,
2319 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2320 const char *message,
2321 const xmlChar *str1,
2322 const xmlChar *str2,
2323 const xmlChar *str3)
2324{
2325 xmlChar *msg = NULL;
2326
2327 xmlSchemaFormatNodeForError(&msg, actxt, node);
2328 msg = xmlStrcat(msg, (const xmlChar *) message);
2329 msg = xmlStrcat(msg, BAD_CAST ".\n");
2330
2331 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002332 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2333 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002334
2335 FREE_AND_NULL(msg)
2336}
2337
2338
2339
2340static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002341xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2342 xmlParserErrors error,
2343 xmlSchemaPSVIIDCNodePtr idcNode,
2344 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2345 const char *message,
2346 const xmlChar *str1,
2347 const xmlChar *str2)
2348{
2349 xmlChar *msg = NULL, *qname = NULL;
2350
2351 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2352 msg = xmlStrcat(msg, (const xmlChar *) message);
2353 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002354 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002355 error, NULL, idcNode->nodeLine, (const char *) msg,
2356 xmlSchemaFormatQName(&qname,
2357 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2358 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002359 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002360 FREE_AND_NULL(qname);
2361 FREE_AND_NULL(msg);
2362}
2363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002364static int
2365xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2366 xmlNodePtr node)
2367{
2368 if (node != NULL)
2369 return (node->type);
2370 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2371 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2372 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2373 return (-1);
2374}
2375
2376static int
2377xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2378{
2379 switch (item->type) {
2380 case XML_SCHEMA_TYPE_COMPLEX:
2381 case XML_SCHEMA_TYPE_SIMPLE:
2382 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2383 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002384 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002385 case XML_SCHEMA_TYPE_GROUP:
2386 return (1);
2387 case XML_SCHEMA_TYPE_ELEMENT:
2388 if ( ((xmlSchemaElementPtr) item)->flags &
2389 XML_SCHEMAS_ELEM_GLOBAL)
2390 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002391 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002392 case XML_SCHEMA_TYPE_ATTRIBUTE:
2393 if ( ((xmlSchemaAttributePtr) item)->flags &
2394 XML_SCHEMAS_ATTR_GLOBAL)
2395 return(1);
2396 break;
2397 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002398 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002399 return(1);
2400 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002401 return (0);
2402}
2403
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002404static void
2405xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2406 xmlParserErrors error,
2407 xmlNodePtr node,
2408 const xmlChar *value,
2409 xmlSchemaTypePtr type,
2410 int displayValue)
2411{
2412 xmlChar *msg = NULL;
2413
2414 xmlSchemaFormatNodeForError(&msg, actxt, node);
2415
2416 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2417 XML_ATTRIBUTE_NODE))
2418 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2419 else
2420 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2421 "value of ");
2422
2423 if (! xmlSchemaIsGlobalItem(type))
2424 msg = xmlStrcat(msg, BAD_CAST "the local ");
2425 else
2426 msg = xmlStrcat(msg, BAD_CAST "the ");
2427
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002428 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002429 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002430 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002431 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002432 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002433 msg = xmlStrcat(msg, BAD_CAST "union type");
2434
2435 if (xmlSchemaIsGlobalItem(type)) {
2436 xmlChar *str = NULL;
2437 msg = xmlStrcat(msg, BAD_CAST " '");
2438 if (type->builtInType != 0) {
2439 msg = xmlStrcat(msg, BAD_CAST "xs:");
2440 msg = xmlStrcat(msg, type->name);
2441 } else
2442 msg = xmlStrcat(msg,
2443 xmlSchemaFormatQName(&str,
2444 type->targetNamespace, type->name));
2445 msg = xmlStrcat(msg, BAD_CAST "'");
2446 FREE_AND_NULL(str);
2447 }
2448 msg = xmlStrcat(msg, BAD_CAST ".\n");
2449 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2450 XML_ATTRIBUTE_NODE))
2451 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2452 else
2453 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2454 FREE_AND_NULL(msg)
2455}
2456
Daniel Veillardc0826a72004-08-10 14:17:33 +00002457static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002458xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2459 xmlSchemaNodeInfoPtr ni,
2460 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002461{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002462 if (node != NULL) {
2463 if (node->ns != NULL)
2464 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2465 else
2466 return (xmlSchemaFormatQName(str, NULL, node->name));
2467 } else if (ni != NULL)
2468 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2469 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002470}
2471
Daniel Veillardc0826a72004-08-10 14:17:33 +00002472static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002473xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2474 xmlParserErrors error,
2475 xmlSchemaAttrInfoPtr ni,
2476 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002477{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002478 xmlChar *msg = NULL, *str = NULL;
2479
2480 xmlSchemaFormatNodeForError(&msg, actxt, node);
2481 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2482 xmlSchemaErr(actxt, error, node, (const char *) msg,
2483 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2484 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002485 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002486 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002487}
2488
Daniel Veillardc0826a72004-08-10 14:17:33 +00002489static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002490xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2491 xmlParserErrors error,
2492 xmlNodePtr node,
2493 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002494 const char *message,
2495 int nbval,
2496 int nbneg,
2497 xmlChar **values)
2498{
2499 xmlChar *str = NULL, *msg = NULL;
2500 xmlChar *localName, *nsName;
2501 const xmlChar *cur, *end;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00002502 int i;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002503
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002505 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002506 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002507 /*
2508 * Note that is does not make sense to report that we have a
2509 * wildcard here, since the wildcard might be unfolded into
2510 * multiple transitions.
2511 */
2512 if (nbval + nbneg > 0) {
2513 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002514 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002515 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002516 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002517 nsName = NULL;
2518
2519 for (i = 0; i < nbval + nbneg; i++) {
2520 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002521 if (cur == NULL)
2522 continue;
2523 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2524 (cur[3] == ' ')) {
Daniel Veillard77005e62005-07-19 16:26:18 +00002525 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002526 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002527 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002528 /*
2529 * Get the local name.
2530 */
2531 localName = NULL;
2532
2533 end = cur;
2534 if (*end == '*') {
2535 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002536 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002537 } else {
2538 while ((*end != 0) && (*end != '|'))
2539 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002540 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002541 }
2542 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002543 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002544 /*
2545 * Skip "*|*" if they come with negated expressions, since
2546 * they represent the same negated wildcard.
2547 */
2548 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2549 /*
2550 * Get the namespace name.
2551 */
2552 cur = end;
2553 if (*end == '*') {
2554 nsName = xmlStrdup(BAD_CAST "{*}");
2555 } else {
2556 while (*end != 0)
2557 end++;
2558
2559 if (i >= nbval)
2560 nsName = xmlStrdup(BAD_CAST "{##other:");
2561 else
2562 nsName = xmlStrdup(BAD_CAST "{");
2563
2564 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2565 nsName = xmlStrcat(nsName, BAD_CAST "}");
2566 }
2567 str = xmlStrcat(str, BAD_CAST nsName);
2568 FREE_AND_NULL(nsName)
2569 } else {
2570 FREE_AND_NULL(localName);
2571 continue;
2572 }
2573 }
2574 str = xmlStrcat(str, BAD_CAST localName);
2575 FREE_AND_NULL(localName);
2576
2577 if (i < nbval + nbneg -1)
2578 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002579 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002580 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002581 msg = xmlStrcat(msg, BAD_CAST str);
2582 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002583 } else
2584 msg = xmlStrcat(msg, BAD_CAST "\n");
2585 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002586 xmlFree(msg);
2587}
2588
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002589static void
2590xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2591 xmlParserErrors error,
2592 xmlNodePtr node,
2593 const xmlChar *value,
2594 unsigned long length,
2595 xmlSchemaTypePtr type,
2596 xmlSchemaFacetPtr facet,
2597 const char *message,
2598 const xmlChar *str1,
2599 const xmlChar *str2)
2600{
2601 xmlChar *str = NULL, *msg = NULL;
2602 xmlSchemaTypeType facetType;
2603 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2604
2605 xmlSchemaFormatNodeForError(&msg, actxt, node);
2606 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2607 facetType = XML_SCHEMA_FACET_ENUMERATION;
2608 /*
2609 * If enumerations are validated, one must not expect the
2610 * facet to be given.
2611 */
2612 } else
2613 facetType = facet->type;
2614 msg = xmlStrcat(msg, BAD_CAST "[");
2615 msg = xmlStrcat(msg, BAD_CAST "facet '");
2616 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2617 msg = xmlStrcat(msg, BAD_CAST "'] ");
2618 if (message == NULL) {
2619 /*
2620 * Use a default message.
2621 */
2622 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2623 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2624 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2625
2626 char len[25], actLen[25];
2627
2628 /* FIXME, TODO: What is the max expected string length of the
2629 * this value?
2630 */
2631 if (nodeType == XML_ATTRIBUTE_NODE)
2632 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2633 else
2634 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2635
2636 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2637 snprintf(actLen, 24, "%lu", length);
2638
2639 if (facetType == XML_SCHEMA_FACET_LENGTH)
2640 msg = xmlStrcat(msg,
2641 BAD_CAST "this differs from the allowed length of '%s'.\n");
2642 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2643 msg = xmlStrcat(msg,
2644 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2645 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2646 msg = xmlStrcat(msg,
2647 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2648
2649 if (nodeType == XML_ATTRIBUTE_NODE)
2650 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2651 value, (const xmlChar *) actLen, (const xmlChar *) len);
2652 else
2653 xmlSchemaErr(actxt, error, node, (const char *) msg,
2654 (const xmlChar *) actLen, (const xmlChar *) len);
2655
2656 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2657 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2658 "of the set {%s}.\n");
2659 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2660 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2661 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2662 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2663 "by the pattern '%s'.\n");
2664 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2665 facet->value);
2666 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2667 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2668 "minimum value allowed ('%s').\n");
2669 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2670 facet->value);
2671 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2672 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2673 "maximum value allowed ('%s').\n");
2674 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2675 facet->value);
2676 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002677 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002678 "'%s'.\n");
2679 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2680 facet->value);
2681 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002682 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002683 "'%s'.\n");
2684 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2685 facet->value);
2686 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2687 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2688 "digits than are allowed ('%s').\n");
2689 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2690 facet->value);
2691 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2692 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2693 "digits than are allowed ('%s').\n");
2694 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2695 facet->value);
2696 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2697 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2698 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2699 } else {
2700 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2701 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2702 }
2703 } else {
2704 msg = xmlStrcat(msg, (const xmlChar *) message);
2705 msg = xmlStrcat(msg, BAD_CAST ".\n");
2706 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2707 }
2708 FREE_AND_NULL(str)
2709 xmlFree(msg);
2710}
2711
2712#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002713 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002714
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002715#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002716
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002717#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2718#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002719
2720#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2721
2722
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002723/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002724 * xmlSchemaPMissingAttrErr:
2725 * @ctxt: the schema validation context
2726 * @ownerDes: the designation of the owner
2727 * @ownerName: the name of the owner
2728 * @ownerItem: the owner as a schema object
2729 * @ownerElem: the owner as an element node
2730 * @node: the parent element node of the missing attribute node
2731 * @type: the corresponding type of the attribute node
2732 *
2733 * Reports an illegal attribute.
2734 */
2735static void
2736xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002737 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002738 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002739 xmlNodePtr ownerElem,
2740 const char *name,
2741 const char *message)
2742{
2743 xmlChar *des = NULL;
2744
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002745 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2746
Daniel Veillardc0826a72004-08-10 14:17:33 +00002747 if (message != NULL)
2748 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002749 else
2750 xmlSchemaPErr(ctxt, ownerElem, error,
2751 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002752 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002753 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002754}
2755
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002756
Daniel Veillardc0826a72004-08-10 14:17:33 +00002757/**
2758 * xmlSchemaPResCompAttrErr:
2759 * @ctxt: the schema validation context
2760 * @error: the error code
2761 * @ownerDes: the designation of the owner
2762 * @ownerItem: the owner as a schema object
2763 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002764 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002765 * @refName: the referenced local name
2766 * @refURI: the referenced namespace URI
2767 * @message: optional message
2768 *
2769 * Used to report QName attribute values that failed to resolve
2770 * to schema components.
2771 */
2772static void
2773xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002774 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002775 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002776 xmlNodePtr ownerElem,
2777 const char *name,
2778 const xmlChar *refName,
2779 const xmlChar *refURI,
2780 xmlSchemaTypeType refType,
2781 const char *refTypeStr)
2782{
2783 xmlChar *des = NULL, *strA = NULL;
2784
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002785 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002786 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002787 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002788 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002789 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002790 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2791 "%s.\n", BAD_CAST des, BAD_CAST name,
2792 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002793 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002794 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002795 FREE_AND_NULL(strA)
2796}
2797
William M. Brack2f2a6632004-08-20 23:09:47 +00002798/**
2799 * xmlSchemaPCustomAttrErr:
2800 * @ctxt: the schema parser context
2801 * @error: the error code
2802 * @ownerDes: the designation of the owner
2803 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002804 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002805 *
2806 * Reports an illegal attribute during the parse.
2807 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002808static void
2809xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002810 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002811 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002812 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002813 xmlAttrPtr attr,
2814 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002815{
2816 xmlChar *des = NULL;
2817
2818 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002819 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002820 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002821 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002822 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002823 } else
2824 des = *ownerDes;
Daniel Veillard14b56432006-03-09 18:41:40 +00002825 if (attr == NULL) {
2826 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2827 "%s, attribute '%s': %s.\n",
2828 BAD_CAST des, "Unknown", (const xmlChar *) msg, NULL, NULL);
2829 } else {
2830 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2831 "%s, attribute '%s': %s.\n",
2832 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2833 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002834 if (ownerDes == NULL)
2835 FREE_AND_NULL(des);
2836}
2837
2838/**
2839 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002840 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002841 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002842 * @ownerDes: the designation of the attribute's owner
2843 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002844 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002845 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002846 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002847 */
2848static void
2849xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002850 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002851 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002852 xmlAttrPtr attr)
2853{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002854 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002855
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002856 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2857 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2858 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2859 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2860 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002861 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002862 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002863}
2864
William M. Brack2f2a6632004-08-20 23:09:47 +00002865/**
2866 * xmlSchemaPCustomErr:
2867 * @ctxt: the schema parser context
2868 * @error: the error code
2869 * @itemDes: the designation of the schema item
2870 * @item: the schema item
2871 * @itemElem: the node of the schema item
2872 * @message: the error message
2873 * @str1: an optional param for the error message
2874 * @str2: an optional param for the error message
2875 * @str3: an optional param for the error message
2876 *
2877 * Reports an error during parsing.
2878 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002879static void
2880xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002881 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002882 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002883 xmlNodePtr itemElem,
2884 const char *message,
2885 const xmlChar *str1,
2886 const xmlChar *str2,
2887 const xmlChar *str3)
2888{
2889 xmlChar *des = NULL, *msg = NULL;
2890
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002891 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002892 msg = xmlStrdup(BAD_CAST "%s: ");
2893 msg = xmlStrcat(msg, (const xmlChar *) message);
2894 msg = xmlStrcat(msg, BAD_CAST ".\n");
2895 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002896 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002897 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002898 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002899 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002900 FREE_AND_NULL(msg);
2901}
2902
William M. Brack2f2a6632004-08-20 23:09:47 +00002903/**
2904 * xmlSchemaPCustomErr:
2905 * @ctxt: the schema parser context
2906 * @error: the error code
2907 * @itemDes: the designation of the schema item
2908 * @item: the schema item
2909 * @itemElem: the node of the schema item
2910 * @message: the error message
2911 * @str1: the optional param for the error message
2912 *
2913 * Reports an error during parsing.
2914 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002915static void
2916xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002917 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002918 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002919 xmlNodePtr itemElem,
2920 const char *message,
2921 const xmlChar *str1)
2922{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002923 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002924 str1, NULL, NULL);
2925}
2926
William M. Brack2f2a6632004-08-20 23:09:47 +00002927/**
2928 * xmlSchemaPAttrUseErr:
2929 * @ctxt: the schema parser context
2930 * @error: the error code
2931 * @itemDes: the designation of the schema type
2932 * @item: the schema type
2933 * @itemElem: the node of the schema type
2934 * @attr: the invalid schema attribute
2935 * @message: the error message
2936 * @str1: the optional param for the error message
2937 *
2938 * Reports an attribute use error during parsing.
2939 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002940static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002941xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002942 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002943 xmlNodePtr node,
2944 xmlSchemaBasicItemPtr ownerItem,
2945 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002946 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002947 const xmlChar *str1, const xmlChar *str2,
2948 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002949{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002950 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002951
2952 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2953 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002954 msg = xmlStrcat(msg,
2955 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002956 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002957 FREE_AND_NULL(str);
2958 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002959 msg = xmlStrcat(msg, (const xmlChar *) message);
2960 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002961 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2962 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002963 xmlFree(msg);
2964}
2965
William M. Brack2f2a6632004-08-20 23:09:47 +00002966/**
2967 * xmlSchemaPIllegalFacetAtomicErr:
2968 * @ctxt: the schema parser context
2969 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002970 * @type: the schema type
2971 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002972 * @facet: the illegal facet
2973 *
2974 * Reports an illegal facet for atomic simple types.
2975 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002976static void
2977xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002978 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002979 xmlSchemaTypePtr type,
2980 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002981 xmlSchemaFacetPtr facet)
2982{
2983 xmlChar *des = NULL, *strT = NULL;
2984
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002985 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2986 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002987 "%s: The facet '%s' is not allowed on types derived from the "
2988 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002989 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002990 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002991 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002992 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002993 FREE_AND_NULL(strT);
2994}
2995
William M. Brack2f2a6632004-08-20 23:09:47 +00002996/**
2997 * xmlSchemaPIllegalFacetListUnionErr:
2998 * @ctxt: the schema parser context
2999 * @error: the error code
3000 * @itemDes: the designation of the schema item involved
3001 * @item: the schema item involved
3002 * @facet: the illegal facet
3003 *
3004 * Reports an illegal facet for <list> and <union>.
3005 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003006static void
3007xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003008 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003009 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003010 xmlSchemaFacetPtr facet)
3011{
3012 xmlChar *des = NULL, *strT = NULL;
3013
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003014 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3015 type->node);
3016 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003017 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003018 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003019 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003020 FREE_AND_NULL(strT);
3021}
3022
3023/**
3024 * xmlSchemaPMutualExclAttrErr:
3025 * @ctxt: the schema validation context
3026 * @error: the error code
3027 * @elemDes: the designation of the parent element node
3028 * @attr: the bad attribute node
3029 * @type: the corresponding type of the attribute node
3030 *
3031 * Reports an illegal attribute.
3032 */
3033static void
3034xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3035 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003036 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003037 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003038 const char *name1,
3039 const char *name2)
3040{
3041 xmlChar *des = NULL;
3042
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003043 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003044 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003045 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003046 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003047 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003048}
3049
3050/**
3051 * xmlSchemaPSimpleTypeErr:
3052 * @ctxt: the schema validation context
3053 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003054 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003055 * @ownerDes: the designation of the owner
3056 * @ownerItem: the schema object if existent
3057 * @node: the validated node
3058 * @value: the validated value
3059 *
3060 * Reports a simple type validation error.
3061 * TODO: Should this report the value of an element as well?
3062 */
3063static void
3064xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3065 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003066 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003067 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003068 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003069 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003070 const xmlChar *value,
3071 const char *message,
3072 const xmlChar *str1,
3073 const xmlChar *str2)
3074{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003075 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003076
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003077 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003078 if (message == NULL) {
3079 /*
3080 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003081 */
3082 if (type != NULL) {
3083 if (node->type == XML_ATTRIBUTE_NODE)
3084 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3085 else
3086 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3087 "valid value of ");
3088 if (! xmlSchemaIsGlobalItem(type))
3089 msg = xmlStrcat(msg, BAD_CAST "the local ");
3090 else
3091 msg = xmlStrcat(msg, BAD_CAST "the ");
3092
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003093 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003094 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003095 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003096 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003097 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003098 msg = xmlStrcat(msg, BAD_CAST "union type");
3099
3100 if (xmlSchemaIsGlobalItem(type)) {
3101 xmlChar *str = NULL;
3102 msg = xmlStrcat(msg, BAD_CAST " '");
3103 if (type->builtInType != 0) {
3104 msg = xmlStrcat(msg, BAD_CAST "xs:");
3105 msg = xmlStrcat(msg, type->name);
3106 } else
3107 msg = xmlStrcat(msg,
3108 xmlSchemaFormatQName(&str,
3109 type->targetNamespace, type->name));
3110 msg = xmlStrcat(msg, BAD_CAST "'.");
3111 FREE_AND_NULL(str);
3112 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003113 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003114 if (node->type == XML_ATTRIBUTE_NODE)
3115 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3116 else
3117 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3118 "valid.");
3119 }
3120 if (expected) {
3121 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3122 msg = xmlStrcat(msg, BAD_CAST expected);
3123 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3124 } else
3125 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003126 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003127 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3128 else
3129 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3130 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003131 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003132 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003133 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003134 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003135 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003136 /* Cleanup. */
3137 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003138}
3139
William M. Brack2f2a6632004-08-20 23:09:47 +00003140/**
3141 * xmlSchemaPContentErr:
3142 * @ctxt: the schema parser context
3143 * @error: the error code
3144 * @onwerDes: the designation of the holder of the content
3145 * @ownerItem: the owner item of the holder of the content
3146 * @ownerElem: the node of the holder of the content
3147 * @child: the invalid child node
3148 * @message: the optional error message
3149 * @content: the optional string describing the correct content
3150 *
3151 * Reports an error concerning the content of a schema element.
3152 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003154xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003155 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003156 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003157 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003158 xmlNodePtr child,
3159 const char *message,
3160 const char *content)
3161{
3162 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003163
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003164 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003165 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003166 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3167 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003168 BAD_CAST des, BAD_CAST message);
3169 else {
3170 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003171 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3172 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003173 BAD_CAST des, BAD_CAST content);
3174 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003175 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3176 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003177 BAD_CAST des, NULL);
3178 }
3179 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003180 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003181}
3182
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003183/************************************************************************
3184 * *
3185 * Streamable error functions *
3186 * *
3187 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003188
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003189
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003190
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003191
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003192/************************************************************************
3193 * *
3194 * Validation helper functions *
3195 * *
3196 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003197
Daniel Veillardc0826a72004-08-10 14:17:33 +00003198
Daniel Veillard4255d502002-04-16 15:50:10 +00003199/************************************************************************
3200 * *
3201 * Allocation functions *
3202 * *
3203 ************************************************************************/
3204
3205/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003206 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003207 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003208 *
3209 * Allocate a new Schema structure.
3210 *
3211 * Returns the newly allocated structure or NULL in case or error
3212 */
3213static xmlSchemaPtr
3214xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3215{
3216 xmlSchemaPtr ret;
3217
3218 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3219 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003220 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003221 return (NULL);
3222 }
3223 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003224 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003225 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003226
3227 return (ret);
3228}
3229
3230/**
3231 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003232 *
3233 * Allocate a new Facet structure.
3234 *
3235 * Returns the newly allocated structure or NULL in case or error
3236 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003237xmlSchemaFacetPtr
3238xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003239{
3240 xmlSchemaFacetPtr ret;
3241
3242 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3243 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003244 return (NULL);
3245 }
3246 memset(ret, 0, sizeof(xmlSchemaFacet));
3247
3248 return (ret);
3249}
3250
3251/**
3252 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003253 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003254 * @node: a node
3255 *
3256 * Allocate a new annotation structure.
3257 *
3258 * Returns the newly allocated structure or NULL in case or error
3259 */
3260static xmlSchemaAnnotPtr
3261xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3262{
3263 xmlSchemaAnnotPtr ret;
3264
3265 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3266 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003267 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003268 return (NULL);
3269 }
3270 memset(ret, 0, sizeof(xmlSchemaAnnot));
3271 ret->content = node;
3272 return (ret);
3273}
3274
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003275static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003276xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003277{
3278 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003279
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003280 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3281 if (ret == NULL) {
3282 xmlSchemaPErrMemory(NULL,
3283 "allocating an item list structure", NULL);
3284 return (NULL);
3285 }
3286 memset(ret, 0, sizeof(xmlSchemaItemList));
3287 return (ret);
3288}
3289
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003290static void
3291xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3292{
3293 if (list->items != NULL) {
3294 xmlFree(list->items);
3295 list->items = NULL;
3296 }
3297 list->nbItems = 0;
3298 list->sizeItems = 0;
3299}
3300
3301static int
3302xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3303{
3304 if (list->items == NULL) {
3305 list->items = (void **) xmlMalloc(
3306 20 * sizeof(void *));
3307 if (list->items == NULL) {
3308 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3309 return(-1);
3310 }
3311 list->sizeItems = 20;
3312 } else if (list->sizeItems <= list->nbItems) {
3313 list->sizeItems *= 2;
3314 list->items = (void **) xmlRealloc(list->items,
3315 list->sizeItems * sizeof(void *));
3316 if (list->items == NULL) {
3317 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3318 list->sizeItems = 0;
3319 return(-1);
3320 }
3321 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003322 list->items[list->nbItems++] = item;
3323 return(0);
3324}
3325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003326static int
3327xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3328 int initialSize,
3329 void *item)
3330{
3331 if (list->items == NULL) {
3332 if (initialSize <= 0)
3333 initialSize = 1;
3334 list->items = (void **) xmlMalloc(
3335 initialSize * sizeof(void *));
3336 if (list->items == NULL) {
3337 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3338 return(-1);
3339 }
3340 list->sizeItems = initialSize;
3341 } else if (list->sizeItems <= list->nbItems) {
3342 list->sizeItems *= 2;
3343 list->items = (void **) xmlRealloc(list->items,
3344 list->sizeItems * sizeof(void *));
3345 if (list->items == NULL) {
3346 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3347 list->sizeItems = 0;
3348 return(-1);
3349 }
3350 }
3351 list->items[list->nbItems++] = item;
3352 return(0);
3353}
3354
3355static int
3356xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3357{
3358 if (list->items == NULL) {
3359 list->items = (void **) xmlMalloc(
3360 20 * sizeof(void *));
3361 if (list->items == NULL) {
3362 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3363 return(-1);
3364 }
3365 list->sizeItems = 20;
3366 } else if (list->sizeItems <= list->nbItems) {
3367 list->sizeItems *= 2;
3368 list->items = (void **) xmlRealloc(list->items,
3369 list->sizeItems * sizeof(void *));
3370 if (list->items == NULL) {
3371 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3372 list->sizeItems = 0;
3373 return(-1);
3374 }
3375 }
3376 /*
3377 * Just append if the index is greater/equal than the item count.
3378 */
3379 if (idx >= list->nbItems) {
3380 list->items[list->nbItems++] = item;
3381 } else {
3382 int i;
3383 for (i = list->nbItems; i > idx; i--)
3384 list->items[i] = list->items[i-1];
3385 list->items[idx] = item;
3386 list->nbItems++;
3387 }
3388 return(0);
3389}
3390
3391#if 0 /* enable if ever needed */
3392static int
3393xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3394 int initialSize,
3395 void *item,
3396 int idx)
3397{
3398 if (list->items == NULL) {
3399 if (initialSize <= 0)
3400 initialSize = 1;
3401 list->items = (void **) xmlMalloc(
3402 initialSize * sizeof(void *));
3403 if (list->items == NULL) {
3404 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3405 return(-1);
3406 }
3407 list->sizeItems = initialSize;
3408 } else if (list->sizeItems <= list->nbItems) {
3409 list->sizeItems *= 2;
3410 list->items = (void **) xmlRealloc(list->items,
3411 list->sizeItems * sizeof(void *));
3412 if (list->items == NULL) {
3413 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3414 list->sizeItems = 0;
3415 return(-1);
3416 }
3417 }
3418 /*
3419 * Just append if the index is greater/equal than the item count.
3420 */
3421 if (idx >= list->nbItems) {
3422 list->items[list->nbItems++] = item;
3423 } else {
3424 int i;
3425 for (i = list->nbItems; i > idx; i--)
3426 list->items[i] = list->items[i-1];
3427 list->items[idx] = item;
3428 list->nbItems++;
3429 }
3430 return(0);
3431}
3432#endif
3433
3434static int
3435xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3436{
3437 int i;
3438 if ((list->items == NULL) || (idx >= list->nbItems)) {
3439 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3440 "index error.\n");
3441 return(-1);
3442 }
3443
3444 if (list->nbItems == 1) {
3445 /* TODO: Really free the list? */
3446 xmlFree(list->items);
3447 list->items = NULL;
3448 list->nbItems = 0;
3449 list->sizeItems = 0;
3450 } else if (list->nbItems -1 == idx) {
3451 list->nbItems--;
3452 } else {
3453 for (i = idx; i < list->nbItems -1; i++)
3454 list->items[i] = list->items[i+1];
3455 list->nbItems--;
3456 }
3457 return(0);
3458}
3459
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003460/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003461 * xmlSchemaItemListFree:
3462 * @annot: a schema type structure
3463 *
3464 * Deallocate a annotation structure
3465 */
3466static void
3467xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3468{
3469 if (list == NULL)
3470 return;
3471 if (list->items != NULL)
3472 xmlFree(list->items);
3473 xmlFree(list);
3474}
3475
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003476static void
3477xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003478{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003479 if (bucket == NULL)
3480 return;
3481 if (bucket->globals != NULL) {
3482 xmlSchemaComponentListFree(bucket->globals);
3483 xmlSchemaItemListFree(bucket->globals);
3484 }
3485 if (bucket->locals != NULL) {
3486 xmlSchemaComponentListFree(bucket->locals);
3487 xmlSchemaItemListFree(bucket->locals);
3488 }
3489 if (bucket->relations != NULL) {
3490 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3491 do {
3492 prev = cur;
3493 cur = cur->next;
3494 xmlFree(prev);
3495 } while (cur != NULL);
3496 }
3497 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3498 xmlFreeDoc(bucket->doc);
3499 }
3500 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003501 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3502 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003503 }
3504 xmlFree(bucket);
3505}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003506
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003507static xmlSchemaBucketPtr
3508xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003509 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003510{
3511 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003512 int size;
3513 xmlSchemaPtr mainSchema;
3514
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003515 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003516 PERROR_INT("xmlSchemaBucketCreate",
3517 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003518 return(NULL);
3519 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003520 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003521 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003522 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003523 size = sizeof(xmlSchemaInclude);
3524 else
3525 size = sizeof(xmlSchemaImport);
3526 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3527 if (ret == NULL) {
3528 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3529 return(NULL);
3530 }
3531 memset(ret, 0, size);
3532 ret->targetNamespace = targetNamespace;
3533 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003534 ret->globals = xmlSchemaItemListCreate();
3535 if (ret->globals == NULL) {
3536 xmlFree(ret);
3537 return(NULL);
3538 }
3539 ret->locals = xmlSchemaItemListCreate();
3540 if (ret->locals == NULL) {
3541 xmlFree(ret);
3542 return(NULL);
3543 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003544 /*
3545 * The following will assure that only the first bucket is marked as
3546 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3547 * For each following import buckets an xmlSchema will be created.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003548 * An xmlSchema will be created for every distinct targetNamespace.
3549 * We assign the targetNamespace to the schemata here.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003550 */
3551 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003552 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003553 PERROR_INT("xmlSchemaBucketCreate",
3554 "first bucket but it's an include or redefine");
3555 xmlSchemaBucketFree(ret);
3556 return(NULL);
3557 }
3558 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3559 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3560 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003561 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3562 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003563 /*
3564 * Ensure that the main schema gets a targetNamespace.
3565 */
3566 mainSchema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003567 } else {
3568 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3569 PERROR_INT("xmlSchemaBucketCreate",
3570 "main bucket but it's not the first one");
3571 xmlSchemaBucketFree(ret);
3572 return(NULL);
3573 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3574 /*
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003575 * Create a schema for imports and assign the
3576 * targetNamespace.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003577 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003578 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3579 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003580 xmlSchemaBucketFree(ret);
3581 return(NULL);
3582 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003583 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003584 }
3585 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003586 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003587 int res;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003588 /*
3589 * Imports go into the "schemasImports" slot of the main *schema*.
3590 * Note that we create an import entry for the main schema as well; i.e.,
3591 * even if there's only one schema, we'll get an import.
3592 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003593 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003594 mainSchema->schemasImports = xmlHashCreateDict(5,
3595 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003596 if (mainSchema->schemasImports == NULL) {
3597 xmlSchemaBucketFree(ret);
3598 return(NULL);
3599 }
3600 }
3601 if (targetNamespace == NULL)
3602 res = xmlHashAddEntry(mainSchema->schemasImports,
3603 XML_SCHEMAS_NO_NAMESPACE, ret);
3604 else
3605 res = xmlHashAddEntry(mainSchema->schemasImports,
3606 targetNamespace, ret);
3607 if (res != 0) {
3608 PERROR_INT("xmlSchemaBucketCreate",
3609 "failed to add the schema bucket to the hash");
3610 xmlSchemaBucketFree(ret);
3611 return(NULL);
3612 }
3613 } else {
3614 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003615 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3616 WXS_INCBUCKET(ret)->ownerImport =
3617 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003618 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003619 WXS_INCBUCKET(ret)->ownerImport =
3620 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003621
3622 /* Includes got into the "includes" slot of the *main* schema. */
3623 if (mainSchema->includes == NULL) {
3624 mainSchema->includes = xmlSchemaItemListCreate();
3625 if (mainSchema->includes == NULL) {
3626 xmlSchemaBucketFree(ret);
3627 return(NULL);
3628 }
3629 }
3630 xmlSchemaItemListAdd(mainSchema->includes, ret);
3631 }
3632 /*
3633 * Add to list of all buckets; this is used for lookup
3634 * during schema construction time only.
3635 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003636 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003637 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003638 return(ret);
3639}
3640
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003641static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003642xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003643{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003644 if (*list == NULL) {
3645 *list = xmlSchemaItemListCreate();
3646 if (*list == NULL)
3647 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003648 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003649 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003650 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003651}
3652
3653/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003654 * xmlSchemaFreeAnnot:
3655 * @annot: a schema type structure
3656 *
3657 * Deallocate a annotation structure
3658 */
3659static void
3660xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3661{
3662 if (annot == NULL)
3663 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003664 if (annot->next == NULL) {
3665 xmlFree(annot);
3666 } else {
3667 xmlSchemaAnnotPtr prev;
3668
3669 do {
3670 prev = annot;
3671 annot = annot->next;
3672 xmlFree(prev);
3673 } while (annot != NULL);
3674 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003675}
3676
3677/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003678 * xmlSchemaFreeNotation:
3679 * @schema: a schema notation structure
3680 *
3681 * Deallocate a Schema Notation structure.
3682 */
3683static void
3684xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3685{
3686 if (nota == NULL)
3687 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003688 xmlFree(nota);
3689}
3690
3691/**
3692 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003693 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003694 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003695 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003696 */
3697static void
3698xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3699{
3700 if (attr == NULL)
3701 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003702 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003703 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003704 if (attr->defVal != NULL)
3705 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003706 xmlFree(attr);
3707}
3708
3709/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003710 * xmlSchemaFreeAttributeUse:
3711 * @use: an attribute use
3712 *
3713 * Deallocates an attribute use structure.
3714 */
3715static void
3716xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3717{
3718 if (use == NULL)
3719 return;
3720 if (use->annot != NULL)
3721 xmlSchemaFreeAnnot(use->annot);
3722 if (use->defVal != NULL)
3723 xmlSchemaFreeValue(use->defVal);
3724 xmlFree(use);
3725}
3726
3727/**
3728 * xmlSchemaFreeAttributeUseProhib:
3729 * @prohib: an attribute use prohibition
3730 *
3731 * Deallocates an attribute use structure.
3732 */
3733static void
3734xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3735{
3736 if (prohib == NULL)
3737 return;
3738 xmlFree(prohib);
3739}
3740
3741/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003742 * xmlSchemaFreeWildcardNsSet:
3743 * set: a schema wildcard namespace
3744 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003745 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003746 */
3747static void
3748xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3749{
3750 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003751
Daniel Veillard3646d642004-06-02 19:19:14 +00003752 while (set != NULL) {
3753 next = set->next;
3754 xmlFree(set);
3755 set = next;
3756 }
3757}
3758
3759/**
3760 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003761 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003762 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003763 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003764 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003765void
Daniel Veillard3646d642004-06-02 19:19:14 +00003766xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3767{
3768 if (wildcard == NULL)
3769 return;
3770 if (wildcard->annot != NULL)
3771 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003772 if (wildcard->nsSet != NULL)
3773 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3774 if (wildcard->negNsSet != NULL)
3775 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003776 xmlFree(wildcard);
3777}
3778
3779/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003780 * xmlSchemaFreeAttributeGroup:
3781 * @schema: a schema attribute group structure
3782 *
3783 * Deallocate a Schema Attribute Group structure.
3784 */
3785static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003786xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003787{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003788 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003789 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003790 if (attrGr->annot != NULL)
3791 xmlSchemaFreeAnnot(attrGr->annot);
3792 if (attrGr->attrUses != NULL)
3793 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3794 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003795}
3796
3797/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003798 * xmlSchemaFreeQNameRef:
3799 * @item: a QName reference structure
3800 *
3801 * Deallocatea a QName reference structure.
3802 */
3803static void
3804xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3805{
3806 xmlFree(item);
3807}
3808
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003809/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003810 * xmlSchemaFreeTypeLinkList:
3811 * @alink: a type link
3812 *
3813 * Deallocate a list of types.
3814 */
3815static void
3816xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3817{
3818 xmlSchemaTypeLinkPtr next;
3819
3820 while (link != NULL) {
3821 next = link->next;
3822 xmlFree(link);
3823 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003824 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003825}
3826
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003827static void
3828xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3829{
3830 xmlSchemaIDCStateObjPtr next;
3831 while (sto != NULL) {
3832 next = sto->next;
3833 if (sto->history != NULL)
3834 xmlFree(sto->history);
3835 if (sto->xpathCtxt != NULL)
3836 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3837 xmlFree(sto);
3838 sto = next;
3839 }
3840}
3841
3842/**
3843 * xmlSchemaFreeIDC:
3844 * @idc: a identity-constraint definition
3845 *
3846 * Deallocates an identity-constraint definition.
3847 */
3848static void
3849xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3850{
3851 xmlSchemaIDCSelectPtr cur, prev;
3852
3853 if (idcDef == NULL)
3854 return;
3855 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003856 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003857 /* Selector */
3858 if (idcDef->selector != NULL) {
3859 if (idcDef->selector->xpathComp != NULL)
3860 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3861 xmlFree(idcDef->selector);
3862 }
3863 /* Fields */
3864 if (idcDef->fields != NULL) {
3865 cur = idcDef->fields;
3866 do {
3867 prev = cur;
3868 cur = cur->next;
3869 if (prev->xpathComp != NULL)
3870 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003871 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003872 } while (cur != NULL);
3873 }
3874 xmlFree(idcDef);
3875}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003876
Daniel Veillard01fa6152004-06-29 17:04:39 +00003877/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003878 * xmlSchemaFreeElement:
3879 * @schema: a schema element structure
3880 *
3881 * Deallocate a Schema Element structure.
3882 */
3883static void
3884xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3885{
3886 if (elem == NULL)
3887 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003888 if (elem->annot != NULL)
3889 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003890 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003891 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003892 if (elem->defVal != NULL)
3893 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003894 xmlFree(elem);
3895}
3896
3897/**
3898 * xmlSchemaFreeFacet:
3899 * @facet: a schema facet structure
3900 *
3901 * Deallocate a Schema Facet structure.
3902 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003903void
Daniel Veillard4255d502002-04-16 15:50:10 +00003904xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3905{
3906 if (facet == NULL)
3907 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003908 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003909 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003910 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003911 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003912 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003913 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003914 xmlFree(facet);
3915}
3916
3917/**
3918 * xmlSchemaFreeType:
3919 * @type: a schema type structure
3920 *
3921 * Deallocate a Schema Type structure.
3922 */
3923void
3924xmlSchemaFreeType(xmlSchemaTypePtr type)
3925{
3926 if (type == NULL)
3927 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003928 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003929 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003930 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003931 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003932
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003933 facet = type->facets;
3934 while (facet != NULL) {
3935 next = facet->next;
3936 xmlSchemaFreeFacet(facet);
3937 facet = next;
3938 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003939 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003940 if (type->attrUses != NULL)
3941 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003942 if (type->memberTypes != NULL)
3943 xmlSchemaFreeTypeLinkList(type->memberTypes);
3944 if (type->facetSet != NULL) {
3945 xmlSchemaFacetLinkPtr next, link;
3946
3947 link = type->facetSet;
3948 do {
3949 next = link->next;
3950 xmlFree(link);
3951 link = next;
3952 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003953 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003954 if (type->contModel != NULL)
3955 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003956 xmlFree(type);
3957}
3958
3959/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003960 * xmlSchemaFreeModelGroupDef:
3961 * @item: a schema model group definition
3962 *
3963 * Deallocates a schema model group definition.
3964 */
3965static void
3966xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3967{
3968 if (item->annot != NULL)
3969 xmlSchemaFreeAnnot(item->annot);
3970 xmlFree(item);
3971}
3972
3973/**
3974 * xmlSchemaFreeModelGroup:
3975 * @item: a schema model group
3976 *
3977 * Deallocates a schema model group structure.
3978 */
3979static void
3980xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3981{
3982 if (item->annot != NULL)
3983 xmlSchemaFreeAnnot(item->annot);
3984 xmlFree(item);
3985}
3986
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003987static void
3988xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3989{
3990 if ((list == NULL) || (list->nbItems == 0))
3991 return;
3992 {
3993 xmlSchemaTreeItemPtr item;
3994 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3995 int i;
3996
3997 for (i = 0; i < list->nbItems; i++) {
3998 item = items[i];
3999 if (item == NULL)
4000 continue;
4001 switch (item->type) {
4002 case XML_SCHEMA_TYPE_SIMPLE:
4003 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004004 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004005 break;
4006 case XML_SCHEMA_TYPE_ATTRIBUTE:
4007 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004008 break;
4009 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
4010 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
4011 break;
4012 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
4013 xmlSchemaFreeAttributeUseProhib(
4014 (xmlSchemaAttributeUseProhibPtr) item);
4015 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004016 case XML_SCHEMA_TYPE_ELEMENT:
4017 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4018 break;
4019 case XML_SCHEMA_TYPE_PARTICLE:
4020 if (item->annot != NULL)
4021 xmlSchemaFreeAnnot(item->annot);
4022 xmlFree(item);
4023 break;
4024 case XML_SCHEMA_TYPE_SEQUENCE:
4025 case XML_SCHEMA_TYPE_CHOICE:
4026 case XML_SCHEMA_TYPE_ALL:
4027 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4028 break;
4029 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4030 xmlSchemaFreeAttributeGroup(
4031 (xmlSchemaAttributeGroupPtr) item);
4032 break;
4033 case XML_SCHEMA_TYPE_GROUP:
4034 xmlSchemaFreeModelGroupDef(
4035 (xmlSchemaModelGroupDefPtr) item);
4036 break;
4037 case XML_SCHEMA_TYPE_ANY:
4038 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4039 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4040 break;
4041 case XML_SCHEMA_TYPE_IDC_KEY:
4042 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4043 case XML_SCHEMA_TYPE_IDC_KEYREF:
4044 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4045 break;
4046 case XML_SCHEMA_TYPE_NOTATION:
4047 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4048 break;
4049 case XML_SCHEMA_EXTRA_QNAMEREF:
4050 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4051 break;
4052 default: {
4053 /* TODO: This should never be hit. */
4054 xmlSchemaPSimpleInternalErr(NULL,
4055 "Internal error: xmlSchemaComponentListFree, "
4056 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004057 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004058 }
4059 break;
4060 }
4061 }
4062 list->nbItems = 0;
4063 }
4064}
4065
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004066/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004067 * xmlSchemaFree:
4068 * @schema: a schema structure
4069 *
4070 * Deallocate a Schema structure.
4071 */
4072void
4073xmlSchemaFree(xmlSchemaPtr schema)
4074{
4075 if (schema == NULL)
4076 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004077 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004078 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004079 TODO
4080 /*
4081 * Note that those slots are not responsible for freeing
4082 * schema components anymore; this will now be done by
4083 * the schema buckets.
4084 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004085 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004086 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004087 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004088 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004089 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004090 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004091 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004092 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004093 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004094 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004095 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004096 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004097 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004098 xmlHashFree(schema->idcDef, NULL);
4099
Daniel Veillard1d913862003-11-21 00:28:39 +00004100 if (schema->schemasImports != NULL)
4101 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004102 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004103 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004104 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4105 int i;
4106 for (i = 0; i < list->nbItems; i++) {
4107 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4108 }
4109 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004110 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004111 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004112 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004113 /* Never free the doc here, since this will be done by the buckets. */
4114
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004115 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004116 xmlFree(schema);
4117}
4118
4119/************************************************************************
4120 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004121 * Debug functions *
4122 * *
4123 ************************************************************************/
4124
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004125#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004126
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004127static void
4128xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4129
Daniel Veillard4255d502002-04-16 15:50:10 +00004130/**
4131 * xmlSchemaElementDump:
4132 * @elem: an element
4133 * @output: the file output
4134 *
4135 * Dump the element
4136 */
4137static void
4138xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004139 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004140 const xmlChar * namespace ATTRIBUTE_UNUSED,
4141 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004142{
4143 if (elem == NULL)
4144 return;
4145
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004146
4147 fprintf(output, "Element");
4148 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4149 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004150 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004151 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004152 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004153 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004154#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004155 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004156 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004157 if (elem->maxOccurs >= UNBOUNDED)
4158 fprintf(output, "max: unbounded\n");
4159 else if (elem->maxOccurs != 1)
4160 fprintf(output, "max: %d\n", elem->maxOccurs);
4161 else
4162 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004163 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004164#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004165 /*
4166 * Misc other properties.
4167 */
4168 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4169 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4170 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004171 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004172 fprintf(output, " props: ");
4173 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4174 fprintf(output, "[fixed] ");
4175 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4176 fprintf(output, "[default] ");
4177 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4178 fprintf(output, "[abstract] ");
4179 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4180 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004181 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004182 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004183 /*
4184 * Default/fixed value.
4185 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004186 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004187 fprintf(output, " value: '%s'\n", elem->value);
4188 /*
4189 * Type.
4190 */
4191 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004192 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004193 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004194 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004195 else
4196 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004197 } else if (elem->subtypes != NULL) {
4198 /*
4199 * Dump local types.
4200 */
4201 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004202 }
4203 /*
4204 * Substitution group.
4205 */
4206 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004207 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004208 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004209 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004210 else
4211 fprintf(output, "\n");
4212 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004213}
4214
4215/**
4216 * xmlSchemaAnnotDump:
4217 * @output: the file output
4218 * @annot: a annotation
4219 *
4220 * Dump the annotation
4221 */
4222static void
4223xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4224{
4225 xmlChar *content;
4226
4227 if (annot == NULL)
4228 return;
4229
4230 content = xmlNodeGetContent(annot->content);
4231 if (content != NULL) {
4232 fprintf(output, " Annot: %s\n", content);
4233 xmlFree(content);
4234 } else
4235 fprintf(output, " Annot: empty\n");
4236}
4237
4238/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004239 * xmlSchemaContentModelDump:
4240 * @particle: the schema particle
4241 * @output: the file output
4242 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004243 *
4244 * Dump a SchemaType structure
4245 */
4246static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004247xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4248{
4249 xmlChar *str = NULL;
4250 xmlSchemaTreeItemPtr term;
4251 char shift[100];
4252 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004253
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004254 if (particle == NULL)
4255 return;
4256 for (i = 0;((i < depth) && (i < 25));i++)
4257 shift[2 * i] = shift[2 * i + 1] = ' ';
4258 shift[2 * i] = shift[2 * i + 1] = 0;
4259 fprintf(output, shift);
4260 if (particle->children == NULL) {
4261 fprintf(output, "MISSING particle term\n");
4262 return;
4263 }
4264 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004265 if (term == NULL) {
4266 fprintf(output, "(NULL)");
4267 } else {
4268 switch (term->type) {
4269 case XML_SCHEMA_TYPE_ELEMENT:
4270 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4271 ((xmlSchemaElementPtr)term)->targetNamespace,
4272 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004273 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004274 break;
4275 case XML_SCHEMA_TYPE_SEQUENCE:
4276 fprintf(output, "SEQUENCE");
4277 break;
4278 case XML_SCHEMA_TYPE_CHOICE:
4279 fprintf(output, "CHOICE");
4280 break;
4281 case XML_SCHEMA_TYPE_ALL:
4282 fprintf(output, "ALL");
4283 break;
4284 case XML_SCHEMA_TYPE_ANY:
4285 fprintf(output, "ANY");
4286 break;
4287 default:
4288 fprintf(output, "UNKNOWN\n");
4289 return;
4290 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004291 }
4292 if (particle->minOccurs != 1)
4293 fprintf(output, " min: %d", particle->minOccurs);
4294 if (particle->maxOccurs >= UNBOUNDED)
4295 fprintf(output, " max: unbounded");
4296 else if (particle->maxOccurs != 1)
4297 fprintf(output, " max: %d", particle->maxOccurs);
4298 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004299 if (term &&
4300 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4301 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4302 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4303 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004304 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4305 output, depth +1);
4306 }
4307 if (particle->next != NULL)
4308 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4309 output, depth);
4310}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004311
4312/**
4313 * xmlSchemaAttrUsesDump:
4314 * @uses: attribute uses list
4315 * @output: the file output
4316 *
4317 * Dumps a list of attribute use components.
4318 */
4319static void
4320xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4321{
4322 xmlSchemaAttributeUsePtr use;
4323 xmlSchemaAttributeUseProhibPtr prohib;
4324 xmlSchemaQNameRefPtr ref;
4325 const xmlChar *name, *tns;
4326 xmlChar *str = NULL;
4327 int i;
4328
4329 if ((uses == NULL) || (uses->nbItems == 0))
4330 return;
4331
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004332 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004333 for (i = 0; i < uses->nbItems; i++) {
4334 use = uses->items[i];
4335 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4336 fprintf(output, " [prohibition] ");
4337 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4338 name = prohib->name;
4339 tns = prohib->targetNamespace;
4340 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4341 fprintf(output, " [reference] ");
4342 ref = (xmlSchemaQNameRefPtr) use;
4343 name = ref->name;
4344 tns = ref->targetNamespace;
4345 } else {
4346 fprintf(output, " [use] ");
4347 name = WXS_ATTRUSE_DECL_NAME(use);
4348 tns = WXS_ATTRUSE_DECL_TNS(use);
4349 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004350 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004351 (const char *) xmlSchemaFormatQName(&str, tns, name));
4352 FREE_AND_NULL(str);
4353 }
4354}
4355
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004356/**
4357 * xmlSchemaTypeDump:
4358 * @output: the file output
4359 * @type: a type structure
4360 *
4361 * Dump a SchemaType structure
4362 */
4363static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004364xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4365{
4366 if (type == NULL) {
4367 fprintf(output, "Type: NULL\n");
4368 return;
4369 }
4370 fprintf(output, "Type: ");
4371 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004372 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004373 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004374 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004375 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004376 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004377 switch (type->type) {
4378 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004379 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004380 break;
4381 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004382 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004383 break;
4384 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004385 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004386 break;
4387 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004388 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004389 break;
4390 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004391 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004392 break;
4393 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004394 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004395 break;
4396 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004397 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 break;
4399 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004401 break;
4402 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 break;
4405 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004407 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004408 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004409 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004410 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004411 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004413 break;
4414 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004415 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004416 break;
4417 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004418 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004419 break;
4420 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004421 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004422 break;
4423 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004424 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004425 break;
4426 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004427 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004428 break;
4429 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004430 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004431 break;
4432 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004433 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004434 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004435 }
4436 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004437 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004438 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004439 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004440 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004441 else
4442 fprintf(output, "\n");
4443 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004444 if (type->attrUses != NULL)
4445 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004446 if (type->annot != NULL)
4447 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004448#ifdef DUMP_CONTENT_MODEL
4449 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4450 (type->subtypes != NULL)) {
4451 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4452 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004453 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004454#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004455}
4456
4457/**
4458 * xmlSchemaDump:
4459 * @output: the file output
4460 * @schema: a schema structure
4461 *
4462 * Dump a Schema structure.
4463 */
4464void
4465xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4466{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004467 if (output == NULL)
4468 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004469 if (schema == NULL) {
4470 fprintf(output, "Schemas: NULL\n");
4471 return;
4472 }
4473 fprintf(output, "Schemas: ");
4474 if (schema->name != NULL)
4475 fprintf(output, "%s, ", schema->name);
4476 else
4477 fprintf(output, "no name, ");
4478 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004479 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004480 else
4481 fprintf(output, "no target namespace");
4482 fprintf(output, "\n");
4483 if (schema->annot != NULL)
4484 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004485 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4486 output);
4487 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004488 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004489}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004490
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004491#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004492/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004493 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004494 * @vctxt: the WXS validation context
4495 *
4496 * Displays the current IDC table for debug purposes.
4497 */
4498static void
4499xmlSchemaDebugDumpIDCTable(FILE * output,
4500 const xmlChar *namespaceName,
4501 const xmlChar *localName,
4502 xmlSchemaPSVIIDCBindingPtr bind)
4503{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004504 xmlChar *str = NULL;
4505 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004506 xmlSchemaPSVIIDCNodePtr tab;
4507 xmlSchemaPSVIIDCKeyPtr key;
4508 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004509
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004510 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004511 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004512 FREE_AND_NULL(str)
4513
4514 if (bind == NULL)
4515 return;
4516 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004517 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004518 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004519 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004520 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004521 for (i = 0; i < bind->nbNodes; i++) {
4522 tab = bind->nodeTable[i];
4523 fprintf(output, " ( ");
4524 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004525 key = tab->keys[j];
4526 if ((key != NULL) && (key->val != NULL)) {
4527 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004528 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004529 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004530 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004531 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004532 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004533 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004534 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004535 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004536 else
4537 fprintf(output, "(key missing), ");
4538 }
4539 fprintf(output, ")\n");
4540 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004541 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004542 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004543 for (i = 0; i < bind->dupls->nbItems; i++) {
4544 tab = bind->dupls->items[i];
4545 fprintf(output, " ( ");
4546 for (j = 0; j < bind->definition->nbFields; j++) {
4547 key = tab->keys[j];
4548 if ((key != NULL) && (key->val != NULL)) {
4549 res = xmlSchemaGetCanonValue(key->val, &value);
4550 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004551 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004552 else
4553 fprintf(output, "CANON-VALUE-FAILED ");
4554 if (res == 0)
4555 FREE_AND_NULL(value)
4556 } else if (key != NULL)
4557 fprintf(output, "(no val), ");
4558 else
4559 fprintf(output, "(key missing), ");
4560 }
4561 fprintf(output, ")\n");
4562 }
4563 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004564 bind = bind->next;
4565 } while (bind != NULL);
4566}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004567#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004568#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004569
4570/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004571 * *
4572 * Utilities *
4573 * *
4574 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004575
Daniel Veillardc0826a72004-08-10 14:17:33 +00004576/**
4577 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004578 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004579 * @name: the name of the attribute
4580 *
4581 * Seeks an attribute with a name of @name in
4582 * no namespace.
4583 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004584 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004585 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004586static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004587xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004588{
4589 xmlAttrPtr prop;
4590
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004591 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004592 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004593 prop = node->properties;
4594 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004595 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004596 return(prop);
4597 prop = prop->next;
4598 }
4599 return (NULL);
4600}
4601
4602/**
4603 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004604 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004605 * @uri: the uri
4606 * @name: the name of the attribute
4607 *
4608 * Seeks an attribute with a local name of @name and
4609 * a namespace URI of @uri.
4610 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004611 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004612 */
4613static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004614xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004615{
4616 xmlAttrPtr prop;
4617
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004618 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004619 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004620 prop = node->properties;
4621 while (prop != NULL) {
4622 if ((prop->ns != NULL) &&
4623 xmlStrEqual(prop->name, BAD_CAST name) &&
4624 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004625 return(prop);
4626 prop = prop->next;
4627 }
4628 return (NULL);
4629}
4630
4631static const xmlChar *
4632xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4633{
4634 xmlChar *val;
4635 const xmlChar *ret;
4636
4637 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004638 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004639 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004640 ret = xmlDictLookup(ctxt->dict, val, -1);
4641 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004642 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004643}
4644
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004645static const xmlChar *
4646xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4647{
4648 return((const xmlChar*) xmlNodeGetContent(node));
4649}
4650
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004651/**
4652 * xmlSchemaGetProp:
4653 * @ctxt: the parser context
4654 * @node: the node
4655 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004656 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004657 * Read a attribute value and internalize the string
4658 *
4659 * Returns the string or NULL if not present.
4660 */
4661static const xmlChar *
4662xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4663 const char *name)
4664{
4665 xmlChar *val;
4666 const xmlChar *ret;
4667
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004668 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004669 if (val == NULL)
4670 return(NULL);
4671 ret = xmlDictLookup(ctxt->dict, val, -1);
4672 xmlFree(val);
4673 return(ret);
4674}
4675
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004676/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004677 * *
4678 * Parsing functions *
4679 * *
4680 ************************************************************************/
4681
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004682#define WXS_FIND_GLOBAL_ITEM(slot) \
4683 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4684 ret = xmlHashLookup(schema->slot, name); \
4685 if (ret != NULL) goto exit; \
4686 } \
4687 if (xmlHashSize(schema->schemasImports) > 1) { \
4688 xmlSchemaImportPtr import; \
4689 if (nsName == NULL) \
4690 import = xmlHashLookup(schema->schemasImports, \
4691 XML_SCHEMAS_NO_NAMESPACE); \
4692 else \
4693 import = xmlHashLookup(schema->schemasImports, nsName); \
4694 if (import == NULL) \
4695 goto exit; \
4696 ret = xmlHashLookup(import->schema->slot, name); \
4697 }
4698
Daniel Veillard4255d502002-04-16 15:50:10 +00004699/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004700 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004701 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004702 * @name: the element name
4703 * @ns: the element namespace
4704 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004705 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004706 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004707 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004708 */
4709static xmlSchemaElementPtr
4710xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004711 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004712{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004713 xmlSchemaElementPtr ret = NULL;
4714
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004715 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004716 return(NULL);
4717 if (schema != NULL) {
4718 WXS_FIND_GLOBAL_ITEM(elemDecl)
4719 }
4720exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004721#ifdef DEBUG
4722 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004723 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004724 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004725 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004726 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004727 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004728 }
4729#endif
4730 return (ret);
4731}
4732
4733/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004734 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004735 * @schema: the main schema
4736 * @name: the type's name
4737 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004738 *
4739 * Lookup a type in the schemas or the predefined types
4740 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004741 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004742 */
4743static xmlSchemaTypePtr
4744xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004745 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004746{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004747 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004748
4749 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004750 return (NULL);
4751 /* First try the built-in types. */
4752 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4753 ret = xmlSchemaGetPredefinedType(name, nsName);
4754 if (ret != NULL)
4755 goto exit;
4756 /*
4757 * Note that we try the parsed schemas as well here
4758 * since one might have parsed the S4S, which contain more
4759 * than the built-in types.
4760 * TODO: Can we optimize this?
4761 */
4762 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004763 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004764 WXS_FIND_GLOBAL_ITEM(typeDecl)
4765 }
4766exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004767
Daniel Veillard4255d502002-04-16 15:50:10 +00004768#ifdef DEBUG
4769 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004770 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004771 fprintf(stderr, "Unable to lookup type %s", name);
4772 else
4773 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004774 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004775 }
4776#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004777 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004778}
4779
Daniel Veillard3646d642004-06-02 19:19:14 +00004780/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004781 * xmlSchemaGetAttributeDecl:
4782 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004783 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004784 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004785 *
4786 * Lookup a an attribute in the schema or imported schemas
4787 *
4788 * Returns the attribute declaration or NULL if not found.
4789 */
4790static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004791xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004792 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004793{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004794 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004795
4796 if ((name == NULL) || (schema == NULL))
4797 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004798 if (schema != NULL) {
4799 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004800 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004801exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004802#ifdef DEBUG
4803 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004804 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004805 fprintf(stderr, "Unable to lookup attribute %s", name);
4806 else
4807 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004808 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004809 }
4810#endif
4811 return (ret);
4812}
4813
4814/**
4815 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004816 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004817 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004818 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004819 *
4820 * Lookup a an attribute group in the schema or imported schemas
4821 *
4822 * Returns the attribute group definition or NULL if not found.
4823 */
4824static xmlSchemaAttributeGroupPtr
4825xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004826 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004827{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004828 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004829
4830 if ((name == NULL) || (schema == NULL))
4831 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004832 if (schema != NULL) {
4833 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4834 }
4835exit:
4836 /* TODO:
4837 if ((ret != NULL) && (ret->redef != NULL)) {
4838 * Return the last redefinition. *
4839 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004840 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004841 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004842#ifdef DEBUG
4843 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004844 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004845 fprintf(stderr, "Unable to lookup attribute group %s", name);
4846 else
4847 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004848 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004849 }
4850#endif
4851 return (ret);
4852}
4853
4854/**
4855 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004856 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004857 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004858 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004859 *
4860 * Lookup a group in the schema or imported schemas
4861 *
4862 * Returns the group definition or NULL if not found.
4863 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004864static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004865xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004866 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004867{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004868 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004869
4870 if ((name == NULL) || (schema == NULL))
4871 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004872 if (schema != NULL) {
4873 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004874 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004875exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004876
Daniel Veillard3646d642004-06-02 19:19:14 +00004877#ifdef DEBUG
4878 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004879 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004880 fprintf(stderr, "Unable to lookup group %s", name);
4881 else
4882 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004883 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004884 }
4885#endif
4886 return (ret);
4887}
4888
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004889static xmlSchemaNotationPtr
4890xmlSchemaGetNotation(xmlSchemaPtr schema,
4891 const xmlChar *name,
4892 const xmlChar *nsName)
4893{
4894 xmlSchemaNotationPtr ret = NULL;
4895
4896 if ((name == NULL) || (schema == NULL))
4897 return (NULL);
4898 if (schema != NULL) {
4899 WXS_FIND_GLOBAL_ITEM(notaDecl)
4900 }
4901exit:
4902 return (ret);
4903}
4904
4905static xmlSchemaIDCPtr
4906xmlSchemaGetIDC(xmlSchemaPtr schema,
4907 const xmlChar *name,
4908 const xmlChar *nsName)
4909{
4910 xmlSchemaIDCPtr ret = NULL;
4911
4912 if ((name == NULL) || (schema == NULL))
4913 return (NULL);
4914 if (schema != NULL) {
4915 WXS_FIND_GLOBAL_ITEM(idcDef)
4916 }
4917exit:
4918 return (ret);
4919}
4920
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004921/**
4922 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004923 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004924 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004925 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004926 *
4927 * Lookup a group in the schema or imported schemas
4928 *
4929 * Returns the group definition or NULL if not found.
4930 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004931static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004932xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4933 xmlSchemaTypeType itemType,
4934 const xmlChar *name,
4935 const xmlChar *targetNs)
4936{
4937 switch (itemType) {
4938 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004939 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004940 name, targetNs));
4941 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004942 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004943 name, targetNs));
4944 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004945 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004946 return (NULL);
4947 }
4948}
4949
Daniel Veillard4255d502002-04-16 15:50:10 +00004950/************************************************************************
4951 * *
4952 * Parsing functions *
4953 * *
4954 ************************************************************************/
4955
4956#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004957 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004958
4959/**
4960 * xmlSchemaIsBlank:
4961 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004962 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004963 *
4964 * Check if a string is ignorable
4965 *
4966 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4967 */
4968static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004969xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004970{
Daniel Veillard4255d502002-04-16 15:50:10 +00004971 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004972 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004973 if (len < 0) {
4974 while (*str != 0) {
4975 if (!(IS_BLANK_CH(*str)))
4976 return (0);
4977 str++;
4978 }
4979 } else while ((*str != 0) && (len != 0)) {
4980 if (!(IS_BLANK_CH(*str)))
4981 return (0);
4982 str++;
4983 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004984 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004985
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004986 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004987}
4988
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004989#define WXS_COMP_NAME(c, t) ((t) (c))->name
4990#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4991/*
4992* xmlSchemaFindRedefCompInGraph:
4993* ATTENTION TODO: This uses pointer comp. for strings.
4994*/
4995static xmlSchemaBasicItemPtr
4996xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4997 xmlSchemaTypeType type,
4998 const xmlChar *name,
4999 const xmlChar *nsName)
5000{
5001 xmlSchemaBasicItemPtr ret;
5002 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005003
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005004 if ((bucket == NULL) || (name == NULL))
5005 return(NULL);
5006 if ((bucket->globals == NULL) ||
5007 (bucket->globals->nbItems == 0))
5008 goto subschemas;
5009 /*
5010 * Search in global components.
5011 */
5012 for (i = 0; i < bucket->globals->nbItems; i++) {
5013 ret = bucket->globals->items[i];
5014 if (ret->type == type) {
5015 switch (type) {
5016 case XML_SCHEMA_TYPE_COMPLEX:
5017 case XML_SCHEMA_TYPE_SIMPLE:
5018 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5019 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5020 nsName))
5021 {
5022 return(ret);
5023 }
5024 break;
5025 case XML_SCHEMA_TYPE_GROUP:
5026 if ((WXS_COMP_NAME(ret,
5027 xmlSchemaModelGroupDefPtr) == name) &&
5028 (WXS_COMP_TNS(ret,
5029 xmlSchemaModelGroupDefPtr) == nsName))
5030 {
5031 return(ret);
5032 }
5033 break;
5034 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5035 if ((WXS_COMP_NAME(ret,
5036 xmlSchemaAttributeGroupPtr) == name) &&
5037 (WXS_COMP_TNS(ret,
5038 xmlSchemaAttributeGroupPtr) == nsName))
5039 {
5040 return(ret);
5041 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005042 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005043 default:
5044 /* Should not be hit. */
5045 return(NULL);
5046 }
5047 }
5048 }
5049subschemas:
5050 /*
5051 * Process imported/included schemas.
5052 */
5053 if (bucket->relations != NULL) {
5054 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5055
5056 /*
5057 * TODO: Marking the bucket will not avoid multiple searches
5058 * in the same schema, but avoids at least circularity.
5059 */
5060 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5061 do {
5062 if ((rel->bucket != NULL) &&
5063 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5064 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5065 type, name, nsName);
5066 if (ret != NULL)
5067 return(ret);
5068 }
5069 rel = rel->next;
5070 } while (rel != NULL);
5071 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5072 }
5073 return(NULL);
5074}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005075
5076/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005077 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005078 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005079 * @schema: the schema being built
5080 * @name: the item name
5081 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005082 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 * *WARNING* this interface is highly subject to change
5084 *
5085 * Returns the new struture or NULL in case of error
5086 */
5087static xmlSchemaNotationPtr
5088xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005089 const xmlChar *name, const xmlChar *nsName,
5090 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005091{
5092 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005093
5094 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5095 return (NULL);
5096
Daniel Veillard4255d502002-04-16 15:50:10 +00005097 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5098 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005099 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005100 return (NULL);
5101 }
5102 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005103 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005104 ret->name = name;
5105 ret->targetNamespace = nsName;
5106 /* TODO: do we need the node to be set?
5107 * ret->node = node;*/
5108 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 return (ret);
5110}
5111
Daniel Veillard4255d502002-04-16 15:50:10 +00005112/**
5113 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005114 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 * @schema: the schema being built
5116 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005117 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 *
5119 * Add an XML schema Attrribute declaration
5120 * *WARNING* this interface is highly subject to change
5121 *
5122 * Returns the new struture or NULL in case of error
5123 */
5124static xmlSchemaAttributePtr
5125xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005126 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005127 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005128{
5129 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005130
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005131 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005132 return (NULL);
5133
5134 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5135 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005136 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005137 return (NULL);
5138 }
5139 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005140 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5141 ret->node = node;
5142 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005143 ret->targetNamespace = nsName;
5144
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005145 if (topLevel)
5146 WXS_ADD_GLOBAL(ctxt, ret);
5147 else
5148 WXS_ADD_LOCAL(ctxt, ret);
5149 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005150 return (ret);
5151}
5152
5153/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005154 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005155 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005156 * @schema: the schema being built
5157 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005158 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005159 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005160 * Add an XML schema Attrribute declaration
5161 * *WARNING* this interface is highly subject to change
5162 *
5163 * Returns the new struture or NULL in case of error
5164 */
5165static xmlSchemaAttributeUsePtr
5166xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5167 xmlNodePtr node)
5168{
5169 xmlSchemaAttributeUsePtr ret = NULL;
5170
5171 if (pctxt == NULL)
5172 return (NULL);
5173
5174 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5175 if (ret == NULL) {
5176 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5177 return (NULL);
5178 }
5179 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5180 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5181 ret->node = node;
5182
5183 WXS_ADD_LOCAL(pctxt, ret);
5184 return (ret);
5185}
5186
5187/*
5188* xmlSchemaAddRedef:
5189*
5190* Adds a redefinition information. This is used at a later stage to:
5191* resolve references to the redefined components and to check constraints.
5192*/
5193static xmlSchemaRedefPtr
5194xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5195 xmlSchemaBucketPtr targetBucket,
5196 void *item,
5197 const xmlChar *refName,
5198 const xmlChar *refTargetNs)
5199{
5200 xmlSchemaRedefPtr ret;
5201
5202 ret = (xmlSchemaRedefPtr)
5203 xmlMalloc(sizeof(xmlSchemaRedef));
5204 if (ret == NULL) {
5205 xmlSchemaPErrMemory(pctxt,
5206 "allocating redefinition info", NULL);
5207 return (NULL);
5208 }
5209 memset(ret, 0, sizeof(xmlSchemaRedef));
5210 ret->item = item;
5211 ret->targetBucket = targetBucket;
5212 ret->refName = refName;
5213 ret->refTargetNs = refTargetNs;
5214 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5215 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5216 else
5217 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5218 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5219
5220 return (ret);
5221}
5222
5223/**
5224 * xmlSchemaAddAttributeGroupDefinition:
5225 * @ctxt: a schema parser context
5226 * @schema: the schema being built
5227 * @name: the item name
5228 * @nsName: the target namespace
5229 * @node: the corresponding node
5230 *
5231 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005232 *
5233 * Returns the new struture or NULL in case of error
5234 */
5235static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005236xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5237 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5238 const xmlChar *name,
5239 const xmlChar *nsName,
5240 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005241{
5242 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005243
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005244 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005245 return (NULL);
5246
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005247 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005248 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005249 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005250 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005251 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005252 }
5253 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005254 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005255 ret->name = name;
5256 ret->targetNamespace = nsName;
5257 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005258
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005259 /* TODO: Remove the flag. */
5260 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5261 if (pctxt->isRedefine) {
5262 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5263 ret, name, nsName);
5264 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005265 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005266 return(NULL);
5267 }
5268 pctxt->redefCounter = 0;
5269 }
5270 WXS_ADD_GLOBAL(pctxt, ret);
5271 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005272 return (ret);
5273}
5274
5275/**
5276 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005277 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005278 * @schema: the schema being built
5279 * @name: the type name
5280 * @namespace: the type namespace
5281 *
5282 * Add an XML schema Element declaration
5283 * *WARNING* this interface is highly subject to change
5284 *
5285 * Returns the new struture or NULL in case of error
5286 */
5287static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005288xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005289 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005290 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005291{
5292 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005293
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005294 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005295 return (NULL);
5296
Daniel Veillard4255d502002-04-16 15:50:10 +00005297 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5298 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005299 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005300 return (NULL);
5301 }
5302 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005303 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5304 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005305 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005306 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005307
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005308 if (topLevel)
5309 WXS_ADD_GLOBAL(ctxt, ret);
5310 else
5311 WXS_ADD_LOCAL(ctxt, ret);
5312 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005313 return (ret);
5314}
5315
5316/**
5317 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005318 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005319 * @schema: the schema being built
5320 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005321 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005322 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005323 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005324 * *WARNING* this interface is highly subject to change
5325 *
5326 * Returns the new struture or NULL in case of error
5327 */
5328static xmlSchemaTypePtr
5329xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005330 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005331 const xmlChar * name, const xmlChar * nsName,
5332 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005333{
5334 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005335
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005336 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005337 return (NULL);
5338
5339 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5340 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005341 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 return (NULL);
5343 }
5344 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005345 ret->type = type;
5346 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005347 ret->targetNamespace = nsName;
5348 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005349 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005350 if (ctxt->isRedefine) {
5351 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5352 ret, name, nsName);
5353 if (ctxt->redef == NULL) {
5354 xmlFree(ret);
5355 return(NULL);
5356 }
5357 ctxt->redefCounter = 0;
5358 }
5359 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005360 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005361 WXS_ADD_LOCAL(ctxt, ret);
5362 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005363 return (ret);
5364}
5365
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005366static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005367xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005368 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005369 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005370 const xmlChar *refNs)
5371{
5372 xmlSchemaQNameRefPtr ret;
5373
5374 ret = (xmlSchemaQNameRefPtr)
5375 xmlMalloc(sizeof(xmlSchemaQNameRef));
5376 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005377 xmlSchemaPErrMemory(pctxt,
5378 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005379 return (NULL);
5380 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005381 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005382 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5383 ret->name = refName;
5384 ret->targetNamespace = refNs;
5385 ret->item = NULL;
5386 ret->itemType = refType;
5387 /*
5388 * Store the reference item in the schema.
5389 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005390 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005391 return (ret);
5392}
5393
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005394static xmlSchemaAttributeUseProhibPtr
5395xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5396{
5397 xmlSchemaAttributeUseProhibPtr ret;
5398
5399 ret = (xmlSchemaAttributeUseProhibPtr)
5400 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5401 if (ret == NULL) {
5402 xmlSchemaPErrMemory(pctxt,
5403 "allocating attribute use prohibition", NULL);
5404 return (NULL);
5405 }
5406 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5407 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5408 WXS_ADD_LOCAL(pctxt, ret);
5409 return (ret);
5410}
5411
5412
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005413/**
5414 * xmlSchemaAddModelGroup:
5415 * @ctxt: a schema parser context
5416 * @schema: the schema being built
5417 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005418 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005419 *
5420 * Adds a schema model group
5421 * *WARNING* this interface is highly subject to change
5422 *
5423 * Returns the new struture or NULL in case of error
5424 */
5425static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005426xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5427 xmlSchemaPtr schema,
5428 xmlSchemaTypeType type,
5429 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005430{
5431 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005432
5433 if ((ctxt == NULL) || (schema == NULL))
5434 return (NULL);
5435
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005436 ret = (xmlSchemaModelGroupPtr)
5437 xmlMalloc(sizeof(xmlSchemaModelGroup));
5438 if (ret == NULL) {
5439 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5440 NULL);
5441 return (NULL);
5442 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005443 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005444 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005445 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005446 WXS_ADD_LOCAL(ctxt, ret);
5447 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5448 (type == XML_SCHEMA_TYPE_CHOICE))
5449 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005450 return (ret);
5451}
5452
5453
5454/**
5455 * xmlSchemaAddParticle:
5456 * @ctxt: a schema parser context
5457 * @schema: the schema being built
5458 * @node: the corresponding node in the schema doc
5459 * @min: the minOccurs
5460 * @max: the maxOccurs
5461 *
5462 * Adds an XML schema particle component.
5463 * *WARNING* this interface is highly subject to change
5464 *
5465 * Returns the new struture or NULL in case of error
5466 */
5467static xmlSchemaParticlePtr
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005468xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005469 xmlNodePtr node, int min, int max)
5470{
5471 xmlSchemaParticlePtr ret = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005472 if (ctxt == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005473 return (NULL);
5474
5475#ifdef DEBUG
5476 fprintf(stderr, "Adding particle component\n");
5477#endif
5478 ret = (xmlSchemaParticlePtr)
5479 xmlMalloc(sizeof(xmlSchemaParticle));
5480 if (ret == NULL) {
5481 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5482 NULL);
5483 return (NULL);
5484 }
5485 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5486 ret->annot = NULL;
5487 ret->node = node;
5488 ret->minOccurs = min;
5489 ret->maxOccurs = max;
5490 ret->next = NULL;
5491 ret->children = NULL;
5492
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005493 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005494 /*
5495 * Note that addition to pending components will be done locally
5496 * to the specific parsing function, since the most particles
5497 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005498 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005499 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005500 return (ret);
5501}
5502
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005503/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005504 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005505 * @ctxt: a schema validation context
5506 * @schema: the schema being built
5507 * @name: the group name
5508 *
5509 * Add an XML schema Group definition
5510 *
5511 * Returns the new struture or NULL in case of error
5512 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005513static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005514xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5515 xmlSchemaPtr schema,
5516 const xmlChar *name,
5517 const xmlChar *nsName,
5518 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005519{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005520 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005521
5522 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5523 return (NULL);
5524
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005525 ret = (xmlSchemaModelGroupDefPtr)
5526 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005527 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005528 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005529 return (NULL);
5530 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005531 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005532 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005533 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005534 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005535 ret->targetNamespace = nsName;
5536
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005537 if (ctxt->isRedefine) {
5538 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5539 ret, name, nsName);
5540 if (ctxt->redef == NULL) {
5541 xmlFree(ret);
5542 return(NULL);
5543 }
5544 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005545 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005546 WXS_ADD_GLOBAL(ctxt, ret);
5547 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005548 return (ret);
5549}
5550
Daniel Veillard3646d642004-06-02 19:19:14 +00005551/**
5552 * xmlSchemaNewWildcardNs:
5553 * @ctxt: a schema validation context
5554 *
5555 * Creates a new wildcard namespace constraint.
5556 *
5557 * Returns the new struture or NULL in case of error
5558 */
5559static xmlSchemaWildcardNsPtr
5560xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5561{
5562 xmlSchemaWildcardNsPtr ret;
5563
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005564 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005565 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5566 if (ret == NULL) {
5567 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005568 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005569 }
5570 ret->value = NULL;
5571 ret->next = NULL;
5572 return (ret);
5573}
5574
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005575static xmlSchemaIDCPtr
5576xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5577 const xmlChar *name, const xmlChar *nsName,
5578 int category, xmlNodePtr node)
5579{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005580 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005581
5582 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5583 return (NULL);
5584
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005585 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5586 if (ret == NULL) {
5587 xmlSchemaPErrMemory(ctxt,
5588 "allocating an identity-constraint definition", NULL);
5589 return (NULL);
5590 }
5591 memset(ret, 0, sizeof(xmlSchemaIDC));
5592 /* The target namespace of the parent element declaration. */
5593 ret->targetNamespace = nsName;
5594 ret->name = name;
5595 ret->type = category;
5596 ret->node = node;
5597
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005598 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005599 /*
5600 * Only keyrefs need to be fixup up.
5601 */
5602 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005603 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005604 return (ret);
5605}
5606
Daniel Veillard3646d642004-06-02 19:19:14 +00005607/**
5608 * xmlSchemaAddWildcard:
5609 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005610 * @schema: a schema
5611 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005612 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005613 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005614 *
5615 * Returns the new struture or NULL in case of error
5616 */
5617static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005618xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5619 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005620{
5621 xmlSchemaWildcardPtr ret = NULL;
5622
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005623 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005624 return (NULL);
5625
5626 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5627 if (ret == NULL) {
5628 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5629 return (NULL);
5630 }
5631 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005632 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005633 ret->node = node;
5634 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005635 return (ret);
5636}
5637
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005638static void
5639xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5640{
5641 if (group == NULL)
5642 return;
5643 if (group->members != NULL)
5644 xmlSchemaItemListFree(group->members);
5645 xmlFree(group);
5646}
5647
5648static xmlSchemaSubstGroupPtr
5649xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5650 xmlSchemaElementPtr head)
5651{
5652 xmlSchemaSubstGroupPtr ret;
5653
5654 /* Init subst group hash. */
5655 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5656 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5657 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5658 return(NULL);
5659 }
5660 /* Create a new substitution group. */
5661 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5662 if (ret == NULL) {
5663 xmlSchemaPErrMemory(NULL,
5664 "allocating a substitution group container", NULL);
5665 return(NULL);
5666 }
5667 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5668 ret->head = head;
5669 /* Create list of members. */
5670 ret->members = xmlSchemaItemListCreate();
5671 if (ret->members == NULL) {
5672 xmlSchemaSubstGroupFree(ret);
5673 return(NULL);
5674 }
5675 /* Add subst group to hash. */
5676 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5677 head->name, head->targetNamespace, ret) != 0) {
5678 PERROR_INT("xmlSchemaSubstGroupAdd",
5679 "failed to add a new substitution container");
5680 xmlSchemaSubstGroupFree(ret);
5681 return(NULL);
5682 }
5683 return(ret);
5684}
5685
5686static xmlSchemaSubstGroupPtr
5687xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5688 xmlSchemaElementPtr head)
5689{
5690 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5691 return(NULL);
5692 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5693 head->name, head->targetNamespace));
5694
5695}
5696
5697/**
5698 * xmlSchemaAddElementSubstitutionMember:
5699 * @pctxt: a schema parser context
5700 * @head: the head of the substitution group
5701 * @member: the new member of the substitution group
5702 *
5703 * Allocate a new annotation structure.
5704 *
5705 * Returns the newly allocated structure or NULL in case or error
5706 */
5707static int
5708xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5709 xmlSchemaElementPtr head,
5710 xmlSchemaElementPtr member)
5711{
5712 xmlSchemaSubstGroupPtr substGroup = NULL;
5713
5714 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5715 return (-1);
5716
5717 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5718 if (substGroup == NULL)
5719 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5720 if (substGroup == NULL)
5721 return(-1);
5722 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5723 return(-1);
5724 return(0);
5725}
5726
Daniel Veillard4255d502002-04-16 15:50:10 +00005727/************************************************************************
5728 * *
5729 * Utilities for parsing *
5730 * *
5731 ************************************************************************/
5732
Daniel Veillard4255d502002-04-16 15:50:10 +00005733/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005734 * xmlSchemaPValAttrNodeQNameValue:
5735 * @ctxt: a schema parser context
5736 * @schema: the schema context
5737 * @ownerDes: the designation of the parent element
5738 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005739 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005740 * @local: the resulting local part if found, the attribute value otherwise
5741 * @uri: the resulting namespace URI if found
5742 *
5743 * Extracts the local name and the URI of a QName value and validates it.
5744 * This one is intended to be used on attribute values that
5745 * should resolve to schema components.
5746 *
5747 * Returns 0, in case the QName is valid, a positive error code
5748 * if not valid and -1 if an internal error occurs.
5749 */
5750static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005751xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005752 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005753 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005754 xmlAttrPtr attr,
5755 const xmlChar *value,
5756 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005757 const xmlChar **local)
5758{
5759 const xmlChar *pref;
5760 xmlNsPtr ns;
5761 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005762
Daniel Veillardc0826a72004-08-10 14:17:33 +00005763 *uri = NULL;
5764 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005765 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005766 if (ret > 0) {
5767 xmlSchemaPSimpleTypeErr(ctxt,
5768 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5769 ownerItem, (xmlNodePtr) attr,
5770 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5771 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005772 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005773 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005774 } else if (ret < 0)
5775 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005776
5777 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005778 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005779 if (ns)
5780 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5781 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005782 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5783 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005784 /*
5785 * This one takes care of included schemas with no
5786 * target namespace.
5787 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005788 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005789 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005790 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005791 return (0);
5792 }
5793 /*
5794 * At this point xmlSplitQName3 has to return a local name.
5795 */
5796 *local = xmlSplitQName3(value, &len);
5797 *local = xmlDictLookup(ctxt->dict, *local, -1);
5798 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005799 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5800 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005801 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005802 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005803 ownerItem, (xmlNodePtr) attr,
5804 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5805 "The value '%s' of simple type 'xs:QName' has no "
5806 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005807 return (ctxt->err);
5808 } else {
5809 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005810 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005811 return (0);
5812}
5813
5814/**
5815 * xmlSchemaPValAttrNodeQName:
5816 * @ctxt: a schema parser context
5817 * @schema: the schema context
5818 * @ownerDes: the designation of the owner element
5819 * @ownerItem: the owner as a schema object
5820 * @attr: the attribute node
5821 * @local: the resulting local part if found, the attribute value otherwise
5822 * @uri: the resulting namespace URI if found
5823 *
5824 * Extracts and validates the QName of an attribute value.
5825 * This one is intended to be used on attribute values that
5826 * should resolve to schema components.
5827 *
5828 * Returns 0, in case the QName is valid, a positive error code
5829 * if not valid and -1 if an internal error occurs.
5830 */
5831static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005832xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005833 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005834 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005835 xmlAttrPtr attr,
5836 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005837 const xmlChar **local)
5838{
5839 const xmlChar *value;
5840
5841 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005842 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005843 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005844}
5845
5846/**
5847 * xmlSchemaPValAttrQName:
5848 * @ctxt: a schema parser context
5849 * @schema: the schema context
5850 * @ownerDes: the designation of the parent element
5851 * @ownerItem: the owner as a schema object
5852 * @ownerElem: the parent node of the attribute
5853 * @name: the name of the attribute
5854 * @local: the resulting local part if found, the attribute value otherwise
5855 * @uri: the resulting namespace URI if found
5856 *
5857 * Extracts and validates the QName of an attribute value.
5858 *
5859 * Returns 0, in case the QName is valid, a positive error code
5860 * if not valid and -1 if an internal error occurs.
5861 */
5862static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005863xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5864 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005865 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005866 xmlNodePtr ownerElem,
5867 const char *name,
5868 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005869 const xmlChar **local)
5870{
5871 xmlAttrPtr attr;
5872
5873 attr = xmlSchemaGetPropNode(ownerElem, name);
5874 if (attr == NULL) {
5875 *local = NULL;
5876 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005877 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005878 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005879 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005880 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005881}
5882
5883/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005884 * xmlSchemaPValAttrID:
5885 * @ctxt: a schema parser context
5886 * @schema: the schema context
5887 * @ownerDes: the designation of the parent element
5888 * @ownerItem: the owner as a schema object
5889 * @ownerElem: the parent node of the attribute
5890 * @name: the name of the attribute
5891 *
5892 * Extracts and validates the ID of an attribute value.
5893 *
5894 * Returns 0, in case the ID is valid, a positive error code
5895 * if not valid and -1 if an internal error occurs.
5896 */
5897static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005898xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005899{
5900 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005901 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005902
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005903 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005904 return(0);
5905 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5906 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005907 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005908 /*
5909 * NOTE: the IDness might have already be declared in the DTD
5910 */
5911 if (attr->atype != XML_ATTRIBUTE_ID) {
5912 xmlIDPtr res;
5913 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005914
5915 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005916 * TODO: Use xmlSchemaStrip here; it's not exported at this
5917 * moment.
5918 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005919 strip = xmlSchemaCollapseString(value);
5920 if (strip != NULL) {
5921 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005922 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005923 }
5924 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005925 if (res == NULL) {
5926 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005927 xmlSchemaPSimpleTypeErr(ctxt,
5928 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005929 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005930 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5931 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005932 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005933 } else
5934 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005935 }
5936 } else if (ret > 0) {
5937 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005938 xmlSchemaPSimpleTypeErr(ctxt,
5939 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005940 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005941 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5942 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5943 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005944 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005945 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005946 if (value != NULL)
5947 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005948
5949 return (ret);
5950}
5951
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005952static int
5953xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5954 xmlNodePtr ownerElem,
5955 const xmlChar *name)
5956{
5957 xmlAttrPtr attr;
5958
5959 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5960 if (attr == NULL)
5961 return(0);
5962 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5963
5964}
5965
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005966/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005967 * xmlGetMaxOccurs:
5968 * @ctxt: a schema validation context
5969 * @node: a subtree containing XML Schema informations
5970 *
5971 * Get the maxOccurs property
5972 *
5973 * Returns the default if not found, or the value
5974 */
5975static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005976xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5977 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005978{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005979 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005980 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005981 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005982
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005983 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5984 if (attr == NULL)
5985 return (def);
5986 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005987
5988 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005989 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005990 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005991 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5992 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005993 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005994 val, NULL, NULL, NULL);
5995 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005996 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005997 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005998 }
5999
6000 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006001 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006002 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006003 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006004 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006005 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6006 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006007 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006008 val, NULL, NULL, NULL);
6009 return (def);
6010 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006011 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006012 ret = ret * 10 + (*cur - '0');
6013 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006014 }
William M. Brack76e95df2003-10-18 16:20:14 +00006015 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006016 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006017 /*
6018 * TODO: Restrict the maximal value to Integer.
6019 */
6020 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006021 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006022 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6023 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006024 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006025 val, NULL, NULL, NULL);
6026 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006027 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006028 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006029}
6030
6031/**
6032 * xmlGetMinOccurs:
6033 * @ctxt: a schema validation context
6034 * @node: a subtree containing XML Schema informations
6035 *
6036 * Get the minOccurs property
6037 *
6038 * Returns the default if not found, or the value
6039 */
6040static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006041xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006042 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006043{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006044 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006045 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006046 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006047
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006048 attr = xmlSchemaGetPropNode(node, "minOccurs");
6049 if (attr == NULL)
6050 return (def);
6051 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006052 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006053 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006054 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006055 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006056 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006057 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6058 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006059 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006060 val, NULL, NULL, NULL);
6061 return (def);
6062 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006063 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006064 ret = ret * 10 + (*cur - '0');
6065 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006066 }
William M. Brack76e95df2003-10-18 16:20:14 +00006067 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006068 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006069 /*
6070 * TODO: Restrict the maximal value to Integer.
6071 */
6072 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006073 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006074 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6075 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006076 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006077 val, NULL, NULL, NULL);
6078 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006079 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006080 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006081}
6082
6083/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006084 * xmlSchemaPGetBoolNodeValue:
6085 * @ctxt: a schema validation context
6086 * @ownerDes: owner designation
6087 * @ownerItem: the owner as a schema item
6088 * @node: the node holding the value
6089 *
6090 * Converts a boolean string value into 1 or 0.
6091 *
6092 * Returns 0 or 1.
6093 */
6094static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006095xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006096 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006097 xmlNodePtr node)
6098{
6099 xmlChar *value = NULL;
6100 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006101
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006102 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006103 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006104 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006105 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006106 * can have the following legal literals {true, false, 1, 0}.
6107 */
6108 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6109 res = 1;
6110 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6111 res = 0;
6112 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6113 res = 1;
6114 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006115 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006116 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006117 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006118 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006119 ownerItem, node,
6120 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6121 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006122 NULL, NULL, NULL);
6123 }
6124 if (value != NULL)
6125 xmlFree(value);
6126 return (res);
6127}
6128
6129/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006130 * xmlGetBooleanProp:
6131 * @ctxt: a schema validation context
6132 * @node: a subtree containing XML Schema informations
6133 * @name: the attribute name
6134 * @def: the default value
6135 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006136 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006137 *
6138 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006139 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006140 */
6141static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006142xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006143 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006144 const char *name, int def)
6145{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006146 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006147
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006148 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006149 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006150 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006151 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006152 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006153 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006154 * can have the following legal literals {true, false, 1, 0}.
6155 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006156 if (xmlStrEqual(val, BAD_CAST "true"))
6157 def = 1;
6158 else if (xmlStrEqual(val, BAD_CAST "false"))
6159 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006160 else if (xmlStrEqual(val, BAD_CAST "1"))
6161 def = 1;
6162 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006163 def = 0;
6164 else {
6165 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006166 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006167 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006168 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006169 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6170 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006171 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006172 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006173}
6174
6175/************************************************************************
6176 * *
6177 * Shema extraction from an Infoset *
6178 * *
6179 ************************************************************************/
6180static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6181 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006182 xmlNodePtr node,
6183 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006184static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6185 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006186 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006187 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006188 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006189static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6190 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006191 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006192 xmlNodePtr node,
6193 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006194static xmlSchemaBasicItemPtr
6195xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6196 xmlSchemaPtr schema,
6197 xmlNodePtr node,
6198 xmlSchemaItemListPtr uses,
6199 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006200static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6201 xmlSchemaPtr schema,
6202 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006203static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006204xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6205 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006206
6207/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006208 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006209 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006210 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006211 * @ownerDes: the designation of the parent element
6212 * @ownerItem: the schema object owner if existent
6213 * @attr: the schema attribute node being validated
6214 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006215 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006216 *
6217 * Validates a value against the given built-in type.
6218 * This one is intended to be used internally for validation
6219 * of schema attribute values during parsing of the schema.
6220 *
6221 * Returns 0 if the value is valid, a positive error code
6222 * number otherwise and -1 in case of an internal or API error.
6223 */
6224static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006225xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006226 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006227 xmlAttrPtr attr,
6228 const xmlChar *value,
6229 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006230{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006231
6232 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006233
6234 /*
6235 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6236 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006237 */
6238 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006239 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006240 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6241 PERROR_INT("xmlSchemaPValAttrNodeValue",
6242 "the given type is not a built-in type");
6243 return (-1);
6244 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006245 switch (type->builtInType) {
6246 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006247 case XML_SCHEMAS_QNAME:
6248 case XML_SCHEMAS_ANYURI:
6249 case XML_SCHEMAS_TOKEN:
6250 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006251 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6252 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006253 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006254 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006255 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006256 "validation using the given type is not supported while "
6257 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006258 return (-1);
6259 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006260 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006261 /*
6262 * TODO: Should we use the S4S error codes instead?
6263 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006264 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006265 PERROR_INT("xmlSchemaPValAttrNodeValue",
6266 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006267 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006268 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006269 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006270 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6271 else
6272 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6273 xmlSchemaPSimpleTypeErr(pctxt,
6274 ret, ownerItem, (xmlNodePtr) attr,
6275 type, NULL, value, NULL, NULL, NULL);
6276 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006277 return (ret);
6278}
6279
6280/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006281 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006282 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006283 * @ctxt: a schema parser context
6284 * @ownerDes: the designation of the parent element
6285 * @ownerItem: the schema object owner if existent
6286 * @attr: the schema attribute node being validated
6287 * @type: the built-in type to be validated against
6288 * @value: the resulting value if any
6289 *
6290 * Extracts and validates a value against the given built-in type.
6291 * This one is intended to be used internally for validation
6292 * of schema attribute values during parsing of the schema.
6293 *
6294 * Returns 0 if the value is valid, a positive error code
6295 * number otherwise and -1 in case of an internal or API error.
6296 */
6297static int
6298xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006299 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006300 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006301 xmlSchemaTypePtr type,
6302 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006303{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006304 const xmlChar *val;
6305
6306 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006307 return (-1);
6308
Daniel Veillardc0826a72004-08-10 14:17:33 +00006309 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6310 if (value != NULL)
6311 *value = val;
6312
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006313 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006314 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006315}
6316
6317/**
6318 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006319 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006320 * @ctxt: a schema parser context
6321 * @node: the element node of the attribute
6322 * @ownerDes: the designation of the parent element
6323 * @ownerItem: the schema object owner if existent
6324 * @ownerElem: the owner element node
6325 * @name: the name of the schema attribute node
6326 * @type: the built-in type to be validated against
6327 * @value: the resulting value if any
6328 *
6329 * Extracts and validates a value against the given built-in type.
6330 * This one is intended to be used internally for validation
6331 * of schema attribute values during parsing of the schema.
6332 *
6333 * Returns 0 if the value is valid, a positive error code
6334 * number otherwise and -1 in case of an internal or API error.
6335 */
6336static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006337xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6338 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006339 xmlNodePtr ownerElem,
6340 const char *name,
6341 xmlSchemaTypePtr type,
6342 const xmlChar **value)
6343{
6344 xmlAttrPtr attr;
6345
6346 if ((ctxt == NULL) || (type == NULL)) {
6347 if (value != NULL)
6348 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006349 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006350 }
6351 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6352 if (value != NULL)
6353 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006354 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006355 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006356 "Internal error: xmlSchemaPValAttr, the given "
6357 "type '%s' is not a built-in type.\n",
6358 type->name, NULL);
6359 return (-1);
6360 }
6361 attr = xmlSchemaGetPropNode(ownerElem, name);
6362 if (attr == NULL) {
6363 if (value != NULL)
6364 *value = NULL;
6365 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006366 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006367 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006368 type, value));
6369}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006370
6371static int
6372xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006373 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006374 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006375 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006376 const xmlChar *namespaceName)
6377{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006378 /* TODO: Pointer comparison instead? */
6379 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006380 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006381 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006382 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006383 /*
6384 * Check if the referenced namespace was <import>ed.
6385 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006386 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006387 xmlSchemaSchemaRelationPtr rel;
6388
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006389 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006390 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006391 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006392 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006393 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006394 rel = rel->next;
6395 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006396 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006397 /*
6398 * No matching <import>ed namespace found.
6399 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006400 {
6401 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6402
6403 if (namespaceName == NULL)
6404 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6405 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6406 "References from this schema to components in no "
6407 "namespace are not allowed, since not indicated by an "
6408 "import statement", NULL, NULL);
6409 else
6410 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6411 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6412 "References from this schema to components in the "
6413 "namespace '%s' are not allowed, since not indicated by an "
6414 "import statement", namespaceName, NULL);
6415 }
6416 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006417}
6418
Daniel Veillardc0826a72004-08-10 14:17:33 +00006419/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006420 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006421 * @ctxt: a schema validation context
6422 * @schema: the schema being built
6423 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006424 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006425 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006426 * Parses attribute uses and attribute declarations and
6427 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006428 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006429static int
6430xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6431 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6432 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006433{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006434 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006435
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006436 while ((IS_SCHEMA((*child), "attribute")) ||
6437 (IS_SCHEMA((*child), "attributeGroup"))) {
6438 if (IS_SCHEMA((*child), "attribute")) {
6439 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6440 *list, parentType);
6441 } else {
6442 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6443 if ((item != NULL) && (hasRefs != NULL))
6444 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006445 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006446 if (item != NULL) {
6447 if (*list == NULL) {
6448 /* TODO: Customize grow factor. */
6449 *list = xmlSchemaItemListCreate();
6450 if (*list == NULL)
6451 return(-1);
6452 }
6453 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6454 return(-1);
6455 }
6456 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006457 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006458 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006459}
6460
6461/**
6462 * xmlSchemaParseAnnotation:
6463 * @ctxt: a schema validation context
6464 * @schema: the schema being built
6465 * @node: a subtree containing XML Schema informations
6466 *
6467 * parse a XML schema Attrribute declaration
6468 * *WARNING* this interface is highly subject to change
6469 *
William M. Bracke7091952004-05-11 15:09:58 +00006470 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006471 * 1 in case of success.
6472 */
6473static xmlSchemaAnnotPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006474xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006475{
6476 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006477 xmlNodePtr child = NULL;
6478 xmlAttrPtr attr;
6479 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006480
Daniel Veillardc0826a72004-08-10 14:17:33 +00006481 /*
6482 * INFO: S4S completed.
6483 */
6484 /*
6485 * id = ID
6486 * {any attributes with non-schema namespace . . .}>
6487 * Content: (appinfo | documentation)*
6488 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006489 if ((ctxt == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00006490 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006491 if (needed)
6492 ret = xmlSchemaNewAnnot(ctxt, node);
6493 else
6494 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006495 attr = node->properties;
6496 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006497 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006498 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006499 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006500 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006501
6502 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006503 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006504 }
6505 attr = attr->next;
6506 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006507 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006508 /*
6509 * And now for the children...
6510 */
6511 child = node->children;
6512 while (child != NULL) {
6513 if (IS_SCHEMA(child, "appinfo")) {
6514 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006515 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006516 * source = anyURI
6517 * {any attributes with non-schema namespace . . .}>
6518 * Content: ({any})*
6519 */
6520 attr = child->properties;
6521 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006522 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006523 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006524 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006525 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006526
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006527 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006528 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006529 }
6530 attr = attr->next;
6531 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006532 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006533 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006534 child = child->next;
6535 } else if (IS_SCHEMA(child, "documentation")) {
6536 /* TODO: make available the content of "documentation". */
6537 /*
6538 * source = anyURI
6539 * {any attributes with non-schema namespace . . .}>
6540 * Content: ({any})*
6541 */
6542 attr = child->properties;
6543 while (attr != NULL) {
6544 if (attr->ns == NULL) {
6545 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006546 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006547 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006548 }
6549 } else {
6550 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6551 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6552 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006553
6554 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006555 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006556 }
6557 }
6558 attr = attr->next;
6559 }
6560 /*
6561 * Attribute "xml:lang".
6562 */
6563 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6564 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006565 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006566 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006567 child = child->next;
6568 } else {
6569 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006570 xmlSchemaPContentErr(ctxt,
6571 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006572 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006573 barked = 1;
6574 child = child->next;
6575 }
6576 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006577
Daniel Veillard4255d502002-04-16 15:50:10 +00006578 return (ret);
6579}
6580
6581/**
6582 * xmlSchemaParseFacet:
6583 * @ctxt: a schema validation context
6584 * @schema: the schema being built
6585 * @node: a subtree containing XML Schema informations
6586 *
6587 * parse a XML schema Facet declaration
6588 * *WARNING* this interface is highly subject to change
6589 *
6590 * Returns the new type structure or NULL in case of error
6591 */
6592static xmlSchemaFacetPtr
6593xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006594 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006595{
6596 xmlSchemaFacetPtr facet;
6597 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006598 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006599
6600 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6601 return (NULL);
6602
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006603 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006604 if (facet == NULL) {
6605 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6606 return (NULL);
6607 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006608 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006609 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006610 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006611 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6612 "Facet %s has no value\n", node->name, NULL);
6613 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006614 return (NULL);
6615 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006616 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006617 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006618 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006619 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006620 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006621 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006622 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006623 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006624 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006625 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006626 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006627 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006628 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006629 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006630 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006631 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006632 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006633 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006634 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006635 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006636 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006637 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6638 } else if (IS_SCHEMA(node, "minLength")) {
6639 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6640 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006641 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6642 "Unknown facet type %s\n", node->name, NULL);
6643 xmlSchemaFreeFacet(facet);
6644 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006645 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006646 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006647 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006648 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6649 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6650 const xmlChar *fixed;
6651
6652 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6653 if (fixed != NULL) {
6654 if (xmlStrEqual(fixed, BAD_CAST "true"))
6655 facet->fixed = 1;
6656 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006657 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006658 child = node->children;
6659
6660 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006661 facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006662 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006663 }
6664 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006665 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6666 "Facet %s has unexpected child content\n",
6667 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006668 }
6669 return (facet);
6670}
6671
6672/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006673 * xmlSchemaParseWildcardNs:
6674 * @ctxt: a schema parser context
6675 * @wildc: the wildcard, already created
6676 * @node: a subtree containing XML Schema informations
6677 *
6678 * Parses the attribute "processContents" and "namespace"
6679 * of a xsd:anyAttribute and xsd:any.
6680 * *WARNING* this interface is highly subject to change
6681 *
6682 * Returns 0 if everything goes fine, a positive error code
6683 * if something is not valid and -1 if an internal error occurs.
6684 */
6685static int
6686xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006687 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006688 xmlSchemaWildcardPtr wildc,
6689 xmlNodePtr node)
6690{
6691 const xmlChar *pc, *ns, *dictnsItem;
6692 int ret = 0;
6693 xmlChar *nsItem;
6694 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6695 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006696
Daniel Veillardc0826a72004-08-10 14:17:33 +00006697 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6698 if ((pc == NULL)
6699 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6700 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6701 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6702 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6703 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6704 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6705 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006706 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006707 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006708 NULL, node,
6709 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006710 NULL, NULL, NULL);
6711 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006712 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006713 }
6714 /*
6715 * Build the namespace constraints.
6716 */
6717 attr = xmlSchemaGetPropNode(node, "namespace");
6718 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006719 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006720 wildc->any = 1;
6721 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6722 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006723 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006724 return (-1);
6725 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006726 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006727 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006728 const xmlChar *end, *cur;
6729
6730 cur = ns;
6731 do {
6732 while (IS_BLANK_CH(*cur))
6733 cur++;
6734 end = cur;
6735 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6736 end++;
6737 if (end == cur)
6738 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006739 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006740 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6741 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006742 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006743 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006744 NULL, (xmlNodePtr) attr,
6745 NULL,
6746 "((##any | ##other) | List of (xs:anyURI | "
6747 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006748 nsItem, NULL, NULL, NULL);
6749 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6750 } else {
6751 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006752 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006753 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6754 dictnsItem = NULL;
6755 } else {
6756 /*
6757 * Validate the item (anyURI).
6758 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006759 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006760 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6761 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6762 }
6763 /*
6764 * Avoid dublicate namespaces.
6765 */
6766 tmp = wildc->nsSet;
6767 while (tmp != NULL) {
6768 if (dictnsItem == tmp->value)
6769 break;
6770 tmp = tmp->next;
6771 }
6772 if (tmp == NULL) {
6773 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6774 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006775 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006776 return (-1);
6777 }
6778 tmp->value = dictnsItem;
6779 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006780 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006781 wildc->nsSet = tmp;
6782 else
6783 lastNs->next = tmp;
6784 lastNs = tmp;
6785 }
6786
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006787 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006788 xmlFree(nsItem);
6789 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006790 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006791 }
6792 return (ret);
6793}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006794
6795static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006796xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6797 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006798 xmlNodePtr node,
6799 int minOccurs,
6800 int maxOccurs) {
6801
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006802 if ((maxOccurs == 0) && ( minOccurs == 0))
6803 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006804 if (maxOccurs != UNBOUNDED) {
6805 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006806 * TODO: Maybe we should better not create the particle,
6807 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006808 * content model.
6809 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006810 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006811 * 3.9.6 Schema Component Constraint: Particle Correct
6812 *
6813 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006814 if (maxOccurs < 1) {
6815 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006816 * 2.2 {max occurs} must be greater than or equal to 1.
6817 */
6818 xmlSchemaPCustomAttrErr(ctxt,
6819 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006820 NULL, NULL,
6821 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006822 "The value must be greater than or equal to 1");
6823 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6824 } else if (minOccurs > maxOccurs) {
6825 /*
6826 * 2.1 {min occurs} must not be greater than {max occurs}.
6827 */
6828 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006829 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006830 NULL, NULL,
6831 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006832 "The value must not be greater than the value of 'maxOccurs'");
6833 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6834 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006835 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006836 return (0);
6837}
6838
Daniel Veillardc0826a72004-08-10 14:17:33 +00006839/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006840 * xmlSchemaParseAny:
6841 * @ctxt: a schema validation context
6842 * @schema: the schema being built
6843 * @node: a subtree containing XML Schema informations
6844 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006845 * Parsea a XML schema <any> element. A particle and wildcard
6846 * will be created (except if minOccurs==maxOccurs==0, in this case
6847 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006848 * *WARNING* this interface is highly subject to change
6849 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006850 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006851 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006852static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006853xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6854 xmlNodePtr node)
6855{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006856 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006857 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006858 xmlSchemaWildcardPtr wild;
6859 int min, max;
6860 xmlAttrPtr attr;
6861 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006862
6863 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6864 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006865 /*
6866 * Check for illegal attributes.
6867 */
6868 attr = node->properties;
6869 while (attr != NULL) {
6870 if (attr->ns == NULL) {
6871 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6872 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6873 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6874 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6875 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006876 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006877 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006878 }
6879 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006880 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006881 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006882 }
6883 attr = attr->next;
6884 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006885 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006886 /*
6887 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006888 */
6889 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6890 "(xs:nonNegativeInteger | unbounded)");
6891 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6892 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006893 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6894 /*
6895 * Create & parse the wildcard.
6896 */
6897 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6898 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006899 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006900 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006901 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006902 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006903 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006904 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006905 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006906 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006907 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006908 }
6909 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006910 xmlSchemaPContentErr(ctxt,
6911 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006912 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006913 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006914 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006915 /*
6916 * No component if minOccurs==maxOccurs==0.
6917 */
6918 if ((min == 0) && (max == 0)) {
6919 /* Don't free the wildcard, since it's already on the list. */
6920 return (NULL);
6921 }
6922 /*
6923 * Create the particle.
6924 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00006925 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006926 if (particle == NULL)
6927 return (NULL);
6928 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006929 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006930
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006931 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006932}
6933
6934/**
6935 * xmlSchemaParseNotation:
6936 * @ctxt: a schema validation context
6937 * @schema: the schema being built
6938 * @node: a subtree containing XML Schema informations
6939 *
6940 * parse a XML schema Notation declaration
6941 *
6942 * Returns the new structure or NULL in case of error
6943 */
6944static xmlSchemaNotationPtr
6945xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006946 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006947{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006948 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006949 xmlSchemaNotationPtr ret;
6950 xmlNodePtr child = NULL;
6951
6952 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6953 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006954 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006955 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006956 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6957 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006958 return (NULL);
6959 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006960 ret = xmlSchemaAddNotation(ctxt, schema, name,
6961 ctxt->targetNamespace, node);
6962 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006963 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006964 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006965
6966 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006967 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006968 child = child->next;
6969 }
6970
Daniel Veillard4255d502002-04-16 15:50:10 +00006971 child = node->children;
6972 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006973 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006974 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006975 }
6976 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006977 xmlSchemaPContentErr(ctxt,
6978 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006979 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006980 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006981 }
6982
6983 return (ret);
6984}
6985
6986/**
6987 * xmlSchemaParseAnyAttribute:
6988 * @ctxt: a schema validation context
6989 * @schema: the schema being built
6990 * @node: a subtree containing XML Schema informations
6991 *
6992 * parse a XML schema AnyAttrribute declaration
6993 * *WARNING* this interface is highly subject to change
6994 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006995 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006996 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006997static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006998xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6999 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007000{
Daniel Veillard3646d642004-06-02 19:19:14 +00007001 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007002 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007003 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007004
7005 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7006 return (NULL);
7007
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007008 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
7009 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00007010 if (ret == NULL) {
7011 return (NULL);
7012 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007013 /*
7014 * Check for illegal attributes.
7015 */
7016 attr = node->properties;
7017 while (attr != NULL) {
7018 if (attr->ns == NULL) {
7019 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7020 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7021 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007022 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007023 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007024 }
7025 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007026 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007027 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007028 }
7029 attr = attr->next;
7030 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007031 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007032 /*
7033 * Parse the namespace list.
7034 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007035 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007036 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007037 /*
7038 * And now for the children...
7039 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007040 child = node->children;
7041 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007042 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007043 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007044 }
7045 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007046 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007047 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007048 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007049 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007050 }
7051
7052 return (ret);
7053}
7054
7055
7056/**
7057 * xmlSchemaParseAttribute:
7058 * @ctxt: a schema validation context
7059 * @schema: the schema being built
7060 * @node: a subtree containing XML Schema informations
7061 *
7062 * parse a XML schema Attrribute declaration
7063 * *WARNING* this interface is highly subject to change
7064 *
William M. Bracke7091952004-05-11 15:09:58 +00007065 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007066 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007067static xmlSchemaBasicItemPtr
7068xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7069 xmlSchemaPtr schema,
7070 xmlNodePtr node,
7071 xmlSchemaItemListPtr uses,
7072 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007073{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007074 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7075 xmlSchemaAttributeUsePtr use = NULL;
7076 xmlNodePtr child = NULL;
7077 xmlAttrPtr attr;
7078 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7079 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7080 int nberrors, hasForm = 0, defValueType = 0;
7081
7082#define WXS_ATTR_DEF_VAL_DEFAULT 1
7083#define WXS_ATTR_DEF_VAL_FIXED 2
7084
7085 /*
7086 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7087 */
7088
7089 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7090 return (NULL);
7091 attr = xmlSchemaGetPropNode(node, "ref");
7092 if (attr != NULL) {
7093 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7094 NULL, attr, &tmpNs, &tmpName) != 0) {
7095 return (NULL);
7096 }
7097 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7098 return(NULL);
7099 isRef = 1;
7100 }
7101 nberrors = pctxt->nberrors;
7102 /*
7103 * Check for illegal attributes.
7104 */
7105 attr = node->properties;
7106 while (attr != NULL) {
7107 if (attr->ns == NULL) {
7108 if (isRef) {
7109 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7110 xmlSchemaPValAttrNodeID(pctxt, attr);
7111 goto attr_next;
7112 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7113 goto attr_next;
7114 }
7115 } else {
7116 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7117 goto attr_next;
7118 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7119 xmlSchemaPValAttrNodeID(pctxt, attr);
7120 goto attr_next;
7121 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7122 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7123 attr, &tmpNs, &tmpName);
7124 goto attr_next;
7125 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7126 /*
7127 * Evaluate the target namespace
7128 */
7129 hasForm = 1;
7130 attrValue = xmlSchemaGetNodeContent(pctxt,
7131 (xmlNodePtr) attr);
7132 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7133 ns = pctxt->targetNamespace;
7134 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7135 {
7136 xmlSchemaPSimpleTypeErr(pctxt,
7137 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7138 NULL, (xmlNodePtr) attr,
7139 NULL, "(qualified | unqualified)",
7140 attrValue, NULL, NULL, NULL);
7141 }
7142 goto attr_next;
7143 }
7144 }
7145 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7146
7147 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7148 /* TODO: Maybe we need to normalize the value beforehand. */
7149 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7150 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7151 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7152 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7153 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7154 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7155 else {
7156 xmlSchemaPSimpleTypeErr(pctxt,
7157 XML_SCHEMAP_INVALID_ATTR_USE,
7158 NULL, (xmlNodePtr) attr,
7159 NULL, "(optional | prohibited | required)",
7160 attrValue, NULL, NULL, NULL);
7161 }
7162 goto attr_next;
7163 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7164 /*
7165 * 3.2.3 : 1
7166 * default and fixed must not both be present.
7167 */
7168 if (defValue) {
7169 xmlSchemaPMutualExclAttrErr(pctxt,
7170 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7171 NULL, attr, "default", "fixed");
7172 } else {
7173 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7174 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7175 }
7176 goto attr_next;
7177 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7178 /*
7179 * 3.2.3 : 1
7180 * default and fixed must not both be present.
7181 */
7182 if (defValue) {
7183 xmlSchemaPMutualExclAttrErr(pctxt,
7184 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7185 NULL, attr, "default", "fixed");
7186 } else {
7187 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7188 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7189 }
7190 goto attr_next;
7191 }
7192 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7193 goto attr_next;
7194
7195 xmlSchemaPIllegalAttrErr(pctxt,
7196 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7197
7198attr_next:
7199 attr = attr->next;
7200 }
7201 /*
7202 * 3.2.3 : 2
7203 * If default and use are both present, use must have
7204 * the actual value optional.
7205 */
7206 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7207 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7208 xmlSchemaPSimpleTypeErr(pctxt,
7209 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7210 NULL, node, NULL,
7211 "(optional | prohibited | required)", NULL,
7212 "The value of the attribute 'use' must be 'optional' "
7213 "if the attribute 'default' is present",
7214 NULL, NULL);
7215 }
7216 /*
7217 * We want correct attributes.
7218 */
7219 if (nberrors != pctxt->nberrors)
7220 return(NULL);
7221 if (! isRef) {
7222 xmlSchemaAttributePtr attrDecl;
7223
7224 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7225 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7226 ns = pctxt->targetNamespace;
7227 /*
7228 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7229 * TODO: Move this to the component layer.
7230 */
7231 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7232 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7233 XML_SCHEMAP_NO_XSI,
7234 node, NULL,
7235 "The target namespace must not match '%s'",
7236 xmlSchemaInstanceNs, NULL);
7237 }
7238 attr = xmlSchemaGetPropNode(node, "name");
7239 if (attr == NULL) {
7240 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7241 NULL, node, "name", NULL);
7242 return (NULL);
7243 }
7244 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7245 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7246 return (NULL);
7247 }
7248 /*
7249 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7250 * TODO: Move this to the component layer.
7251 */
7252 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7253 xmlSchemaPSimpleTypeErr(pctxt,
7254 XML_SCHEMAP_NO_XMLNS,
7255 NULL, (xmlNodePtr) attr,
7256 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7257 "The value of the attribute must not match 'xmlns'",
7258 NULL, NULL);
7259 return (NULL);
7260 }
7261 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7262 goto check_children;
7263 /*
7264 * Create the attribute use component.
7265 */
7266 use = xmlSchemaAddAttributeUse(pctxt, node);
7267 if (use == NULL)
7268 return(NULL);
7269 use->occurs = occurs;
7270 /*
7271 * Create the attribute declaration.
7272 */
7273 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7274 if (attrDecl == NULL)
7275 return (NULL);
7276 if (tmpName != NULL) {
7277 attrDecl->typeName = tmpName;
7278 attrDecl->typeNs = tmpNs;
7279 }
7280 use->attrDecl = attrDecl;
7281 /*
7282 * Value constraint.
7283 */
7284 if (defValue != NULL) {
7285 attrDecl->defValue = defValue;
7286 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7287 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7288 }
7289 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7290 xmlSchemaQNameRefPtr ref;
7291
7292 /*
7293 * Create the attribute use component.
7294 */
7295 use = xmlSchemaAddAttributeUse(pctxt, node);
7296 if (use == NULL)
7297 return(NULL);
7298 /*
7299 * We need to resolve the reference at later stage.
7300 */
7301 WXS_ADD_PENDING(pctxt, use);
7302 use->occurs = occurs;
7303 /*
7304 * Create a QName reference to the attribute declaration.
7305 */
7306 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7307 tmpName, tmpNs);
7308 if (ref == NULL)
7309 return(NULL);
7310 /*
7311 * Assign the reference. This will be substituted for the
7312 * referenced attribute declaration when the QName is resolved.
7313 */
7314 use->attrDecl = WXS_ATTR_CAST ref;
7315 /*
7316 * Value constraint.
7317 */
7318 if (defValue != NULL)
7319 use->defValue = defValue;
7320 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7321 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7322 }
7323
7324check_children:
7325 /*
7326 * And now for the children...
7327 */
7328 child = node->children;
7329 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7330 xmlSchemaAttributeUseProhibPtr prohib;
7331
7332 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007333 xmlSchemaParseAnnotation(pctxt, child, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007334 child = child->next;
7335 }
7336 if (child != NULL) {
7337 xmlSchemaPContentErr(pctxt,
7338 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7339 NULL, node, child, NULL,
7340 "(annotation?)");
7341 }
7342 /*
7343 * Check for pointlessness of attribute prohibitions.
7344 */
7345 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7346 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7347 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7348 node, NULL,
7349 "Skipping attribute use prohibition, since it is "
7350 "pointless inside an <attributeGroup>",
7351 NULL, NULL, NULL);
7352 return(NULL);
7353 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7354 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7355 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7356 node, NULL,
7357 "Skipping attribute use prohibition, since it is "
7358 "pointless when extending a type",
7359 NULL, NULL, NULL);
7360 return(NULL);
7361 }
7362 if (! isRef) {
7363 tmpName = name;
7364 tmpNs = ns;
7365 }
7366 /*
7367 * Check for duplicate attribute prohibitions.
7368 */
7369 if (uses) {
7370 int i;
7371
7372 for (i = 0; i < uses->nbItems; i++) {
7373 use = uses->items[i];
7374 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7375 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7376 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7377 {
7378 xmlChar *str = NULL;
7379
7380 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7381 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7382 node, NULL,
7383 "Skipping duplicate attribute use prohibition '%s'",
7384 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7385 NULL, NULL);
7386 FREE_AND_NULL(str)
7387 return(NULL);
7388 }
7389 }
7390 }
7391 /*
7392 * Create the attribute prohibition helper component.
7393 */
7394 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7395 if (prohib == NULL)
7396 return(NULL);
7397 prohib->node = node;
7398 prohib->name = tmpName;
7399 prohib->targetNamespace = tmpNs;
7400 if (isRef) {
7401 /*
7402 * We need at least to resolve to the attribute declaration.
7403 */
7404 WXS_ADD_PENDING(pctxt, prohib);
7405 }
7406 return(WXS_BASIC_CAST prohib);
7407 } else {
7408 if (IS_SCHEMA(child, "annotation")) {
7409 /*
7410 * TODO: Should this go into the attr decl?
7411 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007412 use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007413 child = child->next;
7414 }
7415 if (isRef) {
7416 if (child != NULL) {
7417 if (IS_SCHEMA(child, "simpleType"))
7418 /*
7419 * 3.2.3 : 3.2
7420 * If ref is present, then all of <simpleType>,
7421 * form and type must be absent.
7422 */
7423 xmlSchemaPContentErr(pctxt,
7424 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7425 NULL, node, child, NULL,
7426 "(annotation?)");
7427 else
7428 xmlSchemaPContentErr(pctxt,
7429 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7430 NULL, node, child, NULL,
7431 "(annotation?)");
7432 }
7433 } else {
7434 if (IS_SCHEMA(child, "simpleType")) {
7435 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7436 /*
7437 * 3.2.3 : 4
7438 * type and <simpleType> must not both be present.
7439 */
7440 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7441 NULL, node, child,
7442 "The attribute 'type' and the <simpleType> child "
7443 "are mutually exclusive", NULL);
7444 } else
7445 WXS_ATTRUSE_TYPEDEF(use) =
7446 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7447 child = child->next;
7448 }
7449 if (child != NULL)
7450 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7451 NULL, node, child, NULL,
7452 "(annotation?, simpleType?)");
7453 }
7454 }
7455 return (WXS_BASIC_CAST use);
7456}
7457
7458
7459static xmlSchemaAttributePtr
7460xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7461 xmlSchemaPtr schema,
7462 xmlNodePtr node)
7463{
7464 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007465 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007466 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007467 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007468
7469 /*
7470 * Note that the w3c spec assumes the schema to be validated with schema
7471 * for schemas beforehand.
7472 *
7473 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007474 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007475 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7476 return (NULL);
7477 /*
7478 * 3.2.3 : 3.1
7479 * One of ref or name must be present, but not both
7480 */
7481 attr = xmlSchemaGetPropNode(node, "name");
7482 if (attr == NULL) {
7483 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7484 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007485 return (NULL);
7486 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007487 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7488 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7489 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007490 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007491 /*
7492 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7493 * TODO: Move this to the component layer.
7494 */
7495 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7496 xmlSchemaPSimpleTypeErr(pctxt,
7497 XML_SCHEMAP_NO_XMLNS,
7498 NULL, (xmlNodePtr) attr,
7499 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7500 "The value of the attribute must not match 'xmlns'",
7501 NULL, NULL);
7502 return (NULL);
7503 }
7504 /*
7505 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7506 * TODO: Move this to the component layer.
7507 * Or better leave it here and add it to the component layer
7508 * if we have a schema construction API.
7509 */
7510 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7511 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7512 XML_SCHEMAP_NO_XSI, node, NULL,
7513 "The target namespace must not match '%s'",
7514 xmlSchemaInstanceNs, NULL);
7515 }
7516
7517 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7518 pctxt->targetNamespace, node, 1);
7519 if (ret == NULL)
7520 return (NULL);
7521 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7522
7523 /*
7524 * Check for illegal attributes.
7525 */
7526 attr = node->properties;
7527 while (attr != NULL) {
7528 if (attr->ns == NULL) {
7529 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7530 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7531 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7532 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7533 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7534 {
7535 xmlSchemaPIllegalAttrErr(pctxt,
7536 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7537 }
7538 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7539 xmlSchemaPIllegalAttrErr(pctxt,
7540 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7541 }
7542 attr = attr->next;
7543 }
7544 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7545 node, "type", &ret->typeNs, &ret->typeName);
7546
7547 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007548 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007549 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007550 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007551 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007552 if (ret->defValue != NULL)
7553 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007554 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007555 * Attribute "default".
7556 */
7557 attr = xmlSchemaGetPropNode(node, "default");
7558 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007559 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007560 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007561 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007562 */
7563 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007564 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7565 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007566 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007567 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007568 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007569 /*
7570 * And now for the children...
7571 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007572 child = node->children;
7573 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007574 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007575 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007576 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007577 if (IS_SCHEMA(child, "simpleType")) {
7578 if (ret->typeName != NULL) {
7579 /*
7580 * 3.2.3 : 4
7581 * type and <simpleType> must not both be present.
7582 */
7583 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7584 NULL, node, child,
7585 "The attribute 'type' and the <simpleType> child "
7586 "are mutually exclusive", NULL);
7587 } else
7588 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7589 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007590 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007591 if (child != NULL)
7592 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7593 NULL, node, child, NULL,
7594 "(annotation?, simpleType?)");
7595
Daniel Veillard4255d502002-04-16 15:50:10 +00007596 return (ret);
7597}
7598
7599/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007600 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007601 * @ctxt: a schema validation context
7602 * @schema: the schema being built
7603 * @node: a subtree containing XML Schema informations
7604 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007605 * Parse an attribute group definition reference.
7606 * Note that a reference to an attribute group does not
7607 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007608 * *WARNING* this interface is highly subject to change
7609 *
7610 * Returns the attribute group or NULL in case of error.
7611 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007612static xmlSchemaQNameRefPtr
7613xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7614 xmlSchemaPtr schema,
7615 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007616{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007617 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007618 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007619 xmlAttrPtr attr;
7620 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007621
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007622 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007623 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007624
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007625 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007626 if (attr == NULL) {
7627 xmlSchemaPMissingAttrErr(pctxt,
7628 XML_SCHEMAP_S4S_ATTR_MISSING,
7629 NULL, node, "ref", NULL);
7630 return (NULL);
7631 }
7632 xmlSchemaPValAttrNodeQName(pctxt, schema,
7633 NULL, attr, &refNs, &ref);
7634 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7635 return(NULL);
7636
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007637 /*
7638 * Check for illegal attributes.
7639 */
7640 attr = node->properties;
7641 while (attr != NULL) {
7642 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007643 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007644 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007645 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007646 xmlSchemaPIllegalAttrErr(pctxt,
7647 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007648 }
7649 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007650 xmlSchemaPIllegalAttrErr(pctxt,
7651 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007652 }
7653 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007654 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007655 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007656 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7657
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007658 /*
7659 * And now for the children...
7660 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007661 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007662 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007663 /*
7664 * TODO: We do not have a place to store the annotation, do we?
7665 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007666 xmlSchemaParseAnnotation(pctxt, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007667 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007668 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007669 if (child != NULL) {
7670 xmlSchemaPContentErr(pctxt,
7671 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7672 NULL, node, child, NULL,
7673 "(annotation?)");
7674 }
7675
7676 /*
7677 * Handle attribute group redefinitions.
7678 */
7679 if (pctxt->isRedefine && pctxt->redef &&
7680 (pctxt->redef->item->type ==
7681 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7682 (ref == pctxt->redef->refName) &&
7683 (refNs == pctxt->redef->refTargetNs))
7684 {
7685 /*
7686 * SPEC src-redefine:
7687 * (7.1) "If it has an <attributeGroup> among its contents
7688 * the ·actual value· of whose ref [attribute] is the same
7689 * as the ·actual value· of its own name attribute plus
7690 * target namespace, then it must have exactly one such group."
7691 */
7692 if (pctxt->redefCounter != 0) {
7693 xmlChar *str = NULL;
7694
7695 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7696 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7697 "The redefining attribute group definition "
7698 "'%s' must not contain more than one "
7699 "reference to the redefined definition",
7700 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7701 FREE_AND_NULL(str);
7702 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007703 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007704 pctxt->redefCounter++;
7705 /*
7706 * URGENT TODO: How to ensure that the reference will not be
7707 * handled by the normal component resolution mechanism?
7708 */
7709 ret = xmlSchemaNewQNameRef(pctxt,
7710 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7711 if (ret == NULL)
7712 return(NULL);
7713 ret->node = node;
7714 pctxt->redef->reference = WXS_BASIC_CAST ret;
7715 } else {
7716 /*
7717 * Create a QName-reference helper component. We will substitute this
7718 * component for the attribute uses of the referenced attribute group
7719 * definition.
7720 */
7721 ret = xmlSchemaNewQNameRef(pctxt,
7722 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7723 if (ret == NULL)
7724 return(NULL);
7725 ret->node = node;
7726 /* Add to pending items, to be able to resolve the reference. */
7727 WXS_ADD_PENDING(pctxt, ret);
7728 }
7729 return (ret);
7730}
7731
7732/**
7733 * xmlSchemaParseAttributeGroupDefinition:
7734 * @pctxt: a schema validation context
7735 * @schema: the schema being built
7736 * @node: a subtree containing XML Schema informations
7737 *
7738 * parse a XML schema Attribute Group declaration
7739 * *WARNING* this interface is highly subject to change
7740 *
7741 * Returns the attribute group definition or NULL in case of error.
7742 */
7743static xmlSchemaAttributeGroupPtr
7744xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7745 xmlSchemaPtr schema,
7746 xmlNodePtr node)
7747{
7748 const xmlChar *name;
7749 xmlSchemaAttributeGroupPtr ret;
7750 xmlNodePtr child = NULL;
7751 xmlAttrPtr attr;
7752 int hasRefs = 0;
7753
7754 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7755 return (NULL);
7756
7757 attr = xmlSchemaGetPropNode(node, "name");
7758 if (attr == NULL) {
7759 xmlSchemaPMissingAttrErr(pctxt,
7760 XML_SCHEMAP_S4S_ATTR_MISSING,
7761 NULL, node, "name", NULL);
7762 return (NULL);
7763 }
7764 /*
7765 * The name is crucial, exit if invalid.
7766 */
7767 if (xmlSchemaPValAttrNode(pctxt,
7768 NULL, attr,
7769 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7770 return (NULL);
7771 }
7772 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7773 name, pctxt->targetNamespace, node);
7774 if (ret == NULL)
7775 return (NULL);
7776 /*
7777 * Check for illegal attributes.
7778 */
7779 attr = node->properties;
7780 while (attr != NULL) {
7781 if (attr->ns == NULL) {
7782 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7783 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7784 {
7785 xmlSchemaPIllegalAttrErr(pctxt,
7786 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7787 }
7788 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7789 xmlSchemaPIllegalAttrErr(pctxt,
7790 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7791 }
7792 attr = attr->next;
7793 }
7794 /* Attribute ID */
7795 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7796 /*
7797 * And now for the children...
7798 */
7799 child = node->children;
7800 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007801 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007802 child = child->next;
7803 }
7804 /*
7805 * Parse contained attribute decls/refs.
7806 */
7807 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7808 (xmlSchemaItemListPtr *) &(ret->attrUses),
7809 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7810 return(NULL);
7811 if (hasRefs)
7812 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7813 /*
7814 * Parse the attribute wildcard.
7815 */
7816 if (IS_SCHEMA(child, "anyAttribute")) {
7817 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7818 schema, child);
7819 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007820 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007821 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007822 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007823 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007824 NULL, node, child, NULL,
7825 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007826 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007827 return (ret);
7828}
7829
7830/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007831 * xmlSchemaPValAttrFormDefault:
7832 * @value: the value
7833 * @flags: the flags to be modified
7834 * @flagQualified: the specific flag for "qualified"
7835 *
7836 * Returns 0 if the value is valid, 1 otherwise.
7837 */
7838static int
7839xmlSchemaPValAttrFormDefault(const xmlChar *value,
7840 int *flags,
7841 int flagQualified)
7842{
7843 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7844 if ((*flags & flagQualified) == 0)
7845 *flags |= flagQualified;
7846 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007847 return (1);
7848
William M. Brack2f2a6632004-08-20 23:09:47 +00007849 return (0);
7850}
7851
7852/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007853 * xmlSchemaPValAttrBlockFinal:
7854 * @value: the value
7855 * @flags: the flags to be modified
7856 * @flagAll: the specific flag for "#all"
7857 * @flagExtension: the specific flag for "extension"
7858 * @flagRestriction: the specific flag for "restriction"
7859 * @flagSubstitution: the specific flag for "substitution"
7860 * @flagList: the specific flag for "list"
7861 * @flagUnion: the specific flag for "union"
7862 *
7863 * Validates the value of the attribute "final" and "block". The value
7864 * is converted into the specified flag values and returned in @flags.
7865 *
7866 * Returns 0 if the value is valid, 1 otherwise.
7867 */
7868
7869static int
7870xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007871 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007872 int flagAll,
7873 int flagExtension,
7874 int flagRestriction,
7875 int flagSubstitution,
7876 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007877 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007878{
7879 int ret = 0;
7880
7881 /*
7882 * TODO: This does not check for dublicate entries.
7883 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007884 if ((flags == NULL) || (value == NULL))
7885 return (-1);
7886 if (value[0] == 0)
7887 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007888 if (xmlStrEqual(value, BAD_CAST "#all")) {
7889 if (flagAll != -1)
7890 *flags |= flagAll;
7891 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007892 if (flagExtension != -1)
7893 *flags |= flagExtension;
7894 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007895 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007896 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007897 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007898 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007899 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007900 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 *flags |= flagUnion;
7902 }
7903 } else {
7904 const xmlChar *end, *cur = value;
7905 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007906
Daniel Veillardc0826a72004-08-10 14:17:33 +00007907 do {
7908 while (IS_BLANK_CH(*cur))
7909 cur++;
7910 end = cur;
7911 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7912 end++;
7913 if (end == cur)
7914 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007915 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007916 if (xmlStrEqual(item, BAD_CAST "extension")) {
7917 if (flagExtension != -1) {
7918 if ((*flags & flagExtension) == 0)
7919 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007920 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007921 ret = 1;
7922 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7923 if (flagRestriction != -1) {
7924 if ((*flags & flagRestriction) == 0)
7925 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007926 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007927 ret = 1;
7928 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7929 if (flagSubstitution != -1) {
7930 if ((*flags & flagSubstitution) == 0)
7931 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007932 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007933 ret = 1;
7934 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7935 if (flagList != -1) {
7936 if ((*flags & flagList) == 0)
7937 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007938 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007939 ret = 1;
7940 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7941 if (flagUnion != -1) {
7942 if ((*flags & flagUnion) == 0)
7943 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007944 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007945 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007946 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007947 ret = 1;
7948 if (item != NULL)
7949 xmlFree(item);
7950 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007951 } while ((ret == 0) && (*cur != 0));
7952 }
7953
Daniel Veillardc0826a72004-08-10 14:17:33 +00007954 return (ret);
7955}
7956
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007957static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007958xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007959 xmlSchemaIDCPtr idc,
7960 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007961 xmlAttrPtr attr,
7962 int isField)
7963{
7964 xmlNodePtr node;
7965
7966 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007967 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007968 * Schema Component Constraint: Selector Value OK
7969 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007970 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007971 * in [XPath].
7972 */
7973 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007974 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007975 XML_SCHEMAP_INTERNAL,
7976 "Internal error: xmlSchemaCheckCSelectorXPath, "
7977 "the selector is not specified.\n", NULL, NULL);
7978 return (-1);
7979 }
7980 if (attr == NULL)
7981 node = idc->node;
7982 else
7983 node = (xmlNodePtr) attr;
7984 if (selector->xpath == NULL) {
7985 xmlSchemaPCustomErr(ctxt,
7986 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007987 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007988 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007989 "The XPath expression of the selector is not valid", NULL);
7990 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7991 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007992 const xmlChar **nsArray = NULL;
7993 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007994 /*
7995 * Compile the XPath expression.
7996 */
7997 /*
7998 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007999 * TODO: Call xmlPatterncompile with different options for selector/
8000 * field.
8001 */
Daniel Veillard14b56432006-03-09 18:41:40 +00008002 if (attr == NULL)
8003 nsList == NULL;
8004 else
8005 nsList = xmlGetNsList(attr->doc, attr->parent);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008006 /*
8007 * Build an array of prefixes and namespaces.
8008 */
8009 if (nsList != NULL) {
8010 int i, count = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008011
8012 for (i = 0; nsList[i] != NULL; i++)
8013 count++;
8014
8015 nsArray = (const xmlChar **) xmlMalloc(
8016 (count * 2 + 1) * sizeof(const xmlChar *));
8017 if (nsArray == NULL) {
8018 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8019 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008020 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008021 return (-1);
8022 }
8023 for (i = 0; i < count; i++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008024 nsArray[2 * i] = nsList[i]->href;
8025 nsArray[2 * i + 1] = nsList[i]->prefix;
8026 }
8027 nsArray[count * 2] = NULL;
8028 xmlFree(nsList);
8029 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008030 /*
8031 * TODO: Differentiate between "selector" and "field".
8032 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008033 if (isField)
8034 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008035 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008036 else
8037 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008038 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008039 if (nsArray != NULL)
8040 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008041
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008042 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008043 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008044 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008045 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008046 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008047 "The XPath expression '%s' could not be "
8048 "compiled", selector->xpath);
8049 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008050 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008051 }
8052 return (0);
8053}
8054
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008055#define ADD_ANNOTATION(annot) \
8056 xmlSchemaAnnotPtr cur = item->annot; \
8057 if (item->annot == NULL) { \
8058 item->annot = annot; \
8059 return (annot); \
8060 } \
8061 cur = item->annot; \
8062 if (cur->next != NULL) { \
8063 cur = cur->next; \
8064 } \
8065 cur->next = annot;
8066
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008067/**
8068 * xmlSchemaAssignAnnotation:
8069 * @item: the schema component
8070 * @annot: the annotation
8071 *
8072 * Adds the annotation to the given schema component.
8073 *
8074 * Returns the given annotaion.
8075 */
8076static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008077xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8078 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008079{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008080 if ((annItem == NULL) || (annot == NULL))
8081 return (NULL);
8082 switch (annItem->type) {
8083 case XML_SCHEMA_TYPE_ELEMENT: {
8084 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8085 ADD_ANNOTATION(annot)
8086 }
8087 break;
8088 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8089 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8090 ADD_ANNOTATION(annot)
8091 }
8092 break;
8093 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8094 case XML_SCHEMA_TYPE_ANY: {
8095 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8096 ADD_ANNOTATION(annot)
8097 }
8098 break;
8099 case XML_SCHEMA_TYPE_PARTICLE:
8100 case XML_SCHEMA_TYPE_IDC_KEY:
8101 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008102 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008103 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8104 ADD_ANNOTATION(annot)
8105 }
8106 break;
8107 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8108 xmlSchemaAttributeGroupPtr item =
8109 (xmlSchemaAttributeGroupPtr) annItem;
8110 ADD_ANNOTATION(annot)
8111 }
8112 break;
8113 case XML_SCHEMA_TYPE_NOTATION: {
8114 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8115 ADD_ANNOTATION(annot)
8116 }
8117 break;
8118 case XML_SCHEMA_FACET_MININCLUSIVE:
8119 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8120 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8121 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8122 case XML_SCHEMA_FACET_TOTALDIGITS:
8123 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8124 case XML_SCHEMA_FACET_PATTERN:
8125 case XML_SCHEMA_FACET_ENUMERATION:
8126 case XML_SCHEMA_FACET_WHITESPACE:
8127 case XML_SCHEMA_FACET_LENGTH:
8128 case XML_SCHEMA_FACET_MAXLENGTH:
8129 case XML_SCHEMA_FACET_MINLENGTH: {
8130 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8131 ADD_ANNOTATION(annot)
8132 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008133 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008134 case XML_SCHEMA_TYPE_SIMPLE:
8135 case XML_SCHEMA_TYPE_COMPLEX: {
8136 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8137 ADD_ANNOTATION(annot)
8138 }
8139 break;
8140 case XML_SCHEMA_TYPE_GROUP: {
8141 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8142 ADD_ANNOTATION(annot)
8143 }
8144 break;
8145 case XML_SCHEMA_TYPE_SEQUENCE:
8146 case XML_SCHEMA_TYPE_CHOICE:
8147 case XML_SCHEMA_TYPE_ALL: {
8148 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8149 ADD_ANNOTATION(annot)
8150 }
8151 break;
8152 default:
8153 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008154 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008155 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008156 "Internal error: xmlSchemaAddAnnotation, "
8157 "The item is not a annotated schema component", NULL);
8158 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008159 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008160 return (annot);
8161}
8162
8163/**
8164 * xmlSchemaParseIDCSelectorAndField:
8165 * @ctxt: a schema validation context
8166 * @schema: the schema being built
8167 * @node: a subtree containing XML Schema informations
8168 *
8169 * Parses a XML Schema identity-contraint definition's
8170 * <selector> and <field> elements.
8171 *
8172 * Returns the parsed identity-constraint definition.
8173 */
8174static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008175xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008176 xmlSchemaIDCPtr idc,
8177 xmlNodePtr node,
8178 int isField)
8179{
8180 xmlSchemaIDCSelectPtr item;
8181 xmlNodePtr child = NULL;
8182 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008183
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008184 /*
8185 * Check for illegal attributes.
8186 */
8187 attr = node->properties;
8188 while (attr != NULL) {
8189 if (attr->ns == NULL) {
8190 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8191 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008192 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008193 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008194 }
8195 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008197 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008198 }
8199 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008200 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008201 /*
8202 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008203 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008204 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8205 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008206 xmlSchemaPErrMemory(ctxt,
8207 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008208 NULL);
8209 return (NULL);
8210 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008211 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008212 /*
8213 * Attribute "xpath" (mandatory).
8214 */
8215 attr = xmlSchemaGetPropNode(node, "xpath");
8216 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008217 xmlSchemaPMissingAttrErr(ctxt,
8218 XML_SCHEMAP_S4S_ATTR_MISSING,
8219 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008220 "name", NULL);
8221 } else {
8222 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8223 /*
8224 * URGENT TODO: "field"s have an other syntax than "selector"s.
8225 */
8226
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008227 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8228 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008229 xmlSchemaPErr(ctxt,
8230 (xmlNodePtr) attr,
8231 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008232 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008233 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008234 NULL, NULL);
8235 }
8236
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008237 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008238 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008239 /*
8240 * And now for the children...
8241 */
8242 child = node->children;
8243 if (IS_SCHEMA(child, "annotation")) {
8244 /*
8245 * Add the annotation to the parent IDC.
8246 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008247 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008248 xmlSchemaParseAnnotation(ctxt, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008249 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008250 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008251 if (child != NULL) {
8252 xmlSchemaPContentErr(ctxt,
8253 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008254 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008255 NULL, "(annotation?)");
8256 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008257
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008258 return (item);
8259}
8260
8261/**
8262 * xmlSchemaParseIDC:
8263 * @ctxt: a schema validation context
8264 * @schema: the schema being built
8265 * @node: a subtree containing XML Schema informations
8266 *
8267 * Parses a XML Schema identity-contraint definition.
8268 *
8269 * Returns the parsed identity-constraint definition.
8270 */
8271static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008272xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008273 xmlSchemaPtr schema,
8274 xmlNodePtr node,
8275 xmlSchemaTypeType idcCategory,
8276 const xmlChar *targetNamespace)
8277{
8278 xmlSchemaIDCPtr item = NULL;
8279 xmlNodePtr child = NULL;
8280 xmlAttrPtr attr;
8281 const xmlChar *name = NULL;
8282 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008283
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008284 /*
8285 * Check for illegal attributes.
8286 */
8287 attr = node->properties;
8288 while (attr != NULL) {
8289 if (attr->ns == NULL) {
8290 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8291 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8292 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8293 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008294 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008295 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008296 }
8297 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008298 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008299 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008300 }
8301 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008302 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008303 /*
8304 * Attribute "name" (mandatory).
8305 */
8306 attr = xmlSchemaGetPropNode(node, "name");
8307 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008308 xmlSchemaPMissingAttrErr(ctxt,
8309 XML_SCHEMAP_S4S_ATTR_MISSING,
8310 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008311 "name", NULL);
8312 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008313 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008314 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008315 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8316 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008317 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008318 /* Create the component. */
8319 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8320 idcCategory, node);
8321 if (item == NULL)
8322 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008323
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008324 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008325 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8326 /*
8327 * Attribute "refer" (mandatory).
8328 */
8329 attr = xmlSchemaGetPropNode(node, "refer");
8330 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008331 xmlSchemaPMissingAttrErr(ctxt,
8332 XML_SCHEMAP_S4S_ATTR_MISSING,
8333 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008334 "refer", NULL);
8335 } else {
8336 /*
8337 * Create a reference item.
8338 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008339 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008340 NULL, NULL);
8341 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008342 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008343 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008344 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008345 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008346 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008347 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008348 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008349 }
8350 }
8351 /*
8352 * And now for the children...
8353 */
8354 child = node->children;
8355 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008356 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008357 child = child->next;
8358 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008359 if (child == NULL) {
8360 xmlSchemaPContentErr(ctxt,
8361 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008362 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008363 "A child element is missing",
8364 "(annotation?, (selector, field+))");
8365 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008366 /*
8367 * Child element <selector>.
8368 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008369 if (IS_SCHEMA(child, "selector")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008370 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008371 item, child, 0);
8372 child = child->next;
8373 /*
8374 * Child elements <field>.
8375 */
8376 if (IS_SCHEMA(child, "field")) {
8377 do {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008378 field = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008379 item, child, 1);
8380 if (field != NULL) {
8381 field->index = item->nbFields;
8382 item->nbFields++;
8383 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008384 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008385 else
8386 item->fields = field;
8387 lastField = field;
8388 }
8389 child = child->next;
8390 } while (IS_SCHEMA(child, "field"));
8391 } else {
8392 xmlSchemaPContentErr(ctxt,
8393 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008394 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008395 NULL, "(annotation?, (selector, field+))");
8396 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008397 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008398 if (child != NULL) {
8399 xmlSchemaPContentErr(ctxt,
8400 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008401 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008402 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008403 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008404
8405 return (item);
8406}
8407
Daniel Veillardc0826a72004-08-10 14:17:33 +00008408/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008409 * xmlSchemaParseElement:
8410 * @ctxt: a schema validation context
8411 * @schema: the schema being built
8412 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008413 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008414 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008415 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008416 * *WARNING* this interface is highly subject to change
8417 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008418 * Returns the element declaration or a particle; NULL in case
8419 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008420 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008421static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008422xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008423 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008424{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008425 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008426 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008427 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008428 xmlNodePtr child = NULL;
8429 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008430 int min, max, isRef = 0;
8431 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008432
8433 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8434 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008435
Daniel Veillard4255d502002-04-16 15:50:10 +00008436 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008437 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008438
8439 if (isElemRef != NULL)
8440 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008441 /*
8442 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008443 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008444 * robust.
8445 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008446 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008447 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008448 if ((topLevel) || (attr == NULL)) {
8449 if (nameAttr == NULL) {
8450 xmlSchemaPMissingAttrErr(ctxt,
8451 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008452 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008453 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008454 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008455 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008456 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008457
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008458 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008459 child = node->children;
8460 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008461 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008462 child = child->next;
8463 }
8464 /*
8465 * Skip particle part if a global declaration.
8466 */
8467 if (topLevel)
8468 goto declaration_part;
8469 /*
8470 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008471 */
8472 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8473 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8474 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00008475 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008476 if (particle == NULL)
8477 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008478
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008479 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8480
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008481 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008482 const xmlChar *refNs = NULL, *ref = NULL;
8483 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008484 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008485 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008486 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008487 if (isElemRef != NULL)
8488 *isElemRef = 1;
8489
Daniel Veillardc0826a72004-08-10 14:17:33 +00008490 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008491 NULL, attr, &refNs, &ref);
8492 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008493 /*
8494 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008495 */
8496 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008497 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008498 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008499 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008500 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008501 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008502 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 attr = node->properties;
8504 while (attr != NULL) {
8505 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008506 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8507 xmlStrEqual(attr->name, BAD_CAST "name") ||
8508 xmlStrEqual(attr->name, BAD_CAST "id") ||
8509 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8510 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8511 {
8512 attr = attr->next;
8513 continue;
8514 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008515 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008516 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008517 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008518 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008519 "Only the attributes 'minOccurs', 'maxOccurs' and "
8520 "'id' are allowed in addition to 'ref'");
8521 break;
8522 }
8523 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8524 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008525 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008526 }
8527 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008528 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008529 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008530 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008531 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008532 if (child != NULL) {
8533 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008534 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008535 }
8536 if ((min == 0) && (max == 0))
8537 goto return_null;
8538 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008539 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008540 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008541 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008542 ref, refNs);
8543 if (refer == NULL)
8544 goto return_null;
8545 particle->children = (xmlSchemaTreeItemPtr) refer;
8546 particle->annot = annot;
8547 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008548 * Add the particle to pending components, since the reference
8549 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008550 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008551 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008552 return ((xmlSchemaBasicItemPtr) particle);
8553 }
8554 /*
8555 * The declaration part ===============================================
8556 */
8557declaration_part:
8558 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008559 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008560 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8561
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008562 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008563 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008564 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008565 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008566 * Evaluate the target namespace.
8567 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008568 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008569 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008570 } else {
8571 attr = xmlSchemaGetPropNode(node, "form");
8572 if (attr != NULL) {
8573 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8574 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008575 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008576 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008577 xmlSchemaPSimpleTypeErr(ctxt,
8578 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8579 NULL, (xmlNodePtr) attr,
8580 NULL, "(qualified | unqualified)",
8581 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008582 }
8583 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008584 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008585 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008586 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008587 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008588 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008589 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008590 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008591 * Check for illegal attributes.
8592 */
William M. Bracke7091952004-05-11 15:09:58 +00008593 attr = node->properties;
8594 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008595 if (attr->ns == NULL) {
8596 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8597 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008599 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008600 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008601 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008602 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8603 {
8604 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008605 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008606 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008607 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008608 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008609 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008610 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008611 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008612 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8613 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008614 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8615
8616 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008617 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008618 }
8619 }
8620 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008621
Daniel Veillardc0826a72004-08-10 14:17:33 +00008622 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008623 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008624 }
8625 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008626 }
William M. Bracke7091952004-05-11 15:09:58 +00008627 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008628 * Extract/validate attributes.
8629 */
8630 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008631 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008632 * Process top attributes of global element declarations here.
8633 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008634 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8635 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008636 xmlSchemaPValAttrQName(ctxt, schema,
8637 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008638 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008639 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008640 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008641 /*
8642 * Attribute "final".
8643 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008644 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008646 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8647 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8648 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8649 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008650 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8652 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008653 -1,
8654 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8655 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008656 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008657 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008658 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008659 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008660 attrValue, NULL, NULL, NULL);
8661 }
8662 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008663 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008664 /*
8665 * Attribute "block".
8666 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008667 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008668 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008669 /*
8670 * Apply default "block" values.
8671 */
8672 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8673 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8674 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8675 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8676 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8677 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008678 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008679 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8680 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008681 -1,
8682 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008683 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008684 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8685 xmlSchemaPSimpleTypeErr(ctxt,
8686 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008687 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008688 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008689 "restriction | substitution))", attrValue,
8690 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 }
8692 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008693 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008694 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008695
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008696 attr = xmlSchemaGetPropNode(node, "type");
8697 if (attr != NULL) {
8698 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008699 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008700 &(decl->namedTypeNs), &(decl->namedType));
8701 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008702 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008703 }
8704 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8705 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008706 if (attr != NULL) {
8707 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008708 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008709 /*
8710 * 3.3.3 : 1
8711 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008712 */
8713 xmlSchemaPMutualExclAttrErr(ctxt,
8714 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008715 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008716 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008717 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8718 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008719 }
William M. Bracke7091952004-05-11 15:09:58 +00008720 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008721 /*
8722 * And now for the children...
8723 */
William M. Bracke7091952004-05-11 15:09:58 +00008724 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008725 /*
8726 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008727 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008728 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008729 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008730 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008731 xmlSchemaPContentErr(ctxt,
8732 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008733 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008734 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008735 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008736 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008737 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008738 child = child->next;
8739 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008740 /*
8741 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008742 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008743 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008744 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008745 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008746 xmlSchemaPContentErr(ctxt,
8747 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008748 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008749 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008750 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008751 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008752 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008753 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008754 }
William M. Bracke7091952004-05-11 15:09:58 +00008755 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008756 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008757 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008758 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008759 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008760 } else if (IS_SCHEMA(child, "key")) {
8761 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008762 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008763 } else if (IS_SCHEMA(child, "keyref")) {
8764 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008765 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008766 }
8767 if (lastIDC != NULL)
8768 lastIDC->next = curIDC;
8769 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008770 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008771 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008772 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008773 }
8774 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008775 xmlSchemaPContentErr(ctxt,
8776 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008777 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008778 NULL, "(annotation?, ((simpleType | complexType)?, "
8779 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008780 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008781 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008782 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008783 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008784 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008785 * different layer.
8786 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008787 FREE_AND_NULL(des)
8788 if (topLevel)
8789 return ((xmlSchemaBasicItemPtr) decl);
8790 else {
8791 particle->children = (xmlSchemaTreeItemPtr) decl;
8792 return ((xmlSchemaBasicItemPtr) particle);
8793 }
8794
8795return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008796 FREE_AND_NULL(des);
8797 if (annot != NULL) {
8798 if (particle != NULL)
8799 particle->annot = NULL;
8800 if (decl != NULL)
8801 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008802 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008803 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008804 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008805}
8806
8807/**
8808 * xmlSchemaParseUnion:
8809 * @ctxt: a schema validation context
8810 * @schema: the schema being built
8811 * @node: a subtree containing XML Schema informations
8812 *
8813 * parse a XML schema Union definition
8814 * *WARNING* this interface is highly subject to change
8815 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008816 * Returns -1 in case of internal error, 0 in case of success and a positive
8817 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008818 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008819static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008820xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008821 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008822{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008823 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008824 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008825 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008826 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008827
8828 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008829 return (-1);
8830 /* Not a component, don't create it. */
8831 type = ctxt->ctxtType;
8832 /*
8833 * Mark the simple type as being of variety "union".
8834 */
8835 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008836 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008837 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8838 * then the ·simple ur-type definition·."
8839 */
8840 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008841 /*
8842 * Check for illegal attributes.
8843 */
8844 attr = node->properties;
8845 while (attr != NULL) {
8846 if (attr->ns == NULL) {
8847 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8848 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008849 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008850 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008851 }
8852 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008853 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008854 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008855 }
8856 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008857 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008858 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008859 /*
8860 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008861 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008862 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008863 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008864 if (attr != NULL) {
8865 const xmlChar *end;
8866 xmlChar *tmp;
8867 const xmlChar *localName, *nsName;
8868 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8869 xmlSchemaQNameRefPtr ref;
8870
8871 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008872 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008873 do {
8874 while (IS_BLANK_CH(*cur))
8875 cur++;
8876 end = cur;
8877 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8878 end++;
8879 if (end == cur)
8880 break;
8881 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008882 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008883 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008884 /*
8885 * Create the member type link.
8886 */
8887 link = (xmlSchemaTypeLinkPtr)
8888 xmlMalloc(sizeof(xmlSchemaTypeLink));
8889 if (link == NULL) {
8890 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8891 "allocating a type link", NULL);
8892 return (-1);
8893 }
8894 link->type = NULL;
8895 link->next = NULL;
8896 if (lastLink == NULL)
8897 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008898 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008899 lastLink->next = link;
8900 lastLink = link;
8901 /*
8902 * Create a reference item.
8903 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008904 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008905 localName, nsName);
8906 if (ref == NULL) {
8907 FREE_AND_NULL(tmp)
8908 return (-1);
8909 }
8910 /*
8911 * Assign the reference to the link, it will be resolved
8912 * later during fixup of the union simple type.
8913 */
8914 link->type = (xmlSchemaTypePtr) ref;
8915 }
8916 FREE_AND_NULL(tmp)
8917 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008918 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008919
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008920 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008921 /*
8922 * And now for the children...
8923 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008924 child = node->children;
8925 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008926 /*
8927 * Add the annotation to the simple type ancestor.
8928 */
8929 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008930 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008931 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008932 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008933 if (IS_SCHEMA(child, "simpleType")) {
8934 xmlSchemaTypePtr subtype, last = NULL;
8935
8936 /*
8937 * Anchor the member types in the "subtypes" field of the
8938 * simple type.
8939 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008940 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008941 subtype = (xmlSchemaTypePtr)
8942 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8943 if (subtype != NULL) {
8944 if (last == NULL) {
8945 type->subtypes = subtype;
8946 last = subtype;
8947 } else {
8948 last->next = subtype;
8949 last = subtype;
8950 }
8951 last->next = NULL;
8952 }
8953 child = child->next;
8954 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008955 }
8956 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008957 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008958 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008959 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008960 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008961 if ((attr == NULL) && (type->subtypes == NULL)) {
8962 /*
8963 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008964 * Either the memberTypes [attribute] of the <union> element must
8965 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008966 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008967 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008968 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008969 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008970 "Either the attribute 'memberTypes' or "
8971 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008972 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008973 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008974}
8975
8976/**
8977 * xmlSchemaParseList:
8978 * @ctxt: a schema validation context
8979 * @schema: the schema being built
8980 * @node: a subtree containing XML Schema informations
8981 *
8982 * parse a XML schema List definition
8983 * *WARNING* this interface is highly subject to change
8984 *
William M. Bracke7091952004-05-11 15:09:58 +00008985 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008986 * 1 in case of success.
8987 */
8988static xmlSchemaTypePtr
8989xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008990 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008991{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008992 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008993 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008994 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008995
8996 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8997 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008998 /* Not a component, don't create it. */
8999 type = ctxt->ctxtType;
9000 /*
9001 * Mark the type as being of variety "list".
9002 */
9003 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009004 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009005 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
9006 * then the ·simple ur-type definition·."
9007 */
9008 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00009009 /*
9010 * Check for illegal attributes.
9011 */
9012 attr = node->properties;
9013 while (attr != NULL) {
9014 if (attr->ns == NULL) {
9015 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9016 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009018 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009019 }
9020 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009021 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009022 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009023 }
9024 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009025 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009026
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009027 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009028
William M. Brack2f2a6632004-08-20 23:09:47 +00009029 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009030 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9031 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009032 *
9033 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9034 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009035 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009036 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9037 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009038 /*
9039 * And now for the children...
9040 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009041 child = node->children;
9042 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009043 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009044 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009045 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009046 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009047 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009048 /*
9049 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009050 * Either the itemType [attribute] or the <simpleType> [child] of
9051 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009052 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009053 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009054 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009055 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009056 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009057 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009058 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009059 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009060 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009061 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009062 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009063 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009064 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009065 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009066 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009067 "Either the attribute 'itemType' or the <simpleType> child "
9068 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009069 }
9070 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009071 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009072 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009073 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009074 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009075 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009076 (type->subtypes == NULL) &&
9077 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009078 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009079 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009080 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009081 "Either the attribute 'itemType' or the <simpleType> child "
9082 "must be present", NULL);
9083 }
9084 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009085}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009086
Daniel Veillard4255d502002-04-16 15:50:10 +00009087/**
9088 * xmlSchemaParseSimpleType:
9089 * @ctxt: a schema validation context
9090 * @schema: the schema being built
9091 * @node: a subtree containing XML Schema informations
9092 *
9093 * parse a XML schema Simple Type definition
9094 * *WARNING* this interface is highly subject to change
9095 *
William M. Bracke7091952004-05-11 15:09:58 +00009096 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009097 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009098 */
9099static xmlSchemaTypePtr
9100xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009101 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009102{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009103 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009104 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009105 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009106 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009107 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009108
9109 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9110 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009111
Daniel Veillardc0826a72004-08-10 14:17:33 +00009112 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009113 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009114 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009115 xmlSchemaPMissingAttrErr(ctxt,
9116 XML_SCHEMAP_S4S_ATTR_MISSING,
9117 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009118 "name", NULL);
9119 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009120 } else {
9121 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009122 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009123 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9124 return (NULL);
9125 /*
9126 * Skip built-in types.
9127 */
9128 if (ctxt->isS4S) {
9129 xmlSchemaTypePtr biType;
9130
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009131 if (ctxt->isRedefine) {
9132 /*
9133 * REDEFINE: Disallow redefinition of built-in-types.
9134 * TODO: It seems that the spec does not say anything
9135 * about this case.
9136 */
9137 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009138 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009139 "Redefinition of built-in simple types is not "
9140 "supported", NULL);
9141 return(NULL);
9142 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009143 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9144 if (biType != NULL)
9145 return (biType);
9146 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009147 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009148 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009149 /*
9150 * TargetNamespace:
9151 * SPEC "The ·actual value· of the targetNamespace [attribute]
9152 * of the <schema> ancestor element information item if present,
9153 * otherwise ·absent·.
9154 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009155 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009156#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009157 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009158#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009159 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009160 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009161 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009162#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009163 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009164 type = xmlSchemaAddType(ctxt, schema,
9165 XML_SCHEMA_TYPE_SIMPLE,
9166 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009167 ctxt->targetNamespace, node, 0);
9168#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009169 type = xmlSchemaAddType(ctxt, schema,
9170 XML_SCHEMA_TYPE_SIMPLE,
9171 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009172#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009173 if (type == NULL)
9174 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009175 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009176 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009177 /*
9178 * Check for illegal attributes.
9179 */
9180 attr = node->properties;
9181 while (attr != NULL) {
9182 if (attr->ns == NULL) {
9183 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009184 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009185 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009186 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009187 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009188 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009189 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009190 }
9191 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009192 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009193 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009194 /*
9195 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009196 *
9197 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009198 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009199 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9200 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009201 if (type == NULL)
9202 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009203 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009204 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009205 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9206 /*
9207 * Check for illegal attributes.
9208 */
9209 attr = node->properties;
9210 while (attr != NULL) {
9211 if (attr->ns == NULL) {
9212 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9213 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009214 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009215 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009216 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009217 }
9218 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009219 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009220 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009221 }
9222 attr = attr->next;
9223 }
9224 /*
9225 * Attribute "final".
9226 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009227 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009228 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009229 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9230 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9231 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9232 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9233 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9234 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009235 } else {
9236 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009237 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9238 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009239 XML_SCHEMAS_TYPE_FINAL_LIST,
9240 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9241
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009242 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009243 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009244 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009245 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009246 attrValue, NULL, NULL, NULL);
9247 }
9248 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009249 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009250 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009251 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009252 /*
9253 * And now for the children...
9254 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009255 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009256
Daniel Veillardc0826a72004-08-10 14:17:33 +00009257 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009258
Daniel Veillard4255d502002-04-16 15:50:10 +00009259 child = node->children;
9260 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009261 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009262 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009263 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009264 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009265 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009266 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009267 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009268 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009269 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009270 XML_SCHEMA_TYPE_SIMPLE);
9271 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009272 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009273 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009274 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009275 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009276 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009277 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009278 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009279 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009280 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009281 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009282 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009283 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009284 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009285 /*
9286 * REDEFINE: SPEC src-redefine (5)
9287 * "Within the [children], each <simpleType> must have a
9288 * <restriction> among its [children] ... the ·actual value· of whose
9289 * base [attribute] must be the same as the ·actual value· of its own
9290 * name attribute plus target namespace;"
9291 */
9292 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9293 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009294 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009295 "<simpleType> must have a <restriction> child", NULL);
9296 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009297
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009298 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009299 return (type);
9300}
9301
Daniel Veillard4255d502002-04-16 15:50:10 +00009302/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009303 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009304 * @ctxt: the parser context
9305 * @schema: the schema being built
9306 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009307 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009308 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009309 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009310 * We will return a particle component with a qname-component or
9311 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009312 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009313static xmlSchemaTreeItemPtr
9314xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9315 xmlSchemaPtr schema,
9316 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009317{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009318 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009319 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009320 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009321 const xmlChar *ref = NULL, *refNs = NULL;
9322 int min, max;
9323
9324 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009325 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009326
9327 attr = xmlSchemaGetPropNode(node, "ref");
9328 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009329 xmlSchemaPMissingAttrErr(ctxt,
9330 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009331 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009332 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009333 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009334 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009335 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009336 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009337 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009338 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009339 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009340 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009341 /*
9342 * Check for illegal attributes.
9343 */
9344 attr = node->properties;
9345 while (attr != NULL) {
9346 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009347 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009348 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9349 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9350 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009351 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009352 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009353 }
9354 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009355 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009356 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009357 }
9358 attr = attr->next;
9359 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009360 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00009361 item = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009362 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009363 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009364 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009365 * Create a qname-reference and set as the term; it will be substituted
9366 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009367 */
9368 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009369 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009370 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9371 /*
9372 * And now for the children...
9373 */
9374 child = node->children;
9375 /* TODO: Is annotation even allowed for a model group reference? */
9376 if (IS_SCHEMA(child, "annotation")) {
9377 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009378 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009379 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009380 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009381 child = child->next;
9382 }
9383 if (child != NULL) {
9384 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009385 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009386 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009387 "(annotation?)");
9388 }
9389 /*
9390 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9391 */
9392 if ((min == 0) && (max == 0))
9393 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009394
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009395 return ((xmlSchemaTreeItemPtr) item);
9396}
9397
9398/**
9399 * xmlSchemaParseModelGroupDefinition:
9400 * @ctxt: a schema validation context
9401 * @schema: the schema being built
9402 * @node: a subtree containing XML Schema informations
9403 *
9404 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009405 *
9406 * Note that the contraint src-redefine (6.2) can't be applied until
9407 * references have been resolved. So we will do this at the
9408 * component fixup level.
9409 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009410 * *WARNING* this interface is highly subject to change
9411 *
9412 * Returns -1 in case of error, 0 if the declaration is improper and
9413 * 1 in case of success.
9414 */
9415static xmlSchemaModelGroupDefPtr
9416xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9417 xmlSchemaPtr schema,
9418 xmlNodePtr node)
9419{
9420 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009421 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009422 xmlAttrPtr attr;
9423 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009424
9425 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009426 return (NULL);
9427
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009428 attr = xmlSchemaGetPropNode(node, "name");
9429 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009430 xmlSchemaPMissingAttrErr(ctxt,
9431 XML_SCHEMAP_S4S_ATTR_MISSING,
9432 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009433 "name", NULL);
9434 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009435 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009436 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9437 return (NULL);
9438 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009439 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9440 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009441 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009442 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009443 /*
9444 * Check for illegal attributes.
9445 */
9446 attr = node->properties;
9447 while (attr != NULL) {
9448 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009449 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009450 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009451 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009452 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009453 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009454 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009455 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009456 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009457 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009458 attr = attr->next;
9459 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009460 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009461 /*
9462 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009463 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009464 child = node->children;
9465 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009466 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009467 child = child->next;
9468 }
9469 if (IS_SCHEMA(child, "all")) {
9470 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9471 XML_SCHEMA_TYPE_ALL, 0);
9472 child = child->next;
9473 } else if (IS_SCHEMA(child, "choice")) {
9474 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9475 XML_SCHEMA_TYPE_CHOICE, 0);
9476 child = child->next;
9477 } else if (IS_SCHEMA(child, "sequence")) {
9478 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9479 XML_SCHEMA_TYPE_SEQUENCE, 0);
9480 child = child->next;
9481 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009482
9483
9484
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009485 if (child != NULL) {
9486 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009487 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009488 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009489 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009490 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009491 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009492}
9493
9494/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009495 * xmlSchemaCleanupDoc:
9496 * @ctxt: a schema validation context
9497 * @node: the root of the document.
9498 *
9499 * removes unwanted nodes in a schemas document tree
9500 */
9501static void
9502xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9503{
9504 xmlNodePtr delete, cur;
9505
9506 if ((ctxt == NULL) || (root == NULL)) return;
9507
9508 /*
9509 * Remove all the blank text nodes
9510 */
9511 delete = NULL;
9512 cur = root;
9513 while (cur != NULL) {
9514 if (delete != NULL) {
9515 xmlUnlinkNode(delete);
9516 xmlFreeNode(delete);
9517 delete = NULL;
9518 }
9519 if (cur->type == XML_TEXT_NODE) {
9520 if (IS_BLANK_NODE(cur)) {
9521 if (xmlNodeGetSpacePreserve(cur) != 1) {
9522 delete = cur;
9523 }
9524 }
9525 } else if ((cur->type != XML_ELEMENT_NODE) &&
9526 (cur->type != XML_CDATA_SECTION_NODE)) {
9527 delete = cur;
9528 goto skip_children;
9529 }
9530
9531 /*
9532 * Skip to next node
9533 */
9534 if (cur->children != NULL) {
9535 if ((cur->children->type != XML_ENTITY_DECL) &&
9536 (cur->children->type != XML_ENTITY_REF_NODE) &&
9537 (cur->children->type != XML_ENTITY_NODE)) {
9538 cur = cur->children;
9539 continue;
9540 }
9541 }
9542 skip_children:
9543 if (cur->next != NULL) {
9544 cur = cur->next;
9545 continue;
9546 }
9547
9548 do {
9549 cur = cur->parent;
9550 if (cur == NULL)
9551 break;
9552 if (cur == root) {
9553 cur = NULL;
9554 break;
9555 }
9556 if (cur->next != NULL) {
9557 cur = cur->next;
9558 break;
9559 }
9560 } while (cur != NULL);
9561 }
9562 if (delete != NULL) {
9563 xmlUnlinkNode(delete);
9564 xmlFreeNode(delete);
9565 delete = NULL;
9566 }
9567}
9568
William M. Brack2f2a6632004-08-20 23:09:47 +00009569
William M. Brack2f2a6632004-08-20 23:09:47 +00009570static void
9571xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9572{
9573 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9574 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9575
9576 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9577 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9578
9579 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9580 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9581 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9582 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9583 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9584 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9585 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9586 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9587
9588 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9589 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9590 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9591 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9592 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9593 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9594}
9595
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009596static int
9597xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009598 xmlSchemaPtr schema,
9599 xmlNodePtr node)
9600{
9601 xmlAttrPtr attr;
9602 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009603 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009604
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009605 /*
9606 * Those flags should be moved to the parser context flags,
9607 * since they are not visible at the component level. I.e.
9608 * they are used if processing schema *documents* only.
9609 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009610 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009611 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009612
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009613 /*
9614 * Since the version is of type xs:token, we won't bother to
9615 * check it.
9616 */
9617 /* REMOVED:
9618 attr = xmlSchemaGetPropNode(node, "version");
9619 if (attr != NULL) {
9620 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9621 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9622 HFAILURE;
9623 }
9624 */
9625 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9626 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009627 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009628 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9629 HFAILURE;
9630 if (res != 0) {
9631 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9632 goto exit;
9633 }
9634 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009635 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009636 if (attr != NULL) {
9637 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009638 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9639 XML_SCHEMAS_QUALIF_ELEM);
9640 HFAILURE;
9641 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009642 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009643 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009644 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009645 "(qualified | unqualified)", val, NULL, NULL, NULL);
9646 }
9647 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009648 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 if (attr != NULL) {
9650 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009651 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9652 XML_SCHEMAS_QUALIF_ATTR);
9653 HFAILURE;
9654 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009655 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009656 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009657 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009658 "(qualified | unqualified)", val, NULL, NULL, NULL);
9659 }
9660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009661 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009662 if (attr != NULL) {
9663 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009664 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009665 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9666 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9667 -1,
9668 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009669 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9670 HFAILURE;
9671 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009672 xmlSchemaPSimpleTypeErr(ctxt,
9673 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009674 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009675 "(#all | List of (extension | restriction | list | union))",
9676 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009677 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009678 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009679 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009680 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009681 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9682 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009683 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9684 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009685 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9686 HFAILURE;
9687 if (res != 0) {
9688 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009689 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009690 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009691 "(#all | List of (extension | restriction | substitution))",
9692 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009693 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009694 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009695
9696exit:
9697 if (oldErrs != ctxt->nberrors)
9698 res = ctxt->err;
9699 return(res);
9700exit_failure:
9701 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009702}
9703
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009704/**
9705 * xmlSchemaParseSchemaTopLevel:
9706 * @ctxt: a schema validation context
9707 * @schema: the schemas
9708 * @nodes: the list of top level nodes
9709 *
9710 * Returns the internal XML Schema structure built from the resource or
9711 * NULL in case of error
9712 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009713static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009714xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9715 xmlSchemaPtr schema, xmlNodePtr nodes)
9716{
9717 xmlNodePtr child;
9718 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009719 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009720
9721 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009722 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009723
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009724 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009725 child = nodes;
9726 while ((IS_SCHEMA(child, "include")) ||
9727 (IS_SCHEMA(child, "import")) ||
9728 (IS_SCHEMA(child, "redefine")) ||
9729 (IS_SCHEMA(child, "annotation"))) {
9730 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009731 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009732 if (schema->annot == NULL)
9733 schema->annot = annot;
9734 else
9735 xmlSchemaFreeAnnot(annot);
9736 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009737 tmpOldErrs = ctxt->nberrors;
9738 res = xmlSchemaParseImport(ctxt, schema, child);
9739 HFAILURE;
9740 HSTOP(ctxt);
9741 if (tmpOldErrs != ctxt->nberrors)
9742 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009743 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009744 tmpOldErrs = ctxt->nberrors;
9745 res = xmlSchemaParseInclude(ctxt, schema, child);
9746 HFAILURE;
9747 HSTOP(ctxt);
9748 if (tmpOldErrs != ctxt->nberrors)
9749 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009750 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009751 tmpOldErrs = ctxt->nberrors;
9752 res = xmlSchemaParseRedefine(ctxt, schema, child);
9753 HFAILURE;
9754 HSTOP(ctxt);
9755 if (tmpOldErrs != ctxt->nberrors)
9756 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009757 }
9758 child = child->next;
9759 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009760 /*
9761 * URGENT TODO: Change the functions to return int results.
9762 * We need especially to catch internal errors.
9763 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009764 while (child != NULL) {
9765 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009766 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009767 child = child->next;
9768 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009769 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009770 child = child->next;
9771 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009772 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009773 child = child->next;
9774 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009775 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009776 child = child->next;
9777 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009778 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009779 child = child->next;
9780 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009781 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009782 child = child->next;
9783 } else if (IS_SCHEMA(child, "notation")) {
9784 xmlSchemaParseNotation(ctxt, schema, child);
9785 child = child->next;
9786 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009787 xmlSchemaPContentErr(ctxt,
9788 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009789 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009790 NULL, "((include | import | redefine | annotation)*, "
9791 "(((simpleType | complexType | group | attributeGroup) "
9792 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009793 child = child->next;
9794 }
9795 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009796 /*
9797 * TODO: We should add all annotations.
9798 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009799 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009800 if (schema->annot == NULL)
9801 schema->annot = annot;
9802 else
9803 xmlSchemaFreeAnnot(annot);
9804 child = child->next;
9805 }
9806 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009807exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009808 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009809 if (oldErrs != ctxt->nberrors)
9810 res = ctxt->err;
9811 return(res);
9812exit_failure:
9813 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009814}
9815
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009816static xmlSchemaSchemaRelationPtr
9817xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009818{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009819 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009820
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009821 ret = (xmlSchemaSchemaRelationPtr)
9822 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009823 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009824 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009825 return(NULL);
9826 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009827 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009828 return(ret);
9829}
9830
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009831#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009832static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009833xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009834{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009835 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009836}
9837#endif
9838
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009839static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009840xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9841{
9842 xmlSchemaRedefPtr prev;
9843
9844 while (redef != NULL) {
9845 prev = redef;
9846 redef = redef->next;
9847 xmlFree(prev);
9848 }
9849}
9850
9851static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009852xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9853{
9854 /*
9855 * After the construction context has been freed, there will be
9856 * no schema graph available any more. Only the schema buckets
9857 * will stay alive, which are put into the "schemasImports" and
9858 * "includes" slots of the xmlSchema.
9859 */
9860 if (con->buckets != NULL)
9861 xmlSchemaItemListFree(con->buckets);
9862 if (con->pending != NULL)
9863 xmlSchemaItemListFree(con->pending);
9864 if (con->substGroups != NULL)
9865 xmlHashFree(con->substGroups,
9866 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009867 if (con->redefs != NULL)
9868 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009869 if (con->dict != NULL)
9870 xmlDictFree(con->dict);
9871 xmlFree(con);
9872}
9873
9874static xmlSchemaConstructionCtxtPtr
9875xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9876{
9877 xmlSchemaConstructionCtxtPtr ret;
9878
9879 ret = (xmlSchemaConstructionCtxtPtr)
9880 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9881 if (ret == NULL) {
9882 xmlSchemaPErrMemory(NULL,
9883 "allocating schema construction context", NULL);
9884 return (NULL);
9885 }
9886 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9887
9888 ret->buckets = xmlSchemaItemListCreate();
9889 if (ret->buckets == NULL) {
9890 xmlSchemaPErrMemory(NULL,
9891 "allocating list of schema buckets", NULL);
9892 xmlFree(ret);
9893 return (NULL);
9894 }
9895 ret->pending = xmlSchemaItemListCreate();
9896 if (ret->pending == NULL) {
9897 xmlSchemaPErrMemory(NULL,
9898 "allocating list of pending global components", NULL);
9899 xmlSchemaConstructionCtxtFree(ret);
9900 return (NULL);
9901 }
9902 ret->dict = dict;
9903 xmlDictReference(dict);
9904 return(ret);
9905}
9906
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009907static xmlSchemaParserCtxtPtr
9908xmlSchemaParserCtxtCreate(void)
9909{
9910 xmlSchemaParserCtxtPtr ret;
9911
9912 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9913 if (ret == NULL) {
9914 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9915 NULL);
9916 return (NULL);
9917 }
9918 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9919 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009920 ret->attrProhibs = xmlSchemaItemListCreate();
9921 if (ret->attrProhibs == NULL) {
9922 xmlFree(ret);
9923 return(NULL);
9924 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009925 return(ret);
9926}
9927
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009928/**
9929 * xmlSchemaNewParserCtxtUseDict:
9930 * @URL: the location of the schema
9931 * @dict: the dictionary to be used
9932 *
9933 * Create an XML Schemas parse context for that file/resource expected
9934 * to contain an XML Schemas file.
9935 *
9936 * Returns the parser context or NULL in case of error
9937 */
9938static xmlSchemaParserCtxtPtr
9939xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9940{
9941 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009942
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009943 ret = xmlSchemaParserCtxtCreate();
9944 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009945 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009946 ret->dict = dict;
9947 xmlDictReference(dict);
9948 if (URL != NULL)
9949 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009950 return (ret);
9951}
9952
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009953static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009954xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9955{
9956 if (vctxt->pctxt == NULL) {
9957 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009958 vctxt->pctxt =
9959 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009960 else
9961 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9962 if (vctxt->pctxt == NULL) {
9963 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9964 "failed to create a temp. parser context");
9965 return (-1);
9966 }
9967 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009968 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009969 vctxt->warning, vctxt->errCtxt);
9970 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9971 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009972 }
9973 return (0);
9974}
9975
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009976/**
9977 * xmlSchemaGetSchemaBucket:
9978 * @pctxt: the schema parser context
9979 * @schemaLocation: the URI of the schema document
9980 *
9981 * Returns a schema bucket if it was already parsed.
9982 *
9983 * Returns a schema bucket if it was already parsed from
9984 * @schemaLocation, NULL otherwise.
9985 */
9986static xmlSchemaBucketPtr
9987xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9988 const xmlChar *schemaLocation)
9989{
9990 xmlSchemaBucketPtr cur;
9991 xmlSchemaItemListPtr list;
9992
9993 list = pctxt->constructor->buckets;
9994 if (list->nbItems == 0)
9995 return(NULL);
9996 else {
9997 int i;
9998 for (i = 0; i < list->nbItems; i++) {
9999 cur = (xmlSchemaBucketPtr) list->items[i];
10000 /* Pointer comparison! */
10001 if (cur->schemaLocation == schemaLocation)
10002 return(cur);
10003 }
10004 }
10005 return(NULL);
10006}
10007
10008static xmlSchemaBucketPtr
10009xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10010 const xmlChar *schemaLocation,
10011 const xmlChar *targetNamespace)
10012{
10013 xmlSchemaBucketPtr cur;
10014 xmlSchemaItemListPtr list;
10015
10016 list = pctxt->constructor->buckets;
10017 if (list->nbItems == 0)
10018 return(NULL);
10019 else {
10020 int i;
10021 for (i = 0; i < list->nbItems; i++) {
10022 cur = (xmlSchemaBucketPtr) list->items[i];
10023 /* Pointer comparison! */
10024 if ((cur->origTargetNamespace == NULL) &&
10025 (cur->schemaLocation == schemaLocation) &&
10026 (cur->targetNamespace == targetNamespace))
10027 return(cur);
10028 }
10029 }
10030 return(NULL);
10031}
10032
10033
10034#define IS_BAD_SCHEMA_DOC(b) \
10035 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10036
10037static xmlSchemaBucketPtr
10038xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10039 const xmlChar *targetNamespace,
10040 int imported)
10041{
10042 xmlSchemaBucketPtr cur;
10043 xmlSchemaItemListPtr list;
10044
10045 list = pctxt->constructor->buckets;
10046 if (list->nbItems == 0)
10047 return(NULL);
10048 else {
10049 int i;
10050 for (i = 0; i < list->nbItems; i++) {
10051 cur = (xmlSchemaBucketPtr) list->items[i];
10052 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10053 (cur->origTargetNamespace == targetNamespace) &&
10054 ((imported && cur->imported) ||
10055 ((!imported) && (!cur->imported))))
10056 return(cur);
10057 }
10058 }
10059 return(NULL);
10060}
10061
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010062static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010063xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10064 xmlSchemaPtr schema,
10065 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010066{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010067 int oldFlags;
10068 xmlDocPtr oldDoc;
10069 xmlNodePtr node;
10070 int ret, oldErrs;
10071 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10072
10073 /*
10074 * Save old values; reset the *main* schema.
10075 * URGENT TODO: This is not good; move the per-document information
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010076 * to the parser. Get rid of passing the main schema to the
10077 * parsing functions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010078 */
10079 oldFlags = schema->flags;
10080 oldDoc = schema->doc;
10081 if (schema->flags != 0)
10082 xmlSchemaClearSchemaDefaults(schema);
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010083 schema->doc = bucket->doc;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010084 pctxt->schema = schema;
10085 /*
10086 * Keep the current target namespace on the parser *not* on the
10087 * main schema.
10088 */
10089 pctxt->targetNamespace = bucket->targetNamespace;
10090 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010091
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010092 if ((bucket->targetNamespace != NULL) &&
10093 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010094 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010095 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010096 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010097 pctxt->isS4S = 1;
10098 }
10099 /* Mark it as parsed, even if parsing fails. */
10100 bucket->parsed++;
10101 /* Compile the schema doc. */
10102 node = xmlDocGetRootElement(bucket->doc);
10103 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10104 if (ret != 0)
10105 goto exit;
10106 /* An empty schema; just get out. */
10107 if (node->children == NULL)
10108 goto exit;
10109 oldErrs = pctxt->nberrors;
10110 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10111 if (ret != 0)
10112 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010113 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010114 * TODO: Not nice, but I'm not 100% sure we will get always an error
10115 * as a result of the obove functions; so better rely on pctxt->err
10116 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010117 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010118 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10119 ret = pctxt->err;
10120 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010121 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010122
10123exit:
10124 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10125 /* Restore schema values. */
10126 schema->doc = oldDoc;
10127 schema->flags = oldFlags;
10128 return(ret);
10129}
10130
10131static int
10132xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10133 xmlSchemaPtr schema,
10134 xmlSchemaBucketPtr bucket)
10135{
10136 xmlSchemaParserCtxtPtr newpctxt;
10137 int res = 0;
10138
10139 if (bucket == NULL)
10140 return(0);
10141 if (bucket->parsed) {
10142 PERROR_INT("xmlSchemaParseNewDoc",
10143 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010144 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010145 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010146 if (bucket->doc == NULL) {
10147 PERROR_INT("xmlSchemaParseNewDoc",
10148 "parsing a schema doc, but there's no doc");
10149 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010150 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010151 if (pctxt->constructor == NULL) {
10152 PERROR_INT("xmlSchemaParseNewDoc",
10153 "no constructor");
10154 return(-1);
10155 }
10156 /* Create and init the temporary parser context. */
10157 newpctxt = xmlSchemaNewParserCtxtUseDict(
10158 (const char *) bucket->schemaLocation, pctxt->dict);
10159 if (newpctxt == NULL)
10160 return(-1);
10161 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010162 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010163 * TODO: Can we avoid that the parser knows about the main schema?
10164 * It would be better if he knows about the current schema bucket
10165 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010166 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010167 newpctxt->schema = schema;
10168 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010169 pctxt->errCtxt);
10170 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10171 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010172 newpctxt->counter = pctxt->counter;
10173
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010174
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010175 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10176
10177 /* Channel back errors and cleanup the temporary parser context. */
10178 if (res != 0)
10179 pctxt->err = res;
10180 pctxt->nberrors += newpctxt->nberrors;
10181 pctxt->counter = newpctxt->counter;
10182 newpctxt->constructor = NULL;
10183 /* Free the parser context. */
10184 xmlSchemaFreeParserCtxt(newpctxt);
10185 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010186}
William M. Brack2f2a6632004-08-20 23:09:47 +000010187
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010188static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010189xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10190 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010191{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010192 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010193
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010194 if (cur == NULL) {
10195 bucket->relations = rel;
10196 return;
10197 }
10198 while (cur->next != NULL)
10199 cur = cur->next;
10200 cur->next = rel;
10201}
10202
10203
10204static const xmlChar *
10205xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10206 xmlNodePtr ctxtNode)
10207{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010208 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010209 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010210 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010211 if (location != NULL) {
10212 if (ctxtNode == NULL)
10213 return(location);
10214 else {
10215 xmlChar *base, *URI;
10216 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010217
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010218 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10219 if (base == NULL) {
10220 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10221 } else {
10222 URI = xmlBuildURI(location, base);
10223 xmlFree(base);
10224 }
10225 if (URI != NULL) {
10226 ret = xmlDictLookup(dict, URI, -1);
10227 xmlFree(URI);
10228 return(ret);
10229 }
10230 }
10231 }
10232 return(NULL);
10233}
10234
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010235
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010236
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010237/**
10238 * xmlSchemaAddSchemaDoc:
10239 * @pctxt: a schema validation context
10240 * @schema: the schema being built
10241 * @node: a subtree containing XML Schema informations
10242 *
10243 * Parse an included (and to-be-redefined) XML schema document.
10244 *
10245 * Returns 0 on success, a positive error code on errors and
10246 * -1 in case of an internal or API error.
10247 */
10248
10249static int
10250xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10251 int type, /* import or include or redefine */
10252 const xmlChar *schemaLocation,
10253 xmlDocPtr schemaDoc,
10254 const char *schemaBuffer,
10255 int schemaBufferLen,
10256 xmlNodePtr invokingNode,
10257 const xmlChar *sourceTargetNamespace,
10258 const xmlChar *importNamespace,
10259 xmlSchemaBucketPtr *bucket)
10260{
10261 const xmlChar *targetNamespace = NULL;
10262 xmlSchemaSchemaRelationPtr relation = NULL;
10263 xmlDocPtr doc = NULL;
10264 int res = 0, err = 0, located = 0, preserveDoc = 0;
10265 xmlSchemaBucketPtr bkt = NULL;
10266
10267 if (bucket != NULL)
10268 *bucket = NULL;
10269
10270 switch (type) {
10271 case XML_SCHEMA_SCHEMA_IMPORT:
10272 case XML_SCHEMA_SCHEMA_MAIN:
10273 err = XML_SCHEMAP_SRC_IMPORT;
10274 break;
10275 case XML_SCHEMA_SCHEMA_INCLUDE:
10276 err = XML_SCHEMAP_SRC_INCLUDE;
10277 break;
10278 case XML_SCHEMA_SCHEMA_REDEFINE:
10279 err = XML_SCHEMAP_SRC_REDEFINE;
10280 break;
10281 }
10282
10283
10284 /* Special handling for the main schema:
10285 * skip the location and relation logic and just parse the doc.
10286 * We need just a bucket to be returned in this case.
10287 */
10288 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10289 goto doc_load;
10290
10291 /* Note that we expect the location to be an absulute URI. */
10292 if (schemaLocation != NULL) {
10293 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10294 if ((bkt != NULL) &&
10295 (pctxt->constructor->bucket == bkt)) {
10296 /* Report self-imports/inclusions/redefinitions. */
10297
10298 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10299 invokingNode, NULL,
10300 "The schema must not import/include/redefine itself",
10301 NULL, NULL);
10302 goto exit;
10303 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010304 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010305 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010306 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010307 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010308 relation = xmlSchemaSchemaRelationCreate();
10309 if (relation == NULL)
10310 return(-1);
10311 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10312 relation);
10313 relation->type = type;
10314
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010315 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010316 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010317 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010318 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010319 relation->importNamespace = importNamespace;
10320 if (schemaLocation == NULL) {
10321 /*
10322 * No location; this is just an import of the namespace.
10323 * Note that we don't assign a bucket to the relation
10324 * in this case.
10325 */
10326 goto exit;
10327 }
10328 targetNamespace = importNamespace;
10329 }
10330
10331 /* Did we already fetch the doc? */
10332 if (bkt != NULL) {
10333 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010334 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010335 /* We included/redefined and then try to import a schema. */
10336 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10337 invokingNode, NULL,
10338 "The schema document '%s' cannot be imported, since "
10339 "it was already included or redefined",
10340 schemaLocation, NULL);
10341 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010342 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010343 /* We imported and then try to include/redefine a schema. */
10344 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10345 invokingNode, NULL,
10346 "The schema document '%s' cannot be included or "
10347 "redefined, since it was already imported",
10348 schemaLocation, NULL);
10349 goto exit;
10350 }
10351 }
10352
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010353 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010354 /*
10355 * Given that the schemaLocation [attribute] is only a hint, it is open
10356 * to applications to ignore all but the first <import> for a given
10357 * namespace, regardless of the ·actual value· of schemaLocation, but
10358 * such a strategy risks missing useful information when new
10359 * schemaLocations are offered.
10360 *
10361 * We will use the first <import> that comes with a location.
10362 * Further <import>s *with* a location, will result in an error.
10363 * TODO: Better would be to just report a warning here, but
10364 * we'll try it this way until someone complains.
10365 *
10366 * Schema Document Location Strategy:
10367 * 3 Based on the namespace name, identify an existing schema document,
10368 * either as a resource which is an XML document or a <schema> element
10369 * information item, in some local schema repository;
10370 * 5 Attempt to resolve the namespace name to locate such a resource.
10371 *
10372 * NOTE: (3) and (5) are not supported.
10373 */
10374 if (bkt != NULL) {
10375 relation->bucket = bkt;
10376 goto exit;
10377 }
10378 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10379 importNamespace, 1);
10380
10381 if (bkt != NULL) {
10382 relation->bucket = bkt;
10383 if (bkt->schemaLocation == NULL) {
10384 /* First given location of the schema; load the doc. */
10385 bkt->schemaLocation = schemaLocation;
10386 } else {
10387 if (!xmlStrEqual(schemaLocation,
10388 bkt->schemaLocation)) {
10389 /*
10390 * Additional location given; just skip it.
10391 * URGENT TODO: We should report a warning here.
10392 * res = XML_SCHEMAP_SRC_IMPORT;
10393 */
10394 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10395 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10396 invokingNode, NULL,
10397 "Skipping import of schema located at '%s' for the "
10398 "namespace '%s', since this namespace was already "
10399 "imported with the schema located at '%s'",
10400 schemaLocation, importNamespace, bkt->schemaLocation);
10401 }
10402 goto exit;
10403 }
10404 }
10405 /*
10406 * No bucket + first location: load the doc and create a
10407 * bucket.
10408 */
10409 } else {
10410 /* <include> and <redefine> */
10411 if (bkt != NULL) {
10412
10413 if ((bkt->origTargetNamespace == NULL) &&
10414 (bkt->targetNamespace != sourceTargetNamespace)) {
10415 xmlSchemaBucketPtr chamel;
10416
10417 /*
10418 * Chameleon include/redefine: skip loading only if it was
10419 * aleady build for the targetNamespace of the including
10420 * schema.
10421 */
10422 /*
10423 * URGENT TODO: If the schema is a chameleon-include then copy
10424 * the components into the including schema and modify the
10425 * targetNamespace of those components, do nothing otherwise.
10426 * NOTE: This is currently worked-around by compiling the
10427 * chameleon for every destinct including targetNamespace; thus
10428 * not performant at the moment.
10429 * TODO: Check when the namespace in wildcards for chameleons
10430 * needs to be converted: before we built wildcard intersections
10431 * or after.
10432 * Answer: after!
10433 */
10434 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10435 schemaLocation, sourceTargetNamespace);
10436 if (chamel != NULL) {
10437 /* A fitting chameleon was already parsed; NOP. */
10438 relation->bucket = chamel;
10439 goto exit;
10440 }
10441 /*
10442 * We need to parse the chameleon again for a different
10443 * targetNamespace.
10444 * CHAMELEON TODO: Optimize this by only parsing the
10445 * chameleon once, and then copying the components to
10446 * the new targetNamespace.
10447 */
10448 bkt = NULL;
10449 } else {
10450 relation->bucket = bkt;
10451 goto exit;
10452 }
10453 }
10454 }
10455 if ((bkt != NULL) && (bkt->doc != NULL)) {
10456 PERROR_INT("xmlSchemaAddSchemaDoc",
10457 "trying to load a schema doc, but a doc is already "
10458 "assigned to the schema bucket");
10459 goto exit_failure;
10460 }
10461
10462doc_load:
10463 /*
10464 * Load the document.
10465 */
10466 if (schemaDoc != NULL) {
10467 doc = schemaDoc;
10468 /* Don' free this one, since it was provided by the caller. */
10469 preserveDoc = 1;
10470 /* TODO: Does the context or the doc hold the location? */
10471 if (schemaDoc->URL != NULL)
10472 schemaLocation = xmlDictLookup(pctxt->dict,
10473 schemaDoc->URL, -1);
10474
10475 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10476 xmlParserCtxtPtr parserCtxt;
10477
10478 parserCtxt = xmlNewParserCtxt();
10479 if (parserCtxt == NULL) {
10480 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10481 "allocating a parser context", NULL);
10482 goto exit_failure;
10483 }
10484 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10485 /*
10486 * TODO: Do we have to burden the schema parser dict with all
10487 * the content of the schema doc?
10488 */
10489 xmlDictFree(parserCtxt->dict);
10490 parserCtxt->dict = pctxt->dict;
10491 xmlDictReference(parserCtxt->dict);
10492 }
10493 if (schemaLocation != NULL) {
10494 /* Parse from file. */
10495 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10496 NULL, SCHEMAS_PARSE_OPTIONS);
10497 } else if (schemaBuffer != NULL) {
10498 /* Parse from memory buffer. */
10499 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10500 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10501 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10502 if (doc != NULL)
10503 doc->URL = schemaLocation;
10504 }
10505 /*
10506 * For <import>:
10507 * 2.1 The referent is (a fragment of) a resource which is an
10508 * XML document (see clause 1.1), which in turn corresponds to
10509 * a <schema> element information item in a well-formed information
10510 * set, which in turn corresponds to a valid schema.
10511 * TODO: (2.1) fragments of XML documents are not supported.
10512 *
10513 * 2.2 The referent is a <schema> element information item in
10514 * a well-formed information set, which in turn corresponds
10515 * to a valid schema.
10516 * TODO: (2.2) is not supported.
10517 */
10518 if (doc == NULL) {
10519 xmlErrorPtr lerr;
10520 lerr = xmlGetLastError();
10521 /*
10522 * Check if this a parser error, or if the document could
10523 * just not be located.
10524 * TODO: Try to find specific error codes to react only on
10525 * localisation failures.
10526 */
10527 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10528 /*
10529 * We assume a parser error here.
10530 */
10531 located = 1;
10532 /* TODO: Error code ?? */
10533 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10534 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10535 invokingNode, NULL,
10536 "Failed to parse the XML resource '%s'",
10537 schemaLocation, NULL);
10538 }
10539 }
10540 xmlFreeParserCtxt(parserCtxt);
10541 if ((doc == NULL) && located)
10542 goto exit_error;
10543 } else {
10544 xmlSchemaPErr(pctxt, NULL,
10545 XML_SCHEMAP_NOTHING_TO_PARSE,
10546 "No information for parsing was provided with the "
10547 "given schema parser context.\n",
10548 NULL, NULL);
10549 goto exit_failure;
10550 }
10551 /*
10552 * Preprocess the document.
10553 */
10554 if (doc != NULL) {
10555 xmlNodePtr docElem = NULL;
10556
10557 located = 1;
10558 docElem = xmlDocGetRootElement(doc);
10559 if (docElem == NULL) {
10560 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10561 invokingNode, NULL,
10562 "The document '%s' has no document element",
10563 schemaLocation, NULL);
10564 xmlFreeDoc(doc);
10565 doc = NULL;
10566 goto exit_error;
10567 }
10568 /*
10569 * Remove all the blank text nodes.
10570 */
10571 xmlSchemaCleanupDoc(pctxt, docElem);
10572 /*
10573 * Check the schema's top level element.
10574 */
10575 if (!IS_SCHEMA(docElem, "schema")) {
10576 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10577 invokingNode, NULL,
10578 "The XML document '%s' is not a schema document",
10579 schemaLocation, NULL);
10580 xmlFreeDoc(doc);
10581 doc = NULL;
10582 goto exit_error;
10583 }
10584 /*
10585 * Note that we don't apply a type check for the
10586 * targetNamespace value here.
10587 */
10588 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10589 "targetNamespace");
10590 }
10591
10592/* after_doc_loading: */
10593 if ((bkt == NULL) && located) {
10594 /* Only create a bucket if the schema was located. */
10595 bkt = xmlSchemaBucketCreate(pctxt, type,
10596 targetNamespace);
10597 if (bkt == NULL)
10598 goto exit_failure;
10599 }
10600 if (bkt != NULL) {
10601 bkt->schemaLocation = schemaLocation;
10602 bkt->located = located;
10603 if (doc != NULL) {
10604 bkt->doc = doc;
10605 bkt->targetNamespace = targetNamespace;
10606 bkt->origTargetNamespace = targetNamespace;
10607 if (preserveDoc)
10608 bkt->preserveDoc = 1;
10609 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010610 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010611 bkt->imported++;
10612 /*
10613 * Add it to the graph of schemas.
10614 */
10615 if (relation != NULL)
10616 relation->bucket = bkt;
10617 }
10618
10619exit:
10620 /*
10621 * Return the bucket explicitely; this is needed for the
10622 * main schema.
10623 */
10624 if (bucket != NULL)
10625 *bucket = bkt;
10626 return (0);
10627
10628exit_error:
10629 if ((doc != NULL) && (! preserveDoc)) {
10630 xmlFreeDoc(doc);
10631 if (bkt != NULL)
10632 bkt->doc = NULL;
10633 }
10634 return(pctxt->err);
10635
10636exit_failure:
10637 if ((doc != NULL) && (! preserveDoc)) {
10638 xmlFreeDoc(doc);
10639 if (bkt != NULL)
10640 bkt->doc = NULL;
10641 }
10642 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010643}
10644
William M. Brack2f2a6632004-08-20 23:09:47 +000010645/**
10646 * xmlSchemaParseImport:
10647 * @ctxt: a schema validation context
10648 * @schema: the schema being built
10649 * @node: a subtree containing XML Schema informations
10650 *
10651 * parse a XML schema Import definition
10652 * *WARNING* this interface is highly subject to change
10653 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010654 * Returns 0 in case of success, a positive error code if
10655 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010656 */
10657static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010658xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010659 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010660{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010661 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010662 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10663 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010664 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010665 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010666 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010667
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010668 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010669 return (-1);
10670
10671 /*
10672 * Check for illegal attributes.
10673 */
10674 attr = node->properties;
10675 while (attr != NULL) {
10676 if (attr->ns == NULL) {
10677 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10678 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10679 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010680 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010681 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010682 }
10683 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010684 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010685 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010686 }
10687 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010688 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010689 /*
10690 * Extract and validate attributes.
10691 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010692 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010693 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010694 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010695 xmlSchemaPSimpleTypeErr(pctxt,
10696 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010697 NULL, node,
10698 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010699 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010700 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010701 }
10702
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010703 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010704 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010705 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010706 xmlSchemaPSimpleTypeErr(pctxt,
10707 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010708 NULL, node,
10709 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010710 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010711 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010712 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010713 /*
10714 * And now for the children...
10715 */
10716 child = node->children;
10717 if (IS_SCHEMA(child, "annotation")) {
10718 /*
10719 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010720 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010721 */
10722 child = child->next;
10723 }
10724 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010725 xmlSchemaPContentErr(pctxt,
10726 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010727 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010728 "(annotation?)");
10729 }
10730 /*
10731 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010732 *
10733 * Note that it is important to use the original @targetNamespace
10734 * (or none at all), to rule out imports of schemas _with_ a
10735 * @targetNamespace if the importing schema is a chameleon schema
10736 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010737 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010738 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010739 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010740 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010741 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10742 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010743 * targetNamespace [attribute].
10744 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010745 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010746 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010747 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010748 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010749 "The value of the attribute 'namespace' must not match "
10750 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010751 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010752 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010753 }
10754 } else {
10755 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010756 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010757 * <schema> must have a targetNamespace [attribute].
10758 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010759 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010760 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010761 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010762 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010763 "The attribute 'namespace' must be existent if "
10764 "the importing schema has no target namespace",
10765 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010766 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010767 }
10768 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010769 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010770 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010771 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010772 if (schemaLocation != NULL)
10773 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10774 schemaLocation, node);
10775 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010776 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010777 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010778
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010779 if (ret != 0)
10780 return(ret);
10781
10782 /*
10783 * For <import>: "It is *not* an error for the application
10784 * schema reference strategy to fail."
10785 * So just don't parse if no schema document was found.
10786 * Note that we will get no bucket if the schema could not be
10787 * located or if there was no schemaLocation.
10788 */
10789 if ((bucket == NULL) && (schemaLocation != NULL)) {
10790 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10791 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10792 node, NULL,
10793 "Failed to locate a schema at location '%s'. "
10794 "Skipping the import", schemaLocation, NULL, NULL);
10795 }
10796
10797 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10798 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10799 }
10800
10801 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010802}
10803
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010804static int
10805xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10806 xmlSchemaPtr schema,
10807 xmlNodePtr node,
10808 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010809 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010810{
10811 xmlAttrPtr attr;
10812
10813 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10814 (schemaLocation == NULL))
10815 return (-1);
10816
10817 *schemaLocation = NULL;
10818 /*
10819 * Check for illegal attributes.
10820 * Applies for both <include> and <redefine>.
10821 */
10822 attr = node->properties;
10823 while (attr != NULL) {
10824 if (attr->ns == NULL) {
10825 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10826 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10827 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010828 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010829 }
10830 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10831 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010832 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010833 }
10834 attr = attr->next;
10835 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010836 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010837 /*
10838 * Preliminary step, extract the URI-Reference and make an URI
10839 * from the base.
10840 */
10841 /*
10842 * Attribute "schemaLocation" is mandatory.
10843 */
10844 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10845 if (attr != NULL) {
10846 xmlChar *base = NULL;
10847 xmlChar *uri = NULL;
10848
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010849 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010850 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10851 (const xmlChar **) schemaLocation) != 0)
10852 goto exit_error;
10853 base = xmlNodeGetBase(node->doc, node);
10854 if (base == NULL) {
10855 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10856 } else {
10857 uri = xmlBuildURI(*schemaLocation, base);
10858 xmlFree(base);
10859 }
10860 if (uri == NULL) {
10861 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10862 "could not build an URI from the schemaLocation")
10863 goto exit_failure;
10864 }
10865 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10866 xmlFree(uri);
10867 } else {
10868 xmlSchemaPMissingAttrErr(pctxt,
10869 XML_SCHEMAP_S4S_ATTR_MISSING,
10870 NULL, node, "schemaLocation", NULL);
10871 goto exit_error;
10872 }
10873 /*
10874 * Report self-inclusion and self-redefinition.
10875 */
10876 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010877 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010878 xmlSchemaPCustomErr(pctxt,
10879 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010880 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010881 "The schema document '%s' cannot redefine itself.",
10882 *schemaLocation);
10883 } else {
10884 xmlSchemaPCustomErr(pctxt,
10885 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010886 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010887 "The schema document '%s' cannot include itself.",
10888 *schemaLocation);
10889 }
10890 goto exit_error;
10891 }
10892
10893 return(0);
10894exit_error:
10895 return(pctxt->err);
10896exit_failure:
10897 return(-1);
10898}
10899
10900static int
10901xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10902 xmlSchemaPtr schema,
10903 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010904 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010905{
10906 xmlNodePtr child = NULL;
10907 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010908 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010909 int isChameleon = 0, wasChameleon = 0;
10910 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010911
10912 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10913 return (-1);
10914
10915 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010916 * Parse attributes. Note that the returned schemaLocation will
10917 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010918 */
10919 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010920 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010921 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010922 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010923 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010924 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010925 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010926 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10927 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010928 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010929 return(res);
10930 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010931 * If we get no schema bucket back, then this means that the schema
10932 * document could not be located or was broken XML or was not
10933 * a schema document.
10934 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010935 if ((bucket == NULL) || (bucket->doc == NULL)) {
10936 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10937 /*
10938 * WARNING for <include>:
10939 * We will raise an error if the schema cannot be located
10940 * for inclusions, since the that was the feedback from the
10941 * schema people. I.e. the following spec piece will *not* be
10942 * satisfied:
10943 * SPEC src-include: "It is not an error for the ·actual value· of the
10944 * schemaLocation [attribute] to fail to resolve it all, in which
10945 * case no corresponding inclusion is performed.
10946 * So do we need a warning report here?"
10947 */
10948 res = XML_SCHEMAP_SRC_INCLUDE;
10949 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10950 node, NULL,
10951 "Failed to load the document '%s' for inclusion",
10952 schemaLocation, NULL);
10953 } else {
10954 /*
10955 * NOTE: This was changed to raise an error even if no redefinitions
10956 * are specified.
10957 *
10958 * SPEC src-redefine (1)
10959 * "If there are any element information items among the [children]
10960 * other than <annotation> then the ·actual value· of the
10961 * schemaLocation [attribute] must successfully resolve."
10962 * TODO: Ask the WG if a the location has always to resolve
10963 * here as well!
10964 */
10965 res = XML_SCHEMAP_SRC_REDEFINE;
10966 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10967 node, NULL,
10968 "Failed to load the document '%s' for redefinition",
10969 schemaLocation, NULL);
10970 }
10971 } else {
10972 /*
10973 * Check targetNamespace sanity before parsing the new schema.
10974 * TODO: Note that we won't check further content if the
10975 * targetNamespace was bad.
10976 */
10977 if (bucket->origTargetNamespace != NULL) {
10978 /*
10979 * SPEC src-include (2.1)
10980 * "SII has a targetNamespace [attribute], and its ·actual
10981 * value· is identical to the ·actual value· of the targetNamespace
10982 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10983 */
10984 if (pctxt->targetNamespace == NULL) {
10985 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10986 XML_SCHEMAP_SRC_INCLUDE,
10987 node, NULL,
10988 "The target namespace of the included/redefined schema "
10989 "'%s' has to be absent, since the including/redefining "
10990 "schema has no target namespace",
10991 schemaLocation, NULL);
10992 goto exit_error;
10993 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10994 pctxt->targetNamespace)) {
10995 /* TODO: Change error function. */
10996 xmlSchemaPCustomErrExt(pctxt,
10997 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010998 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010999 "The target namespace '%s' of the included/redefined "
11000 "schema '%s' differs from '%s' of the "
11001 "including/redefining schema",
11002 bucket->origTargetNamespace, schemaLocation,
11003 pctxt->targetNamespace);
11004 goto exit_error;
11005 }
11006 } else if (pctxt->targetNamespace != NULL) {
11007 /*
11008 * Chameleons: the original target namespace will
11009 * differ from the resulting namespace.
11010 */
11011 isChameleon = 1;
11012 if (bucket->parsed &&
11013 (bucket->targetNamespace != pctxt->targetNamespace)) {
11014 /*
11015 * This is a sanity check, I dunno yet if this can happen.
11016 */
11017 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
11018 "trying to use an already parsed schema for a "
11019 "different targetNamespace");
11020 return(-1);
11021 }
11022 bucket->targetNamespace = pctxt->targetNamespace;
11023 }
11024 }
11025 /*
11026 * Parse the schema.
11027 */
11028 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11029 if (isChameleon) {
11030 /* TODO: Get rid of this flag on the schema itself. */
11031 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11032 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11033 } else
11034 wasChameleon = 1;
11035 }
11036 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11037 /* Restore chameleon flag. */
11038 if (isChameleon && (!wasChameleon))
11039 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11040 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011041 /*
11042 * And now for the children...
11043 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011044 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011045 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011046 /*
11047 * Parse (simpleType | complexType | group | attributeGroup))*
11048 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011049 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011050 /*
11051 * How to proceed if the redefined schema was not located?
11052 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011053 pctxt->isRedefine = 1;
11054 while (IS_SCHEMA(child, "annotation") ||
11055 IS_SCHEMA(child, "simpleType") ||
11056 IS_SCHEMA(child, "complexType") ||
11057 IS_SCHEMA(child, "group") ||
11058 IS_SCHEMA(child, "attributeGroup")) {
11059 if (IS_SCHEMA(child, "annotation")) {
11060 /*
11061 * TODO: discard or not?
11062 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011063 } else if (IS_SCHEMA(child, "simpleType")) {
11064 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11065 } else if (IS_SCHEMA(child, "complexType")) {
11066 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011067 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011068 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011069 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011070 xmlSchemaParseModelGroupDefinition(pctxt,
11071 schema, child);
11072 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011073 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011074 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11075 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011076 }
11077 child = child->next;
11078 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011079 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011080 pctxt->isRedefine = 0;
11081 } else {
11082 if (IS_SCHEMA(child, "annotation")) {
11083 /*
11084 * TODO: discard or not?
11085 */
11086 child = child->next;
11087 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011088 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011089 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011090 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011091 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11092 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011093 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011094 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11095 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011096 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011097 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011098 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011099 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011100 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011101 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011102
11103exit_error:
11104 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011105}
11106
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011107static int
11108xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11109 xmlNodePtr node)
11110{
11111 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011112#ifndef ENABLE_REDEFINE
11113 TODO
11114 return(0);
11115#endif
11116 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11117 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011118 if (res != 0)
11119 return(res);
11120 return(0);
11121}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011122
11123static int
11124xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11125 xmlNodePtr node)
11126{
11127 int res;
11128
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011129 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11130 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011131 if (res != 0)
11132 return(res);
11133 return(0);
11134}
11135
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011136/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011137 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011138 * @ctxt: a schema validation context
11139 * @schema: the schema being built
11140 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011141 * @type: the "compositor" type
11142 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011143 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011144 * parse a XML schema Sequence definition.
11145 * Applies parts of:
11146 * Schema Representation Constraint:
11147 * Redefinition Constraints and Semantics (src-redefine)
11148 * (6.1), (6.1.1), (6.1.2)
11149 *
11150 * Schema Component Constraint:
11151 * All Group Limited (cos-all-limited) (2)
11152 * TODO: Actually this should go to component-level checks,
11153 * but is done here due to performance. Move it to an other layer
11154 * is schema construction via an API is implemented.
11155 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011156 * *WARNING* this interface is highly subject to change
11157 *
William M. Bracke7091952004-05-11 15:09:58 +000011158 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011159 * 1 in case of success.
11160 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011161static xmlSchemaTreeItemPtr
11162xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11163 xmlNodePtr node, xmlSchemaTypeType type,
11164 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011165{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011166 xmlSchemaModelGroupPtr item;
11167 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011168 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011169 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011170 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011171
11172 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011173 return (NULL);
11174 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011175 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011176 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011177 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011178 if (item == NULL)
11179 return (NULL);
11180
11181 if (withParticle) {
11182 if (type == XML_SCHEMA_TYPE_ALL) {
11183 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011184 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011185 } else {
11186 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011187 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11188 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11189 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011190 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011191 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11192 /*
11193 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011194 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000011195 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011196 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011197 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011198 particle->children = (xmlSchemaTreeItemPtr) item;
11199 /*
11200 * Check for illegal attributes.
11201 */
11202 attr = node->properties;
11203 while (attr != NULL) {
11204 if (attr->ns == NULL) {
11205 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11206 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11207 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011208 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011209 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011210 }
11211 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011212 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011213 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011214 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011215 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011216 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011217 } else {
11218 /*
11219 * Check for illegal attributes.
11220 */
11221 attr = node->properties;
11222 while (attr != NULL) {
11223 if (attr->ns == NULL) {
11224 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011225 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011226 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011227 }
11228 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011229 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011230 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011231 }
11232 attr = attr->next;
11233 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011234 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011235
William M. Brack2f2a6632004-08-20 23:09:47 +000011236 /*
11237 * Extract and validate attributes.
11238 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011239 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011240 /*
11241 * And now for the children...
11242 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011243 child = node->children;
11244 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011245 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011246 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011247 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011248 if (type == XML_SCHEMA_TYPE_ALL) {
11249 xmlSchemaParticlePtr part, last = NULL;
11250
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011251 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011252 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011253 schema, child, &isElemRef, 0);
11254 /*
11255 * SPEC cos-all-limited (2)
11256 * "The {max occurs} of all the particles in the {particles}
11257 * of the ('all') group must be 0 or 1.
11258 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011259 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011260 if (isElemRef)
11261 hasRefs++;
11262 if (part->minOccurs > 1) {
11263 xmlSchemaPCustomErr(ctxt,
11264 XML_SCHEMAP_COS_ALL_LIMITED,
11265 NULL, child,
11266 "Invalid value for minOccurs (must be 0 or 1)",
11267 NULL);
11268 /* Reset to 1. */
11269 part->minOccurs = 1;
11270 }
11271 if (part->maxOccurs > 1) {
11272 xmlSchemaPCustomErr(ctxt,
11273 XML_SCHEMAP_COS_ALL_LIMITED,
11274 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011275 "Invalid value for maxOccurs (must be 0 or 1)",
11276 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011277 /* Reset to 1. */
11278 part->maxOccurs = 1;
11279 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011280 if (last == NULL)
11281 item->children = (xmlSchemaTreeItemPtr) part;
11282 else
11283 last->next = (xmlSchemaTreeItemPtr) part;
11284 last = part;
11285 }
11286 child = child->next;
11287 }
11288 if (child != NULL) {
11289 xmlSchemaPContentErr(ctxt,
11290 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011291 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011292 "(annotation?, (annotation?, element*)");
11293 }
11294 } else {
11295 /* choice + sequence */
11296 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11297
11298 while ((IS_SCHEMA(child, "element")) ||
11299 (IS_SCHEMA(child, "group")) ||
11300 (IS_SCHEMA(child, "any")) ||
11301 (IS_SCHEMA(child, "choice")) ||
11302 (IS_SCHEMA(child, "sequence"))) {
11303
11304 if (IS_SCHEMA(child, "element")) {
11305 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011306 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11307 if (part && isElemRef)
11308 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011309 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011310 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011311 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011312 if (part != NULL)
11313 hasRefs++;
11314 /*
11315 * Handle redefinitions.
11316 */
11317 if (ctxt->isRedefine && ctxt->redef &&
11318 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11319 part && part->children)
11320 {
11321 if ((xmlSchemaGetQNameRefName(part->children) ==
11322 ctxt->redef->refName) &&
11323 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11324 ctxt->redef->refTargetNs))
11325 {
11326 /*
11327 * SPEC src-redefine:
11328 * (6.1) "If it has a <group> among its contents at
11329 * some level the ·actual value· of whose ref
11330 * [attribute] is the same as the ·actual value· of
11331 * its own name attribute plus target namespace, then
11332 * all of the following must be true:"
11333 * (6.1.1) "It must have exactly one such group."
11334 */
11335 if (ctxt->redefCounter != 0) {
11336 xmlChar *str = NULL;
11337
11338 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11339 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11340 "The redefining model group definition "
11341 "'%s' must not contain more than one "
11342 "reference to the redefined definition",
11343 xmlSchemaFormatQName(&str,
11344 ctxt->redef->refTargetNs,
11345 ctxt->redef->refName),
11346 NULL);
11347 FREE_AND_NULL(str)
11348 part = NULL;
11349 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11350 ((WXS_PARTICLE(part))->maxOccurs != 1))
11351 {
11352 xmlChar *str = NULL;
11353 /*
11354 * SPEC src-redefine:
11355 * (6.1.2) "The ·actual value· of both that
11356 * group's minOccurs and maxOccurs [attribute]
11357 * must be 1 (or ·absent·).
11358 */
11359 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11360 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11361 "The redefining model group definition "
11362 "'%s' must not contain a reference to the "
11363 "redefined definition with a "
11364 "maxOccurs/minOccurs other than 1",
11365 xmlSchemaFormatQName(&str,
11366 ctxt->redef->refTargetNs,
11367 ctxt->redef->refName),
11368 NULL);
11369 FREE_AND_NULL(str)
11370 part = NULL;
11371 }
11372 ctxt->redef->reference = WXS_BASIC_CAST part;
11373 ctxt->redefCounter++;
11374 }
11375 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011376 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011377 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011378 xmlSchemaParseAny(ctxt, schema, child);
11379 } else if (IS_SCHEMA(child, "choice")) {
11380 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11381 XML_SCHEMA_TYPE_CHOICE, 1);
11382 } else if (IS_SCHEMA(child, "sequence")) {
11383 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11384 XML_SCHEMA_TYPE_SEQUENCE, 1);
11385 }
11386 if (part != NULL) {
11387 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011388 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011389 else
11390 last->next = part;
11391 last = part;
11392 }
11393 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011394 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011395 if (child != NULL) {
11396 xmlSchemaPContentErr(ctxt,
11397 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011398 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011399 "(annotation?, (element | group | choice | sequence | any)*)");
11400 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011401 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011402 if ((max == 0) && (min == 0))
11403 return (NULL);
11404 if (hasRefs) {
11405 /*
11406 * We need to resolve references.
11407 */
11408 WXS_ADD_PENDING(ctxt, item);
11409 }
11410 if (withParticle)
11411 return ((xmlSchemaTreeItemPtr) particle);
11412 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011413 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011414}
11415
11416/**
11417 * xmlSchemaParseRestriction:
11418 * @ctxt: a schema validation context
11419 * @schema: the schema being built
11420 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011421 *
11422 * parse a XML schema Restriction definition
11423 * *WARNING* this interface is highly subject to change
11424 *
11425 * Returns the type definition or NULL in case of error
11426 */
11427static xmlSchemaTypePtr
11428xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011429 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011430{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011431 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011432 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011433 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011434
11435 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11436 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011437 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011438 type = ctxt->ctxtType;
11439 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011440
11441 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011442 * Check for illegal attributes.
11443 */
11444 attr = node->properties;
11445 while (attr != NULL) {
11446 if (attr->ns == NULL) {
11447 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11448 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011449 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011450 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011451 }
11452 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011453 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011454 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011455 }
11456 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011457 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011458 /*
11459 * Extract and validate attributes.
11460 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011461 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011462 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011463 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011465 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011466 * Extract the base type. The "base" attribute is mandatory if inside
11467 * a complex type or if redefining.
11468 *
11469 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011470 * among its [children]), the simple type definition which is
11471 * the {content type} of the type definition ·resolved· to by
11472 * the ·actual value· of the base [attribute]"
11473 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011474 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011475 &(type->baseNs), &(type->base)) == 0)
11476 {
11477 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11478 xmlSchemaPMissingAttrErr(ctxt,
11479 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011480 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011481 } else if ((ctxt->isRedefine) &&
11482 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11483 {
11484 if (type->base == NULL) {
11485 xmlSchemaPMissingAttrErr(ctxt,
11486 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011487 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011488 } else if ((! xmlStrEqual(type->base, type->name)) ||
11489 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11490 {
11491 xmlChar *str1 = NULL, *str2 = NULL;
11492 /*
11493 * REDEFINE: SPEC src-redefine (5)
11494 * "Within the [children], each <simpleType> must have a
11495 * <restriction> among its [children] ... the ·actual value· of
11496 * whose base [attribute] must be the same as the ·actual value·
11497 * of its own name attribute plus target namespace;"
11498 */
11499 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011500 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011501 "value '%s' of the 'base' attribute does not match the "
11502 "type's designation '%s'",
11503 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11504 xmlSchemaFormatQName(&str1, type->targetNamespace,
11505 type->name), NULL);
11506 FREE_AND_NULL(str1);
11507 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011508 /* Avoid confusion and erase the values. */
11509 type->base = NULL;
11510 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011511 }
11512 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011513 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011514 /*
11515 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011516 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011517 child = node->children;
11518 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011519 /*
11520 * Add the annotation to the simple type ancestor.
11521 */
11522 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011523 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011524 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011525 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011526 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11527 /*
11528 * Corresponds to <simpleType><restriction><simpleType>.
11529 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011530 if (IS_SCHEMA(child, "simpleType")) {
11531 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011532 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011533 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011534 * Either the base [attribute] or the simpleType [child] of the
11535 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011536 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011537 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011538 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011539 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011540 "The attribute 'base' and the <simpleType> child are "
11541 "mutually exclusive", NULL);
11542 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011543 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011544 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011545 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011546 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011547 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011548 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011549 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011550 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011551 "Either the attribute 'base' or a <simpleType> child "
11552 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011553 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011554 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11555 /*
11556 * Corresponds to <complexType><complexContent><restriction>...
11557 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011558 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011559 * Model groups <all>, <choice> and <sequence>.
11560 */
11561 if (IS_SCHEMA(child, "all")) {
11562 type->subtypes = (xmlSchemaTypePtr)
11563 xmlSchemaParseModelGroup(ctxt, schema, child,
11564 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011565 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011566 } else if (IS_SCHEMA(child, "choice")) {
11567 type->subtypes = (xmlSchemaTypePtr)
11568 xmlSchemaParseModelGroup(ctxt,
11569 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11570 child = child->next;
11571 } else if (IS_SCHEMA(child, "sequence")) {
11572 type->subtypes = (xmlSchemaTypePtr)
11573 xmlSchemaParseModelGroup(ctxt, schema, child,
11574 XML_SCHEMA_TYPE_SEQUENCE, 1);
11575 child = child->next;
11576 /*
11577 * Model group reference <group>.
11578 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011579 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011580 type->subtypes = (xmlSchemaTypePtr)
11581 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011582 /*
11583 * Note that the reference will be resolved in
11584 * xmlSchemaResolveTypeReferences();
11585 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011586 child = child->next;
11587 }
11588 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011589 /*
11590 * Corresponds to <complexType><simpleContent><restriction>...
11591 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011592 * "1.1 the simple type definition corresponding to the <simpleType>
11593 * among the [children] of <restriction> if there is one;"
11594 */
11595 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011596 /*
11597 * We will store the to-be-restricted simple type in
11598 * type->contentTypeDef *temporarily*.
11599 */
11600 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011601 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011602 if ( type->contentTypeDef == NULL)
11603 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011604 child = child->next;
11605 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011606 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011607
11608 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011609 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011610 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011611 /*
11612 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011613 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011614 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011615
Daniel Veillard01fa6152004-06-29 17:04:39 +000011616 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011617 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011618 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011619 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011620 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11621 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011622 * *Single Facet Value*
11623 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011624 while ((IS_SCHEMA(child, "minInclusive")) ||
11625 (IS_SCHEMA(child, "minExclusive")) ||
11626 (IS_SCHEMA(child, "maxInclusive")) ||
11627 (IS_SCHEMA(child, "maxExclusive")) ||
11628 (IS_SCHEMA(child, "totalDigits")) ||
11629 (IS_SCHEMA(child, "fractionDigits")) ||
11630 (IS_SCHEMA(child, "pattern")) ||
11631 (IS_SCHEMA(child, "enumeration")) ||
11632 (IS_SCHEMA(child, "whiteSpace")) ||
11633 (IS_SCHEMA(child, "length")) ||
11634 (IS_SCHEMA(child, "maxLength")) ||
11635 (IS_SCHEMA(child, "minLength"))) {
11636 facet = xmlSchemaParseFacet(ctxt, schema, child);
11637 if (facet != NULL) {
11638 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011639 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011640 else
11641 lastfacet->next = facet;
11642 lastfacet = facet;
11643 lastfacet->next = NULL;
11644 }
11645 child = child->next;
11646 }
11647 /*
11648 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011649 */
11650 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011651 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11652
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011653 facet = type->facets;
11654 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011655 facetLink = (xmlSchemaFacetLinkPtr)
11656 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011657 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011658 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011659 xmlFree(facetLink);
11660 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011661 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011662 facetLink->facet = facet;
11663 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011664 if (lastFacetLink == NULL)
11665 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011666 else
11667 lastFacetLink->next = facetLink;
11668 lastFacetLink = facetLink;
11669 facet = facet->next;
11670 } while (facet != NULL);
11671 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011672 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011673 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11674 /*
11675 * Attribute uses/declarations.
11676 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011677 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11678 (xmlSchemaItemListPtr *) &(type->attrUses),
11679 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11680 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011681 /*
11682 * Attribute wildcard.
11683 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011684 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011685 type->attributeWildcard =
11686 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011687 child = child->next;
11688 }
11689 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011690 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011691 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11692 xmlSchemaPContentErr(ctxt,
11693 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011694 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011695 "annotation?, (group | all | choice | sequence)?, "
11696 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011697 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011698 xmlSchemaPContentErr(ctxt,
11699 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011700 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011701 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11702 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11703 "length | minLength | maxLength | enumeration | whiteSpace | "
11704 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11705 } else {
11706 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011707 xmlSchemaPContentErr(ctxt,
11708 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011709 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011710 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11711 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11712 "length | minLength | maxLength | enumeration | whiteSpace | "
11713 "pattern)*))");
11714 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011715 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011716 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011717}
11718
11719/**
11720 * xmlSchemaParseExtension:
11721 * @ctxt: a schema validation context
11722 * @schema: the schema being built
11723 * @node: a subtree containing XML Schema informations
11724 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011725 * Parses an <extension>, which is found inside a
11726 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011727 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011728 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011729 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011730 */
11731static xmlSchemaTypePtr
11732xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011733 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011734{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011735 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011736 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011737 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011738
11739 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11740 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011741 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011742 type = ctxt->ctxtType;
11743 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011744
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011745 /*
11746 * Check for illegal attributes.
11747 */
11748 attr = node->properties;
11749 while (attr != NULL) {
11750 if (attr->ns == NULL) {
11751 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11752 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011753 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011754 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011755 }
11756 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011757 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011758 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011759 }
11760 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011761 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011762
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011763 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011764
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011765 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011766 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011767 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011768 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11769 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011770 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011771 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011772 XML_SCHEMAP_S4S_ATTR_MISSING,
11773 NULL, node, "base", NULL);
11774 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011775 /*
11776 * And now for the children...
11777 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011778 child = node->children;
11779 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011780 /*
11781 * Add the annotation to the type ancestor.
11782 */
11783 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011784 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011785 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011786 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011787 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11788 /*
11789 * Corresponds to <complexType><complexContent><extension>... and:
11790 *
11791 * Model groups <all>, <choice>, <sequence> and <group>.
11792 */
11793 if (IS_SCHEMA(child, "all")) {
11794 type->subtypes = (xmlSchemaTypePtr)
11795 xmlSchemaParseModelGroup(ctxt, schema,
11796 child, XML_SCHEMA_TYPE_ALL, 1);
11797 child = child->next;
11798 } else if (IS_SCHEMA(child, "choice")) {
11799 type->subtypes = (xmlSchemaTypePtr)
11800 xmlSchemaParseModelGroup(ctxt, schema,
11801 child, XML_SCHEMA_TYPE_CHOICE, 1);
11802 child = child->next;
11803 } else if (IS_SCHEMA(child, "sequence")) {
11804 type->subtypes = (xmlSchemaTypePtr)
11805 xmlSchemaParseModelGroup(ctxt, schema,
11806 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11807 child = child->next;
11808 } else if (IS_SCHEMA(child, "group")) {
11809 type->subtypes = (xmlSchemaTypePtr)
11810 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011811 /*
11812 * Note that the reference will be resolved in
11813 * xmlSchemaResolveTypeReferences();
11814 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011815 child = child->next;
11816 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011817 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011818 if (child != NULL) {
11819 /*
11820 * Attribute uses/declarations.
11821 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011822 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11823 (xmlSchemaItemListPtr *) &(type->attrUses),
11824 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11825 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011826 /*
11827 * Attribute wildcard.
11828 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011829 if (IS_SCHEMA(child, "anyAttribute")) {
11830 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011831 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11832 child = child->next;
11833 }
11834 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011835 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011836 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11837 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011838 xmlSchemaPContentErr(ctxt,
11839 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011840 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011841 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011842 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011843 } else {
11844 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011845 xmlSchemaPContentErr(ctxt,
11846 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011847 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011848 "(annotation?, ((attribute | attributeGroup)*, "
11849 "anyAttribute?))");
11850 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011851 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011852 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011853}
11854
11855/**
11856 * xmlSchemaParseSimpleContent:
11857 * @ctxt: a schema validation context
11858 * @schema: the schema being built
11859 * @node: a subtree containing XML Schema informations
11860 *
11861 * parse a XML schema SimpleContent definition
11862 * *WARNING* this interface is highly subject to change
11863 *
11864 * Returns the type definition or NULL in case of error
11865 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011866static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011867xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011868 xmlSchemaPtr schema, xmlNodePtr node,
11869 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011870{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011871 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011872 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011873 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011874
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011875 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11876 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011877 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011878 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011879 /* Not a component, don't create it. */
11880 type = ctxt->ctxtType;
11881 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11882 /*
11883 * Check for illegal attributes.
11884 */
11885 attr = node->properties;
11886 while (attr != NULL) {
11887 if (attr->ns == NULL) {
11888 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011889 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011890 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011891 }
11892 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011893 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011894 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011895 }
11896 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011897 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011898
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011899 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011900
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011901 /*
11902 * And now for the children...
11903 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011904 child = node->children;
11905 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011906 /*
11907 * Add the annotation to the complex type ancestor.
11908 */
11909 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011910 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011911 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011912 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011913 if (child == NULL) {
11914 xmlSchemaPContentErr(ctxt,
11915 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011916 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011917 "(annotation?, (restriction | extension))");
11918 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011919 if (child == NULL) {
11920 xmlSchemaPContentErr(ctxt,
11921 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011922 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011923 "(annotation?, (restriction | extension))");
11924 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011925 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011926 xmlSchemaParseRestriction(ctxt, schema, child,
11927 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011928 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011929 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011930 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011931 xmlSchemaParseExtension(ctxt, schema, child,
11932 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011933 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011934 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011935 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011936 if (child != NULL) {
11937 xmlSchemaPContentErr(ctxt,
11938 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011939 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011940 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011941 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011942 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011943}
11944
11945/**
11946 * xmlSchemaParseComplexContent:
11947 * @ctxt: a schema validation context
11948 * @schema: the schema being built
11949 * @node: a subtree containing XML Schema informations
11950 *
11951 * parse a XML schema ComplexContent definition
11952 * *WARNING* this interface is highly subject to change
11953 *
11954 * Returns the type definition or NULL in case of error
11955 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011956static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011957xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011958 xmlSchemaPtr schema, xmlNodePtr node,
11959 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011960{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011961 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011962 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011963 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011964
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011965 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11966 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011967 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011968 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011969 /* Not a component, don't create it. */
11970 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011971 /*
11972 * Check for illegal attributes.
11973 */
11974 attr = node->properties;
11975 while (attr != NULL) {
11976 if (attr->ns == NULL) {
11977 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011978 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011979 {
11980 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011981 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011982 }
11983 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11984 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011985 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011986 }
11987 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011988 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011989
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011990 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011991
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011992 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011993 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011994 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011995 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011996 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11997 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011998 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011999 child = node->children;
12000 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012001 /*
12002 * Add the annotation to the complex type ancestor.
12003 */
12004 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012005 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012006 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012007 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012008 if (child == NULL) {
12009 xmlSchemaPContentErr(ctxt,
12010 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012011 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012012 NULL, "(annotation?, (restriction | extension))");
12013 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012014 if (child == NULL) {
12015 xmlSchemaPContentErr(ctxt,
12016 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012017 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012018 NULL, "(annotation?, (restriction | extension))");
12019 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012020 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012021 xmlSchemaParseRestriction(ctxt, schema, child,
12022 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012023 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012024 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012025 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012026 xmlSchemaParseExtension(ctxt, schema, child,
12027 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012028 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012029 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012030 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012031 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012032 xmlSchemaPContentErr(ctxt,
12033 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012034 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012035 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012036 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012037 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012038}
12039
12040/**
12041 * xmlSchemaParseComplexType:
12042 * @ctxt: a schema validation context
12043 * @schema: the schema being built
12044 * @node: a subtree containing XML Schema informations
12045 *
12046 * parse a XML schema Complex Type definition
12047 * *WARNING* this interface is highly subject to change
12048 *
12049 * Returns the type definition or NULL in case of error
12050 */
12051static xmlSchemaTypePtr
12052xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012053 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012054{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012055 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012056 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012057 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012058 xmlAttrPtr attr;
12059 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012060#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012061 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012062#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012063 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012064
Daniel Veillard4255d502002-04-16 15:50:10 +000012065
12066 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12067 return (NULL);
12068
Daniel Veillard01fa6152004-06-29 17:04:39 +000012069 ctxtType = ctxt->ctxtType;
12070
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012071 if (topLevel) {
12072 attr = xmlSchemaGetPropNode(node, "name");
12073 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012074 xmlSchemaPMissingAttrErr(ctxt,
12075 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012076 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012077 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012078 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12079 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012080 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012081 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012082
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012083 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012084 /*
12085 * Parse as local complex type definition.
12086 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012087#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012088 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012089 type = xmlSchemaAddType(ctxt, schema,
12090 XML_SCHEMA_TYPE_COMPLEX,
12091 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012092 ctxt->targetNamespace, node, 0);
12093#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012094 type = xmlSchemaAddType(ctxt, schema,
12095 XML_SCHEMA_TYPE_COMPLEX,
12096 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012097#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012098 if (type == NULL)
12099 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012100 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012101 type->node = node;
12102 type->type = XML_SCHEMA_TYPE_COMPLEX;
12103 /*
12104 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012105 */
12106 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012107 /*
12108 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012109 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012110 type = xmlSchemaAddType(ctxt, schema,
12111 XML_SCHEMA_TYPE_COMPLEX,
12112 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012113 if (type == NULL)
12114 return (NULL);
12115 type->node = node;
12116 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012117 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012118 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012119 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012120 /*
12121 * Handle attributes.
12122 */
12123 attr = node->properties;
12124 while (attr != NULL) {
12125 if (attr->ns == NULL) {
12126 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12127 /*
12128 * Attribute "id".
12129 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012130 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012131 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12132 /*
12133 * Attribute "mixed".
12134 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012135 if (xmlSchemaPGetBoolNodeValue(ctxt,
12136 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012137 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12138 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012139 /*
12140 * Attributes of global complex type definitions.
12141 */
12142 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12143 /* Pass. */
12144 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12145 /*
12146 * Attribute "abstract".
12147 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012148 if (xmlSchemaPGetBoolNodeValue(ctxt,
12149 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012150 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12151 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12152 /*
12153 * Attribute "final".
12154 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012155 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012156 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012157 if (xmlSchemaPValAttrBlockFinal(attrValue,
12158 &(type->flags),
12159 -1,
12160 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12161 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12162 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012163 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012164 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012165 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012166 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012167 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012168 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012169 } else
12170 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012171 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12172 /*
12173 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012174 */
12175 attrValue = xmlSchemaGetNodeContent(ctxt,
12176 (xmlNodePtr) attr);
12177 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012178 -1,
12179 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012180 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012181 -1, -1, -1) != 0) {
12182 xmlSchemaPSimpleTypeErr(ctxt,
12183 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012184 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012185 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012186 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012187 } else
12188 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012189 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012190 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012191 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012192 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012193 } else {
12194 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012195 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012196 }
12197 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012198 xmlSchemaPIllegalAttrErr(ctxt,
12199 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012200 }
12201 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012202 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012203 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012204 /*
12205 * Apply default "block" values.
12206 */
12207 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12208 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12209 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12210 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012212 if (! final) {
12213 /*
12214 * Apply default "block" values.
12215 */
12216 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12217 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12218 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12219 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12220 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012221 /*
12222 * And now for the children...
12223 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012224 child = node->children;
12225 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012226 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012227 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012228 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012229 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012230 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012231 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012232 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012233 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012234 * Specifying mixed='true' when the <simpleContent>
12235 * alternative is chosen has no effect
12236 */
William M. Bracke7091952004-05-11 15:09:58 +000012237 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12238 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012239 xmlSchemaParseSimpleContent(ctxt, schema, child,
12240 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012241 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012242 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012243 /*
12244 * <complexType><complexContent>...
12245 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012246 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012247 xmlSchemaParseComplexContent(ctxt, schema, child,
12248 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012249 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012250 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012251 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012252 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12253 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012254 * SPEC
12255 * "...the third alternative (neither <simpleContent> nor
12256 * <complexContent>) is chosen. This case is understood as shorthand
12257 * for complex content restricting the ·ur-type definition·, and the
12258 * details of the mappings should be modified as necessary.
12259 */
12260 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12261 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012262 /*
12263 * Parse model groups.
12264 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012265 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012266 type->subtypes = (xmlSchemaTypePtr)
12267 xmlSchemaParseModelGroup(ctxt, schema, child,
12268 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012269 child = child->next;
12270 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012271 type->subtypes = (xmlSchemaTypePtr)
12272 xmlSchemaParseModelGroup(ctxt, schema, child,
12273 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012274 child = child->next;
12275 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012276 type->subtypes = (xmlSchemaTypePtr)
12277 xmlSchemaParseModelGroup(ctxt, schema, child,
12278 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012279 child = child->next;
12280 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012281 type->subtypes = (xmlSchemaTypePtr)
12282 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012283 /*
12284 * Note that the reference will be resolved in
12285 * xmlSchemaResolveTypeReferences();
12286 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012287 child = child->next;
12288 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012289 /*
12290 * Parse attribute decls/refs.
12291 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012292 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12293 (xmlSchemaItemListPtr *) &(type->attrUses),
12294 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12295 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012296 /*
12297 * Parse attribute wildcard.
12298 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012299 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012300 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12301 child = child->next;
12302 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012303 }
12304 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012305 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012306 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012307 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012308 NULL, "(annotation?, (simpleContent | complexContent | "
12309 "((group | all | choice | sequence)?, ((attribute | "
12310 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012311 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012312 /*
12313 * REDEFINE: SPEC src-redefine (5)
12314 */
12315 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12316 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012317 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012318 "<complexType> must have a <restriction> or <extension> "
12319 "grand-child", NULL);
12320 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012321 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012322 return (type);
12323}
12324
Daniel Veillard4255d502002-04-16 15:50:10 +000012325/************************************************************************
12326 * *
12327 * Validating using Schemas *
12328 * *
12329 ************************************************************************/
12330
12331/************************************************************************
12332 * *
12333 * Reading/Writing Schemas *
12334 * *
12335 ************************************************************************/
12336
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012337#if 0 /* Will be enabled if it is clear what options are needed. */
12338/**
12339 * xmlSchemaParserCtxtSetOptions:
12340 * @ctxt: a schema parser context
12341 * @options: a combination of xmlSchemaParserOption
12342 *
12343 * Sets the options to be used during the parse.
12344 *
12345 * Returns 0 in case of success, -1 in case of an
12346 * API error.
12347 */
12348static int
12349xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12350 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012351
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012352{
12353 int i;
12354
12355 if (ctxt == NULL)
12356 return (-1);
12357 /*
12358 * WARNING: Change the start value if adding to the
12359 * xmlSchemaParseOption.
12360 */
12361 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12362 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012363 return (-1);
12364 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012365 }
12366 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012367 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012368}
12369
12370/**
12371 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012372 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012373 *
12374 * Returns the option combination of the parser context.
12375 */
12376static int
12377xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012378
12379{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012380 if (ctxt == NULL)
12381 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012382 else
12383 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012384}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012385#endif
12386
Daniel Veillard4255d502002-04-16 15:50:10 +000012387/**
12388 * xmlSchemaNewParserCtxt:
12389 * @URL: the location of the schema
12390 *
12391 * Create an XML Schemas parse context for that file/resource expected
12392 * to contain an XML Schemas file.
12393 *
12394 * Returns the parser context or NULL in case of error
12395 */
12396xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012397xmlSchemaNewParserCtxt(const char *URL)
12398{
Daniel Veillard4255d502002-04-16 15:50:10 +000012399 xmlSchemaParserCtxtPtr ret;
12400
12401 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012402 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012403
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012404 ret = xmlSchemaParserCtxtCreate();
12405 if (ret == NULL)
12406 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012407 ret->dict = xmlDictCreate();
12408 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012409 return (ret);
12410}
12411
12412/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012413 * xmlSchemaNewMemParserCtxt:
12414 * @buffer: a pointer to a char array containing the schemas
12415 * @size: the size of the array
12416 *
12417 * Create an XML Schemas parse context for that memory buffer expected
12418 * to contain an XML Schemas file.
12419 *
12420 * Returns the parser context or NULL in case of error
12421 */
12422xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012423xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12424{
Daniel Veillard6045c902002-10-09 21:13:59 +000012425 xmlSchemaParserCtxtPtr ret;
12426
12427 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012428 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012429 ret = xmlSchemaParserCtxtCreate();
12430 if (ret == NULL)
12431 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012432 ret->buffer = buffer;
12433 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012434 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012435 return (ret);
12436}
12437
12438/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012439 * xmlSchemaNewDocParserCtxt:
12440 * @doc: a preparsed document tree
12441 *
12442 * Create an XML Schemas parse context for that document.
12443 * NB. The document may be modified during the parsing process.
12444 *
12445 * Returns the parser context or NULL in case of error
12446 */
12447xmlSchemaParserCtxtPtr
12448xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12449{
12450 xmlSchemaParserCtxtPtr ret;
12451
12452 if (doc == NULL)
12453 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012454 ret = xmlSchemaParserCtxtCreate();
12455 if (ret == NULL)
12456 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012457 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012458 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012459 /* The application has responsibility for the document */
12460 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012461
12462 return (ret);
12463}
12464
12465/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012466 * xmlSchemaFreeParserCtxt:
12467 * @ctxt: the schema parser context
12468 *
12469 * Free the resources associated to the schema parser context
12470 */
12471void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012472xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12473{
Daniel Veillard4255d502002-04-16 15:50:10 +000012474 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012475 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012476 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012477 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012478 if (ctxt->vctxt != NULL) {
12479 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12480 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012481 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12482 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12483 ctxt->constructor = NULL;
12484 ctxt->ownsConstructor = 0;
12485 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012486 if (ctxt->attrProhibs != NULL)
12487 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012488 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012489 xmlFree(ctxt);
12490}
12491
12492/************************************************************************
12493 * *
12494 * Building the content models *
12495 * *
12496 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012497
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012498static void
12499xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012500 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012501{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012502 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012503 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012504 xmlSchemaSubstGroupPtr substGroup;
12505 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012506
12507 elemDecl = (xmlSchemaElementPtr) particle->children;
12508 /*
12509 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012510 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012511 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012512 if (end == NULL)
12513 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012514 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012515 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012516 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012517 XML_SCHEMAP_INTERNAL,
12518 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12519 "declaration is marked having a subst. group but none "
12520 "available.\n", elemDecl->name, NULL);
12521 return;
12522 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012523 if (counter >= 0) {
12524 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012525 * NOTE that we put the declaration in, even if it's abstract.
12526 * However, an error will be raised during *validation* if an element
12527 * information item shall be validated against an abstract element
12528 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012529 */
12530 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12531 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12532 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12533 /*
12534 * Add subst. group members.
12535 */
12536 for (i = 0; i < substGroup->members->nbItems; i++) {
12537 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12538 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12539 member->name, member->targetNamespace, member);
12540 }
12541 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012542 /*
12543 * NOTE that we put the declaration in, even if it's abstract,
12544 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012545 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012546 xmlAutomataNewTransition2(pctxt->am,
12547 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012548 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12549 /*
12550 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012551 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012552 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012553 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012554 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12555 member->name, member->targetNamespace,
12556 1, 1, member);
12557 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012558 }
12559 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012560 xmlAutomataStatePtr hop;
12561 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12562 UNBOUNDED : particle->maxOccurs - 1;
12563 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12564
12565 counter =
12566 xmlAutomataNewCounter(pctxt->am, minOccurs,
12567 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012568 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012569
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012570 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012571 xmlAutomataNewTransition2(pctxt->am,
12572 start, NULL,
12573 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012574 hop);
12575 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012576 * Add subst. group members.
12577 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012578 for (i = 0; i < substGroup->members->nbItems; i++) {
12579 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12580 xmlAutomataNewEpsilon(pctxt->am,
12581 xmlAutomataNewTransition2(pctxt->am,
12582 start, NULL,
12583 member->name, member->targetNamespace, member),
12584 hop);
12585 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012586 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12587 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12588 }
12589 if (particle->minOccurs == 0)
12590 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012591 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012592}
12593
12594static void
12595xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12596 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012597{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012598 if (((xmlSchemaElementPtr) particle->children)->flags &
12599 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012600 /*
12601 * Substitution groups.
12602 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012603 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012604 } else {
12605 xmlSchemaElementPtr elemDecl;
12606 xmlAutomataStatePtr start;
12607
12608 elemDecl = (xmlSchemaElementPtr) particle->children;
12609
12610 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012611 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012612 if (particle->maxOccurs == 1) {
12613 start = ctxt->state;
12614 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012615 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12616 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12617 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012618 /* Special case. */
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012619 start = ctxt->state;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012620 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012621 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12622 ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
12623 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012624 } else {
12625 int counter;
12626 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12627 UNBOUNDED : particle->maxOccurs - 1;
12628 int minOccurs = particle->minOccurs < 1 ?
12629 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012630
12631 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012632 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12633 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12634 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12635 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12636 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12637 NULL, counter);
12638 }
12639 if (particle->minOccurs == 0)
12640 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12641 }
12642}
12643
Daniel Veillard4255d502002-04-16 15:50:10 +000012644/**
12645 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012646 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012647 * @particle: the particle component
12648 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012649 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012650 * Create the automaton for the {content type} of a complex type.
12651 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012652 */
12653static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012654xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012655 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012656{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012657 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012658 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012659 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012660 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012661 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012662 /*
12663 * Just return in this case. A missing "term" of the particle
12664 * might arise due to an invalid "term" component.
12665 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012666 return;
12667 }
12668
12669 switch (particle->children->type) {
12670 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012671 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012672 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012673 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012674
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012675 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012676
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012677 start = pctxt->state;
12678 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012679
12680 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012681 if (wild->any == 1) {
12682 /*
12683 * We need to add both transitions:
12684 *
12685 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012686 */
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, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012691 /*
12692 * 2. the {"*"} for elements in no namespace.
12693 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012694 pctxt->state =
12695 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012696 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012697 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012698
12699 } else if (wild->nsSet != NULL) {
12700 ns = wild->nsSet;
12701 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012702 pctxt->state = start;
12703 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12704 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12705 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012706 ns = ns->next;
12707 } while (ns != NULL);
12708
12709 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012710 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12711 start, end, BAD_CAST "*", wild->negNsSet->value,
12712 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012713 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012714 } else {
12715 int counter;
12716 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012717 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012718 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012719 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012720 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012721
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012722 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12723 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012724 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012725 pctxt->state =
12726 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012727 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012728 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12729 pctxt->state =
12730 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012731 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012732 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012733 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012734 ns = wild->nsSet;
12735 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012736 pctxt->state =
12737 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012738 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012739 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012740 ns = ns->next;
12741 } while (ns != NULL);
12742
12743 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012744 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012745 start, hop, BAD_CAST "*", wild->negNsSet->value,
12746 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012747 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012748 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12749 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012750 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012751 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012752 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012753 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012754 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012755 break;
12756 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012757 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012758 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012759 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012760 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012761 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012762
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012763 /*
12764 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012765 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012766 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012767 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12768 sub = particle->children->children;
12769 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012770 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012771 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012772 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012773 }
12774 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012775 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012776
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012777 if (particle->maxOccurs >= UNBOUNDED) {
12778 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012779 xmlAutomataStatePtr tmp;
12780 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012781
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012782 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012783 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012784 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012785
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012786 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012787 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012788
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012789 sub = particle->children->children;
12790 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012791 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012792 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012793 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012794 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012795 tmp = pctxt->state;
12796 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012797 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012798 pctxt->state =
12799 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012800 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012801
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012802 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012803 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012804 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012805 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012806
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012807 sub = particle->children->children;
12808 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012809 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012810 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012811 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012812 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012813 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012814 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012815 /*
12816 * epsilon needed to block previous trans from
12817 * being allowed to enter back from another
12818 * construct
12819 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012820 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12821 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012822 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012823 xmlAutomataNewEpsilon(pctxt->am,
12824 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012825 }
12826 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012827 } else if ((particle->maxOccurs > 1)
12828 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012829 xmlAutomataStatePtr tmp;
12830 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012831
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012832 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012833 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012834 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012835
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012836 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012837 particle->minOccurs - 1,
12838 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012839
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012840 sub = particle->children->children;
12841 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012842 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012843 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012844 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012845 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012846 tmp = pctxt->state;
12847 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012848 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012849 pctxt->state =
12850 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012851 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012852 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012853 xmlAutomataNewEpsilon(pctxt->am,
12854 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012855 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012856 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012857 sub = particle->children->children;
12858 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012859 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012860 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012861 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012862 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012863 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012864 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12865 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012866 }
12867 }
12868 }
12869 break;
12870 }
12871 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012872 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012873 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012874
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012875 start = pctxt->state;
12876 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012877
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012878 /*
12879 * iterate over the subtypes and remerge the end with an
12880 * epsilon transition
12881 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012882 if (particle->maxOccurs == 1) {
12883 sub = particle->children->children;
12884 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012885 pctxt->state = start;
12886 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012887 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012889 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012890 }
12891 } else {
12892 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012893 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012894 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12895 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012896 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012897 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012898
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012899 /*
12900 * use a counter to keep track of the number of transtions
12901 * which went through the choice.
12902 */
12903 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012904 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12905 hop = xmlAutomataNewState(pctxt->am);
12906 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012907
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012908 sub = particle->children->children;
12909 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012910 pctxt->state = base;
12911 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012912 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012913 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012914 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012915 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012916 xmlAutomataNewEpsilon(pctxt->am, start, base);
12917 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12918 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012919 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012920 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012921 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012922 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012923 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012924 break;
12925 }
12926 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012927 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012928 xmlSchemaParticlePtr sub;
12929 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012930 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012931
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012932 sub = (xmlSchemaParticlePtr) particle->children->children;
12933 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012934 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012935 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012936 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012937 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012938
12939 elemDecl = (xmlSchemaElementPtr) sub->children;
12940 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012941 PERROR_INT("xmlSchemaBuildAContentModel",
12942 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012943 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012944 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012945 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012946 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012947 * {particles} of the group must be 0 or 1; this is
12948 * already ensured during the parse of the content of
12949 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012950 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012951 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12952 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012953
Daniel Veillarda980bef2005-07-18 21:34:03 +000012954 /*
12955 * This is an abstract group, we need to share
12956 * the same counter for all the element transitions
12957 * derived from the group
12958 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012959 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012960 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012961 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12962 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012963 } else {
12964 if ((sub->minOccurs == 1) &&
12965 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012966 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12967 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012968 elemDecl->name,
12969 elemDecl->targetNamespace,
12970 1, 1, elemDecl);
12971 } else if ((sub->minOccurs == 0) &&
12972 (sub->maxOccurs == 1)) {
12973
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012974 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12975 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012976 elemDecl->name,
12977 elemDecl->targetNamespace,
12978 0,
12979 1,
12980 elemDecl);
12981 }
12982 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012983 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012984 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012985 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012986 pctxt->state =
12987 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012988 break;
12989 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012990 case XML_SCHEMA_TYPE_GROUP:
12991 /*
12992 * If we hit a model group definition, then this means that
12993 * it was empty, thus was not substituted for the containing
12994 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012995 * TODO: But the group should be substituted and not occur at
12996 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012997 */
12998 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012999 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013000 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
13001 "xmlSchemaBuildAContentModel",
13002 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013003 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013004 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013005 }
13006}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013007
Daniel Veillard4255d502002-04-16 15:50:10 +000013008/**
13009 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000013010 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013011 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013012 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000013013 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013014 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000013015 */
13016static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013017xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013018 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013019{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013020 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13021 (type->contModel != NULL) ||
13022 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13023 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013024 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013025
13026#ifdef DEBUG_CONTENT
13027 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013028 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013029#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013030 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013031 ctxt->am = xmlNewAutomata();
13032 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013033 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013034 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013035 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013036 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013037 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013038 /*
13039 * Build the automaton.
13040 */
13041 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013042 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013043 type->contModel = xmlAutomataCompile(ctxt->am);
13044 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013045 xmlSchemaPCustomErr(ctxt,
13046 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013047 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013048 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013049 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013050 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013051 XML_SCHEMAP_NOT_DETERMINISTIC,
13052 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013053 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013054 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013055 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013056#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013057 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013058 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013059 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013060#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013061 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013062 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013063 xmlFreeAutomata(ctxt->am);
13064 ctxt->am = NULL;
13065}
13066
13067/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013068 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013069 * @elem: the schema element context
13070 * @ctxt: the schema parser context
13071 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013072 * Resolves the references of an element declaration
13073 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013074 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013075 */
13076static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013077xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13078 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013079{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013080 if ((ctxt == NULL) || (elemDecl == NULL) ||
13081 ((elemDecl != NULL) &&
13082 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013083 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013084 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013085
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013086 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013087 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013088
13089 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013090 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013091 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013092 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013093 elemDecl->namedTypeNs);
13094 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013095 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013096 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013097 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013098 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013099 XML_SCHEMA_TYPE_BASIC, "type definition");
13100 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013101 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013102 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013103 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013104 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013105
Daniel Veillardc0826a72004-08-10 14:17:33 +000013106 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013107 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013108 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013109 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013110 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13111 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013112 if (substHead == NULL) {
13113 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013114 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013115 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013116 "substitutionGroup", elemDecl->substGroup,
13117 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013118 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013119 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013120 /*
13121 * Set the "substitution group affiliation".
13122 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013123 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013124 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013125 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013126 * The type definitions is set to:
13127 * SPEC "...the {type definition} of the element
13128 * declaration ·resolved· to by the ·actual value·
13129 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013130 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013131 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013132 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013133 }
13134 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013135 /*
13136 * SPEC "The definition of anyType serves as the default type definition
13137 * for element declarations whose XML representation does not specify one."
13138 */
13139 if ((elemDecl->subtypes == NULL) &&
13140 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013141 (elemDecl->substGroup == NULL))
13142 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013143}
13144
13145/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013146 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013147 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013148 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013149 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013150 * Checks and builds the "member type definitions" property of the union
13151 * simple type. This handles part (1), part (2) is done in
13152 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13153 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013154 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013155 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013156static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013157xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13158 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013159{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013160
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013161 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013162 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013163
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013164 /*
13165 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13166 * define the explicit members as the type definitions ·resolved·
13167 * to by the items in the ·actual value· of the memberTypes [attribute],
13168 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013169 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013170 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013171 /*
13172 * Resolve references.
13173 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013174 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013175 lastLink = NULL;
13176 while (link != NULL) {
13177 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013178
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013179 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13180 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13181
13182 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013183 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013184 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013185 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013186 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13187 /*
13188 * Remove the member type link.
13189 */
13190 if (lastLink == NULL)
13191 type->memberTypes = link->next;
13192 else
13193 lastLink->next = link->next;
13194 newLink = link;
13195 link = link->next;
13196 xmlFree(newLink);
13197 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013198 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013199 lastLink = link;
13200 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013201 }
13202 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013203 /*
13204 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013205 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013206 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013207 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013208 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13209 if (link == NULL) {
13210 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13211 return (-1);
13212 }
13213 link->type = memberType;
13214 link->next = NULL;
13215 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013216 type->memberTypes = link;
13217 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013218 lastLink->next = link;
13219 lastLink = link;
13220 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013221 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013222 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013223}
13224
Daniel Veillard4255d502002-04-16 15:50:10 +000013225/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013226 * xmlSchemaIsDerivedFromBuiltInType:
13227 * @ctxt: the schema parser context
13228 * @type: the type definition
13229 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013230 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013231 *
13232 * Returns 1 if the type has the given value type, or
13233 * is derived from such a type.
13234 */
William M. Brack803812b2004-06-03 02:11:24 +000013235static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013236xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013237{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013238 if (type == NULL)
13239 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013240 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013241 return (0);
13242 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13243 if (type->builtInType == valType)
13244 return(1);
13245 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13246 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13247 return (0);
13248 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013249 }
13250 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013251}
13252
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013253#if 0
13254/**
13255 * xmlSchemaIsDerivedFromBuiltInType:
13256 * @ctxt: the schema parser context
13257 * @type: the type definition
13258 * @valType: the value type
13259 *
13260 *
13261 * Returns 1 if the type has the given value type, or
13262 * is derived from such a type.
13263 */
13264static int
13265xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13266{
13267 if (type == NULL)
13268 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013269 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013270 return (0);
13271 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13272 if (type->builtInType == valType)
13273 return(1);
13274 return (0);
13275 } else
13276 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13277
13278 return (0);
13279}
13280#endif
13281
13282static xmlSchemaTypePtr
13283xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13284{
13285 if (type == NULL)
13286 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013287 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013288 return (NULL);
13289 if (type->type == XML_SCHEMA_TYPE_BASIC)
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013290 return(type);
13291 return(xmlSchemaQueryBuiltInType(type->subtypes));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013292}
13293
Daniel Veillard3646d642004-06-02 19:19:14 +000013294/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013295 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013296 * @type: the simpleType definition
13297 *
13298 * Returns the primitive type of the given type or
13299 * NULL in case of error.
13300 */
13301static xmlSchemaTypePtr
13302xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13303{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013304
Daniel Veillard01fa6152004-06-29 17:04:39 +000013305 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013306 /*
13307 * Note that anySimpleType is actually not a primitive type
13308 * but we need that here.
13309 */
13310 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13311 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013312 return (type);
13313 type = type->baseType;
13314 }
13315
13316 return (NULL);
13317}
13318
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013319#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013320/**
13321 * xmlSchemaGetBuiltInTypeAncestor:
13322 * @type: the simpleType definition
13323 *
13324 * Returns the primitive type of the given type or
13325 * NULL in case of error.
13326 */
13327static xmlSchemaTypePtr
13328xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13329{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013330 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013331 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013332 while (type != NULL) {
13333 if (type->type == XML_SCHEMA_TYPE_BASIC)
13334 return (type);
13335 type = type->baseType;
13336 }
13337
13338 return (NULL);
13339}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013340#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013341
Daniel Veillard01fa6152004-06-29 17:04:39 +000013342/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013343 * xmlSchemaCloneWildcardNsConstraints:
13344 * @ctxt: the schema parser context
13345 * @dest: the destination wildcard
13346 * @source: the source wildcard
13347 *
13348 * Clones the namespace constraints of source
13349 * and assignes them to dest.
13350 * Returns -1 on internal error, 0 otherwise.
13351 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013352static int
13353xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013354 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013355 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013356{
13357 xmlSchemaWildcardNsPtr cur, tmp, last;
13358
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013359 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013360 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013361 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013362 cur = source->nsSet;
13363 last = NULL;
13364 while (cur != NULL) {
13365 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13366 if (tmp == NULL)
13367 return(-1);
13368 tmp->value = cur->value;
13369 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013370 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013371 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013372 last->next = tmp;
13373 last = tmp;
13374 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013375 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013376 if (dest->negNsSet != NULL)
13377 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013378 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013379 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13380 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013381 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013382 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013383 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013384 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013385 return(0);
13386}
13387
Daniel Veillard50355f02004-06-08 17:52:16 +000013388/**
13389 * xmlSchemaUnionWildcards:
13390 * @ctxt: the schema parser context
13391 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013392 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013393 *
13394 * Unions the namespace constraints of the given wildcards.
13395 * @completeWild will hold the resulting union.
13396 * Returns a positive error code on failure, -1 in case of an
13397 * internal error, 0 otherwise.
13398 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013399static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013400xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013401 xmlSchemaWildcardPtr completeWild,
13402 xmlSchemaWildcardPtr curWild)
13403{
13404 xmlSchemaWildcardNsPtr cur, curB, tmp;
13405
13406 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013407 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013408 * value.
13409 */
13410 if ((completeWild->any == curWild->any) &&
13411 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13412 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013413
Daniel Veillard3646d642004-06-02 19:19:14 +000013414 if ((completeWild->negNsSet == NULL) ||
13415 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013416
Daniel Veillard3646d642004-06-02 19:19:14 +000013417 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013418 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013419
13420 /*
13421 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013422 */
13423 cur = completeWild->nsSet;
13424 while (cur != NULL) {
13425 found = 0;
13426 curB = curWild->nsSet;
13427 while (curB != NULL) {
13428 if (cur->value == curB->value) {
13429 found = 1;
13430 break;
13431 }
13432 curB = curB->next;
13433 }
13434 if (!found)
13435 break;
13436 cur = cur->next;
13437 }
13438 if (found)
13439 return(0);
13440 } else
13441 return(0);
13442 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013443 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013444 /*
13445 * 2 If either O1 or O2 is any, then any must be the value
13446 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013447 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013448 if (completeWild->any == 0) {
13449 completeWild->any = 1;
13450 if (completeWild->nsSet != NULL) {
13451 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13452 completeWild->nsSet = NULL;
13453 }
13454 if (completeWild->negNsSet != NULL) {
13455 xmlFree(completeWild->negNsSet);
13456 completeWild->negNsSet = NULL;
13457 }
13458 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013459 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013460 }
13461 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013462 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013463 * then the union of those sets must be the value.
13464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013465 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013466 int found;
13467 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013468
Daniel Veillard3646d642004-06-02 19:19:14 +000013469 cur = curWild->nsSet;
13470 start = completeWild->nsSet;
13471 while (cur != NULL) {
13472 found = 0;
13473 curB = start;
13474 while (curB != NULL) {
13475 if (cur->value == curB->value) {
13476 found = 1;
13477 break;
13478 }
13479 curB = curB->next;
13480 }
13481 if (!found) {
13482 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013484 return (-1);
13485 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013486 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013487 completeWild->nsSet = tmp;
13488 }
13489 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013490 }
13491
Daniel Veillard3646d642004-06-02 19:19:14 +000013492 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013493 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013494 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013495 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013496 * or ·absent·), then a pair of not and ·absent· must be the value.
13497 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013498 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013499 (curWild->negNsSet != NULL) &&
13500 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13501 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013502
13503 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013504 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013505 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013506 * 5.
13507 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013508 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013509 (completeWild->negNsSet->value != NULL) &&
13510 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013511 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013512 (curWild->negNsSet->value != NULL) &&
13513 (completeWild->nsSet != NULL))) {
13514
13515 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013516
Daniel Veillard3646d642004-06-02 19:19:14 +000013517 if (completeWild->nsSet != NULL) {
13518 cur = completeWild->nsSet;
13519 curB = curWild->negNsSet;
13520 } else {
13521 cur = curWild->nsSet;
13522 curB = completeWild->negNsSet;
13523 }
13524 nsFound = 0;
13525 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013526 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013527 absentFound = 1;
13528 else if (cur->value == curB->value)
13529 nsFound = 1;
13530 if (nsFound && absentFound)
13531 break;
13532 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013533 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013534
13535 if (nsFound && absentFound) {
13536 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013537 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013538 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013539 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013540 completeWild->any = 1;
13541 if (completeWild->nsSet != NULL) {
13542 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13543 completeWild->nsSet = NULL;
13544 }
13545 if (completeWild->negNsSet != NULL) {
13546 xmlFree(completeWild->negNsSet);
13547 completeWild->negNsSet = NULL;
13548 }
13549 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013550 /*
13551 * 5.2 If the set S includes the negated namespace name
13552 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013553 * be the value.
13554 */
13555 if (completeWild->nsSet != NULL) {
13556 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13557 completeWild->nsSet = NULL;
13558 }
13559 if (completeWild->negNsSet == NULL) {
13560 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13561 if (completeWild->negNsSet == NULL)
13562 return (-1);
13563 }
13564 completeWild->negNsSet->value = NULL;
13565 } else if ((!nsFound) && absentFound) {
13566 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013567 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013568 * namespace name, then the union is not expressible.
13569 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013570 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013571 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013572 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013573 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013574 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013575 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013576 /*
13577 * 5.4 If the set S does not include either the negated namespace
13578 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013579 * and a namespace name must be the value.
13580 */
13581 if (completeWild->negNsSet == NULL) {
13582 if (completeWild->nsSet != NULL) {
13583 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13584 completeWild->nsSet = NULL;
13585 }
13586 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13587 if (completeWild->negNsSet == NULL)
13588 return (-1);
13589 completeWild->negNsSet->value = curWild->negNsSet->value;
13590 }
13591 }
13592 return (0);
13593 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013594 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013595 * 6.
13596 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013597 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013598 (completeWild->negNsSet->value == NULL) &&
13599 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013600 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013601 (curWild->negNsSet->value == NULL) &&
13602 (completeWild->nsSet != NULL))) {
13603
13604 if (completeWild->nsSet != NULL) {
13605 cur = completeWild->nsSet;
13606 } else {
13607 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013608 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013609 while (cur != NULL) {
13610 if (cur->value == NULL) {
13611 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013612 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013613 * value.
13614 */
13615 completeWild->any = 1;
13616 if (completeWild->nsSet != NULL) {
13617 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13618 completeWild->nsSet = NULL;
13619 }
13620 if (completeWild->negNsSet != NULL) {
13621 xmlFree(completeWild->negNsSet);
13622 completeWild->negNsSet = NULL;
13623 }
13624 return (0);
13625 }
13626 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013627 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013628 if (completeWild->negNsSet == NULL) {
13629 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013630 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013631 * and ·absent· must be the value.
13632 */
13633 if (completeWild->nsSet != NULL) {
13634 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13635 completeWild->nsSet = NULL;
13636 }
13637 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13638 if (completeWild->negNsSet == NULL)
13639 return (-1);
13640 completeWild->negNsSet->value = NULL;
13641 }
13642 return (0);
13643 }
13644 return (0);
13645
13646}
13647
Daniel Veillard50355f02004-06-08 17:52:16 +000013648/**
13649 * xmlSchemaIntersectWildcards:
13650 * @ctxt: the schema parser context
13651 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013652 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013653 *
13654 * Intersects the namespace constraints of the given wildcards.
13655 * @completeWild will hold the resulting intersection.
13656 * Returns a positive error code on failure, -1 in case of an
13657 * internal error, 0 otherwise.
13658 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013659static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013660xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013661 xmlSchemaWildcardPtr completeWild,
13662 xmlSchemaWildcardPtr curWild)
13663{
William M. Brack803812b2004-06-03 02:11:24 +000013664 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013665
13666 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013667 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013668 * value.
13669 */
13670 if ((completeWild->any == curWild->any) &&
13671 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13672 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013673
Daniel Veillard3646d642004-06-02 19:19:14 +000013674 if ((completeWild->negNsSet == NULL) ||
13675 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013676
Daniel Veillard3646d642004-06-02 19:19:14 +000013677 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013678 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013679
13680 /*
13681 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013682 */
13683 cur = completeWild->nsSet;
13684 while (cur != NULL) {
13685 found = 0;
13686 curB = curWild->nsSet;
13687 while (curB != NULL) {
13688 if (cur->value == curB->value) {
13689 found = 1;
13690 break;
13691 }
13692 curB = curB->next;
13693 }
13694 if (!found)
13695 break;
13696 cur = cur->next;
13697 }
13698 if (found)
13699 return(0);
13700 } else
13701 return(0);
13702 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013703 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013704 /*
13705 * 2 If either O1 or O2 is any, then the other must be the value.
13706 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013707 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013708 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013709 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013710 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013711 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013712 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013713 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13714 * name or ·absent·) and the other is a set of (namespace names or
13715 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013716 * the set, minus ·absent· if it was in the set, must be the value.
13717 */
13718 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13719 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13720 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013721
Daniel Veillard3646d642004-06-02 19:19:14 +000013722 if (completeWild->nsSet == NULL) {
13723 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013724 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013725 return(-1);
13726 } else
13727 neg = curWild->negNsSet->value;
13728 /*
13729 * Remove absent and negated.
13730 */
13731 prev = NULL;
13732 cur = completeWild->nsSet;
13733 while (cur != NULL) {
13734 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013735 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013736 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013737 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013738 prev->next = cur->next;
13739 xmlFree(cur);
13740 break;
13741 }
13742 prev = cur;
13743 cur = cur->next;
13744 }
13745 if (neg != NULL) {
13746 prev = NULL;
13747 cur = completeWild->nsSet;
13748 while (cur != NULL) {
13749 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013750 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013751 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013752 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013753 prev->next = cur->next;
13754 xmlFree(cur);
13755 break;
13756 }
13757 prev = cur;
13758 cur = cur->next;
13759 }
13760 }
13761
13762 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013763 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013764 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013765 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013766 * then the intersection of those sets must be the value.
13767 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013768 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013769 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013770
Daniel Veillard3646d642004-06-02 19:19:14 +000013771 cur = completeWild->nsSet;
13772 prev = NULL;
13773 while (cur != NULL) {
13774 found = 0;
13775 curB = curWild->nsSet;
13776 while (curB != NULL) {
13777 if (cur->value == curB->value) {
13778 found = 1;
13779 break;
13780 }
13781 curB = curB->next;
13782 }
13783 if (!found) {
13784 if (prev == NULL)
13785 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013786 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013787 prev->next = cur->next;
13788 tmp = cur->next;
13789 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013790 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013791 continue;
13792 }
13793 prev = cur;
13794 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013795 }
13796
Daniel Veillard3646d642004-06-02 19:19:14 +000013797 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013798 }
13799 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013800 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013801 */
13802 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013803 (curWild->negNsSet != NULL) &&
13804 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013805 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013806 (curWild->negNsSet->value != NULL)) {
13807
13808 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013809 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013810 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013811 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013812 }
13813 /*
13814 * 6 If the one is a negation of a namespace name and the other
13815 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013816 * of a namespace name must be the value.
13817 */
13818 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13819 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013820 (completeWild->negNsSet->value == NULL)) {
13821 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013822 }
13823 return(0);
13824}
13825
Daniel Veillard50355f02004-06-08 17:52:16 +000013826/**
13827 * xmlSchemaIsWildcardNsConstraintSubset:
13828 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013829 * @sub: the first wildcard
13830 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013831 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013832 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13833 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013834 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013835 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013836 */
13837static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013838xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13839 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013840{
Daniel Veillard50355f02004-06-08 17:52:16 +000013841 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013842 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013843 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013844 if (super->any)
13845 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013846 /*
13847 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13848 * 2.2 super must be a pair of not and the same value.
13849 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013850 if ((sub->negNsSet != NULL) &&
13851 (super->negNsSet != NULL) &&
13852 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013853 return (0);
13854 /*
13855 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013856 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013857 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013858 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013859 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013860 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013861 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013862 xmlSchemaWildcardNsPtr cur, curB;
13863 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013864
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013865 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013866 while (cur != NULL) {
13867 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013868 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013869 while (curB != NULL) {
13870 if (cur->value == curB->value) {
13871 found = 1;
13872 break;
13873 }
13874 curB = curB->next;
13875 }
13876 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013877 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013878 cur = cur->next;
13879 }
13880 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013881 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013882 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013883 xmlSchemaWildcardNsPtr cur;
13884 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013885 * 3.2.2 super must be a pair of not and a namespace name or
13886 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013887 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013888 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013889 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013890 if (cur->value == super->negNsSet->value)
13891 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013892 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013893 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013894 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013895 }
13896 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013897 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013898}
13899
Daniel Veillard50355f02004-06-08 17:52:16 +000013900static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013901xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013902 int *fixed,
13903 const xmlChar **value,
13904 xmlSchemaValPtr *val)
13905{
13906 *fixed = 0;
13907 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013908 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013909 *val = NULL;
13910
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013911 if (attruse->defValue != NULL) {
13912 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013913 if (val != NULL)
13914 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013915 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013916 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013917 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013918 } else if ((attruse->attrDecl != NULL) &&
13919 (attruse->attrDecl->defValue != NULL)) {
13920 *value = attruse->attrDecl->defValue;
13921 if (val != NULL)
13922 *val = attruse->attrDecl->defVal;
13923 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013924 *fixed = 1;
13925 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013926 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013927 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013928}
Daniel Veillard3646d642004-06-02 19:19:14 +000013929/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013930 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013931 * @wild: the wildcard
13932 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013933 *
13934 * Validation Rule: Wildcard allows Namespace Name
13935 * (cvc-wildcard-namespace)
13936 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013937 * Returns 0 if the given namespace matches the wildcard,
13938 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013939 */
13940static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013941xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13942 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013943{
13944 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013945 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013946
13947 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013948 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013949 else if (wild->nsSet != NULL) {
13950 xmlSchemaWildcardNsPtr cur;
13951
13952 cur = wild->nsSet;
13953 while (cur != NULL) {
13954 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013955 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013956 cur = cur->next;
13957 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013958 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013959 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013960 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013961
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013962 return(1);
13963}
13964
13965#define XML_SCHEMA_ACTION_DERIVE 0
13966#define XML_SCHEMA_ACTION_REDEFINE 1
13967
13968#define WXS_ACTION_STR(a) \
13969((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13970
13971/*
13972* Schema Component Constraint:
13973* Derivation Valid (Restriction, Complex)
13974* derivation-ok-restriction (2) - (4)
13975*
13976* ATTENTION:
13977* In XML Schema 1.1 this will be:
13978* Validation Rule:
13979* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13980*
13981*/
13982static int
13983xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13984 int action,
13985 xmlSchemaBasicItemPtr item,
13986 xmlSchemaBasicItemPtr baseItem,
13987 xmlSchemaItemListPtr uses,
13988 xmlSchemaItemListPtr baseUses,
13989 xmlSchemaWildcardPtr wild,
13990 xmlSchemaWildcardPtr baseWild)
13991{
13992 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013993 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013994 const xmlChar *bEffValue;
13995 int effFixed;
13996
13997 if (uses != NULL) {
13998 for (i = 0; i < uses->nbItems; i++) {
13999 cur = uses->items[i];
14000 found = 0;
14001 if (baseUses == NULL)
14002 goto not_found;
14003 for (j = 0; j < baseUses->nbItems; j++) {
14004 bcur = baseUses->items[j];
14005 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14006 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14007 (WXS_ATTRUSE_DECL_TNS(cur) ==
14008 WXS_ATTRUSE_DECL_TNS(bcur)))
14009 {
14010 /*
14011 * (2.1) "If there is an attribute use in the {attribute
14012 * uses} of the {base type definition} (call this B) whose
14013 * {attribute declaration} has the same {name} and {target
14014 * namespace}, then all of the following must be true:"
14015 */
14016 found = 1;
14017
14018 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14019 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14020 {
14021 xmlChar *str = NULL;
14022 /*
14023 * (2.1.1) "one of the following must be true:"
14024 * (2.1.1.1) "B's {required} is false."
14025 * (2.1.1.2) "R's {required} is true."
14026 */
14027 xmlSchemaPAttrUseErr4(pctxt,
14028 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14029 WXS_ITEM_NODE(item), item, cur,
14030 "The 'optional' attribute use is inconsistent "
14031 "with the corresponding 'required' attribute use of "
14032 "the %s %s",
14033 WXS_ACTION_STR(action),
14034 xmlSchemaGetComponentDesignation(&str, baseItem),
14035 NULL, NULL);
14036 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014037 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014038 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014039 WXS_ATTRUSE_TYPEDEF(cur),
14040 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14041 {
14042 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14043
14044 /*
14045 * SPEC (2.1.2) "R's {attribute declaration}'s
14046 * {type definition} must be validly derived from
14047 * B's {type definition} given the empty set as
14048 * defined in Type Derivation OK (Simple) (§3.14.6)."
14049 */
14050 xmlSchemaPAttrUseErr4(pctxt,
14051 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14052 WXS_ITEM_NODE(item), item, cur,
14053 "The attribute declaration's %s "
14054 "is not validly derived from "
14055 "the corresponding %s of the "
14056 "attribute declaration in the %s %s",
14057 xmlSchemaGetComponentDesignation(&strA,
14058 WXS_ATTRUSE_TYPEDEF(cur)),
14059 xmlSchemaGetComponentDesignation(&strB,
14060 WXS_ATTRUSE_TYPEDEF(bcur)),
14061 WXS_ACTION_STR(action),
14062 xmlSchemaGetComponentDesignation(&strC, baseItem));
14063 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14064 FREE_AND_NULL(strA);
14065 FREE_AND_NULL(strB);
14066 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014067 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014068 } else {
14069 /*
14070 * 2.1.3 [Definition:] Let the effective value
14071 * constraint of an attribute use be its {value
14072 * constraint}, if present, otherwise its {attribute
14073 * declaration}'s {value constraint} .
14074 */
14075 xmlSchemaGetEffectiveValueConstraint(bcur,
14076 &effFixed, &bEffValue, NULL);
14077 /*
14078 * 2.1.3 ... one of the following must be true
14079 *
14080 * 2.1.3.1 B's ·effective value constraint· is
14081 * ·absent· or default.
14082 */
14083 if ((bEffValue != NULL) &&
14084 (effFixed == 1)) {
14085 const xmlChar *rEffValue = NULL;
14086
14087 xmlSchemaGetEffectiveValueConstraint(bcur,
14088 &effFixed, &rEffValue, NULL);
14089 /*
14090 * 2.1.3.2 R's ·effective value constraint· is
14091 * fixed with the same string as B's.
14092 * MAYBE TODO: Compare the computed values.
14093 * Hmm, it says "same string" so
14094 * string-equality might really be sufficient.
14095 */
14096 if ((effFixed == 0) ||
14097 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14098 {
14099 xmlChar *str = NULL;
14100
14101 xmlSchemaPAttrUseErr4(pctxt,
14102 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14103 WXS_ITEM_NODE(item), item, cur,
14104 "The effective value constraint of the "
14105 "attribute use is inconsistent with "
14106 "its correspondent in the %s %s",
14107 WXS_ACTION_STR(action),
14108 xmlSchemaGetComponentDesignation(&str,
14109 baseItem),
14110 NULL, NULL);
14111 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014112 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014113 }
14114 }
14115 }
14116 break;
14117 }
14118 }
14119not_found:
14120 if (!found) {
14121 /*
14122 * (2.2) "otherwise the {base type definition} must have an
14123 * {attribute wildcard} and the {target namespace} of the
14124 * R's {attribute declaration} must be ·valid· with respect
14125 * to that wildcard, as defined in Wildcard allows Namespace
14126 * Name (§3.10.4)."
14127 */
14128 if ((baseWild == NULL) ||
14129 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14130 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14131 {
14132 xmlChar *str = NULL;
14133
14134 xmlSchemaPAttrUseErr4(pctxt,
14135 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14136 WXS_ITEM_NODE(item), item, cur,
14137 "Neither a matching attribute use, "
14138 "nor a matching wildcard exists in the %s %s",
14139 WXS_ACTION_STR(action),
14140 xmlSchemaGetComponentDesignation(&str, baseItem),
14141 NULL, NULL);
14142 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014143 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014144 }
14145 }
14146 }
14147 }
14148 /*
14149 * SPEC derivation-ok-restriction (3):
14150 * (3) "For each attribute use in the {attribute uses} of the {base type
14151 * definition} whose {required} is true, there must be an attribute
14152 * use with an {attribute declaration} with the same {name} and
14153 * {target namespace} as its {attribute declaration} in the {attribute
14154 * uses} of the complex type definition itself whose {required} is true.
14155 */
14156 if (baseUses != NULL) {
14157 for (j = 0; j < baseUses->nbItems; j++) {
14158 bcur = baseUses->items[j];
14159 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14160 continue;
14161 found = 0;
14162 if (uses != NULL) {
14163 for (i = 0; i < uses->nbItems; i++) {
14164 cur = uses->items[i];
14165 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14166 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14167 (WXS_ATTRUSE_DECL_TNS(cur) ==
14168 WXS_ATTRUSE_DECL_TNS(bcur))) {
14169 found = 1;
14170 break;
14171 }
14172 }
14173 }
14174 if (!found) {
14175 xmlChar *strA = NULL, *strB = NULL;
14176
14177 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14178 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14179 NULL, item,
14180 "A matching attribute use for the "
14181 "'required' %s of the %s %s is missing",
14182 xmlSchemaGetComponentDesignation(&strA, bcur),
14183 WXS_ACTION_STR(action),
14184 xmlSchemaGetComponentDesignation(&strB, baseItem),
14185 NULL);
14186 FREE_AND_NULL(strA);
14187 FREE_AND_NULL(strB);
14188 }
14189 }
14190 }
14191 /*
14192 * derivation-ok-restriction (4)
14193 */
14194 if (wild != NULL) {
14195 /*
14196 * (4) "If there is an {attribute wildcard}, all of the
14197 * following must be true:"
14198 */
14199 if (baseWild == NULL) {
14200 xmlChar *str = NULL;
14201
14202 /*
14203 * (4.1) "The {base type definition} must also have one."
14204 */
14205 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14206 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14207 NULL, item,
14208 "The %s has an attribute wildcard, "
14209 "but the %s %s '%s' does not have one",
14210 WXS_ITEM_TYPE_NAME(item),
14211 WXS_ACTION_STR(action),
14212 WXS_ITEM_TYPE_NAME(baseItem),
14213 xmlSchemaGetComponentQName(&str, baseItem));
14214 FREE_AND_NULL(str);
14215 return(pctxt->err);
14216 } else if ((baseWild->any == 0) &&
14217 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14218 {
14219 xmlChar *str = NULL;
14220 /*
14221 * (4.2) "The complex type definition's {attribute wildcard}'s
14222 * {namespace constraint} must be a subset of the {base type
14223 * definition}'s {attribute wildcard}'s {namespace constraint},
14224 * as defined by Wildcard Subset (§3.10.6)."
14225 */
14226 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14227 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14228 NULL, item,
14229 "The attribute wildcard is not a valid "
14230 "subset of the wildcard in the %s %s '%s'",
14231 WXS_ACTION_STR(action),
14232 WXS_ITEM_TYPE_NAME(baseItem),
14233 xmlSchemaGetComponentQName(&str, baseItem),
14234 NULL);
14235 FREE_AND_NULL(str);
14236 return(pctxt->err);
14237 }
14238 /* 4.3 Unless the {base type definition} is the ·ur-type
14239 * definition·, the complex type definition's {attribute
14240 * wildcard}'s {process contents} must be identical to or
14241 * stronger than the {base type definition}'s {attribute
14242 * wildcard}'s {process contents}, where strict is stronger
14243 * than lax is stronger than skip.
14244 */
14245 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14246 (wild->processContents < baseWild->processContents)) {
14247 xmlChar *str = NULL;
14248 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14249 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14250 NULL, baseItem,
14251 "The {process contents} of the attribute wildcard is "
14252 "weaker than the one in the %s %s '%s'",
14253 WXS_ACTION_STR(action),
14254 WXS_ITEM_TYPE_NAME(baseItem),
14255 xmlSchemaGetComponentQName(&str, baseItem),
14256 NULL);
14257 FREE_AND_NULL(str)
14258 return(pctxt->err);
14259 }
14260 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014261 return(0);
14262}
14263
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014264
14265static int
14266xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14267 xmlSchemaBasicItemPtr item,
14268 xmlSchemaWildcardPtr *completeWild,
14269 xmlSchemaItemListPtr list,
14270 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014271/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014272 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014273 * @ctxt: the schema parser context
14274 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014275 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014276 *
14277 * Builds the wildcard and the attribute uses on the given complex type.
14278 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014279 *
14280 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14281 * strings, so recheck this if we start to hardcode some schemata, since
14282 * they might not be in the same dict.
14283 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014284 */
14285static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014286xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014287 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014288{
14289 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014290 xmlSchemaAttributeUsePtr use;
14291 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014292
Daniel Veillard01fa6152004-06-29 17:04:39 +000014293 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014294 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014295 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014296 return (-1);
14297 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014298 baseType = type->baseType;
14299 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014300 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014301 return(-1);
14302
14303 uses = type->attrUses;
14304 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014305 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014306 * Expand attribute group references. And build the 'complete'
14307 * wildcard, i.e. intersect multiple wildcards.
14308 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014309 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014310 if (uses != NULL) {
14311 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014312 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014313 * This one will transfer all attr. prohibitions
14314 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014315 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014316 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14317 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14318 pctxt->attrProhibs) == -1)
14319 {
14320 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14321 "failed to expand attributes");
14322 }
14323 if (pctxt->attrProhibs->nbItems != 0)
14324 prohibs = pctxt->attrProhibs;
14325 } else {
14326 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14327 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14328 NULL) == -1)
14329 {
14330 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14331 "failed to expand attributes");
14332 }
14333 }
14334 }
14335 /*
14336 * Inherit the attribute uses of the base type.
14337 */
14338 if (baseUses != NULL) {
14339 int i, j;
14340 xmlSchemaAttributeUseProhibPtr pro;
14341
14342 if (WXS_IS_RESTRICTION(type)) {
14343 int usesCount;
14344 xmlSchemaAttributeUsePtr tmp;
14345
14346 if (uses != NULL)
14347 usesCount = uses->nbItems;
14348 else
14349 usesCount = 0;
14350
14351 /* Restriction. */
14352 for (i = 0; i < baseUses->nbItems; i++) {
14353 use = baseUses->items[i];
14354 if (prohibs) {
14355 /*
14356 * Filter out prohibited uses.
14357 */
14358 for (j = 0; j < prohibs->nbItems; j++) {
14359 pro = prohibs->items[j];
14360 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14361 (WXS_ATTRUSE_DECL_TNS(use) ==
14362 pro->targetNamespace))
14363 {
14364 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014365 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014366 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014367 }
14368 if (usesCount) {
14369 /*
14370 * Filter out existing uses.
14371 */
14372 for (j = 0; j < usesCount; j++) {
14373 tmp = uses->items[j];
14374 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14375 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14376 (WXS_ATTRUSE_DECL_TNS(use) ==
14377 WXS_ATTRUSE_DECL_TNS(tmp)))
14378 {
14379 goto inherit_next;
14380 }
14381 }
14382 }
14383 if (uses == NULL) {
14384 type->attrUses = xmlSchemaItemListCreate();
14385 if (type->attrUses == NULL)
14386 goto exit_failure;
14387 uses = type->attrUses;
14388 }
14389 xmlSchemaItemListAddSize(uses, 2, use);
14390inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014391 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014392 } else {
14393 /* Extension. */
14394 for (i = 0; i < baseUses->nbItems; i++) {
14395 use = baseUses->items[i];
14396 if (uses == NULL) {
14397 type->attrUses = xmlSchemaItemListCreate();
14398 if (type->attrUses == NULL)
14399 goto exit_failure;
14400 uses = type->attrUses;
14401 }
14402 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14403 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014404 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014405 }
14406 /*
14407 * Shrink attr. uses.
14408 */
14409 if (uses) {
14410 if (uses->nbItems == 0) {
14411 xmlSchemaItemListFree(uses);
14412 type->attrUses = NULL;
14413 }
14414 /*
14415 * TODO: We could shrink the size of the array
14416 * to fit the actual number of items.
14417 */
14418 }
14419 /*
14420 * Compute the complete wildcard.
14421 */
14422 if (WXS_IS_EXTENSION(type)) {
14423 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014424 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014425 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14426 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014427 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014428 if (type->attributeWildcard != NULL) {
14429 /*
14430 * Union the complete wildcard with the base wildcard.
14431 * SPEC {attribute wildcard}
14432 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14433 * and {annotation} are those of the ·complete wildcard·,
14434 * and whose {namespace constraint} is the intensional union
14435 * of the {namespace constraint} of the ·complete wildcard·
14436 * and of the ·base wildcard·, as defined in Attribute
14437 * Wildcard Union (§3.10.6)."
14438 */
14439 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14440 baseType->attributeWildcard) == -1)
14441 goto exit_failure;
14442 } else {
14443 /*
14444 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14445 * then the ·base wildcard·."
14446 */
14447 type->attributeWildcard = baseType->attributeWildcard;
14448 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014449 } else {
14450 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014451 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14452 * ·complete wildcard"
14453 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014454 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014455 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014456 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014457 /*
14458 * SPEC {attribute wildcard}
14459 * (3.1) "If the <restriction> alternative is chosen, then the
14460 * ·complete wildcard·;"
14461 * NOOP
14462 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014463 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014464
Daniel Veillard3646d642004-06-02 19:19:14 +000014465 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014466
14467exit_failure:
14468 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014469}
14470
14471/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014472 * xmlSchemaTypeFinalContains:
14473 * @schema: the schema
14474 * @type: the type definition
14475 * @final: the final
14476 *
14477 * Evaluates if a type definition contains the given "final".
14478 * This does take "finalDefault" into account as well.
14479 *
14480 * Returns 1 if the type does containt the given "final",
14481 * 0 otherwise.
14482 */
14483static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014484xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014485{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014486 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014487 return (0);
14488 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014489 return (1);
14490 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014491 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014492}
14493
14494/**
14495 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14496 * @type: the Union Simple Type
14497 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014498 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014499 * returns NULL otherwise.
14500 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014501static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014502xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14503{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014504 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014505 if (type->memberTypes != NULL)
14506 return (type->memberTypes);
14507 else
14508 type = type->baseType;
14509 }
14510 return (NULL);
14511}
14512
14513/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014514 * xmlSchemaGetParticleTotalRangeMin:
14515 * @particle: the particle
14516 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014517 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014518 * (all and sequence) + (choice)
14519 *
14520 * Returns the minimun Effective Total Range.
14521 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014522static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014523xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014524{
14525 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014526 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014527 return (0);
14528 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014529 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014530 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014531 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014532
14533 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014534 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014535 while (part != NULL) {
14536 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14537 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014538 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014539 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014540 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014541 if (cur == 0)
14542 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014543 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014544 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014545 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014546 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014547 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014548 } else {
14549 /* <all> and <sequence> */
14550 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014551 xmlSchemaParticlePtr part =
14552 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014553
14554 if (part == NULL)
14555 return (0);
14556 do {
14557 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14558 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014559 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014560 else
14561 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014562 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014563 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014564 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014565 }
14566}
14567
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014568/**
14569 * xmlSchemaGetParticleTotalRangeMax:
14570 * @particle: the particle
14571 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014572 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014573 * (all and sequence) + (choice)
14574 *
14575 * Returns the maximum Effective Total Range.
14576 */
14577static int
14578xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14579{
14580 if ((particle->children == NULL) ||
14581 (particle->children->children == NULL))
14582 return (0);
14583 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14584 int max = -1, cur;
14585 xmlSchemaParticlePtr part =
14586 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014587
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014588 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14589 if (part->children == NULL)
14590 continue;
14591 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14592 (part->children->type == XML_SCHEMA_TYPE_ANY))
14593 cur = part->maxOccurs;
14594 else
14595 cur = xmlSchemaGetParticleTotalRangeMax(part);
14596 if (cur == UNBOUNDED)
14597 return (UNBOUNDED);
14598 if ((max < cur) || (max == -1))
14599 max = cur;
14600 }
14601 /* TODO: Handle overflows? */
14602 return (particle->maxOccurs * max);
14603 } else {
14604 /* <all> and <sequence> */
14605 int sum = 0, cur;
14606 xmlSchemaParticlePtr part =
14607 (xmlSchemaParticlePtr) particle->children->children;
14608
14609 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14610 if (part->children == NULL)
14611 continue;
14612 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14613 (part->children->type == XML_SCHEMA_TYPE_ANY))
14614 cur = part->maxOccurs;
14615 else
14616 cur = xmlSchemaGetParticleTotalRangeMax(part);
14617 if (cur == UNBOUNDED)
14618 return (UNBOUNDED);
14619 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14620 return (UNBOUNDED);
14621 sum += cur;
14622 }
14623 /* TODO: Handle overflows? */
14624 return (particle->maxOccurs * sum);
14625 }
14626}
14627
14628/**
14629 * xmlSchemaIsParticleEmptiable:
14630 * @particle: the particle
14631 *
14632 * Schema Component Constraint: Particle Emptiable
14633 * Checks whether the given particle is emptiable.
14634 *
14635 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014636 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014637static int
14638xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14639{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014640 /*
14641 * SPEC (1) "Its {min occurs} is 0."
14642 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014643 if ((particle == NULL) || (particle->minOccurs == 0) ||
14644 (particle->children == NULL))
14645 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014646 /*
14647 * SPEC (2) "Its {term} is a group and the minimum part of the
14648 * effective total range of that group, [...] is 0."
14649 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014650 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014651 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014652 return (1);
14653 }
14654 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014655}
14656
14657/**
14658 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014659 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014660 * @type: the derived simple type definition
14661 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014662 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014663 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014664 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014665 * Type Derivation OK (Simple) (cos-st-derived-OK)
14666 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014667 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014668 * derived from @baseType.
14669 *
14670 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014671 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014672static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014673xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014674 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014675 xmlSchemaTypePtr baseType,
14676 int subset)
14677{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014678 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014679 * 1 They are the same type definition.
14680 * TODO: The identy check might have to be more complex than this.
14681 */
14682 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014683 return (0);
14684 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014685 * 2.1 restriction is not in the subset, or in the {final}
14686 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014687 *
14688 * NOTE that this will be used also via "xsi:type".
14689 *
14690 * TODO: Revise this, it looks strange. How can the "type"
14691 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014692 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014693 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014694 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014695 return(-1);
14696 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014697 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014698 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014699 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014700 (xmlSchemaTypeFinalContains(type->baseType,
14701 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14702 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014703 }
14704 /* 2.2 */
14705 if (type->baseType == baseType) {
14706 /*
14707 * 2.2.1 D's ·base type definition· is B.
14708 */
14709 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014710 }
14711 /*
14712 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14713 * and is validly derived from B given the subset, as defined by this
14714 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014715 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014716 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014717 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014718 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014719 return (0);
14720 }
14721 /*
14722 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014723 * definition·.
14724 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014725 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14726 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014727 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014728 }
14729 /*
14730 * 2.2.4 B's {variety} is union and D is validly derived from a type
14731 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014732 * defined by this constraint.
14733 *
14734 * NOTE: This seems not to involve built-in types, since there is no
14735 * built-in Union Simple Type.
14736 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014737 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014738 xmlSchemaTypeLinkPtr cur;
14739
14740 cur = baseType->memberTypes;
14741 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014742 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014743 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014744 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014745 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014746 type, cur->type, subset) == 0)
14747 {
14748 /*
14749 * It just has to be validly derived from at least one
14750 * member-type.
14751 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014752 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014753 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014754 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014755 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014756 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014757 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14758}
14759
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014760/**
14761 * xmlSchemaCheckTypeDefCircularInternal:
14762 * @pctxt: the schema parser context
14763 * @ctxtType: the type definition
14764 * @ancestor: an ancestor of @ctxtType
14765 *
14766 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014767 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014768 *
14769 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14770 * circular, 0 otherwise.
14771 */
14772static int
14773xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14774 xmlSchemaTypePtr ctxtType,
14775 xmlSchemaTypePtr ancestor)
14776{
14777 int ret;
14778
14779 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14780 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014781
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014782 if (ctxtType == ancestor) {
14783 xmlSchemaPCustomErr(pctxt,
14784 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014785 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014786 "The definition is circular", NULL);
14787 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14788 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014789 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14790 /*
14791 * Avoid inifinite recursion on circular types not yet checked.
14792 */
14793 return (0);
14794 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014795 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14796 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14797 ancestor->baseType);
14798 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14799 return (ret);
14800}
14801
14802/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014803 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014804 * @item: the complex/simple type definition
14805 * @ctxt: the parser context
14806 * @name: the name
14807 *
14808 * Checks for circular type definitions.
14809 */
14810static void
14811xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014812 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014813{
14814 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014815 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14816 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014817 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014818 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14819 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014820}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014821
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014822/*
14823* Simple Type Definition Representation OK (src-simple-type) 4
14824*
14825* "4 Circular union type definition is disallowed. That is, if the
14826* <union> alternative is chosen, there must not be any entries in the
14827* memberTypes [attribute] at any depth which resolve to the component
14828* corresponding to the <simpleType>."
14829*
14830* Note that this should work on the *representation* of a component,
14831* thus assumes any union types in the member types not being yet
14832* substituted. At this stage we need the variety of the types
14833* to be already computed.
14834*/
14835static int
14836xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14837 xmlSchemaTypePtr ctxType,
14838 xmlSchemaTypeLinkPtr members)
14839{
14840 xmlSchemaTypeLinkPtr member;
14841 xmlSchemaTypePtr memberType;
14842
14843 member = members;
14844 while (member != NULL) {
14845 memberType = member->type;
14846 while ((memberType != NULL) &&
14847 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14848 if (memberType == ctxType) {
14849 xmlSchemaPCustomErr(pctxt,
14850 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014851 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014852 "The union type definition is circular", NULL);
14853 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14854 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014855 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014856 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14857 {
14858 int res;
14859 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14860 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14861 ctxType,
14862 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14863 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14864 if (res != 0)
14865 return(res);
14866 }
14867 memberType = memberType->baseType;
14868 }
14869 member = member->next;
14870 }
14871 return(0);
14872}
14873
14874static int
14875xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14876 xmlSchemaTypePtr type)
14877{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014878 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014879 return(0);
14880 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14881 type->memberTypes));
14882}
14883
Daniel Veillard01fa6152004-06-29 17:04:39 +000014884/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014885 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014886 * @item: the complex/simple type definition
14887 * @ctxt: the parser context
14888 * @name: the name
14889 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014890 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014891 */
14892static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014893xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014894 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014895{
14896 if (typeDef == NULL)
14897 return;
14898
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014899 /*
14900 * Resolve the base type.
14901 */
14902 if (typeDef->baseType == NULL) {
14903 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14904 typeDef->base, typeDef->baseNs);
14905 if (typeDef->baseType == NULL) {
14906 xmlSchemaPResCompAttrErr(ctxt,
14907 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014908 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014909 "base", typeDef->base, typeDef->baseNs,
14910 XML_SCHEMA_TYPE_SIMPLE, NULL);
14911 return;
14912 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014913 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014914 if (WXS_IS_SIMPLE(typeDef)) {
14915 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014916 /*
14917 * Resolve the memberTypes.
14918 */
14919 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14920 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014921 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014922 /*
14923 * Resolve the itemType.
14924 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014925 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14926
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014927 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014928 typeDef->base, typeDef->baseNs);
14929
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014930 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014931 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14932 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014933 typeDef->subtypes = NULL;
14934 xmlSchemaPResCompAttrErr(ctxt,
14935 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014936 WXS_BASIC_CAST typeDef, typeDef->node,
14937 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014938 XML_SCHEMA_TYPE_SIMPLE, NULL);
14939 }
14940 }
14941 return;
14942 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014943 }
14944 /*
14945 * The ball of letters below means, that if we have a particle
14946 * which has a QName-helper component as its {term}, we want
14947 * to resolve it...
14948 */
14949 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14950 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14951 XML_SCHEMA_TYPE_PARTICLE) &&
14952 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14953 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14954 XML_SCHEMA_EXTRA_QNAMEREF))
14955 {
14956 xmlSchemaQNameRefPtr ref =
14957 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14958 xmlSchemaModelGroupDefPtr groupDef;
14959
14960 /*
14961 * URGENT TODO: Test this.
14962 */
14963 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14964 /*
14965 * Resolve the MG definition reference.
14966 */
14967 groupDef =
14968 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14969 ref->itemType, ref->name, ref->targetNamespace);
14970 if (groupDef == NULL) {
14971 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14972 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14973 "ref", ref->name, ref->targetNamespace, ref->itemType,
14974 NULL);
14975 /* Remove the particle. */
14976 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14977 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14978 /* Remove the particle. */
14979 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14980 else {
14981 /*
14982 * Assign the MG definition's {model group} to the
14983 * particle's {term}.
14984 */
14985 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14986
14987 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14988 /*
14989 * SPEC cos-all-limited (1.2)
14990 * "1.2 the {term} property of a particle with
14991 * {max occurs}=1 which is part of a pair which constitutes
14992 * the {content type} of a complex type definition."
14993 */
14994 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14995 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14996 /* TODO: error code */
14997 XML_SCHEMAP_COS_ALL_LIMITED,
14998 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14999 "The particle's {max occurs} must be 1, since the "
15000 "reference resolves to an 'all' model group",
15001 NULL, NULL);
15002 }
15003 }
15004 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015005 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015006}
15007
15008
15009
15010/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015011 * xmlSchemaCheckSTPropsCorrect:
15012 * @ctxt: the schema parser context
15013 * @type: the simple type definition
15014 *
15015 * Checks st-props-correct.
15016 *
15017 * Returns 0 if the properties are correct,
15018 * if not, a positive error code and -1 on internal
15019 * errors.
15020 */
15021static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015022xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015023 xmlSchemaTypePtr type)
15024{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015025 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015026 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015027
Daniel Veillardc0826a72004-08-10 14:17:33 +000015028 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015029 /*
15030 * Schema Component Constraint: Simple Type Definition Properties Correct
15031 *
15032 * NOTE: This is somehow redundant, since we actually built a simple type
15033 * to have all the needed information; this acts as an self test.
15034 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015035 /* Base type: If the datatype has been ·derived· by ·restriction·
15036 * then the Simple Type Definition component from which it is ·derived·,
15037 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015038 */
15039 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015040 /*
15041 * TODO: Think about: "modulo the impact of Missing
15042 * Sub-components (§5.3)."
15043 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015044 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015045 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015046 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015047 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015048 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015049
Daniel Veillard01fa6152004-06-29 17:04:39 +000015050 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015051 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015052 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015053 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015054 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015055 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015056 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015057 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015058 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15059 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015060 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015061 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015062 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015063 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015064 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015065 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015066 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015067 "the simple ur-type definition as base type, not '%s'",
15068 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015069 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015070 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15071 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015072 /*
15073 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015074 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015075 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15076 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015077 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015078 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015079 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015080 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015081 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15082 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015083 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015084
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015085 /*
15086 * 3 The {final} of the {base type definition} must not contain restriction.
15087 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015088 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015089 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15090 xmlSchemaPCustomErr(ctxt,
15091 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015092 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015093 "The 'final' of its base type '%s' must not contain "
15094 "'restriction'",
15095 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015096 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015097 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015098 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015099
15100 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015101 * 2 All simple type definitions must be derived ultimately from the ·simple
15102 * ur-type definition (so· circular definitions are disallowed). That is, it
15103 * must be possible to reach a built-in primitive datatype or the ·simple
15104 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015105 *
15106 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015107 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015108 return (0);
15109}
15110
15111/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015112 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015113 * @ctxt: the schema parser context
15114 * @type: the simple type definition
15115 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015116 * Schema Component Constraint:
15117 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15118
15119 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015120 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015121 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015122 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015123 * a positive error code otherwise.
15124 */
15125static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015126xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015127 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015128{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015129 xmlChar *str = NULL;
15130
Daniel Veillard01fa6152004-06-29 17:04:39 +000015131 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015132 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15133 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015134 return (-1);
15135 }
15136
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015137 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015138 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015139 /*
15140 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015141 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015142 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015143 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015144 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015145 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015146 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015147 "The base type '%s' is not an atomic simple type",
15148 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015149 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015150 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15151 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015152 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015153 * restriction.
15154 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015155 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015156 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015157 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015158 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015159 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015160 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015161 "The final of its base type '%s' must not contain 'restriction'",
15162 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015163 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015164 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15165 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015166
15167 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015168 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015169 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015170 * Primitive datatypes.
15171 */
15172 if (type->facets != NULL) {
15173 xmlSchemaFacetPtr facet;
15174 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015175
Daniel Veillard01fa6152004-06-29 17:04:39 +000015176 primitive = xmlSchemaGetPrimitiveType(type);
15177 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015178 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15179 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015180 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015181 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 facet = type->facets;
15183 do {
15184 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015185 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015186 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015187 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015188 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015189 }
15190 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015191 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015192 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015193 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015194 }
15195 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015196 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15197 * of the {base type definition} (call this BF),then the DF's {value}
15198 * must be a valid restriction of BF's {value} as defined in
15199 * [XML Schemas: Datatypes]."
15200 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015201 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015202 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015203 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015204 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015205 xmlSchemaTypePtr itemType = NULL;
15206
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015207 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015208 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015209 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15210 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015211 return (-1);
15212 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015213 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015214 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015215 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015216 * 2.1 The {item type definition} must have a {variety} of atomic or
15217 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015218 * must be atomic).
15219 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015220 if ((! WXS_IS_ATOMIC(itemType)) &&
15221 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015222 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015223 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015224 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015225 "The item type '%s' does not have a variety of atomic or union",
15226 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015227 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015228 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015229 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015230 xmlSchemaTypeLinkPtr member;
15231
15232 member = itemType->memberTypes;
15233 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015234 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015235 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015236 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015237 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015238 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015239 "member type '%s' of this item type is not atomic",
15240 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015241 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015242 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15243 }
15244 member = member->next;
15245 }
15246 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015247
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015248 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015249 xmlSchemaFacetPtr facet;
15250 /*
15251 * This is the case if we have: <simpleType><list ..
15252 */
15253 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015254 * 2.3.1
15255 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015256 * contain list.
15257 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015258 if (xmlSchemaTypeFinalContains(itemType,
15259 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15260 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015261 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015262 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015263 "The final of its item type '%s' must not contain 'list'",
15264 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015265 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015266 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15267 }
15268 /*
15269 * 2.3.1.2 The {facets} must only contain the whiteSpace
15270 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015271 * OPTIMIZE TODO: the S4S already disallows any facet
15272 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015273 */
15274 if (type->facets != NULL) {
15275 facet = type->facets;
15276 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015277 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015278 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015279 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015280 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015281 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15282 }
15283 facet = facet->next;
15284 } while (facet != NULL);
15285 }
15286 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 * MAYBE TODO: (Hmm, not really) Datatypes states:
15288 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15289 * whose ·lexical space· allows space (such as string or anyURI)or
15290 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015291 * ·lexical space· allows space.
15292 */
15293 } else {
15294 /*
15295 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015296 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015297 */
15298 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015299 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015300 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15301 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015302 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015303 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015304 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015305 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015306 "The base type '%s' must be a list type",
15307 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015308 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015309 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15310 }
15311 /*
15312 * 2.3.2.2 The {final} of the {base type definition} must not
15313 * contain restriction.
15314 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015315 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015316 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015317 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015318 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015319 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015320 "The 'final' of the base type '%s' must not contain 'restriction'",
15321 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015322 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015323 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15324 }
15325 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015326 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015327 * from the {base type definition}'s {item type definition} given
15328 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15329 */
15330 {
15331 xmlSchemaTypePtr baseItemType;
15332
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015333 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015334 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015335 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15336 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015337 return (-1);
15338 }
15339 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015340 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015341 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015342 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015343 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015344 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015345 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015346 "The item type '%s' is not validly derived from "
15347 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015348 xmlSchemaGetComponentQName(&str, itemType),
15349 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15350 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015351
15352 FREE_AND_NULL(str)
15353 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015354 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015355 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15356 }
15357 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015358
Daniel Veillard01fa6152004-06-29 17:04:39 +000015359 if (type->facets != NULL) {
15360 xmlSchemaFacetPtr facet;
15361 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015362 /*
15363 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015364 * and enumeration facet components are allowed among the {facets}.
15365 */
15366 facet = type->facets;
15367 do {
15368 switch (facet->type) {
15369 case XML_SCHEMA_FACET_LENGTH:
15370 case XML_SCHEMA_FACET_MINLENGTH:
15371 case XML_SCHEMA_FACET_MAXLENGTH:
15372 case XML_SCHEMA_FACET_WHITESPACE:
15373 /*
15374 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015375 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015376 */
15377 case XML_SCHEMA_FACET_PATTERN:
15378 case XML_SCHEMA_FACET_ENUMERATION:
15379 break;
15380 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015381 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015382 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015383 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015384 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015385 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015386 * invalid facets.
15387 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015388 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015389 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015390 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015391 facet = facet->next;
15392 } while (facet != NULL);
15393 if (ok == 0)
15394 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15395 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015396 * SPEC (2.3.2.5) (same as 1.3.2)
15397 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015398 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015399 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015400 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015401 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015402 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015403 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015404 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015405 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015406 * atomic or list.
15407 */
15408 xmlSchemaTypeLinkPtr member;
15409
15410 member = type->memberTypes;
15411 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015412 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015413 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015414
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015415 if ((! WXS_IS_ATOMIC(member->type)) &&
15416 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015417 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015418 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015419 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015420 "The member type '%s' is neither an atomic, nor a list type",
15421 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015422 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015423 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15424 }
15425 member = member->next;
15426 }
15427 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015428 * 3.3.1 If the {base type definition} is the ·simple ur-type
15429 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015431 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015432 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015433 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015434 * {final} which does not contain union.
15435 */
15436 member = type->memberTypes;
15437 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015438 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015439 XML_SCHEMAS_TYPE_FINAL_UNION)) {
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_1,
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 member type '%s' contains 'union'",
15444 xmlSchemaGetComponentQName(&str, member->type));
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_1);
15447 }
15448 member = member->next;
15449 }
15450 /*
15451 * 3.3.1.2 The {facets} must be empty.
15452 */
15453 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015454 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015455 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015456 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015457 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015458 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15459 }
15460 } else {
15461 /*
15462 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015463 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015464 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015465 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015466 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015467 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015468 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015469 "The base type '%s' is not a union type",
15470 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015471 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015472 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15473 }
15474 /*
15475 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15476 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015477 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015478 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015479 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015480 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015481 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015482 "The 'final' of its base type '%s' must not contain 'restriction'",
15483 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015484 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015485 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15486 }
15487 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015488 * 3.3.2.3 The {member type definitions}, in order, must be validly
15489 * derived from the corresponding type definitions in the {base
15490 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015491 * as defined in Type Derivation OK (Simple) (§3.14.6).
15492 */
15493 {
15494 xmlSchemaTypeLinkPtr baseMember;
15495
15496 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015497 * OPTIMIZE: if the type is restricting, it has no local defined
15498 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015499 * thus a check for equality can be skipped.
15500 */
15501 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015502 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015503 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015504 * types of it's base type. This check seems not necessary with
15505 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015506 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015507 */
15508 if (type->memberTypes != NULL) {
15509 member = type->memberTypes;
15510 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015511 if ((member == NULL) && (baseMember != NULL)) {
15512 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15513 "different number of member types in base");
15514 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015515 while (member != NULL) {
15516 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15518 "different number of member types in base");
Daniel Veillard14b56432006-03-09 18:41:40 +000015519 } else if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015520 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015521 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015522 xmlChar *strBMT = NULL, *strBT = NULL;
15523
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015524 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015525 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015526 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015527 "The member type %s is not validly "
15528 "derived from its corresponding member "
15529 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015530 xmlSchemaGetComponentQName(&str, member->type),
15531 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15532 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015533 FREE_AND_NULL(str)
15534 FREE_AND_NULL(strBMT)
15535 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015536 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015537 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015538 member = member->next;
15539 baseMember = baseMember->next;
15540 }
15541 }
15542 }
15543 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015544 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015545 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015546 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015547 if (type->facets != NULL) {
15548 xmlSchemaFacetPtr facet;
15549 int ok = 1;
15550
15551 facet = type->facets;
15552 do {
15553 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15554 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015555 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015556 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015557 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015558 ok = 0;
15559 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015560 facet = facet->next;
15561 } while (facet != NULL);
15562 if (ok == 0)
15563 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015564
Daniel Veillard01fa6152004-06-29 17:04:39 +000015565 }
15566 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015567 * SPEC (3.3.2.5) (same as 1.3.2)
15568 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015569 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015570 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015571 */
15572 }
15573 }
15574
15575 return (0);
15576}
15577
15578/**
15579 * xmlSchemaCheckSRCSimpleType:
15580 * @ctxt: the schema parser context
15581 * @type: the simple type definition
15582 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015583 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015584 *
15585 * Returns 0 if the constraints are satisfied,
15586 * if not a positive error code and -1 on internal
15587 * errors.
15588 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015589#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015590static int
15591xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15592 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015593{
15594 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015595 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015596 * must satisfy the conditions set out in Constraints on Simple Type
15597 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015598 */
15599 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015600 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015601 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015602 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015603 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015604 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015605 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015606 /*
15607 *
15608 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015609 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015610 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015611 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015612 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015613 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015614 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015615 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015616 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015617 /*
15618 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015619 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015620 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015621 return (0);
15622}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015623#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015624
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015625static int
15626xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15627{
15628 if (ctxt->vctxt == NULL) {
15629 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15630 if (ctxt->vctxt == NULL) {
15631 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015632 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015633 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015634 "failed to create a temp. validation context.\n",
15635 NULL, NULL);
15636 return (-1);
15637 }
15638 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015639 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015640 ctxt->error, ctxt->warning, ctxt->errCtxt);
15641 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15642 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015643 }
15644 return (0);
15645}
15646
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015647static int
15648xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15649 xmlNodePtr node,
15650 xmlSchemaTypePtr type,
15651 const xmlChar *value,
15652 xmlSchemaValPtr *retVal,
15653 int fireErrors,
15654 int normalize,
15655 int isNormalized);
15656
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015657/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015658 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015659 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015660 * @type: the simple type definition
15661 * @value: the default value
15662 * @node: an optional node (the holder of the value)
15663 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015664 * Schema Component Constraint: Element Default Valid (Immediate)
15665 * (cos-valid-default)
15666 * This will be used by the parser only. For the validator there's
15667 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015668 *
15669 * Returns 0 if the constraints are satisfied,
15670 * if not, a positive error code and -1 on internal
15671 * errors.
15672 */
15673static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015674xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15675 xmlNodePtr node,
15676 xmlSchemaTypePtr type,
15677 const xmlChar *value,
15678 xmlSchemaValPtr *val)
15679{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015680 int ret = 0;
15681
15682 /*
15683 * cos-valid-default:
15684 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015685 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015686 * definition the appropriate case among the following must be true:
15687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015688 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015689 /*
15690 * Complex type.
15691 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015692 * SPEC (2.1) "its {content type} must be a simple type definition
15693 * or mixed."
15694 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015695 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015696 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015697 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015698 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15699 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015700 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015701 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015702 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015703 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015704 "For a string to be a valid default, the type definition "
15705 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015706 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015707 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15708 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015709 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015710 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015711 * 1 If the type definition is a simple type definition, then the string
15712 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015713 * Valid (§3.14.4).
15714 *
15715 * AND
15716 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015717 * 2.2.1 If the {content type} is a simple type definition, then the
15718 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015719 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015720 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015721 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015722 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015723 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015724 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015725 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015726 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015727 else
15728 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015729
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015730 if (ret < 0) {
15731 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15732 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015733 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015734
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015735 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015736}
15737
15738/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015739 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015740 * @ctxt: the schema parser context
15741 * @type: the complex type definition
15742 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015743 *.(4.6) Constraints on Complex Type Definition Schema Components
15744 * Schema Component Constraint:
15745 * Complex Type Definition Properties Correct (ct-props-correct)
15746 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015747 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015748 * Returns 0 if the constraints are satisfied, a positive
15749 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015750 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015751static int
15752xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15753 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015754{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015755 /*
15756 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15757 *
15758 * SPEC (1) "The values of the properties of a complex type definition must
15759 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015760 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015761 * Sub-components (§5.3)."
15762 */
15763 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015764 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015765 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015766 /*
15767 * SPEC (2) "If the {base type definition} is a simple type definition,
15768 * the {derivation method} must be extension."
15769 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015770 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015771 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015772 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015773 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015774 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015775 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015776 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015777 /*
15778 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15779 * definition·. That is, it must be possible to reach the ·ur-type
15780 * definition by repeatedly following the {base type definition}."
15781 *
15782 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015783 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015784 /*
15785 * NOTE that (4) and (5) need the following:
15786 * - attribute uses need to be already inherited (apply attr. prohibitions)
15787 * - attribute group references need to be expanded already
15788 * - simple types need to be typefixed already
15789 */
15790 if (type->attrUses &&
15791 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15792 {
15793 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15794 xmlSchemaAttributeUsePtr use, tmp;
15795 int i, j, hasId = 0;
15796
15797 for (i = uses->nbItems -1; i >= 0; i--) {
15798 use = uses->items[i];
15799
15800 /*
15801 * SPEC ct-props-correct
15802 * (4) "Two distinct attribute declarations in the
15803 * {attribute uses} must not have identical {name}s and
15804 * {target namespace}s."
15805 */
15806 if (i > 0) {
15807 for (j = i -1; j >= 0; j--) {
15808 tmp = uses->items[j];
15809 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15810 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15811 (WXS_ATTRUSE_DECL_TNS(use) ==
15812 WXS_ATTRUSE_DECL_TNS(tmp)))
15813 {
15814 xmlChar *str = NULL;
15815
15816 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15817 XML_SCHEMAP_AG_PROPS_CORRECT,
15818 NULL, WXS_BASIC_CAST type,
15819 "Duplicate %s",
15820 xmlSchemaGetComponentDesignation(&str, use),
15821 NULL);
15822 FREE_AND_NULL(str);
15823 /*
15824 * Remove the duplicate.
15825 */
15826 if (xmlSchemaItemListRemove(uses, i) == -1)
15827 goto exit_failure;
15828 goto next_use;
15829 }
15830 }
15831 }
15832 /*
15833 * SPEC ct-props-correct
15834 * (5) "Two distinct attribute declarations in the
15835 * {attribute uses} must not have {type definition}s which
15836 * are or are derived from ID."
15837 */
15838 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15839 if (xmlSchemaIsDerivedFromBuiltInType(
15840 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15841 {
15842 if (hasId) {
15843 xmlChar *str = NULL;
15844
15845 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15846 XML_SCHEMAP_AG_PROPS_CORRECT,
15847 NULL, WXS_BASIC_CAST type,
15848 "There must not exist more than one attribute "
15849 "declaration of type 'xs:ID' "
15850 "(or derived from 'xs:ID'). The %s violates this "
15851 "constraint",
15852 xmlSchemaGetComponentDesignation(&str, use),
15853 NULL);
15854 FREE_AND_NULL(str);
15855 if (xmlSchemaItemListRemove(uses, i) == -1)
15856 goto exit_failure;
15857 }
15858
15859 hasId = 1;
15860 }
15861 }
15862next_use: {}
15863 }
15864 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015865 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015866exit_failure:
15867 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015868}
15869
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015870static int
15871xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15872 xmlSchemaTypePtr typeB)
15873{
15874 /*
15875 * TODO: This should implement component-identity
15876 * in the future.
15877 */
15878 if ((typeA == NULL) || (typeB == NULL))
15879 return (0);
15880 return (typeA == typeB);
15881}
15882
15883/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015884 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015885 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015886 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015887 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015888 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015889 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015890 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015891 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15892 *
15893 * STATUS: completed
15894 *
15895 * Returns 0 if the constraints are satisfied, or 1
15896 * if not.
15897 */
15898static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015899xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015900 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015901 xmlSchemaTypePtr baseType,
15902 int set)
15903{
15904 int equal = xmlSchemaAreEqualTypes(type, baseType);
15905 /* TODO: Error codes. */
15906 /*
15907 * SPEC "For a complex type definition (call it D, for derived)
15908 * to be validly derived from a type definition (call this
15909 * B, for base) given a subset of {extension, restriction}
15910 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015911 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015912 if (! equal) {
15913 /*
15914 * SPEC (1) "If B and D are not the same type definition, then the
15915 * {derivation method} of D must not be in the subset."
15916 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015917 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15918 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015919 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015920 } else {
15921 /*
15922 * SPEC (2.1) "B and D must be the same type definition."
15923 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015924 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015925 }
15926 /*
15927 * SPEC (2.2) "B must be D's {base type definition}."
15928 */
15929 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015930 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015931 /*
15932 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15933 * definition·."
15934 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015935 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015936 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015937
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015938 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015939 /*
15940 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15941 * must be validly derived from B given the subset as defined by this
15942 * constraint."
15943 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015944 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015945 baseType, set));
15946 } else {
15947 /*
15948 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15949 * must be validly derived from B given the subset as defined in Type
15950 * Derivation OK (Simple) (§3.14.6).
15951 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015952 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015953 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015954 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015955}
15956
15957/**
15958 * xmlSchemaCheckCOSDerivedOK:
15959 * @type: the derived simple type definition
15960 * @baseType: the base type definition
15961 *
15962 * Calls:
15963 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015964 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015965 * Checks wheter @type can be validly derived from @baseType.
15966 *
15967 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015968 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015969static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015970xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015971 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015972 xmlSchemaTypePtr baseType,
15973 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015974{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015975 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015976 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015977 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015978 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015979}
15980
William M. Brack2f2a6632004-08-20 23:09:47 +000015981/**
15982 * xmlSchemaCheckCOSCTExtends:
15983 * @ctxt: the schema parser context
15984 * @type: the complex type definition
15985 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015986 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015987 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015988 * Derivation Valid (Extension) (cos-ct-extends)
15989 *
15990 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015991 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015992 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015993 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015994 *
15995 * Returns 0 if the constraints are satisfied, a positive
15996 * error code if not and -1 if an internal error occured.
15997 */
15998static int
15999xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
16000 xmlSchemaTypePtr type)
16001{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016002 xmlSchemaTypePtr base = type->baseType;
16003 /*
16004 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
16005 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000016006 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016007 /*
16008 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016009 * then all of the following must be true:"
16010 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016011 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016012 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016013 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016014 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016015 */
16016 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16017 xmlSchemaPCustomErr(ctxt,
16018 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016019 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016020 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016021 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016022 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16023 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016024
16025 /*
16026 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16027 * since they are automatically satisfied through the
16028 * inheriting mechanism.
16029 * Note that even if redefining components, the inheriting mechanism
16030 * is used.
16031 */
16032#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016033 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016034 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016035 * uses}
16036 * of the complex type definition itself, that is, for every attribute
16037 * use in the {attribute uses} of the {base type definition}, there
16038 * must be an attribute use in the {attribute uses} of the complex
16039 * type definition itself whose {attribute declaration} has the same
16040 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016041 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016042 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016043 if (base->attrUses != NULL) {
16044 int i, j, found;
16045 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016046
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016047 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16048 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16049 found = 0;
16050 if (type->attrUses != NULL) {
16051 use = (WXS_LIST_CAST type->attrUses)->items[j];
16052 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16053 {
16054 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16055 WXS_ATTRUSE_DECL_NAME(buse)) &&
16056 (WXS_ATTRUSE_DECL_TNS(use) ==
16057 WXS_ATTRUSE_DECL_TNS(buse)) &&
16058 (WXS_ATTRUSE_TYPEDEF(use) ==
16059 WXS_ATTRUSE_TYPEDEF(buse))
16060 {
16061 found = 1;
16062 break;
16063 }
16064 }
16065 }
16066 if (! found) {
16067 xmlChar *str = NULL;
16068
16069 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16070 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16071 NULL, WXS_BASIC_CAST type,
16072 /*
16073 * TODO: The report does not indicate that also the
16074 * type needs to be the same.
16075 */
16076 "This type is missing a matching correspondent "
16077 "for its {base type}'s %s in its {attribute uses}",
16078 xmlSchemaGetComponentDesignation(&str,
16079 buse->children),
16080 NULL);
16081 FREE_AND_NULL(str)
16082 }
16083 }
16084 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016085 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016086 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16087 * definition must also have one, and the base type definition's
16088 * {attribute wildcard}'s {namespace constraint} must be a subset
16089 * of the complex type definition's {attribute wildcard}'s {namespace
16090 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016091 */
16092
16093 /*
16094 * MAYBE TODO: Enable if ever needed. But this will be needed only
16095 * if created the type via a schema construction API.
16096 */
16097 if (base->attributeWildcard != NULL) {
16098 if (type->attributeWilcard == NULL) {
16099 xmlChar *str = NULL;
16100
16101 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16102 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16103 NULL, type,
16104 "The base %s has an attribute wildcard, "
16105 "but this type is missing an attribute wildcard",
16106 xmlSchemaGetComponentDesignation(&str, base));
16107 FREE_AND_NULL(str)
16108
16109 } else if (xmlSchemaCheckCOSNSSubset(
16110 base->attributeWildcard, type->attributeWildcard))
16111 {
16112 xmlChar *str = NULL;
16113
16114 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16115 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16116 NULL, type,
16117 "The attribute wildcard is not a valid "
16118 "superset of the one in the base %s",
16119 xmlSchemaGetComponentDesignation(&str, base));
16120 FREE_AND_NULL(str)
16121 }
16122 }
16123#endif
16124 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016125 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016126 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016127 if ((type->contentTypeDef != NULL) &&
16128 (type->contentTypeDef == base->contentTypeDef)) {
16129 /*
16130 * SPEC (1.4.1) "The {content type} of the {base type definition}
16131 * and the {content type} of the complex type definition itself
16132 * must be the same simple type definition"
16133 * PASS
16134 */
16135 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16136 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16137 /*
16138 * SPEC (1.4.2) "The {content type} of both the {base type
16139 * definition} and the complex type definition itself must
16140 * be empty."
16141 * PASS
16142 */
16143 } else {
16144 /*
16145 * SPEC (1.4.3) "All of the following must be true:"
16146 */
16147 if (type->subtypes == NULL) {
16148 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016149 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016150 * definition itself must specify a particle.
16151 */
16152 xmlSchemaPCustomErr(ctxt,
16153 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016154 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016155 "The content type must specify a particle", NULL);
16156 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16157 }
16158 /*
16159 * SPEC (1.4.3.2) "One of the following must be true:"
16160 */
16161 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16162 /*
16163 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16164 * definition} must be empty.
16165 * PASS
16166 */
16167 } else {
16168 /*
16169 * SPEC (1.4.3.2.2) "All of the following must be true:"
16170 */
16171 if ((type->contentType != base->contentType) ||
16172 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16173 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16174 /*
16175 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16176 * or both must be element-only."
16177 */
16178 xmlSchemaPCustomErr(ctxt,
16179 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016180 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016181 "The content type of both, the type and its base "
16182 "type, must either 'mixed' or 'element-only'", NULL);
16183 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016184 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016185 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016186 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016187 * complex type definition must be a ·valid extension·
16188 * of the {base type definition}'s particle, as defined
16189 * in Particle Valid (Extension) (§3.9.6)."
16190 *
16191 * NOTE that we won't check "Particle Valid (Extension)",
16192 * since it is ensured by the derivation process in
16193 * xmlSchemaTypeFixup(). We need to implement this when heading
16194 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016195 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016196 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016197 }
16198 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016199 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016200 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016201 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016202 } else {
16203 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016204 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016205 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016206 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016207 if (type->contentTypeDef != base) {
16208 /*
16209 * SPEC (2.1) "The {content type} must be the same simple type
16210 * definition."
16211 */
16212 xmlSchemaPCustomErr(ctxt,
16213 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016214 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016215 "The content type must be the simple base type", NULL);
16216 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16217 }
16218 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16219 /*
16220 * SPEC (2.2) "The {final} of the {base type definition} must not
16221 * contain extension"
16222 * NOTE that this is the same as (1.1).
16223 */
16224 xmlSchemaPCustomErr(ctxt,
16225 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016226 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016227 "The 'final' of the base type definition "
16228 "contains 'extension'", NULL);
16229 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016230 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016231 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016232 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016233}
16234
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016235/**
16236 * xmlSchemaCheckDerivationOKRestriction:
16237 * @ctxt: the schema parser context
16238 * @type: the complex type definition
16239 *
16240 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016241 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016242 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16243 *
16244 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016245 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016246 * (5.4.2) ???
16247 *
16248 * ATTENTION:
16249 * In XML Schema 1.1 this will be:
16250 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016251 *
16252 * Returns 0 if the constraints are satisfied, a positive
16253 * error code if not and -1 if an internal error occured.
16254 */
16255static int
16256xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16257 xmlSchemaTypePtr type)
16258{
16259 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016260
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016261 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016262 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016263 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016264 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016265 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016266 if (! WXS_IS_COMPLEX(base)) {
16267 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16268 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16269 type->node, WXS_BASIC_CAST type,
16270 "The base type must be a complex type", NULL, NULL);
16271 return(ctxt->err);
16272 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016273 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16274 /*
16275 * SPEC (1) "The {base type definition} must be a complex type
16276 * definition whose {final} does not contain restriction."
16277 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016278 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16279 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16280 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016281 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016282 "contains 'restriction'", NULL, NULL);
16283 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016284 }
16285 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016286 * SPEC (2), (3) and (4)
16287 * Those are handled in a separate function, since the
16288 * same constraints are needed for redefinition of
16289 * attribute groups as well.
16290 */
16291 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16292 XML_SCHEMA_ACTION_DERIVE,
16293 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16294 type->attrUses, base->attrUses,
16295 type->attributeWildcard,
16296 base->attributeWildcard) == -1)
16297 {
16298 return(-1);
16299 }
16300 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016301 * SPEC (5) "One of the following must be true:"
16302 */
16303 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16304 /*
16305 * SPEC (5.1) "The {base type definition} must be the
16306 * ·ur-type definition·."
16307 * PASS
16308 */
16309 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16310 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16311 /*
16312 * SPEC (5.2.1) "The {content type} of the complex type definition
16313 * must be a simple type definition"
16314 *
16315 * SPEC (5.2.2) "One of the following must be true:"
16316 */
16317 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016318 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16319 {
16320 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016321 /*
16322 * SPEC (5.2.2.1) "The {content type} of the {base type
16323 * definition} must be a simple type definition from which
16324 * the {content type} is validly derived given the empty
16325 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016326 *
16327 * ATTENTION TODO: This seems not needed if the type implicitely
16328 * derived from the base type.
16329 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016330 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016331 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16332 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016333 if (err != 0) {
16334 xmlChar *strA = NULL, *strB = NULL;
16335
16336 if (err == -1)
16337 return(-1);
16338 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16339 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16340 NULL, WXS_BASIC_CAST type,
16341 "The {content type} %s is not validly derived from the "
16342 "base type's {content type} %s",
16343 xmlSchemaGetComponentDesignation(&strA,
16344 type->contentTypeDef),
16345 xmlSchemaGetComponentDesignation(&strB,
16346 base->contentTypeDef));
16347 FREE_AND_NULL(strA);
16348 FREE_AND_NULL(strB);
16349 return(ctxt->err);
16350 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016351 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16352 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016353 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016354 /*
16355 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16356 * and have a particle which is ·emptiable· as defined in
16357 * Particle Emptiable (§3.9.6)."
16358 * PASS
16359 */
16360 } else {
16361 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016362 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16363 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016364 "The content type of the base type must be either "
16365 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016366 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016367 }
16368 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16369 /*
16370 * SPEC (5.3.1) "The {content type} of the complex type itself must
16371 * be empty"
16372 */
16373 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16374 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016375 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016376 * definition} must also be empty."
16377 * PASS
16378 */
16379 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16380 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16381 xmlSchemaIsParticleEmptiable(
16382 (xmlSchemaParticlePtr) base->subtypes)) {
16383 /*
16384 * SPEC (5.3.2.2) "The {content type} of the {base type
16385 * definition} must be elementOnly or mixed and have a particle
16386 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16387 * PASS
16388 */
16389 } else {
16390 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016391 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16392 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016393 "The content type of the base type must be either "
16394 "empty or 'mixed' (or 'elements-only') and an emptiable "
16395 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016396 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016397 }
16398 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016399 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016400 /*
16401 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16402 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016403 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016404 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016405 /*
16406 * SPEC (5.4.1.2) "The {content type} of the complex type
16407 * definition itself and of the {base type definition} must be
16408 * mixed"
16409 */
16410 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016411 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16412 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016413 "If the content type is 'mixed', then the content type of the "
16414 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016415 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016416 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016417 /*
16418 * SPEC (5.4.2) "The particle of the complex type definition itself
16419 * must be a ·valid restriction· of the particle of the {content
16420 * type} of the {base type definition} as defined in Particle Valid
16421 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016422 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016423 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016424 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016425 } else {
16426 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016427 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16428 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016429 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016430 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016431 }
16432 return (0);
16433}
16434
16435/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016436 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016437 * @ctxt: the schema parser context
16438 * @type: the complex type definition
16439 *
16440 * (3.4.6) Constraints on Complex Type Definition Schema Components
16441 *
16442 * Returns 0 if the constraints are satisfied, a positive
16443 * error code if not and -1 if an internal error occured.
16444 */
16445static int
16446xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16447 xmlSchemaTypePtr type)
16448{
16449 int ret;
16450 /*
16451 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016452 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016453 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16454 if (ret != 0)
16455 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016456 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016457 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16458 else
16459 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16460 return (ret);
16461}
16462
16463/**
16464 * xmlSchemaCheckSRCCT:
16465 * @ctxt: the schema parser context
16466 * @type: the complex type definition
16467 *
16468 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016469 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016470 * Complex Type Definition Representation OK (src-ct)
16471 *
16472 * Returns 0 if the constraints are satisfied, a positive
16473 * error code if not and -1 if an internal error occured.
16474 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016475static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016476xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016477 xmlSchemaTypePtr type)
16478{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016479 xmlSchemaTypePtr base;
16480 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016481
16482 /*
16483 * TODO: Adjust the error codes here, as I used
16484 * XML_SCHEMAP_SRC_CT_1 only yet.
16485 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016486 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016487 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016488 /*
16489 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016490 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016491 * must be a complex type definition;
16492 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016493 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016494 xmlChar *str = NULL;
16495 xmlSchemaPCustomErr(ctxt,
16496 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016497 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016498 "If using <complexContent>, the base type is expected to be "
16499 "a complex type. The base type '%s' is a simple type",
16500 xmlSchemaFormatQName(&str, base->targetNamespace,
16501 base->name));
16502 FREE_AND_NULL(str)
16503 return (XML_SCHEMAP_SRC_CT_1);
16504 }
16505 } else {
16506 /*
16507 * SPEC
16508 * 2 If the <simpleContent> alternative is chosen, all of the
16509 * following must be true:
16510 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16511 * base [attribute] must be one of the following:
16512 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016513 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016514 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016515 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016516 /*
16517 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016518 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016519 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016520 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016521 xmlSchemaPCustomErr(ctxt,
16522 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016523 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016524 "If using <simpleContent> and <restriction>, the base "
16525 "type must be a complex type. The base type '%s' is "
16526 "a simple type",
16527 xmlSchemaFormatQName(&str, base->targetNamespace,
16528 base->name));
16529 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016530 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016531 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016532 } else {
16533 /* Base type is a complex type. */
16534 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16535 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16536 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016537 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016538 * simple type definition;
16539 * PASS
16540 */
16541 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016542 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016543 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016545 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016546 type->name);
16547 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016548 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016549 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016550 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016551
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016552 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016553 * 2.1.2 only if the <restriction> alternative is also
16554 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016555 * is mixed and a particle emptiable.
16556 */
16557 if (! xmlSchemaIsParticleEmptiable(
16558 (xmlSchemaParticlePtr) base->subtypes)) {
16559 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016560 } else
16561 /*
16562 * Attention: at this point the <simpleType> child is in
16563 * ->contentTypeDef (put there during parsing).
16564 */
16565 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016566 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016567 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016568 * 2.2 If clause 2.1.2 above is satisfied, then there
16569 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016570 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016571 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016572 /* TODO: Change error code to ..._SRC_CT_2_2. */
16573 xmlSchemaPCustomErr(ctxt,
16574 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016575 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016576 "A <simpleType> is expected among the children "
16577 "of <restriction>, if <simpleContent> is used and "
16578 "the base type '%s' is a complex type",
16579 xmlSchemaFormatQName(&str, base->targetNamespace,
16580 base->name));
16581 FREE_AND_NULL(str)
16582 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016583 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016584 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016585 ret = XML_SCHEMAP_SRC_CT_1;
16586 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016587 }
16588 if (ret > 0) {
16589 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016590 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016591 xmlSchemaPCustomErr(ctxt,
16592 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016593 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016594 "If <simpleContent> and <restriction> is used, the "
16595 "base type must be a simple type or a complex type with "
16596 "mixed content and particle emptiable. The base type "
16597 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016598 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016599 base->name));
16600 } else {
16601 xmlSchemaPCustomErr(ctxt,
16602 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016603 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016604 "If <simpleContent> and <extension> is used, the "
16605 "base type must be a simple type. The base type '%s' "
16606 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016607 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016608 base->name));
16609 }
16610 FREE_AND_NULL(str)
16611 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016612 }
16613 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016614 * SPEC (3) "The corresponding complex type definition component must
16615 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016616 * Definition Schema Components (§3.4.6);"
16617 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016618 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016619 /*
16620 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016621 * above for {attribute wildcard} is satisfied, the intensional
16622 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016623 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016624 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016625 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016626 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016627}
William M. Brack2f2a6632004-08-20 23:09:47 +000016628
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016629#ifdef ENABLE_PARTICLE_RESTRICTION
16630/**
16631 * xmlSchemaCheckParticleRangeOK:
16632 * @ctxt: the schema parser context
16633 * @type: the complex type definition
16634 *
16635 * (3.9.6) Constraints on Particle Schema Components
16636 * Schema Component Constraint:
16637 * Occurrence Range OK (range-ok)
16638 *
16639 * STATUS: complete
16640 *
16641 * Returns 0 if the constraints are satisfied, a positive
16642 * error code if not and -1 if an internal error occured.
16643 */
16644static int
16645xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16646 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016647{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016648 if (rmin < bmin)
16649 return (1);
16650 if ((bmax != UNBOUNDED) &&
16651 (rmax > bmax))
16652 return (1);
16653 return (0);
16654}
16655
16656/**
16657 * xmlSchemaCheckRCaseNameAndTypeOK:
16658 * @ctxt: the schema parser context
16659 * @r: the restricting element declaration particle
16660 * @b: the base element declaration particle
16661 *
16662 * (3.9.6) Constraints on Particle Schema Components
16663 * Schema Component Constraint:
16664 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16665 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016666 *
16667 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016668 * MISSING (3.2.3)
16669 * CLARIFY: (3.2.2)
16670 *
16671 * Returns 0 if the constraints are satisfied, a positive
16672 * error code if not and -1 if an internal error occured.
16673 */
16674static int
16675xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16676 xmlSchemaParticlePtr r,
16677 xmlSchemaParticlePtr b)
16678{
16679 xmlSchemaElementPtr elemR, elemB;
16680
16681 /* TODO: Error codes (rcase-NameAndTypeOK). */
16682 elemR = (xmlSchemaElementPtr) r->children;
16683 elemB = (xmlSchemaElementPtr) b->children;
16684 /*
16685 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16686 * the same."
16687 */
16688 if ((elemR != elemB) &&
16689 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16690 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16691 return (1);
16692 /*
16693 * SPEC (2) "R's occurrence range is a valid restriction of B's
16694 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16695 */
16696 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16697 b->minOccurs, b->maxOccurs) != 0)
16698 return (1);
16699 /*
16700 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16701 * {scope} are global."
16702 */
16703 if (elemR == elemB)
16704 return (0);
16705 /*
16706 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16707 */
16708 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16709 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16710 return (1);
16711 /*
16712 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16713 * or is not fixed, or R's declaration's {value constraint} is fixed
16714 * with the same value."
16715 */
16716 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16717 ((elemR->value == NULL) ||
16718 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16719 /* TODO: Equality of the initial value or normalized or canonical? */
16720 (! xmlStrEqual(elemR->value, elemB->value))))
16721 return (1);
16722 /*
16723 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16724 * definitions} is a subset of B's declaration's {identity-constraint
16725 * definitions}, if any."
16726 */
16727 if (elemB->idcs != NULL) {
16728 /* TODO */
16729 }
16730 /*
16731 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16732 * superset of B's declaration's {disallowed substitutions}."
16733 */
16734 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16735 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16736 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16737 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16738 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16739 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16740 return (1);
16741 /*
16742 * SPEC (3.2.5) "R's {type definition} is validly derived given
16743 * {extension, list, union} from B's {type definition}"
16744 *
16745 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16746 * set, if the corresponding constraints handle "restriction" and
16747 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016748 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016749 */
16750 {
16751 int set = 0;
16752
16753 set |= SUBSET_EXTENSION;
16754 set |= SUBSET_LIST;
16755 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016756 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016757 elemB->subtypes, set) != 0)
16758 return (1);
16759 }
16760 return (0);
16761}
16762
16763/**
16764 * xmlSchemaCheckRCaseNSCompat:
16765 * @ctxt: the schema parser context
16766 * @r: the restricting element declaration particle
16767 * @b: the base wildcard particle
16768 *
16769 * (3.9.6) Constraints on Particle Schema Components
16770 * Schema Component Constraint:
16771 * Particle Derivation OK (Elt:Any -- NSCompat)
16772 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016773 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016774 * STATUS: complete
16775 *
16776 * Returns 0 if the constraints are satisfied, a positive
16777 * error code if not and -1 if an internal error occured.
16778 */
16779static int
16780xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16781 xmlSchemaParticlePtr r,
16782 xmlSchemaParticlePtr b)
16783{
16784 /* TODO:Error codes (rcase-NSCompat). */
16785 /*
16786 * SPEC "For an element declaration particle to be a ·valid restriction·
16787 * of a wildcard particle all of the following must be true:"
16788 *
16789 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16790 * with respect to the wildcard's {namespace constraint} as defined by
16791 * Wildcard allows Namespace Name (§3.10.4)."
16792 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016793 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016794 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16795 return (1);
16796 /*
16797 * SPEC (2) "R's occurrence range is a valid restriction of B's
16798 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16799 */
16800 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16801 b->minOccurs, b->maxOccurs) != 0)
16802 return (1);
16803
16804 return (0);
16805}
16806
16807/**
16808 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16809 * @ctxt: the schema parser context
16810 * @r: the restricting element declaration particle
16811 * @b: the base model group particle
16812 *
16813 * (3.9.6) Constraints on Particle Schema Components
16814 * Schema Component Constraint:
16815 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16816 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016817 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016818 * STATUS: TODO
16819 *
16820 * Returns 0 if the constraints are satisfied, a positive
16821 * error code if not and -1 if an internal error occured.
16822 */
16823static int
16824xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16825 xmlSchemaParticlePtr r,
16826 xmlSchemaParticlePtr b)
16827{
16828 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16829 TODO
16830 return (0);
16831}
16832
16833/**
16834 * xmlSchemaCheckRCaseNSSubset:
16835 * @ctxt: the schema parser context
16836 * @r: the restricting wildcard particle
16837 * @b: the base wildcard particle
16838 *
16839 * (3.9.6) Constraints on Particle Schema Components
16840 * Schema Component Constraint:
16841 * Particle Derivation OK (Any:Any -- NSSubset)
16842 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016843 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016844 * STATUS: complete
16845 *
16846 * Returns 0 if the constraints are satisfied, a positive
16847 * error code if not and -1 if an internal error occured.
16848 */
16849static int
16850xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16851 xmlSchemaParticlePtr r,
16852 xmlSchemaParticlePtr b,
16853 int isAnyTypeBase)
16854{
16855 /* TODO: Error codes (rcase-NSSubset). */
16856 /*
16857 * SPEC (1) "R's occurrence range is a valid restriction of B's
16858 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16859 */
16860 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16861 b->minOccurs, b->maxOccurs))
16862 return (1);
16863 /*
16864 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16865 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16866 */
16867 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16868 (xmlSchemaWildcardPtr) b->children))
16869 return (1);
16870 /*
16871 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16872 * definition·, R's {process contents} must be identical to or stronger
16873 * than B's {process contents}, where strict is stronger than lax is
16874 * stronger than skip."
16875 */
16876 if (! isAnyTypeBase) {
16877 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16878 ((xmlSchemaWildcardPtr) b->children)->processContents)
16879 return (1);
16880 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016881
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016882 return (0);
16883}
16884
16885/**
16886 * xmlSchemaCheckCOSParticleRestrict:
16887 * @ctxt: the schema parser context
16888 * @type: the complex type definition
16889 *
16890 * (3.9.6) Constraints on Particle Schema Components
16891 * Schema Component Constraint:
16892 * Particle Valid (Restriction) (cos-particle-restrict)
16893 *
16894 * STATUS: TODO
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
16900xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16901 xmlSchemaParticlePtr r,
16902 xmlSchemaParticlePtr b)
16903{
16904 int ret = 0;
16905
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016906 /*part = WXS_TYPE_PARTICLE(type);
16907 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016908 */
16909
16910 TODO
16911
16912 /*
16913 * SPEC (1) "They are the same particle."
16914 */
16915 if (r == b)
16916 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016917
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016918
16919 return (0);
16920}
16921
16922/**
16923 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16924 * @ctxt: the schema parser context
16925 * @r: the model group particle
16926 * @b: the base wildcard particle
16927 *
16928 * (3.9.6) Constraints on Particle Schema Components
16929 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016930 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016931 * NSRecurseCheckCardinality)
16932 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016933 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016934 * STATUS: TODO: subst-groups
16935 *
16936 * Returns 0 if the constraints are satisfied, a positive
16937 * error code if not and -1 if an internal error occured.
16938 */
16939static int
16940xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16941 xmlSchemaParticlePtr r,
16942 xmlSchemaParticlePtr b)
16943{
16944 xmlSchemaParticlePtr part;
16945 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16946 if ((r->children == NULL) || (r->children->children == NULL))
16947 return (-1);
16948 /*
16949 * SPEC "For a group particle to be a ·valid restriction· of a
16950 * wildcard particle..."
16951 *
16952 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016953 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016954 * Particle Valid (Restriction) (§3.9.6)."
16955 */
16956 part = (xmlSchemaParticlePtr) r->children->children;
16957 do {
16958 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16959 return (1);
16960 part = (xmlSchemaParticlePtr) part->next;
16961 } while (part != NULL);
16962 /*
16963 * SPEC (2) "The effective total range of the group [...] is a
16964 * valid restriction of B's occurrence range as defined by
16965 * Occurrence Range OK (§3.9.6)."
16966 */
16967 if (xmlSchemaCheckParticleRangeOK(
16968 xmlSchemaGetParticleTotalRangeMin(r),
16969 xmlSchemaGetParticleTotalRangeMax(r),
16970 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016971 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016972 return (0);
16973}
16974
16975/**
16976 * xmlSchemaCheckRCaseRecurse:
16977 * @ctxt: the schema parser context
16978 * @r: the <all> or <sequence> model group particle
16979 * @b: the base <all> or <sequence> model group particle
16980 *
16981 * (3.9.6) Constraints on Particle Schema Components
16982 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016983 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016984 Recurse)
16985 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016986 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016987 * STATUS: ?
16988 * TODO: subst-groups
16989 *
16990 * Returns 0 if the constraints are satisfied, a positive
16991 * error code if not and -1 if an internal error occured.
16992 */
16993static int
16994xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16995 xmlSchemaParticlePtr r,
16996 xmlSchemaParticlePtr b)
16997{
16998 /* xmlSchemaParticlePtr part; */
16999 /* TODO: Error codes (rcase-Recurse). */
17000 if ((r->children == NULL) || (b->children == NULL) ||
17001 (r->children->type != b->children->type))
17002 return (-1);
17003 /*
17004 * SPEC "For an all or sequence group particle to be a ·valid
17005 * restriction· of another group particle with the same {compositor}..."
17006 *
17007 * SPEC (1) "R's occurrence range is a valid restriction of B's
17008 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
17009 */
17010 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17011 b->minOccurs, b->maxOccurs))
17012 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017013
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017014
17015 return (0);
17016}
17017
17018#endif
17019
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017020#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17021 xmlSchemaPCustomErrExt(pctxt, \
17022 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017023 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017024 "It is an error for both '%s' and '%s' to be specified on the "\
17025 "same type definition", \
17026 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17027 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17028
17029#define FACET_RESTR_ERR(fac1, msg) \
17030 xmlSchemaPCustomErr(pctxt, \
17031 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017032 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017033 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017034
17035#define FACET_RESTR_FIXED_ERR(fac) \
17036 xmlSchemaPCustomErr(pctxt, \
17037 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017038 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017039 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017040 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017041
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017042static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017043xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17044 xmlSchemaFacetPtr facet1,
17045 xmlSchemaFacetPtr facet2,
17046 int lessGreater,
17047 int orEqual,
17048 int ofBase)
17049{
17050 xmlChar *msg = NULL;
17051
17052 msg = xmlStrdup(BAD_CAST "'");
17053 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17054 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17055 if (lessGreater == 0)
17056 msg = xmlStrcat(msg, BAD_CAST " equal to");
17057 if (lessGreater == 1)
17058 msg = xmlStrcat(msg, BAD_CAST " greater than");
17059 else
17060 msg = xmlStrcat(msg, BAD_CAST " less than");
17061
17062 if (orEqual)
17063 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17064 msg = xmlStrcat(msg, BAD_CAST " '");
17065 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17066 if (ofBase)
17067 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17068 else
17069 msg = xmlStrcat(msg, BAD_CAST "'");
17070
17071 xmlSchemaPCustomErr(pctxt,
17072 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017073 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017074 (const char *) msg, NULL);
17075
17076 if (msg != NULL)
17077 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017078}
17079
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017080/*
17081* xmlSchemaDeriveAndValidateFacets:
17082*
17083* Schema Component Constraint: Simple Type Restriction (Facets)
17084* (st-restrict-facets)
17085*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017086static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017087xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17088 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017089{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017090 xmlSchemaTypePtr base = type->baseType;
17091 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017092 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017093 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17094 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17095 fmininc = NULL, fmaxinc = NULL,
17096 fminexc = NULL, fmaxexc = NULL,
17097 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17098 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17099 bfmininc = NULL, bfmaxinc = NULL,
17100 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017101 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017102
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017103 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017104 * SPEC st-restrict-facets 1:
17105 * "The {variety} of R is the same as that of B."
17106 */
17107 /*
17108 * SPEC st-restrict-facets 2:
17109 * "If {variety} is atomic, the {primitive type definition}
17110 * of R is the same as that of B."
17111 *
17112 * NOTE: we leave 1 & 2 out for now, since this will be
17113 * satisfied by the derivation process.
17114 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17115 */
17116 /*
17117 * SPEC st-restrict-facets 3:
17118 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017119 * of B, eliminating duplicates. To eliminate duplicates,
17120 * when a facet of the same kind occurs in both S and the
17121 * {facets} of B, the one in the {facets} of B is not
17122 * included, with the exception of enumeration and pattern
17123 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017124 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017125 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017126
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017127 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17128 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017129
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017130 last = type->facetSet;
17131 if (last != NULL)
17132 while (last->next != NULL)
17133 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017134
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017135 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17136 facet = cur->facet;
17137 switch (facet->type) {
17138 case XML_SCHEMA_FACET_LENGTH:
17139 flength = facet; break;
17140 case XML_SCHEMA_FACET_MINLENGTH:
17141 fminlen = facet; break;
17142 case XML_SCHEMA_FACET_MININCLUSIVE:
17143 fmininc = facet; break;
17144 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17145 fminexc = facet; break;
17146 case XML_SCHEMA_FACET_MAXLENGTH:
17147 fmaxlen = facet; break;
17148 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17149 fmaxinc = facet; break;
17150 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17151 fmaxexc = facet; break;
17152 case XML_SCHEMA_FACET_TOTALDIGITS:
17153 ftotdig = facet; break;
17154 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17155 ffracdig = facet; break;
17156 default:
17157 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017158 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017159 }
17160 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17161 facet = cur->facet;
17162 switch (facet->type) {
17163 case XML_SCHEMA_FACET_LENGTH:
17164 bflength = facet; break;
17165 case XML_SCHEMA_FACET_MINLENGTH:
17166 bfminlen = facet; break;
17167 case XML_SCHEMA_FACET_MININCLUSIVE:
17168 bfmininc = facet; break;
17169 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17170 bfminexc = facet; break;
17171 case XML_SCHEMA_FACET_MAXLENGTH:
17172 bfmaxlen = facet; break;
17173 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17174 bfmaxinc = facet; break;
17175 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17176 bfmaxexc = facet; break;
17177 case XML_SCHEMA_FACET_TOTALDIGITS:
17178 bftotdig = facet; break;
17179 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17180 bffracdig = facet; break;
17181 default:
17182 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017183 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017184 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017185 /*
17186 * length and minLength or maxLength (2.2) + (3.2)
17187 */
17188 if (flength && (fminlen || fmaxlen)) {
17189 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17190 "either of 'minLength' or 'maxLength' to be specified on "
17191 "the same type definition")
17192 }
17193 /*
17194 * Mutual exclusions in the same derivation step.
17195 */
17196 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017197 /*
17198 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017199 */
17200 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17201 }
17202 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017203 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017204 * SCC "minInclusive and minExclusive"
17205 */
17206 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017207 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017208
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017209 if (flength && bflength) {
17210 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017211 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017212 * The values have to be equal.
17213 */
17214 res = xmlSchemaCompareValues(flength->val, bflength->val);
17215 if (res == -2)
17216 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017217 if (res != 0)
17218 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17219 if ((res != 0) && (bflength->fixed)) {
17220 FACET_RESTR_FIXED_ERR(flength)
17221 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017222
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017223 }
17224 if (fminlen && bfminlen) {
17225 /*
17226 * SCC "minLength valid restriction"
17227 * minLength >= BASE minLength
17228 */
17229 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17230 if (res == -2)
17231 goto internal_error;
17232 if (res == -1)
17233 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17234 if ((res != 0) && (bfminlen->fixed)) {
17235 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017236 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017237 }
17238 if (fmaxlen && bfmaxlen) {
17239 /*
17240 * SCC "maxLength valid restriction"
17241 * maxLength <= BASE minLength
17242 */
17243 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17244 if (res == -2)
17245 goto internal_error;
17246 if (res == 1)
17247 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17248 if ((res != 0) && (bfmaxlen->fixed)) {
17249 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017250 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017251 }
17252 /*
17253 * SCC "length and minLength or maxLength"
17254 */
17255 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017256 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017257 if (flength) {
17258 if (! fminlen)
17259 flength = bflength;
17260 if (fminlen) {
17261 /* (1.1) length >= minLength */
17262 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17263 if (res == -2)
17264 goto internal_error;
17265 if (res == -1)
17266 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17267 }
17268 if (! fmaxlen)
17269 fmaxlen = bfmaxlen;
17270 if (fmaxlen) {
17271 /* (2.1) length <= maxLength */
17272 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17273 if (res == -2)
17274 goto internal_error;
17275 if (res == 1)
17276 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17277 }
17278 }
17279 if (fmaxinc) {
17280 /*
17281 * "maxInclusive"
17282 */
17283 if (fmininc) {
17284 /* SCC "maxInclusive >= minInclusive" */
17285 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17286 if (res == -2)
17287 goto internal_error;
17288 if (res == -1) {
17289 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17290 }
17291 }
17292 /*
17293 * SCC "maxInclusive valid restriction"
17294 */
17295 if (bfmaxinc) {
17296 /* maxInclusive <= BASE maxInclusive */
17297 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17298 if (res == -2)
17299 goto internal_error;
17300 if (res == 1)
17301 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17302 if ((res != 0) && (bfmaxinc->fixed)) {
17303 FACET_RESTR_FIXED_ERR(fmaxinc)
17304 }
17305 }
17306 if (bfmaxexc) {
17307 /* maxInclusive < BASE maxExclusive */
17308 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17309 if (res == -2)
17310 goto internal_error;
17311 if (res != -1) {
17312 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17313 }
17314 }
17315 if (bfmininc) {
17316 /* maxInclusive >= BASE minInclusive */
17317 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17318 if (res == -2)
17319 goto internal_error;
17320 if (res == -1) {
17321 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17322 }
17323 }
17324 if (bfminexc) {
17325 /* maxInclusive > BASE minExclusive */
17326 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17327 if (res == -2)
17328 goto internal_error;
17329 if (res != 1) {
17330 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17331 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017332 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017333 }
17334 if (fmaxexc) {
17335 /*
17336 * "maxExclusive >= minExclusive"
17337 */
17338 if (fminexc) {
17339 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17340 if (res == -2)
17341 goto internal_error;
17342 if (res == -1) {
17343 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17344 }
17345 }
17346 /*
17347 * "maxExclusive valid restriction"
17348 */
17349 if (bfmaxexc) {
17350 /* maxExclusive <= BASE maxExclusive */
17351 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17352 if (res == -2)
17353 goto internal_error;
17354 if (res == 1) {
17355 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17356 }
17357 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017358 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017359 }
17360 }
17361 if (bfmaxinc) {
17362 /* maxExclusive <= BASE maxInclusive */
17363 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17364 if (res == -2)
17365 goto internal_error;
17366 if (res == 1) {
17367 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17368 }
17369 }
17370 if (bfmininc) {
17371 /* maxExclusive > BASE minInclusive */
17372 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17373 if (res == -2)
17374 goto internal_error;
17375 if (res != 1) {
17376 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17377 }
17378 }
17379 if (bfminexc) {
17380 /* maxExclusive > BASE minExclusive */
17381 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17382 if (res == -2)
17383 goto internal_error;
17384 if (res != 1) {
17385 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17386 }
17387 }
17388 }
17389 if (fminexc) {
17390 /*
17391 * "minExclusive < maxInclusive"
17392 */
17393 if (fmaxinc) {
17394 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17395 if (res == -2)
17396 goto internal_error;
17397 if (res != -1) {
17398 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17399 }
17400 }
17401 /*
17402 * "minExclusive valid restriction"
17403 */
17404 if (bfminexc) {
17405 /* minExclusive >= BASE minExclusive */
17406 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17407 if (res == -2)
17408 goto internal_error;
17409 if (res == -1) {
17410 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17411 }
17412 if ((res != 0) && (bfminexc->fixed)) {
17413 FACET_RESTR_FIXED_ERR(fminexc)
17414 }
17415 }
17416 if (bfmaxinc) {
17417 /* minExclusive <= BASE maxInclusive */
17418 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17419 if (res == -2)
17420 goto internal_error;
17421 if (res == 1) {
17422 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17423 }
17424 }
17425 if (bfmininc) {
17426 /* minExclusive >= BASE minInclusive */
17427 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17428 if (res == -2)
17429 goto internal_error;
17430 if (res == -1) {
17431 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17432 }
17433 }
17434 if (bfmaxexc) {
17435 /* minExclusive < BASE maxExclusive */
17436 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17437 if (res == -2)
17438 goto internal_error;
17439 if (res != -1) {
17440 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17441 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017442 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017443 }
17444 if (fmininc) {
17445 /*
17446 * "minInclusive < maxExclusive"
17447 */
17448 if (fmaxexc) {
17449 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17450 if (res == -2)
17451 goto internal_error;
17452 if (res != -1) {
17453 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17454 }
17455 }
17456 /*
17457 * "minExclusive valid restriction"
17458 */
17459 if (bfmininc) {
17460 /* minInclusive >= BASE minInclusive */
17461 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17462 if (res == -2)
17463 goto internal_error;
17464 if (res == -1) {
17465 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17466 }
17467 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017468 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017469 }
17470 }
17471 if (bfmaxinc) {
17472 /* minInclusive <= BASE maxInclusive */
17473 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17474 if (res == -2)
17475 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017476 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017477 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17478 }
17479 }
17480 if (bfminexc) {
17481 /* minInclusive > BASE minExclusive */
17482 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17483 if (res == -2)
17484 goto internal_error;
17485 if (res != 1)
17486 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17487 }
17488 if (bfmaxexc) {
17489 /* minInclusive < BASE maxExclusive */
17490 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17491 if (res == -2)
17492 goto internal_error;
17493 if (res != -1)
17494 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17495 }
17496 }
17497 if (ftotdig && bftotdig) {
17498 /*
17499 * SCC " totalDigits valid restriction"
17500 * totalDigits <= BASE totalDigits
17501 */
17502 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17503 if (res == -2)
17504 goto internal_error;
17505 if (res == 1)
17506 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17507 -1, 1, 1);
17508 if ((res != 0) && (bftotdig->fixed)) {
17509 FACET_RESTR_FIXED_ERR(ftotdig)
17510 }
17511 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017512 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017513 /*
17514 * SCC "fractionDigits valid restriction"
17515 * fractionDigits <= BASE fractionDigits
17516 */
17517 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17518 if (res == -2)
17519 goto internal_error;
17520 if (res == 1)
17521 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17522 -1, 1, 1);
17523 if ((res != 0) && (bffracdig->fixed)) {
17524 FACET_RESTR_FIXED_ERR(ffracdig)
17525 }
17526 }
17527 /*
17528 * SCC "fractionDigits less than or equal to totalDigits"
17529 */
17530 if (! ftotdig)
17531 ftotdig = bftotdig;
17532 if (! ffracdig)
17533 ffracdig = bffracdig;
17534 if (ftotdig && ffracdig) {
17535 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17536 if (res == -2)
17537 goto internal_error;
17538 if (res == 1)
17539 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17540 -1, 1, 0);
17541 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017542 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017543 * *Enumerations* won' be added here, since only the first set
17544 * of enumerations in the ancestor-or-self axis is used
17545 * for validation, plus we need to use the base type of those
17546 * enumerations for whitespace.
17547 *
17548 * *Patterns*: won't be add here, since they are ORed at
17549 * type level and ANDed at ancestor level. This will
17550 * happed during validation by walking the base axis
17551 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017552 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017553 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17554 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017555 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017556 * Special handling of enumerations and patterns.
17557 * TODO: hmm, they should not appear in the set, so remove this.
17558 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017559 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017560 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017561 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017562 /*
17563 * Search for a duplicate facet in the current type.
17564 */
17565 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017566 /* err = 0; */
17567 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017568 while (link != NULL) {
17569 facet = link->facet;
17570 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017571 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 case XML_SCHEMA_FACET_WHITESPACE:
17573 /*
17574 * The whitespace must be stronger.
17575 */
17576 if (facet->whitespace < bfacet->whitespace) {
17577 FACET_RESTR_ERR(flength,
17578 "The 'whitespace' value has to be equal to "
17579 "or stronger than the 'whitespace' value of "
17580 "the base type")
17581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017582 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017583 (facet->whitespace != bfacet->whitespace)) {
17584 FACET_RESTR_FIXED_ERR(facet)
17585 }
17586 break;
17587 default:
17588 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017589 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017590 /* Duplicate found. */
17591 break;
17592 }
17593 link = link->next;
17594 }
17595 /*
17596 * If no duplicate was found: add the base types's facet
17597 * to the set.
17598 */
17599 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017600 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017601 xmlMalloc(sizeof(xmlSchemaFacetLink));
17602 if (link == NULL) {
17603 xmlSchemaPErrMemory(pctxt,
17604 "deriving facets, creating a facet link", NULL);
17605 return (-1);
17606 }
17607 link->facet = cur->facet;
17608 link->next = NULL;
17609 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017610 type->facetSet = link;
17611 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017612 last->next = link;
17613 last = link;
17614 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017615
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017616 }
17617
17618 return (0);
17619internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017620 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17621 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017622 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017623}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017624
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017625static int
17626xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17627 xmlSchemaTypePtr type)
17628{
17629 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17630 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017631 * The actual value is then formed by replacing any union type
17632 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017633 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017634 *
17635 * TODO: There's a bug entry at
17636 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17637 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017638 */
17639 link = type->memberTypes;
17640 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017641
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017642 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017643 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017644
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017645 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017646 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017647 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017648 link->type = subLink->type;
17649 if (subLink->next != NULL) {
17650 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017651 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017652 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017653 while (subLink != NULL) {
17654 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017655 xmlMalloc(sizeof(xmlSchemaTypeLink));
17656 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017657 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017658 NULL);
17659 return (-1);
17660 }
17661 newLink->type = subLink->type;
17662 prevLink->next = newLink;
17663 prevLink = newLink;
17664 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017665
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017666 subLink = subLink->next;
17667 }
17668 }
17669 }
17670 }
17671 link = link->next;
17672 }
17673 return (0);
17674}
17675
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017676static void
17677xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17678{
17679 int has = 0, needVal = 0, normVal = 0;
17680
17681 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17682 if (has) {
17683 needVal = (type->baseType->flags &
17684 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17685 normVal = (type->baseType->flags &
17686 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17687 }
17688 if (type->facets != NULL) {
17689 xmlSchemaFacetPtr fac;
17690
17691 for (fac = type->facets; fac != NULL; fac = fac->next) {
17692 switch (fac->type) {
17693 case XML_SCHEMA_FACET_WHITESPACE:
17694 break;
17695 case XML_SCHEMA_FACET_PATTERN:
17696 normVal = 1;
17697 has = 1;
17698 break;
17699 case XML_SCHEMA_FACET_ENUMERATION:
17700 needVal = 1;
17701 normVal = 1;
17702 has = 1;
17703 break;
17704 default:
17705 has = 1;
17706 break;
17707 }
17708 }
17709 }
17710 if (normVal)
17711 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17712 if (needVal)
17713 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17714 if (has)
17715 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17716
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017717 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017718 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17719 /*
17720 * OPTIMIZE VAL TODO: Some facets need a computed value.
17721 */
17722 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17723 (prim->builtInType != XML_SCHEMAS_STRING)) {
17724 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17725 }
17726 }
17727}
17728
17729static int
17730xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17731{
17732
17733
17734 /*
17735 * Evaluate the whitespace-facet value.
17736 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017737 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017738 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17739 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017740 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017741 return (0);
17742
17743 if (type->facetSet != NULL) {
17744 xmlSchemaFacetLinkPtr lin;
17745
17746 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17747 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17748 switch (lin->facet->whitespace) {
17749 case XML_SCHEMAS_FACET_PRESERVE:
17750 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17751 break;
17752 case XML_SCHEMAS_FACET_REPLACE:
17753 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17754 break;
17755 case XML_SCHEMAS_FACET_COLLAPSE:
17756 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17757 break;
17758 default:
17759 return (-1);
17760 }
17761 return (0);
17762 }
17763 }
17764 }
17765 /*
17766 * For all ·atomic· datatypes other than string (and types ·derived·
17767 * by ·restriction· from it) the value of whiteSpace is fixed to
17768 * collapse
17769 */
17770 {
17771 xmlSchemaTypePtr anc;
17772
17773 for (anc = type->baseType; anc != NULL &&
17774 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17775 anc = anc->baseType) {
17776
17777 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17778 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17779 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17780
17781 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17782 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17783 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17784
17785 } else
17786 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17787 break;
17788 }
17789 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017790 }
17791 return (0);
17792}
17793
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017794static int
17795xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17796 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017797{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017798 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17799 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017800 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017801 return(0);
17802 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017803
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017804 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017805 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017806 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017807 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017808 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017809 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017810 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017811 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017812 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017813 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017814 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017815 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017816 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017817 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017818 * Corresponds to <simpleType><union>...
17819 */
17820 if (type->memberTypes == NULL) {
17821 /*
17822 * This one is really needed, so get out.
17823 */
17824 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17825 "union type has no member-types assigned");
17826 return(-1);
17827 }
17828 } else {
17829 /*
17830 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017831 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017832 if (type->baseType == NULL) {
17833 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17834 "type has no base-type assigned");
17835 return(-1);
17836 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017837 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017838 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17839 return(-1);
17840 /*
17841 * Variety
17842 * If the <restriction> alternative is chosen, then the
17843 * {variety} of the {base type definition}.
17844 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017845 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017846 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017847 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017848 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017849 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017850 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017851 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017852 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017853 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017854 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017855 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017856 * NOTE that we won't assign the memberTypes of the base,
17857 * since this will make trouble when freeing them; we will
17858 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017859 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017860 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017861 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017862 return(0);
17863}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017864
Daniel Veillard8651f532002-04-17 09:06:27 +000017865#ifdef DEBUG_TYPE
Daniel Veillard67952602006-01-05 15:29:44 +000017866static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017867xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17868 xmlSchemaTypePtr type)
17869{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017870 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017871 xmlGenericError(xmlGenericErrorContext,
17872 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017873 type->node->doc->URL,
17874 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017875 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017876 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017877 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017878 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017879 switch (type->contentType) {
17880 case XML_SCHEMA_CONTENT_SIMPLE:
17881 xmlGenericError(xmlGenericErrorContext, "simple\n");
17882 break;
17883 case XML_SCHEMA_CONTENT_ELEMENTS:
17884 xmlGenericError(xmlGenericErrorContext, "elements\n");
17885 break;
17886 case XML_SCHEMA_CONTENT_UNKNOWN:
17887 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17888 break;
17889 case XML_SCHEMA_CONTENT_EMPTY:
17890 xmlGenericError(xmlGenericErrorContext, "empty\n");
17891 break;
17892 case XML_SCHEMA_CONTENT_MIXED:
17893 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017894 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017895 xmlGenericError(xmlGenericErrorContext,
17896 "mixed as emptiable particle\n");
17897 else
17898 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17899 break;
17900 /* Removed, since not used. */
17901 /*
17902 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17903 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17904 break;
17905 */
17906 case XML_SCHEMA_CONTENT_BASIC:
17907 xmlGenericError(xmlGenericErrorContext, "basic\n");
17908 break;
17909 default:
17910 xmlGenericError(xmlGenericErrorContext,
17911 "not registered !!!\n");
17912 break;
17913 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017914 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017915}
Daniel Veillard8651f532002-04-17 09:06:27 +000017916#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017917
17918/*
17919* 3.14.6 Constraints on Simple Type Definition Schema Components
17920*/
17921static int
17922xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17923 xmlSchemaTypePtr type)
17924{
17925 int res, olderrs = pctxt->nberrors;
17926
17927 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17928 return(-1);
17929
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017930 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017931 return(0);
17932
17933 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17934 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17935
17936 if (type->baseType == NULL) {
17937 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17938 "missing baseType");
17939 goto exit_failure;
17940 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017941 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017942 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017943 /*
17944 * If a member type of a union is a union itself, we need to substitute
17945 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017946 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17947 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017948 */
17949 if ((type->memberTypes != NULL) &&
17950 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17951 return(-1);
17952 /*
17953 * SPEC src-simple-type 1
17954 * "The corresponding simple type definition, if any, must satisfy
17955 * the conditions set out in Constraints on Simple Type Definition
17956 * Schema Components (§3.14.6)."
17957 */
17958 /*
17959 * Schema Component Constraint: Simple Type Definition Properties Correct
17960 * (st-props-correct)
17961 */
17962 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17963 HFAILURE HERROR
17964 /*
17965 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17966 * (cos-st-restricts)
17967 */
17968 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17969 HFAILURE HERROR
17970 /*
17971 * TODO: Removed the error report, since it got annoying to get an
17972 * extra error report, if anything failed until now.
17973 * Enable this if needed.
17974 *
17975 * xmlSchemaPErr(ctxt, type->node,
17976 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17977 * "Simple type '%s' does not satisfy the constraints "
17978 * "on simple type definitions.\n",
17979 * type->name, NULL);
17980 */
17981 /*
17982 * Schema Component Constraint: Simple Type Restriction (Facets)
17983 * (st-restrict-facets)
17984 */
17985 res = xmlSchemaCheckFacetValues(type, pctxt);
17986 HFAILURE HERROR
17987 if ((type->facetSet != NULL) ||
17988 (type->baseType->facetSet != NULL)) {
17989 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17990 HFAILURE HERROR
17991 }
17992 /*
17993 * Whitespace value.
17994 */
17995 res = xmlSchemaTypeFixupWhitespace(type);
17996 HFAILURE HERROR
17997 xmlSchemaTypeFixupOptimFacets(type);
17998
17999exit_error:
18000#ifdef DEBUG_TYPE
18001 xmlSchemaDebugFixedType(pctxt, type);
18002#endif
18003 if (olderrs != pctxt->nberrors)
18004 return(pctxt->err);
18005 return(0);
18006
18007exit_failure:
18008#ifdef DEBUG_TYPE
18009 xmlSchemaDebugFixedType(pctxt, type);
18010#endif
18011 return(-1);
18012}
18013
18014static int
18015xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18016 xmlSchemaTypePtr type)
18017{
18018 int res = 0, olderrs = pctxt->nberrors;
18019 xmlSchemaTypePtr baseType = type->baseType;
18020
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018021 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018022 return(0);
18023 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18024 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018025 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018026 "missing baseType");
18027 goto exit_failure;
18028 }
18029 /*
18030 * Fixup the base type.
18031 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018032 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018033 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018034 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18035 /*
18036 * Skip fixup if the base type is invalid.
18037 * TODO: Generate a warning!
18038 */
18039 return(0);
18040 }
18041 /*
18042 * This basically checks if the base type can be derived.
18043 */
18044 res = xmlSchemaCheckSRCCT(pctxt, type);
18045 HFAILURE HERROR
18046 /*
18047 * Fixup the content type.
18048 */
18049 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18050 /*
18051 * Corresponds to <complexType><simpleContent>...
18052 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018053 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018054 (baseType->contentTypeDef != NULL) &&
18055 (WXS_IS_RESTRICTION(type))) {
18056 xmlSchemaTypePtr contentBase, content;
18057#ifdef ENABLE_NAMED_LOCALS
18058 char buf[30];
18059 const xmlChar *tmpname;
18060#endif
18061 /*
18062 * SPEC (1) If <restriction> + base type is <complexType>,
18063 * "whose own {content type} is a simple type..."
18064 */
18065 if (type->contentTypeDef != NULL) {
18066 /*
18067 * SPEC (1.1) "the simple type definition corresponding to the
18068 * <simpleType> among the [children] of <restriction> if there
18069 * is one;"
18070 * Note that this "<simpleType> among the [children]" was put
18071 * into ->contentTypeDef during parsing.
18072 */
18073 contentBase = type->contentTypeDef;
18074 type->contentTypeDef = NULL;
18075 } else {
18076 /*
18077 * (1.2) "...otherwise (<restriction> has no <simpleType>
18078 * among its [children]), the simple type definition which
18079 * is the {content type} of the ... base type."
18080 */
18081 contentBase = baseType->contentTypeDef;
18082 }
18083 /*
18084 * SPEC
18085 * "... a simple type definition which restricts the simple
18086 * type definition identified in clause 1.1 or clause 1.2
18087 * with a set of facet components"
18088 *
18089 * Create the anonymous simple type, which will be the content
18090 * type of the complex type.
18091 */
18092#ifdef ENABLE_NAMED_LOCALS
18093 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18094 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018095 content = xmlSchemaAddType(pctxt, pctxt->schema,
18096 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018097 type->node, 0);
18098#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018099 content = xmlSchemaAddType(pctxt, pctxt->schema,
18100 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018101 type->node, 0);
18102#endif
18103 if (content == NULL)
18104 goto exit_failure;
18105 /*
18106 * We will use the same node as for the <complexType>
18107 * to have it somehow anchored in the schema doc.
18108 */
18109 content->type = XML_SCHEMA_TYPE_SIMPLE;
18110 content->baseType = contentBase;
18111 /*
18112 * Move the facets, previously anchored on the
18113 * complexType during parsing.
18114 */
18115 content->facets = type->facets;
18116 type->facets = NULL;
18117 content->facetSet = type->facetSet;
18118 type->facetSet = NULL;
18119
18120 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018121 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018122 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018123 /*
18124 * Fixup the newly created type. We don't need to check
18125 * for circularity here.
18126 */
18127 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18128 HFAILURE HERROR
18129 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18130 HFAILURE HERROR
18131
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018132 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018133 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18134 (WXS_IS_RESTRICTION(type))) {
18135 /*
18136 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18137 * an emptiable particle, then a simple type definition which
18138 * restricts the <restriction>'s <simpleType> child.
18139 */
18140 if ((type->contentTypeDef == NULL) ||
18141 (type->contentTypeDef->baseType == NULL)) {
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': the <simpleContent><restriction> "
18150 "is missing a <simpleType> child, but was not catched "
18151 "by xmlSchemaCheckSRCCT()", type->name);
18152 goto exit_failure;
18153 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018154 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018155 /*
18156 * SPEC (3) If <extension> + base is <complexType> with
18157 * <simpleType> content, "...then the {content type} of that
18158 * complex type definition"
18159 */
18160 if (baseType->contentTypeDef == NULL) {
18161 /*
18162 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18163 * should have catched this already.
18164 */
18165 xmlSchemaPCustomErr(pctxt,
18166 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018167 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018168 "Internal error: xmlSchemaTypeFixup, "
18169 "complex type '%s': the <extension>ed base type is "
18170 "a complex type with no simple content type",
18171 type->name);
18172 goto exit_failure;
18173 }
18174 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018175 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018176 /*
18177 * SPEC (4) <extension> + base is <simpleType>
18178 * "... then that simple type definition"
18179 */
18180 type->contentTypeDef = baseType;
18181 } else {
18182 /*
18183 * TODO: Check if this ever happens.
18184 */
18185 xmlSchemaPCustomErr(pctxt,
18186 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018187 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018188 "Internal error: xmlSchemaTypeFixup, "
18189 "complex type '%s' with <simpleContent>: unhandled "
18190 "derivation case", type->name);
18191 goto exit_failure;
18192 }
18193 } else {
18194 int dummySequence = 0;
18195 xmlSchemaParticlePtr particle =
18196 (xmlSchemaParticlePtr) type->subtypes;
18197 /*
18198 * Corresponds to <complexType><complexContent>...
18199 *
18200 * NOTE that the effective mixed was already set during parsing of
18201 * <complexType> and <complexContent>; its flag value is
18202 * XML_SCHEMAS_TYPE_MIXED.
18203 *
18204 * Compute the "effective content":
18205 * (2.1.1) + (2.1.2) + (2.1.3)
18206 */
18207 if ((particle == NULL) ||
18208 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18209 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18210 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18211 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18212 (particle->minOccurs == 0))) &&
18213 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18214 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18215 /*
18216 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18217 * a particle whose properties are as follows:..."
18218 *
18219 * Empty sequence model group with
18220 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18221 * NOTE that we sill assign it the <complexType> node to
18222 * somehow anchor it in the doc.
18223 */
18224 if ((particle == NULL) ||
18225 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18226 /*
18227 * Create the particle.
18228 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018229 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018230 type->node, 1, 1);
18231 if (particle == NULL)
18232 goto exit_failure;
18233 /*
18234 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018235 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018236 particle->children = (xmlSchemaTreeItemPtr)
18237 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18238 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18239 if (particle->children == NULL)
18240 goto exit_failure;
18241
18242 type->subtypes = (xmlSchemaTypePtr) particle;
18243 }
18244 dummySequence = 1;
18245 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18246 } else {
18247 /*
18248 * SPEC (2.1.5) "otherwise empty"
18249 */
18250 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18251 }
18252 } else {
18253 /*
18254 * SPEC (2.2) "otherwise the particle corresponding to the
18255 * <all>, <choice>, <group> or <sequence> among the
18256 * [children]."
18257 */
18258 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18259 }
18260 /*
18261 * Compute the "content type".
18262 */
18263 if (WXS_IS_RESTRICTION(type)) {
18264 /*
18265 * SPEC (3.1) "If <restriction>..."
18266 * (3.1.1) + (3.1.2) */
18267 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18268 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18269 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18270 }
18271 } else {
18272 /*
18273 * SPEC (3.2) "If <extension>..."
18274 */
18275 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18276 /*
18277 * SPEC (3.2.1)
18278 */
18279 type->contentType = baseType->contentType;
18280 type->subtypes = baseType->subtypes;
18281 /*
18282 * NOTE that the effective mixed is ignored here.
18283 */
18284 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18285 /*
18286 * SPEC (3.2.2)
18287 */
18288 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18289 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18290 } else {
18291 /*
18292 * SPEC (3.2.3)
18293 */
18294 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18295 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18296 /*
18297 * "A model group whose {compositor} is sequence and whose
18298 * {particles} are..."
18299 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018300 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18301 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18302 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18303 XML_SCHEMA_TYPE_ALL))
18304 {
18305 /*
18306 * SPEC cos-all-limited (1)
18307 */
18308 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18309 /* TODO: error code */
18310 XML_SCHEMAP_COS_ALL_LIMITED,
18311 WXS_ITEM_NODE(type), NULL,
18312 "The type has an 'all' model group in its "
18313 "{content type} and thus cannot be derived from "
18314 "a non-empty type, since this would produce a "
18315 "'sequence' model group containing the 'all' "
18316 "model group; 'all' model groups are not "
18317 "allowed to appear inside other model groups",
18318 NULL, NULL);
18319
18320 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18321 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18322 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18323 XML_SCHEMA_TYPE_ALL))
18324 {
18325 /*
18326 * SPEC cos-all-limited (1)
18327 */
18328 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18329 /* TODO: error code */
18330 XML_SCHEMAP_COS_ALL_LIMITED,
18331 WXS_ITEM_NODE(type), NULL,
18332 "A type cannot be derived by extension from a type "
18333 "which has an 'all' model group in its "
18334 "{content type}, since this would produce a "
18335 "'sequence' model group containing the 'all' "
18336 "model group; 'all' model groups are not "
18337 "allowed to appear inside other model groups",
18338 NULL, NULL);
18339
18340 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018341 xmlSchemaTreeItemPtr effectiveContent =
18342 (xmlSchemaTreeItemPtr) type->subtypes;
18343 /*
18344 * Create the particle.
18345 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018346 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018347 type->node, 1, 1);
18348 if (particle == NULL)
18349 goto exit_failure;
18350 /*
18351 * Create the "sequence" model group.
18352 */
18353 particle->children = (xmlSchemaTreeItemPtr)
18354 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18355 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18356 if (particle->children == NULL)
18357 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018358 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018359 /*
18360 * SPEC "the particle of the {content type} of
18361 * the ... base ..."
18362 * Create a duplicate of the base type's particle
18363 * and assign its "term" to it.
18364 */
18365 particle->children->children =
18366 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018367 type->node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018368 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18369 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18370 if (particle->children->children == NULL)
18371 goto exit_failure;
18372 particle = (xmlSchemaParticlePtr)
18373 particle->children->children;
18374 particle->children =
18375 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18376 /*
18377 * SPEC "followed by the ·effective content·."
18378 */
18379 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018380 /*
18381 * This all will result in:
18382 * new-particle
18383 * --> new-sequence(
18384 * new-particle
18385 * --> base-model,
18386 * this-particle
18387 * --> this-model
18388 * )
18389 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018390 } else {
18391 /*
18392 * This is the case when there is already an empty
18393 * <sequence> with minOccurs==maxOccurs==1.
18394 * Just add the base types's content type.
18395 * NOTE that, although we miss to add an intermediate
18396 * <sequence>, this should produce no difference to
18397 * neither the regex compilation of the content model,
18398 * nor to the complex type contraints.
18399 */
18400 particle->children->children =
18401 (xmlSchemaTreeItemPtr) baseType->subtypes;
18402 }
18403 }
18404 }
18405 }
18406 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018407 * Now fixup attribute uses:
18408 * - expand attr. group references
18409 * - intersect attribute wildcards
18410 * - inherit attribute uses of the base type
18411 * - inherit or union attr. wildcards if extending
18412 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018413 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018414 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018415 HFAILURE HERROR
18416 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018417 * Apply the complex type component constraints; this will not
18418 * check attributes, since this is done in
18419 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018420 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018421 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018422 HFAILURE HERROR
18423
18424#ifdef DEBUG_TYPE
18425 xmlSchemaDebugFixedType(pctxt, type);
18426#endif
18427 if (olderrs != pctxt->nberrors)
18428 return(pctxt->err);
18429 else
18430 return(0);
18431
18432exit_error:
18433 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18434#ifdef DEBUG_TYPE
18435 xmlSchemaDebugFixedType(pctxt, type);
18436#endif
18437 return(pctxt->err);
18438
18439exit_failure:
18440 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18441#ifdef DEBUG_TYPE
18442 xmlSchemaDebugFixedType(pctxt, type);
18443#endif
18444 return(-1);
18445}
18446
18447
18448/**
18449 * xmlSchemaTypeFixup:
18450 * @typeDecl: the schema type definition
18451 * @ctxt: the schema parser context
18452 *
18453 * Fixes the content model of the type.
18454 * URGENT TODO: We need an int result!
18455 */
18456static int
18457xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018458 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018459{
18460 if (type == NULL)
18461 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018462 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18463 AERROR_INT("xmlSchemaTypeFixup",
18464 "this function needs a parser context");
18465 return(-1);
18466 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018467 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018468 return(0);
18469 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018470 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018471 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018472 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018473 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018474}
18475
18476/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018477 * xmlSchemaCheckFacet:
18478 * @facet: the facet
18479 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018480 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018481 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018482 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018483 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018484 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018485 * Returns 0 if valid, a positive error code if not valid and
18486 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018487 */
18488int
18489xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018490 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018491 xmlSchemaParserCtxtPtr pctxt,
18492 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018493{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018494 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018495
Daniel Veillardce682bc2004-11-05 17:22:25 +000018496 if ((facet == NULL) || (typeDecl == NULL))
18497 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018498 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018499 * TODO: will the parser context be given if used from
18500 * the relaxNG module?
18501 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018502 if (pctxt == NULL)
18503 ctxtGiven = 0;
18504 else
18505 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018506
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018507 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018508 case XML_SCHEMA_FACET_MININCLUSIVE:
18509 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18510 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018511 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18512 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018513 /*
18514 * Okay we need to validate the value
18515 * at that point.
18516 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018517 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018518
18519 /* 4.3.5.5 Constraints on enumeration Schema Components
18520 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018521 * It is an ·error· if any member of {value} is not in the
18522 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018523 *
18524 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018525 * The value ·must· be in the
18526 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018527 */
18528 /*
18529 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018530 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018531 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018532 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018533 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018534 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018536 */
18537 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18538 base = typeDecl->baseType;
18539 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018540 PERROR_INT("xmlSchemaCheckFacet",
18541 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018542 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018543 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018544 } else
18545 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018546
18547 if (! ctxtGiven) {
18548 /*
18549 * A context is needed if called from RelaxNG.
18550 */
18551 pctxt = xmlSchemaNewParserCtxt("*");
18552 if (pctxt == NULL)
18553 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018554 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018555 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018556 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018557 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018558 * facet->node is just the node holding the facet
18559 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018560 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018561 */
18562 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018563 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018564 facet->value, &(facet->val), 1, 1, 0);
18565 if (ret != 0) {
18566 if (ret < 0) {
18567 /* No error message for RelaxNG. */
18568 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018569 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018570 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18571 "Internal error: xmlSchemaCheckFacet, "
18572 "failed to validate the value '%s' of the "
18573 "facet '%s' against the base type",
18574 facet->value, xmlSchemaFacetTypeToString(facet->type));
18575 }
18576 goto internal_error;
18577 }
18578 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18579 /* No error message for RelaxNG. */
18580 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018581 xmlChar *str = NULL;
18582
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018583 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018584 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018585 "The value '%s' of the facet does not validate "
18586 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018587 facet->value,
18588 xmlSchemaFormatQName(&str,
18589 base->targetNamespace, base->name));
18590 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018591 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018592 goto exit;
18593 } else if (facet->val == NULL) {
18594 if (ctxtGiven) {
18595 PERROR_INT("xmlSchemaCheckFacet",
18596 "value was not computed");
18597 }
18598 TODO
18599 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018600 break;
18601 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018602 case XML_SCHEMA_FACET_PATTERN:
18603 facet->regexp = xmlRegexpCompile(facet->value);
18604 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018605 ret = XML_SCHEMAP_REGEXP_INVALID;
18606 /* No error message for RelaxNG. */
18607 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018608 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018609 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018610 "The value '%s' of the facet 'pattern' is not a "
18611 "valid regular expression",
18612 facet->value, NULL);
18613 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018614 }
18615 break;
18616 case XML_SCHEMA_FACET_TOTALDIGITS:
18617 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18618 case XML_SCHEMA_FACET_LENGTH:
18619 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018620 case XML_SCHEMA_FACET_MINLENGTH:
18621
18622 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18623 ret = xmlSchemaValidatePredefinedType(
18624 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18625 facet->value, &(facet->val));
18626 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018627 ret = xmlSchemaValidatePredefinedType(
18628 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18629 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018630 }
18631 if (ret != 0) {
18632 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018633 /* No error message for RelaxNG. */
18634 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018635 PERROR_INT("xmlSchemaCheckFacet",
18636 "validating facet value");
18637 }
18638 goto internal_error;
18639 }
18640 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18641 /* No error message for RelaxNG. */
18642 if (ctxtGiven) {
18643 /* error code */
18644 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18645 ret, facet->node, WXS_BASIC_CAST typeDecl,
18646 "The value '%s' of the facet '%s' is not a valid '%s'",
18647 facet->value,
18648 xmlSchemaFacetTypeToString(facet->type),
18649 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
18650 BAD_CAST "nonNegativeInteger" :
18651 BAD_CAST "positiveInteger",
18652 NULL);
18653 }
18654 }
18655 break;
18656
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018657 case XML_SCHEMA_FACET_WHITESPACE:{
18658 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18659 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18660 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18661 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18662 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18663 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18664 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018665 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18666 /* No error message for RelaxNG. */
18667 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018668 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018669 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018670 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018671 "The value '%s' of the facet 'whitespace' is not "
18672 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018673 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018674 }
18675 }
18676 default:
18677 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018678 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018679exit:
18680 if ((! ctxtGiven) && (pctxt != NULL))
18681 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018682 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018683internal_error:
18684 if ((! ctxtGiven) && (pctxt != NULL))
18685 xmlSchemaFreeParserCtxt(pctxt);
18686 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018687}
18688
18689/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018690 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018691 * @typeDecl: the schema type definition
18692 * @ctxt: the schema parser context
18693 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018694 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018695 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018696static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018697xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018698 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018699{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018700 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018701 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018702 /*
18703 * NOTE: It is intended to use the facets list, instead
18704 * of facetSet.
18705 */
18706 if (typeDecl->facets != NULL) {
18707 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018708
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018709 /*
18710 * Temporarily assign the "schema" to the validation context
18711 * of the parser context. This is needed for NOTATION validation.
18712 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018713 if (pctxt->vctxt == NULL) {
18714 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18715 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018716 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018717 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018718 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018719 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18720 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018721 facet = facet->next;
18722 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018723 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018724 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018725 if (olderrs != pctxt->nberrors)
18726 return(pctxt->err);
18727 return(0);
18728exit_failure:
18729 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018730}
18731
18732/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018733 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018734 * @ctxtMGroup: the searched model group
18735 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018736 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018737 *
18738 * This one is intended to be used by
18739 * xmlSchemaCheckGroupDefCircular only.
18740 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018741 * Returns the particle with the circular model group definition reference,
18742 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018743 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018744static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018745xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018746 xmlSchemaTreeItemPtr particle)
18747{
18748 xmlSchemaTreeItemPtr circ = NULL;
18749 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018750 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018751
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018752 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018753 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018754 if (term == NULL)
18755 continue;
18756 switch (term->type) {
18757 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018758 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018759 if (gdef == groupDef)
18760 return (particle);
18761 /*
18762 * Mark this model group definition to avoid infinite
18763 * recursion on circular references not yet examined.
18764 */
18765 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18766 continue;
18767 if (gdef->children != NULL) {
18768 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18769 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18770 gdef->children->children);
18771 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18772 if (circ != NULL)
18773 return (circ);
18774 }
18775 break;
18776 case XML_SCHEMA_TYPE_SEQUENCE:
18777 case XML_SCHEMA_TYPE_CHOICE:
18778 case XML_SCHEMA_TYPE_ALL:
18779 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18780 if (circ != NULL)
18781 return (circ);
18782 break;
18783 default:
18784 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018785 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018786 }
18787 return (NULL);
18788}
18789
18790/**
18791 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018792 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018793 * @ctxt: the parser context
18794 * @name: the name
18795 *
18796 * Checks for circular references to model group definitions.
18797 */
18798static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018799xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018800 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018801{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018802 /*
18803 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018804 * 2 Circular groups are disallowed. That is, within the {particles}
18805 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018806 * is the group itself.
18807 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018808 if ((item == NULL) ||
18809 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18810 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018811 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018812 {
18813 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018814
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018815 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018816 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018817 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018818 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018819 * TODO: The error report is not adequate: this constraint
18820 * is defined for model groups but not definitions, but since
18821 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018822 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018823 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018824 */
18825 xmlSchemaPCustomErr(ctxt,
18826 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018827 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018828 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018829 "defined", xmlSchemaFormatQName(&str,
18830 item->targetNamespace, item->name));
18831 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018832 /*
18833 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018834 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018835 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018836 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018837 }
18838 }
18839}
18840
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018841/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018842 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018843 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018844 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018845 *
18846 * Assigns the model group of model group definitions to the "term"
18847 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018848 * In xmlSchemaResolveModelGroupParticleReferences the model group
18849 * definitions were assigned to the "term", since needed for the
18850 * circularity check.
18851 *
18852 * Schema Component Constraint:
18853 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018854 */
18855static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018856xmlSchemaModelGroupToModelGroupDefFixup(
18857 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18858 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018859{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018860 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18861
18862 while (particle != NULL) {
18863 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18864 ((WXS_PARTICLE_TERM(particle))->type !=
18865 XML_SCHEMA_TYPE_GROUP))
18866 {
18867 particle = WXS_PTC_CAST particle->next;
18868 continue;
18869 }
18870 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18871 /*
18872 * TODO: Remove the particle.
18873 */
18874 WXS_PARTICLE_TERM(particle) = NULL;
18875 particle = WXS_PTC_CAST particle->next;
18876 continue;
18877 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018878 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018879 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018880 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018881 WXS_PARTICLE_TERM(particle) =
18882 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18883
18884 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018885 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018886}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018887
18888/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018889 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018890 * @ctxtGr: the searched attribute group
18891 * @attr: the current attribute list to be processed
18892 *
18893 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018894 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018895 *
18896 * Returns the circular attribute grou reference, otherwise NULL.
18897 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018898static xmlSchemaQNameRefPtr
18899xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18900 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018901{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018902 xmlSchemaAttributeGroupPtr gr;
18903 xmlSchemaQNameRefPtr ref, circ;
18904 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018905 /*
18906 * We will search for an attribute group reference which
18907 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018908 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018909 for (i = 0; i < list->nbItems; i++) {
18910 ref = list->items[i];
18911 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18912 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18913 (ref->item != NULL))
18914 {
18915 gr = WXS_ATTR_GROUP_CAST ref->item;
18916 if (gr == ctxtGr)
18917 return(ref);
18918 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18919 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018920 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018921 * Mark as visited to avoid infinite recursion on
18922 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018923 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018924 if ((gr->attrUses) &&
18925 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18926 {
18927 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18928 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18929 (xmlSchemaItemListPtr) gr->attrUses);
18930 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18931 if (circ != NULL)
18932 return (circ);
18933 }
18934
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018935 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018936 }
18937 return (NULL);
18938}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018939
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018940/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018941 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018942 * attrGr: the attribute group definition
18943 * @ctxt: the parser context
18944 * @name: the name
18945 *
18946 * Checks for circular references of attribute groups.
18947 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018948static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018949xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018950 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018951{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018952 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018953 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018954 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018955 * 3 Circular group reference is disallowed outside <redefine>.
18956 * That is, unless this element information item's parent is
18957 * <redefine>, then among the [children], if any, there must
18958 * not be an <attributeGroup> with ref [attribute] which resolves
18959 * to the component corresponding to this <attributeGroup>. Indirect
18960 * circularity is also ruled out. That is, when QName resolution
18961 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18962 * any <attributeGroup>s with a ref [attribute] among the [children],
18963 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018964 * which resolves to the component corresponding to this <attributeGroup>.
18965 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018966 if (attrGr->attrUses == NULL)
18967 return(0);
18968 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18969 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018970 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018971 xmlSchemaQNameRefPtr circ;
18972
18973 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18974 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018975 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018976 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018977 /*
18978 * TODO: Report the referenced attr group as QName.
18979 */
18980 xmlSchemaPCustomErr(ctxt,
18981 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018982 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018983 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018984 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18985 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018986 /*
18987 * NOTE: We will cut the reference to avoid further
18988 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018989 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018990 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018991 circ->item = NULL;
18992 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018993 }
18994 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018995 return(0);
18996}
18997
18998static int
18999xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19000 xmlSchemaAttributeGroupPtr attrGr);
19001
19002/**
19003 * xmlSchemaExpandAttributeGroupRefs:
19004 * @pctxt: the parser context
19005 * @node: the node of the component holding the attribute uses
19006 * @completeWild: the intersected wildcard to be returned
19007 * @list: the attribute uses
19008 *
19009 * Substitutes contained attribute group references
19010 * for their attribute uses. Wilcards are intersected.
19011 * Attribute use prohibitions are removed from the list
19012 * and returned via the @prohibs list.
19013 * Pointlessness of attr. prohibs, if a matching attr. decl
19014 * is existent a well, are checked.
19015 */
19016static int
19017xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19018 xmlSchemaBasicItemPtr item,
19019 xmlSchemaWildcardPtr *completeWild,
19020 xmlSchemaItemListPtr list,
19021 xmlSchemaItemListPtr prohibs)
19022{
19023 xmlSchemaAttributeGroupPtr gr;
19024 xmlSchemaAttributeUsePtr use;
19025 xmlSchemaItemListPtr sublist;
19026 int i, j;
19027 int created = (*completeWild == NULL) ? 0 : 1;
19028
19029 if (prohibs)
19030 prohibs->nbItems = 0;
19031
19032 for (i = 0; i < list->nbItems; i++) {
19033 use = list->items[i];
19034
19035 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
19036 if (prohibs == NULL) {
19037 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19038 "unexpected attr prohibition found");
19039 return(-1);
19040 }
19041 /*
19042 * Remove from attribute uses.
19043 */
19044 if (xmlSchemaItemListRemove(list, i) == -1)
19045 return(-1);
19046 i--;
19047 /*
19048 * Note that duplicate prohibitions were already
19049 * handled at parsing time.
19050 */
19051 /*
19052 * Add to list of prohibitions.
19053 */
19054 xmlSchemaItemListAddSize(prohibs, 2, use);
19055 continue;
19056 }
19057 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19058 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19059 {
19060 if ((WXS_QNAME_CAST use)->item == NULL)
19061 return(-1);
19062 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19063 /*
19064 * Expand the referenced attr. group.
19065 * TODO: remove this, this is done in a previous step, so
19066 * already done here.
19067 */
19068 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19069 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19070 return(-1);
19071 }
19072 /*
19073 * Build the 'complete' wildcard; i.e. intersect multiple
19074 * wildcards.
19075 */
19076 if (gr->attributeWildcard != NULL) {
19077 if (*completeWild == NULL) {
19078 *completeWild = gr->attributeWildcard;
19079 } else {
19080 if (! created) {
19081 xmlSchemaWildcardPtr tmpWild;
19082
19083 /*
19084 * Copy the first encountered wildcard as context,
19085 * except for the annotation.
19086 *
19087 * Although the complete wildcard might not correspond
19088 * to any node in the schema, we will anchor it on
19089 * the node of the owner component.
19090 */
19091 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19092 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19093 WXS_ITEM_NODE(item));
19094 if (tmpWild == NULL)
19095 return(-1);
19096 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19097 tmpWild, *completeWild) == -1)
19098 return (-1);
19099 tmpWild->processContents = (*completeWild)->processContents;
19100 *completeWild = tmpWild;
19101 created = 1;
19102 }
19103
19104 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19105 gr->attributeWildcard) == -1)
19106 return(-1);
19107 }
19108 }
19109 /*
19110 * Just remove the reference if the referenced group does not
19111 * contain any attribute uses.
19112 */
19113 if (gr->attrUses == NULL) {
19114 if (xmlSchemaItemListRemove(list, i) == -1)
19115 return(-1);
19116 i--;
19117 continue;
19118 }
19119 /*
19120 * Add the attribute uses.
19121 */
19122 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19123 if (sublist->nbItems != 0) {
19124 list->items[i] = sublist->items[0];
19125 if (sublist->nbItems != 1) {
19126 for (j = 1; j < sublist->nbItems; j++) {
19127 i++;
19128 if (xmlSchemaItemListInsert(list,
19129 sublist->items[j], i) == -1)
19130 return(-1);
19131 }
19132 }
19133 }
19134 }
19135
19136 }
19137 /*
19138 * Handle pointless prohibitions of declared attributes.
19139 */
19140 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19141 xmlSchemaAttributeUseProhibPtr prohib;
19142
19143 for (i = prohibs->nbItems -1; i >= 0; i--) {
19144 prohib = prohibs->items[i];
19145 for (j = 0; j < list->nbItems; j++) {
19146 use = list->items[j];
19147
19148 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19149 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19150 {
19151 xmlChar *str = NULL;
19152
19153 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19154 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19155 prohib->node, NULL,
19156 "Skipping pointless attribute use prohibition "
19157 "'%s', since a corresponding attribute use "
19158 "exists already in the type definition",
19159 xmlSchemaFormatQName(&str,
19160 prohib->targetNamespace, prohib->name),
19161 NULL, NULL);
19162 FREE_AND_NULL(str);
19163 /*
19164 * Remove the prohibition.
19165 */
19166 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19167 return(-1);
19168 break;
19169 }
19170 }
19171 }
19172 }
19173 return(0);
19174}
19175
19176/**
19177 * xmlSchemaAttributeGroupExpandRefs:
19178 * @pctxt: the parser context
19179 * @attrGr: the attribute group definition
19180 *
19181 * Computation of:
19182 * {attribute uses} property
19183 * {attribute wildcard} property
19184 *
19185 * Substitutes contained attribute group references
19186 * for their attribute uses. Wilcards are intersected.
19187 */
19188static int
19189xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19190 xmlSchemaAttributeGroupPtr attrGr)
19191{
19192 if ((attrGr->attrUses == NULL) ||
19193 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19194 return(0);
19195
19196 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19197 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19198 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19199 return(-1);
19200 return(0);
19201}
19202
19203/**
19204 * xmlSchemaAttributeGroupExpandRefs:
19205 * @pctxt: the parser context
19206 * @attrGr: the attribute group definition
19207 *
19208 * Substitutes contained attribute group references
19209 * for their attribute uses. Wilcards are intersected.
19210 *
19211 * Schema Component Constraint:
19212 * Attribute Group Definition Properties Correct (ag-props-correct)
19213 */
19214static int
19215xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19216 xmlSchemaAttributeGroupPtr attrGr)
19217{
19218 /*
19219 * SPEC ag-props-correct
19220 * (1) "The values of the properties of an attribute group definition
19221 * must be as described in the property tableau in The Attribute
19222 * Group Definition Schema Component (§3.6.1), modulo the impact of
19223 * Missing Sub-components (§5.3);"
19224 */
19225
19226 if ((attrGr->attrUses != NULL) &&
19227 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19228 {
19229 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19230 xmlSchemaAttributeUsePtr use, tmp;
19231 int i, j, hasId = 0;
19232
19233 for (i = uses->nbItems -1; i >= 0; i--) {
19234 use = uses->items[i];
19235 /*
19236 * SPEC ag-props-correct
19237 * (2) "Two distinct members of the {attribute uses} must not have
19238 * {attribute declaration}s both of whose {name}s match and whose
19239 * {target namespace}s are identical."
19240 */
19241 if (i > 0) {
19242 for (j = i -1; j >= 0; j--) {
19243 tmp = uses->items[j];
19244 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19245 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19246 (WXS_ATTRUSE_DECL_TNS(use) ==
19247 WXS_ATTRUSE_DECL_TNS(tmp)))
19248 {
19249 xmlChar *str = NULL;
19250
19251 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19252 XML_SCHEMAP_AG_PROPS_CORRECT,
19253 attrGr->node, WXS_BASIC_CAST attrGr,
19254 "Duplicate %s",
19255 xmlSchemaGetComponentDesignation(&str, use),
19256 NULL);
19257 FREE_AND_NULL(str);
19258 /*
19259 * Remove the duplicate.
19260 */
19261 if (xmlSchemaItemListRemove(uses, i) == -1)
19262 return(-1);
19263 goto next_use;
19264 }
19265 }
19266 }
19267 /*
19268 * SPEC ag-props-correct
19269 * (3) "Two distinct members of the {attribute uses} must not have
19270 * {attribute declaration}s both of whose {type definition}s are or
19271 * are derived from ID."
19272 * TODO: Does 'derived' include member-types of unions?
19273 */
19274 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19275 if (xmlSchemaIsDerivedFromBuiltInType(
19276 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19277 {
19278 if (hasId) {
19279 xmlChar *str = NULL;
19280
19281 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19282 XML_SCHEMAP_AG_PROPS_CORRECT,
19283 attrGr->node, WXS_BASIC_CAST attrGr,
19284 "There must not exist more than one attribute "
19285 "declaration of type 'xs:ID' "
19286 "(or derived from 'xs:ID'). The %s violates this "
19287 "constraint",
19288 xmlSchemaGetComponentDesignation(&str, use),
19289 NULL);
19290 FREE_AND_NULL(str);
19291 if (xmlSchemaItemListRemove(uses, i) == -1)
19292 return(-1);
19293 }
19294 hasId = 1;
19295 }
19296 }
19297next_use: {}
19298 }
19299 }
19300 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019301}
19302
19303/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019304 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019305 * @attrgrpDecl: the schema attribute definition
19306 * @ctxt: the schema parser context
19307 * @name: the attribute name
19308 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019309 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019310 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019311static int
19312xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19313 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019314{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019315 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019316
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019317 if (ref->item != NULL)
19318 return(0);
19319 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19320 ref->name,
19321 ref->targetNamespace);
19322 if (group == NULL) {
19323 xmlSchemaPResCompAttrErr(ctxt,
19324 XML_SCHEMAP_SRC_RESOLVE,
19325 NULL, ref->node,
19326 "ref", ref->name, ref->targetNamespace,
19327 ref->itemType, NULL);
19328 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019329 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019330 ref->item = WXS_BASIC_CAST group;
19331 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019332}
19333
19334/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019335 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019336 * @item: an schema attribute declaration/use
19337 * @ctxt: a schema parser context
19338 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019339 *
19340 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019341 * Schema Component Constraint:
19342 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019343 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 * Validates the value constraints of an attribute declaration/use.
19345 * NOTE that this needs the simle type definitions to be already
19346 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019347 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019348static int
19349xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19350 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019351{
19352
19353 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019354 * SPEC a-props-correct (1)
19355 * "The values of the properties of an attribute declaration must
19356 * be as described in the property tableau in The Attribute
19357 * Declaration Schema Component (§3.2.1), modulo the impact of
19358 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019359 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019360
19361 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19362 return(0);
19363
19364 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019365 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019366
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019367 /*
19368 * SPEC a-props-correct (3)
19369 * "If the {type definition} is or is derived from ID then there
19370 * must not be a {value constraint}."
19371 */
19372 if (xmlSchemaIsDerivedFromBuiltInType(
19373 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19374 {
19375 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19376 XML_SCHEMAP_A_PROPS_CORRECT_3,
19377 NULL, WXS_BASIC_CAST attr,
19378 "Value constraints are not allowed if the type definition "
19379 "is or is derived from xs:ID",
19380 NULL, NULL);
19381 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019382 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019383 /*
19384 * SPEC a-props-correct (2)
19385 * "if there is a {value constraint}, the canonical lexical
19386 * representation of its value must be ·valid· with respect
19387 * to the {type definition} as defined in String Valid (§3.14.4)."
19388 * TODO: Don't care about the *cononical* stuff here, this requirement
19389 * will be removed in WXS 1.1 anyway.
19390 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019391 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019392 attr->node, WXS_ATTR_TYPEDEF(attr),
19393 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019394 1, 1, 0);
19395 if (ret != 0) {
19396 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019397 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019398 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019399 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019400 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019401 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019402 XML_SCHEMAP_A_PROPS_CORRECT_2,
19403 NULL, WXS_BASIC_CAST attr,
19404 "The value of the value constraint is not valid",
19405 NULL, NULL);
19406 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019407 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019408 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019409
19410 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019411}
19412
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019413static xmlSchemaElementPtr
19414xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19415 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019416{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019417 xmlSchemaElementPtr ret;
19418
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019419 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019420 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019421 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019422 return (ancestor);
19423
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019424 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019425 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019426 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019427 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019428 WXS_SUBST_HEAD(ancestor));
19429 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019430
19431 return (ret);
19432}
19433
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019434/**
19435 * xmlSchemaCheckElemPropsCorrect:
19436 * @ctxt: a schema parser context
19437 * @decl: the element declaration
19438 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019439 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019440 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019441 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019442 *
19443 * STATUS:
19444 * missing: (6)
19445 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019446static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019447xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19448 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019449{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019450 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019451 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019452 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019453 * SPEC (1) "The values of the properties of an element declaration
19454 * must be as described in the property tableau in The Element
19455 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19456 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019457 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019458 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19459 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019460
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019461 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019462 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019463 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019464 * affiliation}, then {scope} must be global."
19465 */
19466 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19467 xmlSchemaPCustomErr(pctxt,
19468 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019469 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019470 "Only global element declarations can have a "
19471 "substitution group affiliation", NULL);
19472 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019473 }
19474 /*
19475 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19476 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019477 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019478 * property."
19479 */
19480 if (head == elemDecl)
19481 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019482 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019483 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19484 else
19485 circ = NULL;
19486 if (circ != NULL) {
19487 xmlChar *strA = NULL, *strB = NULL;
19488
19489 xmlSchemaPCustomErrExt(pctxt,
19490 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019491 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019492 "The element declaration '%s' defines a circular "
19493 "substitution group to element declaration '%s'",
19494 xmlSchemaGetComponentQName(&strA, circ),
19495 xmlSchemaGetComponentQName(&strB, head),
19496 NULL);
19497 FREE_AND_NULL(strA)
19498 FREE_AND_NULL(strB)
19499 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19500 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019501 /*
19502 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019503 * the {type definition}
19504 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019505 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019506 * of the {substitution group exclusions} of the {substitution group
19507 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19508 * (if the {type definition} is complex) or as defined in
19509 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019510 * simple)."
19511 *
19512 * NOTE: {substitution group exclusions} means the values of the
19513 * attribute "final".
19514 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019515
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019516 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019517 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019518
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019519 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19520 set |= SUBSET_EXTENSION;
19521 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19522 set |= SUBSET_RESTRICTION;
19523
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019524 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019525 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019526 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19527
19528 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019529 xmlSchemaPCustomErrExt(pctxt,
19530 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019531 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019532 "The type definition '%s' was "
19533 "either rejected by the substitution group "
19534 "affiliation '%s', or not validly derived from its type "
19535 "definition '%s'",
19536 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019537 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019538 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019539 FREE_AND_NULL(strA)
19540 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019541 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019542 }
19543 }
19544 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019545 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019546 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019547 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019548 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019549 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019550 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019551 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019552 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019553 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019554 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019555 (WXS_IS_COMPLEX(typeDef) &&
19556 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019557 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19558 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019559
19560 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19561 xmlSchemaPCustomErr(pctxt,
19562 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019563 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019564 "The type definition (or type definition's content type) is or "
19565 "is derived from ID; value constraints are not allowed in "
19566 "conjunction with such a type definition", NULL);
19567 } else if (elemDecl->value != NULL) {
19568 int vcret;
19569 xmlNodePtr node = NULL;
19570
19571 /*
19572 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19573 * representation of its value must be ·valid· with respect to the
19574 * {type definition} as defined in Element Default Valid (Immediate)
19575 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019576 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019577 if (typeDef == NULL) {
19578 xmlSchemaPErr(pctxt, elemDecl->node,
19579 XML_SCHEMAP_INTERNAL,
19580 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19581 "type is missing... skipping validation of "
19582 "the value constraint", NULL, NULL);
19583 return (-1);
19584 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019585 if (elemDecl->node != NULL) {
19586 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19587 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19588 BAD_CAST "fixed");
19589 else
19590 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19591 BAD_CAST "default");
19592 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019593 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19594 typeDef, elemDecl->value, &(elemDecl->defVal));
19595 if (vcret != 0) {
19596 if (vcret < 0) {
19597 PERROR_INT("xmlSchemaElemCheckValConstr",
19598 "failed to validate the value constraint of an "
19599 "element declaration");
19600 return (-1);
19601 }
19602 return (vcret);
19603 }
19604 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019605
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019606 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019607}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019608
19609/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019610 * xmlSchemaCheckElemSubstGroup:
19611 * @ctxt: a schema parser context
19612 * @decl: the element declaration
19613 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019614 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019615 * Schema Component Constraint:
19616 * Substitution Group (cos-equiv-class)
19617 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019618 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019619 * a list will be built for each subst. group head, holding all direct
19620 * referents to this head.
19621 * NOTE that this function needs:
19622 * 1. circular subst. groups to be checked beforehand
19623 * 2. the declaration's type to be derived from the head's type
19624 *
19625 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019626 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019627 */
19628static void
19629xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19630 xmlSchemaElementPtr elemDecl)
19631{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019632 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019633 /* SPEC (1) "Its {abstract} is false." */
19634 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19635 return;
19636 {
19637 xmlSchemaElementPtr head;
19638 xmlSchemaTypePtr headType, type;
19639 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019640 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019641 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19642 * {disallowed substitutions} as the blocking constraint, as defined in
19643 * Substitution Group OK (Transitive) (§3.3.6)."
19644 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019645 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19646 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019647 set = 0;
19648 methSet = 0;
19649 /*
19650 * The blocking constraints.
19651 */
19652 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19653 continue;
19654 headType = head->subtypes;
19655 type = elemDecl->subtypes;
19656 if (headType == type)
19657 goto add_member;
19658 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19659 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19660 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19661 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19662 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019663 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019664 * "The set of all {derivation method}s involved in the
19665 * derivation of D's {type definition} from C's {type definition}
19666 * does not intersect with the union of the blocking constraint,
19667 * C's {prohibited substitutions} (if C is complex, otherwise the
19668 * empty set) and the {prohibited substitutions} (respectively the
19669 * empty set) of any intermediate {type definition}s in the
19670 * derivation of D's {type definition} from C's {type definition}."
19671 */
19672 /*
19673 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19674 * subst.head axis, the methSet does not need to be computed for
19675 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019676 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019677 /*
19678 * The set of all {derivation method}s involved in the derivation
19679 */
19680 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019681 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019682 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19683 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019684
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019685 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019686 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19687 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19688
19689 type = type->baseType;
19690 }
19691 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019692 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019693 * the head's type.
19694 */
19695 type = elemDecl->subtypes->baseType;
19696 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019697 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019698 if ((type->flags &
19699 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19700 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19701 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19702 if ((type->flags &
19703 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19704 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19705 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19706 } else
19707 break;
19708 if (type == headType)
19709 break;
19710 type = type->baseType;
19711 }
19712 if ((set != 0) &&
19713 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19714 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19715 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19716 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19717 continue;
19718 }
19719add_member:
19720 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19721 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19722 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19723 }
19724 }
19725}
19726
19727/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019728 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019729 * @item: an schema element declaration/particle
19730 * @ctxt: a schema parser context
19731 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019732 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019733 * Validates the value constraints of an element declaration.
19734 *
19735 * Fixes finish doing the computations on the element declarations.
19736 */
19737static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019738xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019739 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019740{
19741 if (elemDecl == NULL)
19742 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019743 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19744 return;
19745 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019746 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19747 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019748}
19749
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019750/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019751 * xmlSchemaResolveModelGroupParticleReferences:
19752 * @particle: a particle component
19753 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019754 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019755 * Resolves references of a model group's {particles} to
19756 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019757 */
19758static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019759xmlSchemaResolveModelGroupParticleReferences(
19760 xmlSchemaParserCtxtPtr ctxt,
19761 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019762{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019763 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19764 xmlSchemaQNameRefPtr ref;
19765 xmlSchemaBasicItemPtr refItem;
19766
19767 /*
19768 * URGENT TODO: Test this.
19769 */
19770 while (particle != NULL) {
19771 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19772 ((WXS_PARTICLE_TERM(particle))->type !=
19773 XML_SCHEMA_EXTRA_QNAMEREF))
19774 {
19775 goto next_particle;
19776 }
19777 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019778 /*
19779 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019780 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019781 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019782 particle->children = NULL;
19783
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019784 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19785 ref->itemType, ref->name, ref->targetNamespace);
19786 if (refItem == NULL) {
19787 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019788 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019789 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019790 /* TODO: remove the particle. */
19791 goto next_particle;
19792 }
19793 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19794 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19795 /* TODO: remove the particle. */
19796 goto next_particle;
19797 /*
19798 * NOTE that we will assign the model group definition
19799 * itself to the "term" of the particle. This will ease
19800 * the check for circular model group definitions. After
19801 * that the "term" will be assigned the model group of the
19802 * model group definition.
19803 */
19804 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19805 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019806 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019807 * SPEC cos-all-limited (1)
19808 * SPEC cos-all-limited (1.2)
19809 * "It appears only as the value of one or both of the
19810 * following properties:"
19811 * (1.1) "the {model group} property of a model group
19812 * definition."
19813 * (1.2) "the {term} property of a particle [... of] the "
19814 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019815 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019816 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19817 /* TODO: error code */
19818 XML_SCHEMAP_COS_ALL_LIMITED,
19819 WXS_ITEM_NODE(particle), NULL,
19820 "A model group definition is referenced, but "
19821 "it contains an 'all' model group, which "
19822 "cannot be contained by model groups",
19823 NULL, NULL);
19824 /* TODO: remove the particle. */
19825 goto next_particle;
19826 }
19827 particle->children = (xmlSchemaTreeItemPtr) refItem;
19828 } else {
19829 /*
19830 * TODO: Are referenced element declarations the only
19831 * other components we expect here?
19832 */
19833 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019834 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019835next_particle:
19836 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019837 }
19838}
19839
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019840static int
19841xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19842 xmlSchemaValPtr y)
19843{
19844 xmlSchemaTypePtr tx, ty, ptx, pty;
19845 int ret;
19846
19847 while (x != NULL) {
19848 /* Same types. */
19849 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19850 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19851 ptx = xmlSchemaGetPrimitiveType(tx);
19852 pty = xmlSchemaGetPrimitiveType(ty);
19853 /*
19854 * (1) if a datatype T' is ·derived· by ·restriction· from an
19855 * atomic datatype T then the ·value space· of T' is a subset of
19856 * the ·value space· of T. */
19857 /*
19858 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19859 * from a common atomic ancestor T then the ·value space·s of T'
19860 * and T'' may overlap.
19861 */
19862 if (ptx != pty)
19863 return(0);
19864 /*
19865 * We assume computed values to be normalized, so do a fast
19866 * string comparison for string based types.
19867 */
19868 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019869 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019870 if (! xmlStrEqual(
19871 xmlSchemaValueGetAsString(x),
19872 xmlSchemaValueGetAsString(y)))
19873 return (0);
19874 } else {
19875 ret = xmlSchemaCompareValuesWhtsp(
19876 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19877 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19878 if (ret == -2)
19879 return(-1);
19880 if (ret != 0)
19881 return(0);
19882 }
19883 /*
19884 * Lists.
19885 */
19886 x = xmlSchemaValueGetNext(x);
19887 if (x != NULL) {
19888 y = xmlSchemaValueGetNext(y);
19889 if (y == NULL)
19890 return (0);
19891 } else if (xmlSchemaValueGetNext(y) != NULL)
19892 return (0);
19893 else
19894 return (1);
19895 }
19896 return (0);
19897}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019898
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019899/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019900 * xmlSchemaResolveAttrUseReferences:
19901 * @item: an attribute use
19902 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019903 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019904 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019905 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019906static int
19907xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19908 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019909{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019910 if ((ctxt == NULL) || (ause == NULL))
19911 return(-1);
19912 if ((ause->attrDecl == NULL) ||
19913 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19914 return(0);
19915
19916 {
19917 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19918
19919 /*
19920 * TODO: Evaluate, what errors could occur if the declaration is not
19921 * found.
19922 */
19923 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19924 ref->name, ref->targetNamespace);
19925 if (ause->attrDecl == NULL) {
19926 xmlSchemaPResCompAttrErr(ctxt,
19927 XML_SCHEMAP_SRC_RESOLVE,
19928 WXS_BASIC_CAST ause, ause->node,
19929 "ref", ref->name, ref->targetNamespace,
19930 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19931 return(ctxt->err);;
19932 }
19933 }
19934 return(0);
19935}
19936
19937/**
19938 * xmlSchemaCheckAttrUsePropsCorrect:
19939 * @ctxt: a parser context
19940 * @use: an attribute use
19941 *
19942 * Schema Component Constraint:
19943 * Attribute Use Correct (au-props-correct)
19944 *
19945 */
19946static int
19947xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19948 xmlSchemaAttributeUsePtr use)
19949{
19950 if ((ctxt == NULL) || (use == NULL))
19951 return(-1);
19952 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19953 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19954 return(0);
19955
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019956 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019957 * SPEC au-props-correct (1)
19958 * "The values of the properties of an attribute use must be as
19959 * described in the property tableau in The Attribute Use Schema
19960 * Component (§3.5.1), modulo the impact of Missing
19961 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019962 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019963
19964 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19965 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19966 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19967 {
19968 xmlSchemaPCustomErr(ctxt,
19969 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19970 WXS_BASIC_CAST use, NULL,
19971 "The attribute declaration has a 'fixed' value constraint "
19972 ", thus the attribute use must also have a 'fixed' value "
19973 "constraint",
19974 NULL);
19975 return(ctxt->err);
19976 }
19977 /*
19978 * Compute and check the value constraint's value.
19979 */
19980 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19981 int ret;
19982 /*
19983 * TODO: The spec seems to be missing a check of the
19984 * value constraint of the attribute use. We will do it here.
19985 */
19986 /*
19987 * SPEC a-props-correct (3)
19988 */
19989 if (xmlSchemaIsDerivedFromBuiltInType(
19990 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19991 {
19992 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19993 XML_SCHEMAP_AU_PROPS_CORRECT,
19994 NULL, WXS_BASIC_CAST use,
19995 "Value constraints are not allowed if the type definition "
19996 "is or is derived from xs:ID",
19997 NULL, NULL);
19998 return(ctxt->err);
19999 }
20000
20001 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
20002 use->node, WXS_ATTRUSE_TYPEDEF(use),
20003 use->defValue, &(use->defVal),
20004 1, 1, 0);
20005 if (ret != 0) {
20006 if (ret < 0) {
20007 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
20008 "calling xmlSchemaVCheckCVCSimpleType()");
20009 return(-1);
20010 }
20011 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20012 XML_SCHEMAP_AU_PROPS_CORRECT,
20013 NULL, WXS_BASIC_CAST use,
20014 "The value of the value constraint is not valid",
20015 NULL, NULL);
20016 return(ctxt->err);
20017 }
20018 }
20019 /*
20020 * SPEC au-props-correct (2)
20021 * "If the {attribute declaration} has a fixed
20022 * {value constraint}, then if the attribute use itself has a
20023 * {value constraint}, it must also be fixed and its value must match
20024 * that of the {attribute declaration}'s {value constraint}."
20025 */
20026 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20027 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20028 {
20029 if (! xmlSchemaAreValuesEqual(use->defVal,
20030 (WXS_ATTRUSE_DECL(use))->defVal))
20031 {
20032 xmlSchemaPCustomErr(ctxt,
20033 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20034 WXS_BASIC_CAST use, NULL,
20035 "The 'fixed' value constraint of the attribute use "
20036 "must match the attribute declaration's value "
20037 "constraint '%s'",
20038 (WXS_ATTRUSE_DECL(use))->defValue);
20039 }
20040 return(ctxt->err);
20041 }
20042 return(0);
20043}
20044
20045
20046
20047
20048/**
20049 * xmlSchemaResolveAttrTypeReferences:
20050 * @item: an attribute declaration
20051 * @ctxt: a parser context
20052 *
20053 * Resolves the referenced type definition component.
20054 */
20055static int
20056xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20057 xmlSchemaParserCtxtPtr ctxt)
20058{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020059 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020060 * The simple type definition corresponding to the <simpleType> element
20061 * information item in the [children], if present, otherwise the simple
20062 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020063 * [attribute], if present, otherwise the ·simple ur-type definition·.
20064 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020065 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020066 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020067 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20068 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020069 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020070 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020071 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020072
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020073 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20074 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020075 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020076 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020077 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020078 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020079 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020080 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020081 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020082 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020083 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020084
Daniel Veillard3646d642004-06-02 19:19:14 +000020085 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020086 /*
20087 * The type defaults to the xs:anySimpleType.
20088 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020089 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20090 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020091 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020092}
20093
20094/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020095 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020096 * @idc: the identity-constraint definition
20097 * @ctxt: the schema parser context
20098 * @name: the attribute name
20099 *
20100 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020101 * Schema Component Constraint:
20102 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020103 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020104static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020105xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020106 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020107{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020108 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020109 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020110 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020111 idc->ref->item = (xmlSchemaBasicItemPtr)
20112 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20113 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020114 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020115 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020116 * TODO: It is actually not an error to fail to resolve
20117 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020118 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020119 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020120 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020121 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020122 "refer", idc->ref->name,
20123 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020124 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020125 return(pctxt->err);
20126 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20127 /*
20128 * SPEC c-props-correct (1)
20129 */
20130 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20131 XML_SCHEMAP_C_PROPS_CORRECT,
20132 NULL, WXS_BASIC_CAST idc,
20133 "The keyref references a keyref",
20134 NULL, NULL);
20135 idc->ref->item = NULL;
20136 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020137 } else {
20138 if (idc->nbFields !=
20139 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20140 xmlChar *str = NULL;
20141 xmlSchemaIDCPtr refer;
20142
20143 refer = (xmlSchemaIDCPtr) idc->ref->item;
20144 /*
20145 * SPEC c-props-correct(2)
20146 * "If the {identity-constraint category} is keyref,
20147 * the cardinality of the {fields} must equal that of
20148 * the {fields} of the {referenced key}.
20149 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020150 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020151 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020152 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020153 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020154 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020155 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020156 refer->name),
20157 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020158 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020159 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020160 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020161 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020162 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020163 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020164}
20165
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020166static int
20167xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20168 xmlSchemaParserCtxtPtr pctxt)
20169{
20170 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20171 prohib->targetNamespace) == NULL) {
20172
20173 xmlSchemaPResCompAttrErr(pctxt,
20174 XML_SCHEMAP_SRC_RESOLVE,
20175 NULL, prohib->node,
20176 "ref", prohib->name, prohib->targetNamespace,
20177 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20178 return(XML_SCHEMAP_SRC_RESOLVE);
20179 }
20180 return(0);
20181}
20182
20183#define WXS_REDEFINED_TYPE(c) \
20184(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20185
20186#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20187(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20188
20189#define WXS_REDEFINED_ATTR_GROUP(c) \
20190(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20191
20192static int
20193xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20194{
20195 int err = 0;
20196 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20197 xmlSchemaBasicItemPtr prev, item;
20198 int wasRedefined;
20199
20200 if (redef == NULL)
20201 return(0);
20202
20203 do {
20204 item = redef->item;
20205 /*
20206 * First try to locate the redefined component in the
20207 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020208 * NOTE: According to this schema bug entry:
20209 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20210 * it's not clear if the referenced component needs to originate
20211 * from the <redefine>d schema _document_ or the schema; the latter
20212 * would include all imported and included sub-schemas of the
20213 * <redefine>d schema. Currenlty we latter approach is used.
20214 * SUPPLEMENT: It seems that the WG moves towards the latter
20215 * approach, so we are doing it right.
20216 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020217 */
20218 prev = xmlSchemaFindRedefCompInGraph(
20219 redef->targetBucket, item->type,
20220 redef->refName, redef->refTargetNs);
20221 if (prev == NULL) {
20222 xmlChar *str = NULL;
20223 xmlNodePtr node;
20224
20225 /*
20226 * SPEC src-redefine:
20227 * (6.2.1) "The ·actual value· of its own name attribute plus
20228 * target namespace must successfully ·resolve· to a model
20229 * group definition in I."
20230 * (7.2.1) "The ·actual value· of its own name attribute plus
20231 * target namespace must successfully ·resolve· to an attribute
20232 * group definition in I."
20233
20234 *
20235 * Note that, if we are redefining with the use of references
20236 * to components, the spec assumes the src-resolve to be used;
20237 * but this won't assure that we search only *inside* the
20238 * redefined schema.
20239 */
20240 if (redef->reference)
20241 node = WXS_ITEM_NODE(redef->reference);
20242 else
20243 node = WXS_ITEM_NODE(item);
20244 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20245 /*
20246 * TODO: error code.
20247 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20248 * reference kind.
20249 */
20250 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020251 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020252 "the redefined schema",
20253 WXS_ITEM_TYPE_NAME(item),
20254 xmlSchemaFormatQName(&str, redef->refTargetNs,
20255 redef->refName));
20256 FREE_AND_NULL(str);
20257 err = pctxt->err;
20258 redef = redef->next;
20259 continue;
20260 }
20261 /*
20262 * TODO: Obtaining and setting the redefinition state is really
20263 * clumsy.
20264 */
20265 wasRedefined = 0;
20266 switch (item->type) {
20267 case XML_SCHEMA_TYPE_COMPLEX:
20268 case XML_SCHEMA_TYPE_SIMPLE:
20269 if ((WXS_TYPE_CAST prev)->flags &
20270 XML_SCHEMAS_TYPE_REDEFINED)
20271 {
20272 wasRedefined = 1;
20273 break;
20274 }
20275 /* Mark it as redefined. */
20276 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20277 /*
20278 * Assign the redefined type to the
20279 * base type of the redefining type.
20280 * TODO: How
20281 */
20282 ((xmlSchemaTypePtr) item)->baseType =
20283 (xmlSchemaTypePtr) prev;
20284 break;
20285 case XML_SCHEMA_TYPE_GROUP:
20286 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20287 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20288 {
20289 wasRedefined = 1;
20290 break;
20291 }
20292 /* Mark it as redefined. */
20293 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20294 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20295 if (redef->reference != NULL) {
20296 /*
20297 * Overwrite the QName-reference with the
20298 * referenced model group def.
20299 */
20300 (WXS_PTC_CAST redef->reference)->children =
20301 WXS_TREE_CAST prev;
20302 }
20303 redef->target = prev;
20304 break;
20305 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20306 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20307 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20308 {
20309 wasRedefined = 1;
20310 break;
20311 }
20312 (WXS_ATTR_GROUP_CAST prev)->flags |=
20313 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20314 if (redef->reference != NULL) {
20315 /*
20316 * Assign the redefined attribute group to the
20317 * QName-reference component.
20318 * This is the easy case, since we will just
20319 * expand the redefined group.
20320 */
20321 (WXS_QNAME_CAST redef->reference)->item = prev;
20322 redef->target = NULL;
20323 } else {
20324 /*
20325 * This is the complicated case: we need
20326 * to apply src-redefine (7.2.2) at a later
20327 * stage, i.e. when attribute group references
20328 * have beed expanded and simple types have
20329 * beed fixed.
20330 */
20331 redef->target = prev;
20332 }
20333 break;
20334 default:
20335 PERROR_INT("xmlSchemaResolveRedefReferences",
20336 "Unexpected redefined component type");
20337 return(-1);
20338 }
20339 if (wasRedefined) {
20340 xmlChar *str = NULL;
20341 xmlNodePtr node;
20342
20343 if (redef->reference)
20344 node = WXS_ITEM_NODE(redef->reference);
20345 else
20346 node = WXS_ITEM_NODE(redef->item);
20347
20348 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20349 /* TODO: error code. */
20350 XML_SCHEMAP_SRC_REDEFINE,
20351 node, NULL,
20352 "The referenced %s was already redefined. Multiple "
20353 "redefinition of the same component is not supported",
20354 xmlSchemaGetComponentDesignation(&str, prev),
20355 NULL);
20356 FREE_AND_NULL(str)
20357 err = pctxt->err;
20358 redef = redef->next;
20359 continue;
20360 }
20361 redef = redef->next;
20362 } while (redef != NULL);
20363
20364 return(err);
20365}
20366
20367static int
20368xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20369{
20370 int err = 0;
20371 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20372 xmlSchemaBasicItemPtr item;
20373
20374 if (redef == NULL)
20375 return(0);
20376
20377 do {
20378 if (redef->target == NULL) {
20379 redef = redef->next;
20380 continue;
20381 }
20382 item = redef->item;
20383
20384 switch (item->type) {
20385 case XML_SCHEMA_TYPE_SIMPLE:
20386 case XML_SCHEMA_TYPE_COMPLEX:
20387 /*
20388 * Since the spec wants the {name} of the redefined
20389 * type to be 'absent', we'll NULL it.
20390 */
20391 (WXS_TYPE_CAST redef->target)->name = NULL;
20392
20393 /*
20394 * TODO: Seems like there's nothing more to do. The normal
20395 * inheritance mechanism is used. But not 100% sure.
20396 */
20397 break;
20398 case XML_SCHEMA_TYPE_GROUP:
20399 /*
20400 * URGENT TODO:
20401 * SPEC src-redefine:
20402 * (6.2.2) "The {model group} of the model group definition
20403 * which corresponds to it per XML Representation of Model
20404 * Group Definition Schema Components (§3.7.2) must be a
20405 * ·valid restriction· of the {model group} of that model
20406 * group definition in I, as defined in Particle Valid
20407 * (Restriction) (§3.9.6)."
20408 */
20409 break;
20410 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20411 /*
20412 * SPEC src-redefine:
20413 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20414 * the attribute group definition which corresponds to it
20415 * per XML Representation of Attribute Group Definition Schema
20416 * Components (§3.6.2) must be ·valid restrictions· of the
20417 * {attribute uses} and {attribute wildcard} of that attribute
20418 * group definition in I, as defined in clause 2, clause 3 and
20419 * clause 4 of Derivation Valid (Restriction, Complex)
20420 * (§3.4.6) (where references to the base type definition are
20421 * understood as references to the attribute group definition
20422 * in I)."
20423 */
20424 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20425 XML_SCHEMA_ACTION_REDEFINE,
20426 item, redef->target,
20427 (WXS_ATTR_GROUP_CAST item)->attrUses,
20428 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20429 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20430 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20431 if (err == -1)
20432 return(-1);
20433 break;
20434 default:
20435 break;
20436 }
20437 redef = redef->next;
20438 } while (redef != NULL);
20439 return(0);
20440}
20441
20442
20443static int
20444xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20445 xmlSchemaBucketPtr bucket)
20446{
20447 xmlSchemaBasicItemPtr item;
20448 int err;
20449 xmlHashTablePtr *table;
20450 const xmlChar *name;
20451 int i;
20452
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020453#define WXS_GET_GLOBAL_HASH(c, slot) { \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020454 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20455 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20456 else \
20457 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20458
20459 /*
20460 * Add global components to the schema's hash tables.
20461 * This is the place where duplicate components will be
20462 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020463 * TODO: I think normally we should support imports of the
20464 * same namespace from multiple locations. We don't do currently,
20465 * but if we do then according to:
20466 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20467 * we would need, if imported directly, to import redefined
20468 * components as well to be able to catch clashing components.
20469 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020470 */
20471 if (bucket == NULL)
20472 return(-1);
20473 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20474 return(0);
20475 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20476
20477 for (i = 0; i < bucket->globals->nbItems; i++) {
20478 item = bucket->globals->items[i];
20479 table = NULL;
20480 switch (item->type) {
20481 case XML_SCHEMA_TYPE_COMPLEX:
20482 case XML_SCHEMA_TYPE_SIMPLE:
20483 if (WXS_REDEFINED_TYPE(item))
20484 continue;
20485 name = (WXS_TYPE_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020486 WXS_GET_GLOBAL_HASH(bucket, typeDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020487 break;
20488 case XML_SCHEMA_TYPE_ELEMENT:
20489 name = (WXS_ELEM_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020490 WXS_GET_GLOBAL_HASH(bucket, elemDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020491 break;
20492 case XML_SCHEMA_TYPE_ATTRIBUTE:
20493 name = (WXS_ATTR_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020494 WXS_GET_GLOBAL_HASH(bucket, attrDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020495 break;
20496 case XML_SCHEMA_TYPE_GROUP:
20497 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20498 continue;
20499 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020500 WXS_GET_GLOBAL_HASH(bucket, groupDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020501 break;
20502 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20503 if (WXS_REDEFINED_ATTR_GROUP(item))
20504 continue;
20505 name = (WXS_ATTR_GROUP_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020506 WXS_GET_GLOBAL_HASH(bucket, attrgrpDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020507 break;
20508 case XML_SCHEMA_TYPE_IDC_KEY:
20509 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20510 case XML_SCHEMA_TYPE_IDC_KEYREF:
20511 name = (WXS_IDC_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020512 WXS_GET_GLOBAL_HASH(bucket, idcDef)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020513 break;
20514 case XML_SCHEMA_TYPE_NOTATION:
20515 name = ((xmlSchemaNotationPtr) item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020516 WXS_GET_GLOBAL_HASH(bucket, notaDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020517 break;
20518 default:
20519 PERROR_INT("xmlSchemaAddComponents",
20520 "Unexpected global component type");
20521 continue;
20522 }
20523 if (*table == NULL) {
20524 *table = xmlHashCreateDict(10, pctxt->dict);
20525 if (*table == NULL) {
20526 PERROR_INT("xmlSchemaAddComponents",
20527 "failed to create a component hash table");
20528 return(-1);
20529 }
20530 }
20531 err = xmlHashAddEntry(*table, name, item);
20532 if (err != 0) {
20533 xmlChar *str = NULL;
20534
20535 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20536 XML_SCHEMAP_REDEFINED_TYPE,
20537 WXS_ITEM_NODE(item),
20538 WXS_BASIC_CAST item,
20539 "A global %s '%s' does already exist",
20540 WXS_ITEM_TYPE_NAME(item),
20541 xmlSchemaGetComponentQName(&str, item));
20542 FREE_AND_NULL(str);
20543 }
20544 }
20545 /*
20546 * Process imported/included schemas.
20547 */
20548 if (bucket->relations != NULL) {
20549 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20550 do {
20551 if ((rel->bucket != NULL) &&
20552 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20553 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20554 return(-1);
20555 }
20556 rel = rel->next;
20557 } while (rel != NULL);
20558 }
20559 return(0);
20560}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020561
20562static int
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020563xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt,
20564 xmlSchemaBucketPtr rootBucket)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020565{
20566 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20567 xmlSchemaTreeItemPtr item, *items;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020568 int nbItems, i, ret = 0;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020569 xmlSchemaBucketPtr oldbucket = con->bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020570
20571#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20572
20573 if ((con->pending == NULL) ||
20574 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020575 return(0);
20576
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020577 /*
20578 * Since xmlSchemaFixupComplexType() will create new particles
20579 * (local components), and those particle components need a bucket
20580 * on the constructor, we'll assure here that the constructor has
20581 * a bucket.
20582 * TODO: Think about storing locals _only_ on the main bucket.
20583 */
20584 if (con->bucket == NULL)
20585 con->bucket = rootBucket;
20586
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020587 /* TODO:
20588 * SPEC (src-redefine):
20589 * (6.2) "If it has no such self-reference, then all of the
20590 * following must be true:"
20591
20592 * (6.2.2) The {model group} of the model group definition which
20593 * corresponds to it per XML Representation of Model Group
20594 * Definition Schema Components (§3.7.2) must be a ·valid
20595 * restriction· of the {model group} of that model group definition
20596 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20597 */
20598 xmlSchemaCheckSRCRedefineFirst(pctxt);
20599
20600 /*
20601 * Add global components to the schemata's hash tables.
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020602 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020603 xmlSchemaAddComponents(pctxt, rootBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020604
20605 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020606 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20607 nbItems = con->pending->nbItems;
20608 /*
20609 * Now that we have parsed *all* the schema document(s) and converted
20610 * them to schema components, we can resolve references, apply component
20611 * constraints, create the FSA from the content model, etc.
20612 */
20613 /*
20614 * Resolve references of..
20615 *
20616 * 1. element declarations:
20617 * - the type definition
20618 * - the substitution group affiliation
20619 * 2. simple/complex types:
20620 * - the base type definition
20621 * - the memberTypes of union types
20622 * - the itemType of list types
20623 * 3. attributes declarations and attribute uses:
20624 * - the type definition
20625 * - if an attribute use, then the attribute declaration
20626 * 4. attribute group references:
20627 * - the attribute group definition
20628 * 5. particles:
20629 * - the term of the particle (e.g. a model group)
20630 * 6. IDC key-references:
20631 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020632 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020633 */
20634 for (i = 0; i < nbItems; i++) {
20635 item = items[i];
20636 switch (item->type) {
20637 case XML_SCHEMA_TYPE_ELEMENT:
20638 xmlSchemaResolveElementReferences(
20639 (xmlSchemaElementPtr) item, pctxt);
20640 FIXHFAILURE;
20641 break;
20642 case XML_SCHEMA_TYPE_COMPLEX:
20643 case XML_SCHEMA_TYPE_SIMPLE:
20644 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020645 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020646 FIXHFAILURE;
20647 break;
20648 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020649 xmlSchemaResolveAttrTypeReferences(
20650 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020651 FIXHFAILURE;
20652 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020653 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20654 xmlSchemaResolveAttrUseReferences(
20655 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020656 FIXHFAILURE;
20657 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020658 case XML_SCHEMA_EXTRA_QNAMEREF:
20659 if ((WXS_QNAME_CAST item)->itemType ==
20660 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20661 {
20662 xmlSchemaResolveAttrGroupReferences(
20663 WXS_QNAME_CAST item, pctxt);
20664 }
20665 FIXHFAILURE;
20666 break;
20667 case XML_SCHEMA_TYPE_SEQUENCE:
20668 case XML_SCHEMA_TYPE_CHOICE:
20669 case XML_SCHEMA_TYPE_ALL:
20670 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20671 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020672 FIXHFAILURE;
20673 break;
20674 case XML_SCHEMA_TYPE_IDC_KEY:
20675 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20676 case XML_SCHEMA_TYPE_IDC_KEYREF:
20677 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020678 (xmlSchemaIDCPtr) item, pctxt);
20679 FIXHFAILURE;
20680 break;
20681 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20682 /*
20683 * Handle attribue prohibition which had a
20684 * "ref" attribute.
20685 */
20686 xmlSchemaResolveAttrUseProhibReferences(
20687 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020688 FIXHFAILURE;
20689 break;
20690 default:
20691 break;
20692 }
20693 }
20694 if (pctxt->nberrors != 0)
20695 goto exit_error;
20696
20697 /*
20698 * Now that all references are resolved we
20699 * can check for circularity of...
20700 * 1. the base axis of type definitions
20701 * 2. nested model group definitions
20702 * 3. nested attribute group definitions
20703 * TODO: check for circual substitution groups.
20704 */
20705 for (i = 0; i < nbItems; i++) {
20706 item = items[i];
20707 /*
20708 * Let's better stop on the first error here.
20709 */
20710 switch (item->type) {
20711 case XML_SCHEMA_TYPE_COMPLEX:
20712 case XML_SCHEMA_TYPE_SIMPLE:
20713 xmlSchemaCheckTypeDefCircular(
20714 (xmlSchemaTypePtr) item, pctxt);
20715 FIXHFAILURE;
20716 if (pctxt->nberrors != 0)
20717 goto exit_error;
20718 break;
20719 case XML_SCHEMA_TYPE_GROUP:
20720 xmlSchemaCheckGroupDefCircular(
20721 (xmlSchemaModelGroupDefPtr) item, pctxt);
20722 FIXHFAILURE;
20723 if (pctxt->nberrors != 0)
20724 goto exit_error;
20725 break;
20726 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20727 xmlSchemaCheckAttrGroupCircular(
20728 (xmlSchemaAttributeGroupPtr) item, pctxt);
20729 FIXHFAILURE;
20730 if (pctxt->nberrors != 0)
20731 goto exit_error;
20732 break;
20733 default:
20734 break;
20735 }
20736 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020737 if (pctxt->nberrors != 0)
20738 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020739 /*
20740 * Model group definition references:
20741 * Such a reference is reflected by a particle at the component
20742 * level. Until now the 'term' of such particles pointed
20743 * to the model group definition; this was done, in order to
20744 * ease circularity checks. Now we need to set the 'term' of
20745 * such particles to the model group of the model group definition.
20746 */
20747 for (i = 0; i < nbItems; i++) {
20748 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020749 switch (item->type) {
20750 case XML_SCHEMA_TYPE_SEQUENCE:
20751 case XML_SCHEMA_TYPE_CHOICE:
20752 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20753 WXS_MODEL_GROUP_CAST item);
20754 break;
20755 default:
20756 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020757 }
20758 }
20759 if (pctxt->nberrors != 0)
20760 goto exit_error;
20761 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020762 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020763 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020764 for (i = 0; i < nbItems; i++) {
20765 item = items[i];
20766 switch (item->type) {
20767 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20768 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20769 WXS_ATTR_GROUP_HAS_REFS(item))
20770 {
20771 xmlSchemaAttributeGroupExpandRefs(pctxt,
20772 WXS_ATTR_GROUP_CAST item);
20773 FIXHFAILURE;
20774 }
20775 break;
20776 default:
20777 break;
20778 }
20779 }
20780 if (pctxt->nberrors != 0)
20781 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020782 /*
20783 * First compute the variety of simple types. This is needed as
20784 * a seperate step, since otherwise we won't be able to detect
20785 * circular union types in all cases.
20786 */
20787 for (i = 0; i < nbItems; i++) {
20788 item = items[i];
20789 switch (item->type) {
20790 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020791 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020792 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20793 (xmlSchemaTypePtr) item);
20794 FIXHFAILURE;
20795 }
20796 break;
20797 default:
20798 break;
20799 }
20800 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020801 if (pctxt->nberrors != 0)
20802 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020803 /*
20804 * Detect circular union types. Note that this needs the variety to
20805 * be already computed.
20806 */
20807 for (i = 0; i < nbItems; i++) {
20808 item = items[i];
20809 switch (item->type) {
20810 case XML_SCHEMA_TYPE_SIMPLE:
20811 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20812 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20813 (xmlSchemaTypePtr) item);
20814 FIXHFAILURE;
20815 }
20816 break;
20817 default:
20818 break;
20819 }
20820 }
20821 if (pctxt->nberrors != 0)
20822 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020823
20824 /*
20825 * Do the complete type fixup for simple types.
20826 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020827 for (i = 0; i < nbItems; i++) {
20828 item = items[i];
20829 switch (item->type) {
20830 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020831 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20832 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20833 FIXHFAILURE;
20834 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020835 break;
20836 default:
20837 break;
20838 }
20839 }
20840 if (pctxt->nberrors != 0)
20841 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020842 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020843 * At this point we need all simple types to be builded and checked.
20844 */
20845 /*
20846 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020847 */
20848 for (i = 0; i < nbItems; i++) {
20849 item = items[i];
20850 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020851 case XML_SCHEMA_TYPE_ATTRIBUTE:
20852 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20853 FIXHFAILURE;
20854 break;
20855 default:
20856 break;
20857 }
20858 }
20859 if (pctxt->nberrors != 0)
20860 goto exit_error;
20861 /*
20862 * Apply constraints for attribute uses.
20863 */
20864 for (i = 0; i < nbItems; i++) {
20865 item = items[i];
20866 switch (item->type) {
20867 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20868 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20869 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20870 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020871 FIXHFAILURE;
20872 }
20873 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020874 default:
20875 break;
20876 }
20877 }
20878 if (pctxt->nberrors != 0)
20879 goto exit_error;
20880
20881 /*
20882 * Apply constraints for attribute group definitions.
20883 */
20884 for (i = 0; i < nbItems; i++) {
20885 item = items[i];
20886 switch (item->type) {
20887 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20888 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20889 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20890 {
20891 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20892 FIXHFAILURE;
20893 }
20894 break;
20895 default:
20896 break;
20897 }
20898 }
20899 if (pctxt->nberrors != 0)
20900 goto exit_error;
20901
20902 /*
20903 * Apply constraints for redefinitions.
20904 */
20905 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20906 xmlSchemaCheckSRCRedefineSecond(pctxt);
20907 if (pctxt->nberrors != 0)
20908 goto exit_error;
20909
20910 /*
20911 * Fixup complex types.
20912 */
20913 for (i = 0; i < nbItems; i++) {
20914 item = con->pending->items[i];
20915 switch (item->type) {
20916 case XML_SCHEMA_TYPE_COMPLEX:
20917 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20918 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20919 FIXHFAILURE;
20920 }
20921 break;
20922 default:
20923 break;
20924 }
20925 }
20926 if (pctxt->nberrors != 0)
20927 goto exit_error;
20928
20929 /*
20930 * The list could have changed, since xmlSchemaFixupComplexType()
20931 * will create particles and model groups in some cases.
20932 */
20933 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20934 nbItems = con->pending->nbItems;
20935
20936 /*
20937 * At this point all complex types need to be builded and checked.
20938 */
20939 /*
20940 * Apply some constraints for element declarations.
20941 */
20942 for (i = 0; i < nbItems; i++) {
20943 item = items[i];
20944 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020945 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020946
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020947 if ((((xmlSchemaElementPtr) item)->flags &
20948 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20949 xmlSchemaCheckElementDeclComponent(
20950 (xmlSchemaElementPtr) item, pctxt);
20951 FIXHFAILURE;
20952 }
20953 break;
20954 default:
20955 break;
20956 }
20957 }
20958 if (pctxt->nberrors != 0)
20959 goto exit_error;
20960 /*
20961 * Finally we can build the automaton from the content model of
20962 * complex types.
20963 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020964
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020965 for (i = 0; i < nbItems; i++) {
20966 item = items[i];
20967 switch (item->type) {
20968 case XML_SCHEMA_TYPE_COMPLEX:
20969 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020970 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020971 break;
20972 default:
20973 break;
20974 }
20975 }
20976 if (pctxt->nberrors != 0)
20977 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020978 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020979 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020980 */
20981 goto exit;
20982
20983exit_error:
20984 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020985 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020986
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020987exit_failure:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020988 ret = -1;
20989
20990exit:
20991 /*
20992 * Reset the constructor. This is needed for XSI acquisition, since
20993 * those items will be processed over and over again for every XSI
20994 * if not cleared here.
20995 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020996 con->bucket = oldbucket;
20997 con->pending->nbItems = 0;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020998 if (con->substGroups != NULL) {
20999 xmlHashFree(con->substGroups,
21000 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
21001 con->substGroups = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021002 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021003 if (con->redefs != NULL) {
21004 xmlSchemaRedefListFree(con->redefs);
21005 con->redefs = NULL;
21006 }
21007 return(ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021008}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021009/**
Daniel Veillard4255d502002-04-16 15:50:10 +000021010 * xmlSchemaParse:
21011 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021012 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021013 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000021014 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000021015 *
21016 * Returns the internal XML Schema structure built from the resource or
21017 * NULL in case of error
21018 */
21019xmlSchemaPtr
21020xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
21021{
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021022 xmlSchemaPtr mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021023 xmlSchemaBucketPtr bucket = NULL;
21024 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000021025
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021026 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021027 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021028 * the API; i.e. not automatically by the validated instance document.
21029 */
21030
Daniel Veillard4255d502002-04-16 15:50:10 +000021031 xmlSchemaInitTypes();
21032
Daniel Veillard6045c902002-10-09 21:13:59 +000021033 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000021034 return (NULL);
21035
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021036 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021037 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021038 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021039 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021040
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021041 /* Create the *main* schema. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021042 mainSchema = xmlSchemaNewSchema(ctxt);
21043 if (mainSchema == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021044 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021045 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021046 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021047 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021048 if (ctxt->constructor == NULL) {
21049 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21050 if (ctxt->constructor == NULL)
21051 return(NULL);
21052 /* Take ownership of the constructor to be able to free it. */
21053 ctxt->ownsConstructor = 1;
21054 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021055 ctxt->constructor->mainSchema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021056 /*
21057 * Locate and add the schema document.
21058 */
21059 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21060 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21061 NULL, NULL, &bucket);
21062 if (res == -1)
21063 goto exit_failure;
21064 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000021065 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021066
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021067 if (bucket == NULL) {
21068 /* TODO: Error code, actually we failed to *locate* the schema. */
21069 if (ctxt->URL)
21070 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21071 NULL, NULL,
21072 "Failed to locate the main schema resource at '%s'",
21073 ctxt->URL, NULL);
21074 else
21075 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21076 NULL, NULL,
21077 "Failed to locate the main schema resource",
21078 NULL, NULL);
21079 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021080 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021081 /* Then do the parsing for good. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021082 if (xmlSchemaParseNewDocWithContext(ctxt, mainSchema, bucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021083 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021084 if (ctxt->nberrors != 0)
21085 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021086
21087 mainSchema->doc = bucket->doc;
21088 mainSchema->preserve = ctxt->preserve;
21089
21090 ctxt->schema = mainSchema;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021091
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021092 if (xmlSchemaFixupComponents(ctxt, WXS_CONSTRUCTOR(ctxt)->mainBucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021093 goto exit_failure;
21094
21095 /*
21096 * TODO: This is not nice, since we cannot distinguish from the
21097 * result if there was an internal error or not.
21098 */
21099exit:
21100 if (ctxt->nberrors != 0) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021101 if (mainSchema) {
21102 xmlSchemaFree(mainSchema);
21103 mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021104 }
21105 if (ctxt->constructor) {
21106 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21107 ctxt->constructor = NULL;
21108 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021109 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021110 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021111 ctxt->schema = NULL;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021112 return(mainSchema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021113exit_failure:
21114 /*
21115 * Quite verbose, but should catch internal errors, which were
21116 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021117 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021118 if (mainSchema) {
21119 xmlSchemaFree(mainSchema);
21120 mainSchema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021121 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021122 if (ctxt->constructor) {
21123 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21124 ctxt->constructor = NULL;
21125 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021126 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021127 PERROR_INT2("xmlSchemaParse",
21128 "An internal error occured");
21129 ctxt->schema = NULL;
21130 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021131}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021132
Daniel Veillard4255d502002-04-16 15:50:10 +000021133/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021134 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021135 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021136 * @err: the error callback
21137 * @warn: the warning callback
21138 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021139 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021140 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021141 */
21142void
21143xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021144 xmlSchemaValidityErrorFunc err,
21145 xmlSchemaValidityWarningFunc warn, void *ctx)
21146{
Daniel Veillard4255d502002-04-16 15:50:10 +000021147 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021148 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021149 ctxt->error = err;
21150 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021151 ctxt->errCtxt = ctx;
21152 if (ctxt->vctxt != NULL)
21153 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21154}
21155
21156/**
21157 * xmlSchemaSetParserStructuredErrors:
21158 * @ctxt: a schema parser context
21159 * @serror: the structured error function
21160 * @ctx: the functions context
21161 *
21162 * Set the structured error callback
21163 */
21164void
21165xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21166 xmlStructuredErrorFunc serror,
21167 void *ctx)
21168{
21169 if (ctxt == NULL)
21170 return;
21171 ctxt->serror = serror;
21172 ctxt->errCtxt = ctx;
21173 if (ctxt->vctxt != NULL)
21174 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021175}
21176
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021177/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021178 * xmlSchemaGetParserErrors:
21179 * @ctxt: a XMl-Schema parser context
21180 * @err: the error callback result
21181 * @warn: the warning callback result
21182 * @ctx: contextual data for the callbacks result
21183 *
21184 * Get the callback information used to handle errors for a parser context
21185 *
21186 * Returns -1 in case of failure, 0 otherwise
21187 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021188int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021189xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021190 xmlSchemaValidityErrorFunc * err,
21191 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021192{
21193 if (ctxt == NULL)
21194 return(-1);
21195 if (err != NULL)
21196 *err = ctxt->error;
21197 if (warn != NULL)
21198 *warn = ctxt->warning;
21199 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021200 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021201 return(0);
21202}
21203
21204/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021205 * xmlSchemaFacetTypeToString:
21206 * @type: the facet type
21207 *
21208 * Convert the xmlSchemaTypeType to a char string.
21209 *
21210 * Returns the char string representation of the facet type if the
21211 * type is a facet and an "Internal Error" string otherwise.
21212 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021213static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021214xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21215{
21216 switch (type) {
21217 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021218 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021219 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021220 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021221 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021222 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021223 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021224 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021225 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021226 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021227 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021228 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021229 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021230 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021231 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021232 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021233 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021234 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021235 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021236 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021237 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021238 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021239 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021240 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021241 default:
21242 break;
21243 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021244 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021245}
21246
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021247static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021248xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21249{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021250 /*
21251 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021252 * from xsd:string.
21253 */
21254 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021255 /*
21256 * Note that we assume a whitespace of preserve for anySimpleType.
21257 */
21258 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21259 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21260 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021261 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021262 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021263 else {
21264 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021265 * For all ·atomic· datatypes other than string (and types ·derived·
21266 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021267 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021268 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021269 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021270 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021271 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021272 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021273 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021274 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021275 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021276 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021277 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021278 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021279 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021280 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21281 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21282 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21283 return (XML_SCHEMA_WHITESPACE_REPLACE);
21284 else
21285 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021286 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021287 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021288}
21289
Daniel Veillard4255d502002-04-16 15:50:10 +000021290/************************************************************************
21291 * *
21292 * Simple type validation *
21293 * *
21294 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021295
Daniel Veillard4255d502002-04-16 15:50:10 +000021296
21297/************************************************************************
21298 * *
21299 * DOM Validation code *
21300 * *
21301 ************************************************************************/
21302
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021303/**
21304 * xmlSchemaAssembleByLocation:
21305 * @pctxt: a schema parser context
21306 * @vctxt: a schema validation context
21307 * @schema: the existing schema
21308 * @node: the node that fired the assembling
21309 * @nsName: the namespace name of the new schema
21310 * @location: the location of the schema
21311 *
21312 * Expands an existing schema by an additional schema.
21313 *
21314 * Returns 0 if the new schema is correct, a positive error code
21315 * number otherwise and -1 in case of an internal or API error.
21316 */
21317static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021318xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021319 xmlSchemaPtr schema,
21320 xmlNodePtr node,
21321 const xmlChar *nsName,
21322 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021323{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021324 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021325 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021326 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021327
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021328 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021329 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021330
21331 if (vctxt->pctxt == NULL) {
21332 VERROR_INT("xmlSchemaAssembleByLocation",
21333 "no parser context available");
21334 return(-1);
21335 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021336 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021337 if (pctxt->constructor == NULL) {
21338 PERROR_INT("xmlSchemaAssembleByLocation",
21339 "no constructor");
21340 return(-1);
21341 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021342 /*
21343 * Acquire the schema document.
21344 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021345 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21346 location, node);
21347 /*
21348 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21349 * the process will automatically change this to
21350 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21351 */
21352 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21353 location, NULL, NULL, 0, node, NULL, nsName,
21354 &bucket);
21355 if (ret != 0)
21356 return(ret);
21357 if (bucket == NULL) {
21358 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021359 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021360 return(-1);
21361 }
21362 /*
21363 * The first located schema will be handled as if all other
21364 * schemas imported by XSI were imported by this first schema.
21365 */
21366 if ((bucket != NULL) &&
21367 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21368 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21369 /*
21370 * TODO: Is this handled like an import? I.e. is it not an error
21371 * if the schema cannot be located?
21372 */
21373 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21374 return(0);
21375 /*
21376 * We will reuse the parser context for every schema imported
21377 * directly via XSI. So reset the context.
21378 */
21379 pctxt->nberrors = 0;
21380 pctxt->err = 0;
21381 pctxt->doc = bucket->doc;
21382
21383 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21384 if (ret == -1) {
21385 pctxt->doc = NULL;
21386 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021387 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021388 /* Paranoid error channelling. */
21389 if ((ret == 0) && (pctxt->nberrors != 0))
21390 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021391 if (pctxt->nberrors == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021392 /*
21393 * Only bother to fixup pending components, if there was
21394 * no error yet.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021395 * For every XSI acquired schema (and its sub-schemata) we will
21396 * fixup the components.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021397 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021398 xmlSchemaFixupComponents(pctxt, bucket);
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000021399 ret = pctxt->err;
21400 /*
21401 * Not nice, but we need somehow to channel the schema parser
21402 * error to the validation context.
21403 */
21404 if ((ret != 0) && (vctxt->err == 0))
21405 vctxt->err = ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021406 vctxt->nberrors += pctxt->nberrors;
21407 } else {
21408 /* Add to validation error sum. */
21409 vctxt->nberrors += pctxt->nberrors;
21410 }
21411 pctxt->doc = NULL;
21412 return(ret);
21413exit_failure:
21414 pctxt->doc = NULL;
21415 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021416}
21417
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021418static xmlSchemaAttrInfoPtr
21419xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21420 int metaType)
21421{
21422 if (vctxt->nbAttrInfos == 0)
21423 return (NULL);
21424 {
21425 int i;
21426 xmlSchemaAttrInfoPtr iattr;
21427
21428 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21429 iattr = vctxt->attrInfos[i];
21430 if (iattr->metaType == metaType)
21431 return (iattr);
21432 }
21433
21434 }
21435 return (NULL);
21436}
21437
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021438/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021439 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021440 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021441 *
21442 * Expands an existing schema by an additional schema using
21443 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21444 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21445 * must be set to 1.
21446 *
21447 * Returns 0 if the new schema is correct, a positive error code
21448 * number otherwise and -1 in case of an internal or API error.
21449 */
21450static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021451xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021452{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021453 const xmlChar *cur, *end;
21454 const xmlChar *nsname = NULL, *location;
21455 int count = 0;
21456 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021457 xmlSchemaAttrInfoPtr iattr;
21458
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021459 /*
21460 * Parse the value; we will assume an even number of values
21461 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021462 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021463 * URGENT TODO: !! This needs to work for both
21464 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21465 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021466 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021467 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21468 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21469 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021470 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021471 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21472 if (iattr == NULL)
21473 return (0);
21474 cur = iattr->value;
21475 do {
21476 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021477 /*
21478 * Get the namespace name.
21479 */
21480 while (IS_BLANK_CH(*cur))
21481 cur++;
21482 end = cur;
21483 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21484 end++;
21485 if (end == cur)
21486 break;
21487 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021488 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021489 cur = end;
21490 }
21491 /*
21492 * Get the URI.
21493 */
21494 while (IS_BLANK_CH(*cur))
21495 cur++;
21496 end = cur;
21497 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21498 end++;
21499 if (end == cur)
21500 break;
21501 count++;
21502 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021503 cur = end;
21504 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21505 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021506 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021507 VERROR_INT("xmlSchemaAssembleByXSI",
21508 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021509 return (-1);
21510 }
21511 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021512 return (ret);
21513}
21514
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021515static const xmlChar *
21516xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21517 const xmlChar *prefix)
21518{
21519 if (vctxt->sax != NULL) {
21520 int i, j;
21521 xmlSchemaNodeInfoPtr inode;
21522
21523 for (i = vctxt->depth; i >= 0; i--) {
21524 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21525 inode = vctxt->elemInfos[i];
21526 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21527 if (((prefix == NULL) &&
21528 (inode->nsBindings[j] == NULL)) ||
21529 ((prefix != NULL) && xmlStrEqual(prefix,
21530 inode->nsBindings[j]))) {
21531
21532 /*
21533 * Note that the namespace bindings are already
21534 * in a string dict.
21535 */
21536 return (inode->nsBindings[j+1]);
21537 }
21538 }
21539 }
21540 }
21541 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021542#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021543 } else if (vctxt->reader != NULL) {
21544 xmlChar *nsName;
21545
21546 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21547 if (nsName != NULL) {
21548 const xmlChar *ret;
21549
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021550 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21551 xmlFree(nsName);
21552 return (ret);
21553 } else
21554 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021555#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021556 } else {
21557 xmlNsPtr ns;
21558
21559 if ((vctxt->inode->node == NULL) ||
21560 (vctxt->inode->node->doc == NULL)) {
21561 VERROR_INT("xmlSchemaLookupNamespace",
21562 "no node or node's doc avaliable");
21563 return (NULL);
21564 }
21565 ns = xmlSearchNs(vctxt->inode->node->doc,
21566 vctxt->inode->node, prefix);
21567 if (ns != NULL)
21568 return (ns->href);
21569 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021570 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021571}
21572
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021573/*
21574* This one works on the schema of the validation context.
21575*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021576static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021577xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21578 xmlSchemaPtr schema,
21579 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021580 const xmlChar *value,
21581 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021582 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021583{
21584 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021585
21586 if (vctxt && (vctxt->schema == NULL)) {
21587 VERROR_INT("xmlSchemaValidateNotation",
21588 "a schema is needed on the validation context");
21589 return (-1);
21590 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021591 ret = xmlValidateQName(value, 1);
21592 if (ret != 0)
21593 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021594 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021595 xmlChar *localName = NULL;
21596 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021597
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021598 localName = xmlSplitQName2(value, &prefix);
21599 if (prefix != NULL) {
21600 const xmlChar *nsName = NULL;
21601
21602 if (vctxt != NULL)
21603 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21604 else if (node != NULL) {
21605 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21606 if (ns != NULL)
21607 nsName = ns->href;
21608 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021609 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021610 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021611 return (1);
21612 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021613 if (nsName == NULL) {
21614 xmlFree(prefix);
21615 xmlFree(localName);
21616 return (1);
21617 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021618 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021619 if (valNeeded && (val != NULL)) {
21620 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21621 BAD_CAST xmlStrdup(nsName));
21622 if (*val == NULL)
21623 ret = -1;
21624 }
21625 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021626 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021627 xmlFree(prefix);
21628 xmlFree(localName);
21629 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021630 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021631 if (valNeeded && (val != NULL)) {
21632 (*val) = xmlSchemaNewNOTATIONValue(
21633 BAD_CAST xmlStrdup(value), NULL);
21634 if (*val == NULL)
21635 ret = -1;
21636 }
21637 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021638 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021639 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021640 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021641 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021642}
21643
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021644static int
21645xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21646 const xmlChar* lname,
21647 const xmlChar* nsname)
21648{
21649 int i;
21650
21651 lname = xmlDictLookup(vctxt->dict, lname, -1);
21652 if (lname == NULL)
21653 return(-1);
21654 if (nsname != NULL) {
21655 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21656 if (nsname == NULL)
21657 return(-1);
21658 }
21659 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21660 if ((vctxt->nodeQNames->items [i] == lname) &&
21661 (vctxt->nodeQNames->items[i +1] == nsname))
21662 /* Already there */
21663 return(i);
21664 }
21665 /* Add new entry. */
21666 i = vctxt->nodeQNames->nbItems;
21667 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21668 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21669 return(i);
21670}
21671
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021672/************************************************************************
21673 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021674 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021675 * *
21676 ************************************************************************/
21677
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021678/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021679 * xmlSchemaAugmentIDC:
21680 * @idcDef: the IDC definition
21681 *
21682 * Creates an augmented IDC definition item.
21683 *
21684 * Returns the item, or NULL on internal errors.
21685 */
21686static void
21687xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21688 xmlSchemaValidCtxtPtr vctxt)
21689{
21690 xmlSchemaIDCAugPtr aidc;
21691
21692 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21693 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021694 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021695 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21696 NULL);
21697 return;
21698 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021699 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021700 aidc->def = idcDef;
21701 aidc->next = NULL;
21702 if (vctxt->aidcs == NULL)
21703 vctxt->aidcs = aidc;
21704 else {
21705 aidc->next = vctxt->aidcs;
21706 vctxt->aidcs = aidc;
21707 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021708 /*
21709 * Save if we have keyrefs at all.
21710 */
21711 if ((vctxt->hasKeyrefs == 0) &&
21712 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21713 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021714}
21715
21716/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021717 * xmlSchemaIDCNewBinding:
21718 * @idcDef: the IDC definition of this binding
21719 *
21720 * Creates a new IDC binding.
21721 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021722 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021723 */
21724static xmlSchemaPSVIIDCBindingPtr
21725xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21726{
21727 xmlSchemaPSVIIDCBindingPtr ret;
21728
21729 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21730 sizeof(xmlSchemaPSVIIDCBinding));
21731 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021732 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021733 "allocating a PSVI IDC binding item", NULL);
21734 return (NULL);
21735 }
21736 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21737 ret->definition = idcDef;
21738 return (ret);
21739}
21740
21741/**
21742 * xmlSchemaIDCStoreNodeTableItem:
21743 * @vctxt: the WXS validation context
21744 * @item: the IDC node table item
21745 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021746 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021747 * They are stored to avoid copying them if IDC node-tables are merged
21748 * with corresponding parent IDC node-tables (bubbling).
21749 *
21750 * Returns 0 if succeeded, -1 on internal errors.
21751 */
21752static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021753xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021754 xmlSchemaPSVIIDCNodePtr item)
21755{
21756 /*
21757 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021758 */
21759 if (vctxt->idcNodes == NULL) {
21760 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021761 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21762 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021763 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021764 "allocating the IDC node table item list", NULL);
21765 return (-1);
21766 }
21767 vctxt->sizeIdcNodes = 20;
21768 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21769 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021770 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21771 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021772 sizeof(xmlSchemaPSVIIDCNodePtr));
21773 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021774 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021775 "re-allocating the IDC node table item list", NULL);
21776 return (-1);
21777 }
21778 }
21779 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021780
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021781 return (0);
21782}
21783
21784/**
21785 * xmlSchemaIDCStoreKey:
21786 * @vctxt: the WXS validation context
21787 * @item: the IDC key
21788 *
21789 * The validation context is used to store an IDC key.
21790 *
21791 * Returns 0 if succeeded, -1 on internal errors.
21792 */
21793static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021794xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021795 xmlSchemaPSVIIDCKeyPtr key)
21796{
21797 /*
21798 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021799 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021800 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021801 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021802 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21803 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021804 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021805 "allocating the IDC key storage list", NULL);
21806 return (-1);
21807 }
21808 vctxt->sizeIdcKeys = 40;
21809 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21810 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021811 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21812 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021813 sizeof(xmlSchemaPSVIIDCKeyPtr));
21814 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021815 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021816 "re-allocating the IDC key storage list", NULL);
21817 return (-1);
21818 }
21819 }
21820 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021821
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021822 return (0);
21823}
21824
21825/**
21826 * xmlSchemaIDCAppendNodeTableItem:
21827 * @bind: the IDC binding
21828 * @ntItem: the node-table item
21829 *
21830 * Appends the IDC node-table item to the binding.
21831 *
21832 * Returns 0 on success and -1 on internal errors.
21833 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021834static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021835xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21836 xmlSchemaPSVIIDCNodePtr ntItem)
21837{
21838 if (bind->nodeTable == NULL) {
21839 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021840 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021841 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21842 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021843 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021844 "allocating an array of IDC node-table items", NULL);
21845 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021846 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021847 } else if (bind->sizeNodes <= bind->nbNodes) {
21848 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021849 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21850 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021851 sizeof(xmlSchemaPSVIIDCNodePtr));
21852 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021853 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021854 "re-allocating an array of IDC node-table items", NULL);
21855 return(-1);
21856 }
21857 }
21858 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021859 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021860}
21861
21862/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021863 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021864 * @vctxt: the WXS validation context
21865 * @matcher: the IDC matcher
21866 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021867 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021868 * of the given matcher. If none found, a new one is created
21869 * and added to the IDC table.
21870 *
21871 * Returns an IDC binding or NULL on internal errors.
21872 */
21873static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021874xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021875 xmlSchemaIDCMatcherPtr matcher)
21876{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021877 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021878
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021879 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021880
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021881 if (ielem->idcTable == NULL) {
21882 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21883 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021884 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021885 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021886 } else {
21887 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021888
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021889 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021890 do {
21891 if (bind->definition == matcher->aidc->def)
21892 return(bind);
21893 if (bind->next == NULL) {
21894 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21895 if (bind->next == NULL)
21896 return (NULL);
21897 return(bind->next);
21898 }
21899 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021900 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021901 }
21902 return (NULL);
21903}
21904
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021905static xmlSchemaItemListPtr
21906xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21907 xmlSchemaIDCMatcherPtr matcher)
21908{
21909 if (matcher->targets == NULL)
21910 matcher->targets = xmlSchemaItemListCreate();
21911 return(matcher->targets);
21912}
21913
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021914/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021915 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021916 * @key: the IDC key
21917 *
21918 * Frees an IDC key together with its compiled value.
21919 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021920static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021921xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21922{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021923 if (key->val != NULL)
21924 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021925 xmlFree(key);
21926}
21927
21928/**
21929 * xmlSchemaIDCFreeBinding:
21930 *
21931 * Frees an IDC binding. Note that the node table-items
21932 * are not freed.
21933 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021934static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021935xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21936{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021937 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021938 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021939 if (bind->dupls != NULL)
21940 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021941 xmlFree(bind);
21942}
21943
21944/**
21945 * xmlSchemaIDCFreeIDCTable:
21946 * @bind: the first IDC binding in the list
21947 *
21948 * Frees an IDC table, i.e. all the IDC bindings in the list.
21949 */
21950static void
21951xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21952{
21953 xmlSchemaPSVIIDCBindingPtr prev;
21954
21955 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021956 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021957 bind = bind->next;
21958 xmlSchemaIDCFreeBinding(prev);
21959 }
21960}
21961
21962/**
21963 * xmlSchemaIDCFreeMatcherList:
21964 * @matcher: the first IDC matcher in the list
21965 *
21966 * Frees a list of IDC matchers.
21967 */
21968static void
21969xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21970{
21971 xmlSchemaIDCMatcherPtr next;
21972
21973 while (matcher != NULL) {
21974 next = matcher->next;
21975 if (matcher->keySeqs != NULL) {
21976 int i;
21977 for (i = 0; i < matcher->sizeKeySeqs; i++)
21978 if (matcher->keySeqs[i] != NULL)
21979 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021980 xmlFree(matcher->keySeqs);
21981 }
21982 if (matcher->targets != NULL) {
21983 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21984 int i;
21985 xmlSchemaPSVIIDCNodePtr idcNode;
21986 /*
21987 * Node-table items for keyrefs are not stored globally
21988 * to the validation context, since they are not bubbled.
21989 * We need to free them here.
21990 */
21991 for (i = 0; i < matcher->targets->nbItems; i++) {
21992 idcNode =
21993 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21994 xmlFree(idcNode->keys);
21995 xmlFree(idcNode);
21996 }
21997 }
21998 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021999 }
22000 xmlFree(matcher);
22001 matcher = next;
22002 }
22003}
22004
22005/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022006 * xmlSchemaIDCAddStateObject:
22007 * @vctxt: the WXS validation context
22008 * @matcher: the IDC matcher
22009 * @sel: the XPath information
22010 * @parent: the parent "selector" state object if any
22011 * @type: "selector" or "field"
22012 *
22013 * Creates/reuses and activates state objects for the given
22014 * XPath information; if the XPath expression consists of unions,
22015 * multiple state objects are created for every unioned expression.
22016 *
22017 * Returns 0 on success and -1 on internal errors.
22018 */
22019static int
22020xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
22021 xmlSchemaIDCMatcherPtr matcher,
22022 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022023 int type)
22024{
22025 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022026
22027 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022028 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022029 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022030 if (vctxt->xpathStatePool != NULL) {
22031 sto = vctxt->xpathStatePool;
22032 vctxt->xpathStatePool = sto->next;
22033 sto->next = NULL;
22034 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022035 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022036 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022037 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022038 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
22039 if (sto == NULL) {
22040 xmlSchemaVErrMemory(NULL,
22041 "allocating an IDC state object", NULL);
22042 return (-1);
22043 }
22044 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
22045 }
22046 /*
22047 * Add to global list.
22048 */
22049 if (vctxt->xpathStates != NULL)
22050 sto->next = vctxt->xpathStates;
22051 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022052
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022053 /*
22054 * Free the old xpath validation context.
22055 */
22056 if (sto->xpathCtxt != NULL)
22057 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22058
22059 /*
22060 * Create a new XPath (pattern) validation context.
22061 */
22062 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22063 (xmlPatternPtr) sel->xpathComp);
22064 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022065 VERROR_INT("xmlSchemaIDCAddStateObject",
22066 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022067 return (-1);
22068 }
22069 sto->type = type;
22070 sto->depth = vctxt->depth;
22071 sto->matcher = matcher;
22072 sto->sel = sel;
22073 sto->nbHistory = 0;
22074
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022075#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022076 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22077 sto->sel->xpath);
22078#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022079 return (0);
22080}
22081
22082/**
22083 * xmlSchemaXPathEvaluate:
22084 * @vctxt: the WXS validation context
22085 * @nodeType: the nodeType of the current node
22086 *
22087 * Evaluates all active XPath state objects.
22088 *
22089 * Returns the number of IC "field" state objects which resolved to
22090 * this node, 0 if none resolved and -1 on internal errors.
22091 */
22092static int
22093xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022094 xmlElementType nodeType)
22095{
22096 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022097 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022098
22099 if (vctxt->xpathStates == NULL)
22100 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022101
22102 if (nodeType == XML_ATTRIBUTE_NODE)
22103 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022104#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022105 {
22106 xmlChar *str = NULL;
22107 xmlGenericError(xmlGenericErrorContext,
22108 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022109 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22110 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022111 FREE_AND_NULL(str)
22112 }
22113#endif
22114 /*
22115 * Process all active XPath state objects.
22116 */
22117 first = vctxt->xpathStates;
22118 sto = first;
22119 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022120#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022121 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022122 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22123 sto->matcher->aidc->def->name, sto->sel->xpath);
22124 else
22125 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22126 sto->matcher->aidc->def->name, sto->sel->xpath);
22127#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022128 if (nodeType == XML_ELEMENT_NODE)
22129 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022130 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022131 else
22132 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022133 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022134
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022135 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022136 VERROR_INT("xmlSchemaXPathEvaluate",
22137 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022138 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022139 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022140 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022141 goto next_sto;
22142 /*
22143 * Full match.
22144 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022145#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022146 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022147 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022148#endif
22149 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022150 * Register a match in the state object history.
22151 */
22152 if (sto->history == NULL) {
22153 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22154 if (sto->history == NULL) {
22155 xmlSchemaVErrMemory(NULL,
22156 "allocating the state object history", NULL);
22157 return(-1);
22158 }
22159 sto->sizeHistory = 10;
22160 } else if (sto->sizeHistory <= sto->nbHistory) {
22161 sto->sizeHistory *= 2;
22162 sto->history = (int *) xmlRealloc(sto->history,
22163 sto->sizeHistory * sizeof(int));
22164 if (sto->history == NULL) {
22165 xmlSchemaVErrMemory(NULL,
22166 "re-allocating the state object history", NULL);
22167 return(-1);
22168 }
22169 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022170 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022171
22172#ifdef DEBUG_IDC
22173 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22174 vctxt->depth);
22175#endif
22176
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022177 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22178 xmlSchemaIDCSelectPtr sel;
22179 /*
22180 * Activate state objects for the IDC fields of
22181 * the IDC selector.
22182 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022183#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022184 xmlGenericError(xmlGenericErrorContext, "IDC: "
22185 "activating field states\n");
22186#endif
22187 sel = sto->matcher->aidc->def->fields;
22188 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022189 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22190 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22191 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022192 sel = sel->next;
22193 }
22194 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22195 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022196 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022197 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022198#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022199 xmlGenericError(xmlGenericErrorContext,
22200 "IDC: key found\n");
22201#endif
22202 /*
22203 * Notify that the character value of this node is
22204 * needed.
22205 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022206 if (resolved == 0) {
22207 if ((vctxt->inode->flags &
22208 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22209 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22210 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022211 resolved++;
22212 }
22213next_sto:
22214 if (sto->next == NULL) {
22215 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022216 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022217 */
22218 head = first;
22219 sto = vctxt->xpathStates;
22220 } else
22221 sto = sto->next;
22222 }
22223 return (resolved);
22224}
22225
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022226static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022227xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022228 xmlChar **buf,
22229 xmlSchemaPSVIIDCKeyPtr *seq,
22230 int count)
22231{
22232 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022233 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022234
22235 *buf = xmlStrdup(BAD_CAST "[");
22236 for (i = 0; i < count; i++) {
22237 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022238 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
22239 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22240 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022241 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022242 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022243 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022244 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22245 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022246 *buf = xmlStrcat(*buf, BAD_CAST "???");
22247 }
22248 if (i < count -1)
22249 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22250 else
22251 *buf = xmlStrcat(*buf, BAD_CAST "'");
22252 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022253 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022254 value = NULL;
22255 }
22256 }
22257 *buf = xmlStrcat(*buf, BAD_CAST "]");
22258
22259 return (BAD_CAST *buf);
22260}
22261
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022262/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022263 * xmlSchemaXPathPop:
22264 * @vctxt: the WXS validation context
22265 *
22266 * Pops all XPath states.
22267 *
22268 * Returns 0 on success and -1 on internal errors.
22269 */
22270static int
22271xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22272{
22273 xmlSchemaIDCStateObjPtr sto;
22274 int res;
22275
22276 if (vctxt->xpathStates == NULL)
22277 return(0);
22278 sto = vctxt->xpathStates;
22279 do {
22280 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22281 if (res == -1)
22282 return (-1);
22283 sto = sto->next;
22284 } while (sto != NULL);
22285 return(0);
22286}
22287
22288/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022289 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022290 * @vctxt: the WXS validation context
22291 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022292 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022293 *
22294 * Processes and pops the history items of the IDC state objects.
22295 * IDC key-sequences are validated/created on IDC bindings.
22296 *
22297 * Returns 0 on success and -1 on internal errors.
22298 */
22299static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022300xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022301 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022302{
22303 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022304 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022305 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022306 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022307
22308 if (vctxt->xpathStates == NULL)
22309 return (0);
22310 sto = vctxt->xpathStates;
22311
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022312#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022313 {
22314 xmlChar *str = NULL;
22315 xmlGenericError(xmlGenericErrorContext,
22316 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022317 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22318 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022319 FREE_AND_NULL(str)
22320 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022321#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022322 /*
22323 * Evaluate the state objects.
22324 */
22325 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022326 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22327 if (res == -1) {
22328 VERROR_INT("xmlSchemaXPathProcessHistory",
22329 "calling xmlStreamPop()");
22330 return (-1);
22331 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022332#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022333 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22334 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022335#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022336 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022337 goto deregister_check;
22338
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022339 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022340
22341 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022342 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022343 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022344 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022345 sto = sto->next;
22346 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022347 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022348 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022349 /*
22350 * NOTE: According to
22351 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22352 * ... the simple-content of complex types is also allowed.
22353 */
22354
22355 if (WXS_IS_COMPLEX(type)) {
22356 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22357 /*
22358 * Sanity check for complex types with simple content.
22359 */
22360 simpleType = type->contentTypeDef;
22361 if (simpleType == NULL) {
22362 VERROR_INT("xmlSchemaXPathProcessHistory",
22363 "field resolves to a CT with simple content "
22364 "but the CT is missing the ST definition");
22365 return (-1);
22366 }
22367 } else
22368 simpleType = NULL;
22369 } else
22370 simpleType = type;
22371 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022372 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022373
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022374 /*
22375 * Not qualified if the field resolves to a node of non
22376 * simple type.
22377 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022378 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022379 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022380 WXS_BASIC_CAST sto->matcher->aidc->def,
22381 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022382 "non-simple type",
22383 sto->sel->xpath,
22384 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22385 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022386 sto->nbHistory--;
22387 goto deregister_check;
22388 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022389
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022390 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022391 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022392 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022393 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022394 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022395 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022396 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022397 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022398 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022399 sto->nbHistory--;
22400 goto deregister_check;
22401 } else {
22402 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22403 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022404 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022405
22406 /*
22407 * The key will be anchored on the matcher's list of
22408 * key-sequences. The position in this list is determined
22409 * by the target node's depth relative to the matcher's
22410 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022411 *
22412 * Element Depth Pos List-entries
22413 * <scope> 0 NULL
22414 * <bar> 1 NULL
22415 * <target/> 2 2 target
22416 * <bar>
22417 * </scope>
22418 *
22419 * The size of the list is only dependant on the depth of
22420 * the tree.
22421 * An entry will be NULLed in selector_leave, i.e. when
22422 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022423 */
22424 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022425 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022426
22427 /*
22428 * Create/grow the array of key-sequences.
22429 */
22430 if (matcher->keySeqs == NULL) {
22431 if (pos > 9)
22432 matcher->sizeKeySeqs = pos * 2;
22433 else
22434 matcher->sizeKeySeqs = 10;
22435 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22436 xmlMalloc(matcher->sizeKeySeqs *
22437 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22438 if (matcher->keySeqs == NULL) {
22439 xmlSchemaVErrMemory(NULL,
22440 "allocating an array of key-sequences",
22441 NULL);
22442 return(-1);
22443 }
22444 memset(matcher->keySeqs, 0,
22445 matcher->sizeKeySeqs *
22446 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22447 } else if (pos >= matcher->sizeKeySeqs) {
22448 int i = matcher->sizeKeySeqs;
22449
22450 matcher->sizeKeySeqs *= 2;
22451 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22452 xmlRealloc(matcher->keySeqs,
22453 matcher->sizeKeySeqs *
22454 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022455 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022456 xmlSchemaVErrMemory(NULL,
22457 "reallocating an array of key-sequences",
22458 NULL);
22459 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022460 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022461 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022462 * The array needs to be NULLed.
22463 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022464 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022465 for (; i < matcher->sizeKeySeqs; i++)
22466 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022467 }
22468
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022469 /*
22470 * Get/create the key-sequence.
22471 */
22472 keySeq = matcher->keySeqs[pos];
22473 if (keySeq == NULL) {
22474 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022475 } else if (keySeq[idx] != NULL) {
22476 xmlChar *str = NULL;
22477 /*
22478 * cvc-identity-constraint:
22479 * 3 For each node in the ·target node set· all
22480 * of the {fields}, with that node as the context
22481 * node, evaluate to either an empty node-set or
22482 * a node-set with exactly one member, which must
22483 * have a simple type.
22484 *
22485 * The key was already set; report an error.
22486 */
22487 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22488 XML_SCHEMAV_CVC_IDC, NULL,
22489 WXS_BASIC_CAST matcher->aidc->def,
22490 "The XPath '%s' of a field of %s evaluates to a "
22491 "node-set with more than one member",
22492 sto->sel->xpath,
22493 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22494 FREE_AND_NULL(str);
22495 sto->nbHistory--;
22496 goto deregister_check;
22497 } else
22498 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022499
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022500create_sequence:
22501 /*
22502 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022503 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022504 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22505 matcher->aidc->def->nbFields *
22506 sizeof(xmlSchemaPSVIIDCKeyPtr));
22507 if (keySeq == NULL) {
22508 xmlSchemaVErrMemory(NULL,
22509 "allocating an IDC key-sequence", NULL);
22510 return(-1);
22511 }
22512 memset(keySeq, 0, matcher->aidc->def->nbFields *
22513 sizeof(xmlSchemaPSVIIDCKeyPtr));
22514 matcher->keySeqs[pos] = keySeq;
22515create_key:
22516 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022517 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022518 */
22519 if (key == NULL) {
22520 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22521 sizeof(xmlSchemaPSVIIDCKey));
22522 if (key == NULL) {
22523 xmlSchemaVErrMemory(NULL,
22524 "allocating a IDC key", NULL);
22525 xmlFree(keySeq);
22526 matcher->keySeqs[pos] = NULL;
22527 return(-1);
22528 }
22529 /*
22530 * Consume the compiled value.
22531 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022532 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022533 key->val = vctxt->inode->val;
22534 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022535 /*
22536 * Store the key in a global list.
22537 */
22538 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22539 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022540 return (-1);
22541 }
22542 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022543 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022544 }
22545 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022546
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022547 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022548 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022549 xmlSchemaPSVIIDCNodePtr ntItem;
22550 xmlSchemaIDCMatcherPtr matcher;
22551 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022552 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022553 int pos, i, j, nbKeys;
22554 /*
22555 * Here we have the following scenario:
22556 * An IDC 'selector' state object resolved to a target node,
22557 * during the time this target node was in the
22558 * ancestor-or-self axis, the 'field' state object(s) looked
22559 * out for matching nodes to create a key-sequence for this
22560 * target node. Now we are back to this target node and need
22561 * to put the key-sequence, together with the target node
22562 * itself, into the node-table of the corresponding IDC
22563 * binding.
22564 */
22565 matcher = sto->matcher;
22566 idc = matcher->aidc->def;
22567 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022568 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022569 /*
22570 * Check if the matcher has any key-sequences at all, plus
22571 * if it has a key-sequence for the current target node.
22572 */
22573 if ((matcher->keySeqs == NULL) ||
22574 (matcher->sizeKeySeqs <= pos)) {
22575 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22576 goto selector_key_error;
22577 else
22578 goto selector_leave;
22579 }
22580
22581 keySeq = &(matcher->keySeqs[pos]);
22582 if (*keySeq == NULL) {
22583 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22584 goto selector_key_error;
22585 else
22586 goto selector_leave;
22587 }
22588
22589 for (i = 0; i < nbKeys; i++) {
22590 if ((*keySeq)[i] == NULL) {
22591 /*
22592 * Not qualified, if not all fields did resolve.
22593 */
22594 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22595 /*
22596 * All fields of a "key" IDC must resolve.
22597 */
22598 goto selector_key_error;
22599 }
22600 goto selector_leave;
22601 }
22602 }
22603 /*
22604 * All fields did resolve.
22605 */
22606
22607 /*
22608 * 4.1 If the {identity-constraint category} is unique(/key),
22609 * then no two members of the ·qualified node set· have
22610 * ·key-sequences· whose members are pairwise equal, as
22611 * defined by Equal in [XML Schemas: Datatypes].
22612 *
22613 * Get the IDC binding from the matcher and check for
22614 * duplicate key-sequences.
22615 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022616#if 0
22617 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22618#endif
22619 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022620 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022621 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022622 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022623
22624 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022625 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022626 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022627 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022628 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022629 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022630 bkeySeq =
22631 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022632 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022633 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022634 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022635 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022636 if (res == -1) {
22637 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022638 } else if (res == 0) {
22639 /*
22640 * One of the keys differs, so the key-sequence
22641 * won't be equal; get out.
22642 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022643 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022644 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022645 }
22646 if (res == 1) {
22647 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022648 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022649 */
22650 break;
22651 }
22652 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022653 } while (i < targets->nbItems);
22654 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022655 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022656 /*
22657 * TODO: Try to report the key-sequence.
22658 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022659 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022660 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022661 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022662 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022663 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022664 (*keySeq), nbKeys),
22665 xmlSchemaGetIDCDesignation(&strB, idc));
22666 FREE_AND_NULL(str);
22667 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022668 goto selector_leave;
22669 }
22670 }
22671 /*
22672 * Add a node-table item to the IDC binding.
22673 */
22674 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22675 sizeof(xmlSchemaPSVIIDCNode));
22676 if (ntItem == NULL) {
22677 xmlSchemaVErrMemory(NULL,
22678 "allocating an IDC node-table item", NULL);
22679 xmlFree(*keySeq);
22680 *keySeq = NULL;
22681 return(-1);
22682 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022683 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022684
22685 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022686 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022687 */
22688 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22689 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22690 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022691 xmlFree(*keySeq);
22692 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022693 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022694 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022695 ntItem->nodeQNameID = -1;
22696 } else {
22697 /*
22698 * Save a cached QName for this node on the IDC node, to be
22699 * able to report it, even if the node is not saved.
22700 */
22701 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22702 vctxt->inode->localName, vctxt->inode->nsName);
22703 if (ntItem->nodeQNameID == -1) {
22704 xmlFree(ntItem);
22705 xmlFree(*keySeq);
22706 *keySeq = NULL;
22707 return (-1);
22708 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022709 }
22710 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022711 * Init the node-table item: Save the node, position and
22712 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022713 */
22714 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022715 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022716 ntItem->keys = *keySeq;
22717 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022718#if 0
22719 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22720#endif
22721 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022722 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22723 /*
22724 * Free the item, since keyref items won't be
22725 * put on a global list.
22726 */
22727 xmlFree(ntItem->keys);
22728 xmlFree(ntItem);
22729 }
22730 return (-1);
22731 }
22732
22733 goto selector_leave;
22734selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022735 {
22736 xmlChar *str = NULL;
22737 /*
22738 * 4.2.1 (KEY) The ·target node set· and the
22739 * ·qualified node set· are equal, that is, every
22740 * member of the ·target node set· is also a member
22741 * of the ·qualified node set· and vice versa.
22742 */
22743 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22744 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022745 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022746 "Not all fields of %s evaluate to a node",
22747 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22748 FREE_AND_NULL(str);
22749 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022750selector_leave:
22751 /*
22752 * Free the key-sequence if not added to the IDC table.
22753 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022754 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022755 xmlFree(*keySeq);
22756 *keySeq = NULL;
22757 }
22758 } /* if selector */
22759
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022760 sto->nbHistory--;
22761
22762deregister_check:
22763 /*
22764 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022765 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022766 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022767#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022768 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22769 sto->sel->xpath);
22770#endif
22771 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022772 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022773 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022774 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022775 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022776 nextsto = sto->next;
22777 /*
22778 * Unlink from the list of active XPath state objects.
22779 */
22780 vctxt->xpathStates = sto->next;
22781 sto->next = vctxt->xpathStatePool;
22782 /*
22783 * Link it to the pool of reusable state objects.
22784 */
22785 vctxt->xpathStatePool = sto;
22786 sto = nextsto;
22787 } else
22788 sto = sto->next;
22789 } /* while (sto != NULL) */
22790 return (0);
22791}
22792
22793/**
22794 * xmlSchemaIDCRegisterMatchers:
22795 * @vctxt: the WXS validation context
22796 * @elemDecl: the element declaration
22797 *
22798 * Creates helper objects to evaluate IDC selectors/fields
22799 * successively.
22800 *
22801 * Returns 0 if OK and -1 on internal errors.
22802 */
22803static int
22804xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22805 xmlSchemaElementPtr elemDecl)
22806{
22807 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22808 xmlSchemaIDCPtr idc, refIdc;
22809 xmlSchemaIDCAugPtr aidc;
22810
22811 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22812 if (idc == NULL)
22813 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022814
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022815#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022816 {
22817 xmlChar *str = NULL;
22818 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022819 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022820 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22821 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022822 FREE_AND_NULL(str)
22823 }
22824#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022825 if (vctxt->inode->idcMatchers != NULL) {
22826 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22827 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022828 return (-1);
22829 }
22830 do {
22831 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22832 /*
22833 * Since IDCs bubbles are expensive we need to know the
22834 * depth at which the bubbles should stop; this will be
22835 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022836 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022837 * be -1, indicating that no bubbles are needed.
22838 */
22839 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22840 if (refIdc != NULL) {
22841 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022842 * Remember that we have keyrefs on this node.
22843 */
22844 vctxt->inode->hasKeyrefs = 1;
22845 /*
22846 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022847 */
22848 aidc = vctxt->aidcs;
22849 while (aidc != NULL) {
22850 if (aidc->def == refIdc)
22851 break;
22852 aidc = aidc->next;
22853 }
22854 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022855 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022856 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022857 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022858 return (-1);
22859 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022860 if ((aidc->keyrefDepth == -1) ||
22861 (vctxt->depth < aidc->keyrefDepth))
22862 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022863 }
22864 }
22865 /*
22866 * Lookup the augmented IDC item for the IDC definition.
22867 */
22868 aidc = vctxt->aidcs;
22869 while (aidc != NULL) {
22870 if (aidc->def == idc)
22871 break;
22872 aidc = aidc->next;
22873 }
22874 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022875 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22876 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022877 return (-1);
22878 }
22879 /*
22880 * Create an IDC matcher for every IDC definition.
22881 */
22882 matcher = (xmlSchemaIDCMatcherPtr)
22883 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22884 if (matcher == NULL) {
22885 xmlSchemaVErrMemory(vctxt,
22886 "allocating an IDC matcher", NULL);
22887 return (-1);
22888 }
22889 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22890 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022891 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022892 else
22893 last->next = matcher;
22894 last = matcher;
22895
22896 matcher->type = IDC_MATCHER;
22897 matcher->depth = vctxt->depth;
22898 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022899 matcher->idcType = aidc->def->type;
22900#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022901 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22902#endif
22903 /*
22904 * Init the automaton state object.
22905 */
22906 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022907 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022908 return (-1);
22909
22910 idc = idc->next;
22911 } while (idc != NULL);
22912 return (0);
22913}
22914
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022915static int
22916xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22917 xmlSchemaNodeInfoPtr ielem)
22918{
22919 xmlSchemaPSVIIDCBindingPtr bind;
22920 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22921 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22922 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22923
22924 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22925 /* vctxt->createIDCNodeTables */
22926 while (matcher != NULL) {
22927 /*
22928 * Skip keyref IDCs and empty IDC target-lists.
22929 */
22930 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22931 WXS_ILIST_IS_EMPTY(matcher->targets))
22932 {
22933 matcher = matcher->next;
22934 continue;
22935 }
22936 /*
22937 * If we _want_ the IDC node-table to be created in any case
22938 * then do so. Otherwise create them only if keyrefs need them.
22939 */
22940 if ((! vctxt->createIDCNodeTables) &&
22941 ((matcher->aidc->keyrefDepth == -1) ||
22942 (matcher->aidc->keyrefDepth > vctxt->depth)))
22943 {
22944 matcher = matcher->next;
22945 continue;
22946 }
22947 /*
22948 * Get/create the IDC binding on this element for the IDC definition.
22949 */
22950 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22951
22952 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22953 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22954 nbDupls = bind->dupls->nbItems;
22955 } else {
22956 dupls = NULL;
22957 nbDupls = 0;
22958 }
22959 if (bind->nodeTable != NULL) {
22960 nbNodeTable = bind->nbNodes;
22961 } else {
22962 nbNodeTable = 0;
22963 }
22964
22965 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22966 /*
22967 * Transfer all IDC target-nodes to the IDC node-table.
22968 */
22969 bind->nodeTable =
22970 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22971 bind->sizeNodes = matcher->targets->sizeItems;
22972 bind->nbNodes = matcher->targets->nbItems;
22973
22974 matcher->targets->items = NULL;
22975 matcher->targets->sizeItems = 0;
22976 matcher->targets->nbItems = 0;
22977 } else {
22978 /*
22979 * Compare the key-sequences and add to the IDC node-table.
22980 */
22981 nbTargets = matcher->targets->nbItems;
22982 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22983 nbFields = matcher->aidc->def->nbFields;
22984 i = 0;
22985 do {
22986 keys = targets[i]->keys;
22987 if (nbDupls) {
22988 /*
22989 * Search in already found duplicates first.
22990 */
22991 j = 0;
22992 do {
22993 if (nbFields == 1) {
22994 res = xmlSchemaAreValuesEqual(keys[0]->val,
22995 dupls[j]->keys[0]->val);
22996 if (res == -1)
22997 goto internal_error;
22998 if (res == 1) {
22999 /*
23000 * Equal key-sequence.
23001 */
23002 goto next_target;
23003 }
23004 } else {
23005 res = 0;
23006 ntkeys = dupls[j]->keys;
23007 for (k = 0; k < nbFields; k++) {
23008 res = xmlSchemaAreValuesEqual(keys[k]->val,
23009 ntkeys[k]->val);
23010 if (res == -1)
23011 goto internal_error;
23012 if (res == 0) {
23013 /*
23014 * One of the keys differs.
23015 */
23016 break;
23017 }
23018 }
23019 if (res == 1) {
23020 /*
23021 * Equal key-sequence found.
23022 */
23023 goto next_target;
23024 }
23025 }
23026 j++;
23027 } while (j < nbDupls);
23028 }
23029 if (nbNodeTable) {
23030 j = 0;
23031 do {
23032 if (nbFields == 1) {
23033 res = xmlSchemaAreValuesEqual(keys[0]->val,
23034 bind->nodeTable[j]->keys[0]->val);
23035 if (res == -1)
23036 goto internal_error;
23037 if (res == 0) {
23038 /*
23039 * The key-sequence differs.
23040 */
23041 goto next_node_table_entry;
23042 }
23043 } else {
23044 res = 0;
23045 ntkeys = bind->nodeTable[j]->keys;
23046 for (k = 0; k < nbFields; k++) {
23047 res = xmlSchemaAreValuesEqual(keys[k]->val,
23048 ntkeys[k]->val);
23049 if (res == -1)
23050 goto internal_error;
23051 if (res == 0) {
23052 /*
23053 * One of the keys differs.
23054 */
23055 goto next_node_table_entry;
23056 }
23057 }
23058 }
23059 /*
23060 * Add the duplicate to the list of duplicates.
23061 */
23062 if (bind->dupls == NULL) {
23063 bind->dupls = xmlSchemaItemListCreate();
23064 if (bind->dupls == NULL)
23065 goto internal_error;
23066 }
23067 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23068 goto internal_error;
23069 /*
23070 * Remove the duplicate entry from the IDC node-table.
23071 */
23072 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23073 bind->nbNodes--;
23074
23075 goto next_target;
23076
23077next_node_table_entry:
23078 j++;
23079 } while (j < nbNodeTable);
23080 }
23081 /*
23082 * If everything is fine, then add the IDC target-node to
23083 * the IDC node-table.
23084 */
23085 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23086 goto internal_error;
23087
23088next_target:
23089 i++;
23090 } while (i < nbTargets);
23091 }
23092 matcher = matcher->next;
23093 }
23094 return(0);
23095
23096internal_error:
23097 return(-1);
23098}
23099
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023100/**
23101 * xmlSchemaBubbleIDCNodeTables:
23102 * @depth: the current tree depth
23103 *
23104 * Merges IDC bindings of an element at @depth into the corresponding IDC
23105 * bindings of its parent element. If a duplicate note-table entry is found,
23106 * both, the parent node-table entry and child entry are discarded from the
23107 * node-table of the parent.
23108 *
23109 * Returns 0 if OK and -1 on internal errors.
23110 */
23111static int
23112xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23113{
23114 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023115 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23116 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023117 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023118 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023119
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023120 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023121 if (bind == NULL) {
23122 /* Fine, no table, no bubbles. */
23123 return (0);
23124 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023125
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023126 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23127 /*
23128 * Walk all bindings; create new or add to existing bindings.
23129 * Remove duplicate key-sequences.
23130 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023131 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023132
23133 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23134 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023135 /*
23136 * Check if the key/unique IDC table needs to be bubbled.
23137 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023138 if (! vctxt->createIDCNodeTables) {
23139 aidc = vctxt->aidcs;
23140 do {
23141 if (aidc->def == bind->definition) {
23142 if ((aidc->keyrefDepth == -1) ||
23143 (aidc->keyrefDepth >= vctxt->depth)) {
23144 goto next_binding;
23145 }
23146 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023147 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023148 aidc = aidc->next;
23149 } while (aidc != NULL);
23150 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023151
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023152 if (parTable != NULL)
23153 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023154 /*
23155 * Search a matching parent binding for the
23156 * IDC definition.
23157 */
23158 while (parBind != NULL) {
23159 if (parBind->definition == bind->definition)
23160 break;
23161 parBind = parBind->next;
23162 }
23163
23164 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023165 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023166 * Compare every node-table entry of the child node,
23167 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023168 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023169 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023170
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023171 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23172 oldDupls = parBind->dupls->nbItems;
23173 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23174 } else {
23175 dupls = NULL;
23176 oldDupls = 0;
23177 }
23178
23179 parNodes = parBind->nodeTable;
23180 nbFields = bind->definition->nbFields;
23181
23182 for (i = 0; i < bind->nbNodes; i++) {
23183 node = bind->nodeTable[i];
23184 if (node == NULL)
23185 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023186 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023187 * ...with every key-sequence of the parent node, already
23188 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023189 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023190 if (oldDupls) {
23191 j = 0;
23192 while (j < oldDupls) {
23193 if (nbFields == 1) {
23194 ret = xmlSchemaAreValuesEqual(
23195 node->keys[0]->val,
23196 dupls[j]->keys[0]->val);
23197 if (ret == -1)
23198 goto internal_error;
23199 if (ret == 0) {
23200 j++;
23201 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023202 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023203 } else {
23204 parNode = dupls[j];
23205 for (k = 0; k < nbFields; k++) {
23206 ret = xmlSchemaAreValuesEqual(
23207 node->keys[k]->val,
23208 parNode->keys[k]->val);
23209 if (ret == -1)
23210 goto internal_error;
23211 if (ret == 0)
23212 break;
23213 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023214 }
23215 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023216 /* Duplicate found. */
23217 break;
23218 j++;
23219 }
23220 if (j != oldDupls) {
23221 /* Duplicate found. Skip this entry. */
23222 continue;
23223 }
23224 }
23225 /*
23226 * ... and with every key-sequence of the parent node.
23227 */
23228 if (oldNum) {
23229 j = 0;
23230 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023231 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023232 if (nbFields == 1) {
23233 ret = xmlSchemaAreValuesEqual(
23234 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023235 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023236 if (ret == -1)
23237 goto internal_error;
23238 if (ret == 0) {
23239 j++;
23240 continue;
23241 }
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023242 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023243 for (k = 0; k < nbFields; k++) {
23244 ret = xmlSchemaAreValuesEqual(
23245 node->keys[k]->val,
23246 parNode->keys[k]->val);
23247 if (ret == -1)
23248 goto internal_error;
23249 if (ret == 0)
23250 break;
23251 }
23252 }
23253 if (ret == 1)
23254 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023255 break;
23256 j++;
23257 }
23258 if (j != oldNum) {
23259 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023260 * Handle duplicates. Move the duplicate in
23261 * the parent's node-table to the list of
23262 * duplicates.
23263 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023264 oldNum--;
23265 parBind->nbNodes--;
23266 /*
23267 * Move last old item to pos of duplicate.
23268 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023269 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023270
23271 if (parBind->nbNodes != oldNum) {
23272 /*
23273 * If new items exist, move last new item to
23274 * last of old items.
23275 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023276 parNodes[oldNum] =
23277 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023278 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023279 if (parBind->dupls == NULL) {
23280 parBind->dupls = xmlSchemaItemListCreate();
23281 if (parBind->dupls == NULL)
23282 goto internal_error;
23283 }
23284 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023285 } else {
23286 /*
23287 * Add the node-table entry (node and key-sequence) of
23288 * the child node to the node table of the parent node.
23289 */
23290 if (parBind->nodeTable == NULL) {
23291 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023292 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023293 if (parBind->nodeTable == NULL) {
23294 xmlSchemaVErrMemory(NULL,
23295 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023296 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023297 }
23298 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023299 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023300 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023301 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23302 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23303 sizeof(xmlSchemaPSVIIDCNodePtr));
23304 if (parBind->nodeTable == NULL) {
23305 xmlSchemaVErrMemory(NULL,
23306 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023307 goto internal_error;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023308 }
23309 }
23310 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023311 /*
23312 * Append the new node-table entry to the 'new node-table
23313 * entries' section.
23314 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023315 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023316 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023317
23318 }
23319
23320 }
23321 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023322 /*
23323 * No binding for the IDC was found: create a new one and
23324 * copy all node-tables.
23325 */
23326 parBind = xmlSchemaIDCNewBinding(bind->definition);
23327 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023328 goto internal_error;
23329
23330 /*
23331 * TODO: Hmm, how to optimize the initial number of
23332 * allocated entries?
23333 */
23334 if (bind->nbNodes != 0) {
23335 /*
23336 * Add all IDC node-table entries.
23337 */
23338 if (! vctxt->psviExposeIDCNodeTables) {
23339 /*
23340 * Just move the entries.
23341 * NOTE: this is quite save here, since
23342 * all the keyref lookups have already been
23343 * performed.
23344 */
23345 parBind->nodeTable = bind->nodeTable;
23346 bind->nodeTable = NULL;
23347 parBind->sizeNodes = bind->sizeNodes;
23348 bind->sizeNodes = 0;
23349 parBind->nbNodes = bind->nbNodes;
23350 bind->nbNodes = 0;
23351 } else {
23352 /*
23353 * Copy the entries.
23354 */
23355 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23356 xmlMalloc(bind->nbNodes *
23357 sizeof(xmlSchemaPSVIIDCNodePtr));
23358 if (parBind->nodeTable == NULL) {
23359 xmlSchemaVErrMemory(NULL,
23360 "allocating an array of IDC node-table "
23361 "items", NULL);
23362 xmlSchemaIDCFreeBinding(parBind);
23363 goto internal_error;
23364 }
23365 parBind->sizeNodes = bind->nbNodes;
23366 parBind->nbNodes = bind->nbNodes;
23367 memcpy(parBind->nodeTable, bind->nodeTable,
23368 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23369 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023370 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023371 if (bind->dupls) {
23372 /*
23373 * Move the duplicates.
23374 */
23375 if (parBind->dupls != NULL)
23376 xmlSchemaItemListFree(parBind->dupls);
23377 parBind->dupls = bind->dupls;
23378 bind->dupls = NULL;
23379 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023380 if (*parTable == NULL)
23381 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023382 else {
23383 parBind->next = *parTable;
23384 *parTable = parBind;
23385 }
23386 }
23387
23388next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023389 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023390 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023391 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023392
23393internal_error:
23394 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023395}
23396
23397/**
23398 * xmlSchemaCheckCVCIDCKeyRef:
23399 * @vctxt: the WXS validation context
23400 * @elemDecl: the element declaration
23401 *
23402 * Check the cvc-idc-keyref constraints.
23403 */
23404static int
23405xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23406{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023407 xmlSchemaIDCMatcherPtr matcher;
23408 xmlSchemaPSVIIDCBindingPtr bind;
23409
23410 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023411 /*
23412 * Find a keyref.
23413 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023414 while (matcher != NULL) {
23415 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23416 matcher->targets &&
23417 matcher->targets->nbItems)
23418 {
23419 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023420 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023421 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023422
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023423 nbFields = matcher->aidc->def->nbFields;
23424
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023425 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023426 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023427 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023428 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023429 while (bind != NULL) {
23430 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023431 bind->definition)
23432 break;
23433 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023434 }
23435 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023436 /*
23437 * Search for a matching key-sequences.
23438 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023439 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023440 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023441 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023442 if (bind != NULL) {
23443 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023444 for (j = 0; j < bind->nbNodes; j++) {
23445 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023446 for (k = 0; k < nbFields; k++) {
23447 res = xmlSchemaAreValuesEqual(keys[k]->val,
23448 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023449 if (res == 0)
23450 break;
23451 else if (res == -1) {
23452 return (-1);
23453 }
23454 }
23455 if (res == 1) {
23456 /*
23457 * Match found.
23458 */
23459 break;
23460 }
23461 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023462 if ((res == 0) && hasDupls) {
23463 /*
23464 * Search in duplicates
23465 */
23466 for (j = 0; j < bind->dupls->nbItems; j++) {
23467 keys = ((xmlSchemaPSVIIDCNodePtr)
23468 bind->dupls->items[j])->keys;
23469 for (k = 0; k < nbFields; k++) {
23470 res = xmlSchemaAreValuesEqual(keys[k]->val,
23471 refKeys[k]->val);
23472 if (res == 0)
23473 break;
23474 else if (res == -1) {
23475 return (-1);
23476 }
23477 }
23478 if (res == 1) {
23479 /*
23480 * Match in duplicates found.
23481 */
23482 xmlChar *str = NULL, *strB = NULL;
23483 xmlSchemaKeyrefErr(vctxt,
23484 XML_SCHEMAV_CVC_IDC, refNode,
23485 (xmlSchemaTypePtr) matcher->aidc->def,
23486 "More than one match found for "
23487 "key-sequence %s of keyref '%s'",
23488 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23489 refNode->keys, nbFields),
23490 xmlSchemaGetComponentQName(&strB,
23491 matcher->aidc->def));
23492 FREE_AND_NULL(str);
23493 FREE_AND_NULL(strB);
23494 break;
23495 }
23496 }
23497 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023498 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023499
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023500 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023501 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023502 xmlSchemaKeyrefErr(vctxt,
23503 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023504 (xmlSchemaTypePtr) matcher->aidc->def,
23505 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023506 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023507 refNode->keys, nbFields),
23508 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023509 FREE_AND_NULL(str);
23510 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023511 }
23512 }
23513 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023514 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023515 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023516 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023517 return (0);
23518}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023519
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023520/************************************************************************
23521 * *
23522 * XML Reader validation code *
23523 * *
23524 ************************************************************************/
23525
23526static xmlSchemaAttrInfoPtr
23527xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023528{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023529 xmlSchemaAttrInfoPtr iattr;
23530 /*
23531 * Grow/create list of attribute infos.
23532 */
23533 if (vctxt->attrInfos == NULL) {
23534 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23535 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23536 vctxt->sizeAttrInfos = 1;
23537 if (vctxt->attrInfos == NULL) {
23538 xmlSchemaVErrMemory(vctxt,
23539 "allocating attribute info list", NULL);
23540 return (NULL);
23541 }
23542 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23543 vctxt->sizeAttrInfos++;
23544 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23545 xmlRealloc(vctxt->attrInfos,
23546 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23547 if (vctxt->attrInfos == NULL) {
23548 xmlSchemaVErrMemory(vctxt,
23549 "re-allocating attribute info list", NULL);
23550 return (NULL);
23551 }
23552 } else {
23553 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23554 if (iattr->localName != NULL) {
23555 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23556 "attr info not cleared");
23557 return (NULL);
23558 }
23559 iattr->nodeType = XML_ATTRIBUTE_NODE;
23560 return (iattr);
23561 }
23562 /*
23563 * Create an attribute info.
23564 */
23565 iattr = (xmlSchemaAttrInfoPtr)
23566 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23567 if (iattr == NULL) {
23568 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23569 return (NULL);
23570 }
23571 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23572 iattr->nodeType = XML_ATTRIBUTE_NODE;
23573 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23574
23575 return (iattr);
23576}
23577
23578static int
23579xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23580 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023581 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023582 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023583 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023584 int ownedNames,
23585 xmlChar *value,
23586 int ownedValue)
23587{
23588 xmlSchemaAttrInfoPtr attr;
23589
23590 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23591 if (attr == NULL) {
23592 VERROR_INT("xmlSchemaPushAttribute",
23593 "calling xmlSchemaGetFreshAttrInfo()");
23594 return (-1);
23595 }
23596 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023597 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023598 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23599 attr->localName = localName;
23600 attr->nsName = nsName;
23601 if (ownedNames)
23602 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23603 /*
23604 * Evaluate if it's an XSI attribute.
23605 */
23606 if (nsName != NULL) {
23607 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23608 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23609 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23610 }
23611 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23612 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23613 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23614 }
23615 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23616 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23617 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23618 }
23619 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23620 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23621 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23622 }
23623 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23624 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23625 }
23626 }
23627 attr->value = value;
23628 if (ownedValue)
23629 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23630 if (attr->metaType != 0)
23631 attr->state = XML_SCHEMAS_ATTR_META;
23632 return (0);
23633}
23634
23635static void
23636xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23637{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023638 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023639 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023640 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23641 FREE_AND_NULL(ielem->localName);
23642 FREE_AND_NULL(ielem->nsName);
23643 } else {
23644 ielem->localName = NULL;
23645 ielem->nsName = NULL;
23646 }
23647 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23648 FREE_AND_NULL(ielem->value);
23649 } else {
23650 ielem->value = NULL;
23651 }
23652 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023653 /*
23654 * PSVI TODO: Be careful not to free it when the value is
23655 * exposed via PSVI.
23656 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023657 xmlSchemaFreeValue(ielem->val);
23658 ielem->val = NULL;
23659 }
23660 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023661 /*
23662 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23663 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023664 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23665 ielem->idcMatchers = NULL;
23666 }
23667 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023668 /*
23669 * OPTIMIZE TODO: Use a pool of IDC tables??.
23670 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023671 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23672 ielem->idcTable = NULL;
23673 }
23674 if (ielem->regexCtxt != NULL) {
23675 xmlRegFreeExecCtxt(ielem->regexCtxt);
23676 ielem->regexCtxt = NULL;
23677 }
23678 if (ielem->nsBindings != NULL) {
23679 xmlFree((xmlChar **)ielem->nsBindings);
23680 ielem->nsBindings = NULL;
23681 ielem->nbNsBindings = 0;
23682 ielem->sizeNsBindings = 0;
23683 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023684}
23685
23686/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023687 * xmlSchemaGetFreshElemInfo:
23688 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023689 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023690 * Creates/reuses and initializes the element info item for
23691 * the currect tree depth.
23692 *
23693 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023694 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023695static xmlSchemaNodeInfoPtr
23696xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023697{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023698 xmlSchemaNodeInfoPtr info = NULL;
23699
23700 if (vctxt->depth > vctxt->sizeElemInfos) {
23701 VERROR_INT("xmlSchemaGetFreshElemInfo",
23702 "inconsistent depth encountered");
23703 return (NULL);
23704 }
23705 if (vctxt->elemInfos == NULL) {
23706 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23707 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23708 if (vctxt->elemInfos == NULL) {
23709 xmlSchemaVErrMemory(vctxt,
23710 "allocating the element info array", NULL);
23711 return (NULL);
23712 }
23713 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23714 vctxt->sizeElemInfos = 10;
23715 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23716 int i = vctxt->sizeElemInfos;
23717
23718 vctxt->sizeElemInfos *= 2;
23719 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23720 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23721 sizeof(xmlSchemaNodeInfoPtr));
23722 if (vctxt->elemInfos == NULL) {
23723 xmlSchemaVErrMemory(vctxt,
23724 "re-allocating the element info array", NULL);
23725 return (NULL);
23726 }
23727 /*
23728 * We need the new memory to be NULLed.
23729 * TODO: Use memset instead?
23730 */
23731 for (; i < vctxt->sizeElemInfos; i++)
23732 vctxt->elemInfos[i] = NULL;
23733 } else
23734 info = vctxt->elemInfos[vctxt->depth];
23735
23736 if (info == NULL) {
23737 info = (xmlSchemaNodeInfoPtr)
23738 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23739 if (info == NULL) {
23740 xmlSchemaVErrMemory(vctxt,
23741 "allocating an element info", NULL);
23742 return (NULL);
23743 }
23744 vctxt->elemInfos[vctxt->depth] = info;
23745 } else {
23746 if (info->localName != NULL) {
23747 VERROR_INT("xmlSchemaGetFreshElemInfo",
23748 "elem info has not been cleared");
23749 return (NULL);
23750 }
23751 }
23752 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23753 info->nodeType = XML_ELEMENT_NODE;
23754 info->depth = vctxt->depth;
23755
23756 return (info);
23757}
23758
23759#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23760#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23761#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23762
23763static int
23764xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23765 xmlNodePtr node,
23766 xmlSchemaTypePtr type,
23767 xmlSchemaValType valType,
23768 const xmlChar * value,
23769 xmlSchemaValPtr val,
23770 unsigned long length,
23771 int fireErrors)
23772{
23773 int ret, error = 0;
23774
23775 xmlSchemaTypePtr tmpType;
23776 xmlSchemaFacetLinkPtr facetLink;
23777 xmlSchemaFacetPtr facet;
23778 unsigned long len = 0;
23779 xmlSchemaWhitespaceValueType ws;
23780
23781 /*
23782 * In Libxml2, derived built-in types have currently no explicit facets.
23783 */
23784 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023785 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023786
23787 /*
23788 * NOTE: Do not jump away, if the facetSet of the given type is
23789 * empty: until now, "pattern" and "enumeration" facets of the
23790 * *base types* need to be checked as well.
23791 */
23792 if (type->facetSet == NULL)
23793 goto pattern_and_enum;
23794
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023795 if (! WXS_IS_ATOMIC(type)) {
23796 if (WXS_IS_LIST(type))
23797 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023798 else
23799 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023800 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023801 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023802 * Whitespace handling is only of importance for string-based
23803 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023804 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023805 tmpType = xmlSchemaGetPrimitiveType(type);
23806 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023807 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023808 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23809 } else
23810 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23811 /*
23812 * If the value was not computed (for string or
23813 * anySimpleType based types), then use the provided
23814 * type.
23815 */
23816 if (val == NULL)
23817 valType = valType;
23818 else
23819 valType = xmlSchemaGetValType(val);
23820
23821 ret = 0;
23822 for (facetLink = type->facetSet; facetLink != NULL;
23823 facetLink = facetLink->next) {
23824 /*
23825 * Skip the pattern "whiteSpace": it is used to
23826 * format the character content beforehand.
23827 */
23828 switch (facetLink->facet->type) {
23829 case XML_SCHEMA_FACET_WHITESPACE:
23830 case XML_SCHEMA_FACET_PATTERN:
23831 case XML_SCHEMA_FACET_ENUMERATION:
23832 continue;
23833 case XML_SCHEMA_FACET_LENGTH:
23834 case XML_SCHEMA_FACET_MINLENGTH:
23835 case XML_SCHEMA_FACET_MAXLENGTH:
23836 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23837 valType, value, val, &len, ws);
23838 break;
23839 default:
23840 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23841 valType, value, val, ws);
23842 break;
23843 }
23844 if (ret < 0) {
23845 AERROR_INT("xmlSchemaValidateFacets",
23846 "validating against a atomic type facet");
23847 return (-1);
23848 } else if (ret > 0) {
23849 if (fireErrors)
23850 xmlSchemaFacetErr(actxt, ret, node,
23851 value, len, type, facetLink->facet, NULL, NULL, NULL);
23852 else
23853 return (ret);
23854 if (error == 0)
23855 error = ret;
23856 }
23857 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023858 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023859
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023860WXS_IS_LIST:
23861 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023862 goto pattern_and_enum;
23863 /*
23864 * "length", "minLength" and "maxLength" of list types.
23865 */
23866 ret = 0;
23867 for (facetLink = type->facetSet; facetLink != NULL;
23868 facetLink = facetLink->next) {
23869
23870 switch (facetLink->facet->type) {
23871 case XML_SCHEMA_FACET_LENGTH:
23872 case XML_SCHEMA_FACET_MINLENGTH:
23873 case XML_SCHEMA_FACET_MAXLENGTH:
23874 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23875 value, length, NULL);
23876 break;
23877 default:
23878 continue;
23879 }
23880 if (ret < 0) {
23881 AERROR_INT("xmlSchemaValidateFacets",
23882 "validating against a list type facet");
23883 return (-1);
23884 } else if (ret > 0) {
23885 if (fireErrors)
23886 xmlSchemaFacetErr(actxt, ret, node,
23887 value, length, type, facetLink->facet, NULL, NULL, NULL);
23888 else
23889 return (ret);
23890 if (error == 0)
23891 error = ret;
23892 }
23893 ret = 0;
23894 }
23895
23896pattern_and_enum:
23897 if (error >= 0) {
23898 int found = 0;
23899 /*
23900 * Process enumerations. Facet values are in the value space
23901 * of the defining type's base type. This seems to be a bug in the
23902 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23903 * Only the first set of enumerations in the ancestor-or-self axis
23904 * is used for validation.
23905 */
23906 ret = 0;
23907 tmpType = type;
23908 do {
23909 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23910 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23911 continue;
23912 found = 1;
23913 ret = xmlSchemaAreValuesEqual(facet->val, val);
23914 if (ret == 1)
23915 break;
23916 else if (ret < 0) {
23917 AERROR_INT("xmlSchemaValidateFacets",
23918 "validating against an enumeration facet");
23919 return (-1);
23920 }
23921 }
23922 if (ret != 0)
23923 break;
23924 tmpType = tmpType->baseType;
23925 } while ((tmpType != NULL) &&
23926 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23927 if (found && (ret == 0)) {
23928 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23929 if (fireErrors) {
23930 xmlSchemaFacetErr(actxt, ret, node,
23931 value, 0, type, NULL, NULL, NULL, NULL);
23932 } else
23933 return (ret);
23934 if (error == 0)
23935 error = ret;
23936 }
23937 }
23938
23939 if (error >= 0) {
23940 int found;
23941 /*
23942 * Process patters. Pattern facets are ORed at type level
23943 * and ANDed if derived. Walk the base type axis.
23944 */
23945 tmpType = type;
23946 facet = NULL;
23947 do {
23948 found = 0;
23949 for (facetLink = tmpType->facetSet; facetLink != NULL;
23950 facetLink = facetLink->next) {
23951 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23952 continue;
23953 found = 1;
23954 /*
23955 * NOTE that for patterns, @value needs to be the
23956 * normalized vaule.
23957 */
23958 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23959 if (ret == 1)
23960 break;
23961 else if (ret < 0) {
23962 AERROR_INT("xmlSchemaValidateFacets",
23963 "validating against a pattern facet");
23964 return (-1);
23965 } else {
23966 /*
23967 * Save the last non-validating facet.
23968 */
23969 facet = facetLink->facet;
23970 }
23971 }
23972 if (found && (ret != 1)) {
23973 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23974 if (fireErrors) {
23975 xmlSchemaFacetErr(actxt, ret, node,
23976 value, 0, type, facet, NULL, NULL, NULL);
23977 } else
23978 return (ret);
23979 if (error == 0)
23980 error = ret;
23981 break;
23982 }
23983 tmpType = tmpType->baseType;
23984 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23985 }
23986
23987 return (error);
23988}
23989
23990static xmlChar *
23991xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23992 const xmlChar *value)
23993{
23994 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23995 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23996 return (xmlSchemaCollapseString(value));
23997 case XML_SCHEMA_WHITESPACE_REPLACE:
23998 return (xmlSchemaWhiteSpaceReplace(value));
23999 default:
24000 return (NULL);
24001 }
24002}
24003
24004static int
24005xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
24006 const xmlChar *value,
24007 xmlSchemaValPtr *val,
24008 int valNeeded)
24009{
24010 int ret;
24011 const xmlChar *nsName;
24012 xmlChar *local, *prefix = NULL;
24013
24014 ret = xmlValidateQName(value, 1);
24015 if (ret != 0) {
24016 if (ret == -1) {
24017 VERROR_INT("xmlSchemaValidateQName",
24018 "calling xmlValidateQName()");
24019 return (-1);
24020 }
24021 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
24022 }
24023 /*
24024 * NOTE: xmlSplitQName2 will always return a duplicated
24025 * strings.
24026 */
24027 local = xmlSplitQName2(value, &prefix);
24028 if (local == NULL)
24029 local = xmlStrdup(value);
24030 /*
24031 * OPTIMIZE TODO: Use flags for:
24032 * - is there any namespace binding?
24033 * - is there a default namespace?
24034 */
24035 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24036
24037 if (prefix != NULL) {
24038 xmlFree(prefix);
24039 /*
24040 * A namespace must be found if the prefix is
24041 * NOT NULL.
24042 */
24043 if (nsName == NULL) {
24044 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024045 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024046 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024047 "The QName value '%s' has no "
24048 "corresponding namespace declaration in "
24049 "scope", value, NULL);
24050 if (local != NULL)
24051 xmlFree(local);
24052 return (ret);
24053 }
24054 }
24055 if (valNeeded && val) {
24056 if (nsName != NULL)
24057 *val = xmlSchemaNewQNameValue(
24058 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24059 else
24060 *val = xmlSchemaNewQNameValue(NULL,
24061 BAD_CAST local);
24062 } else
24063 xmlFree(local);
24064 return (0);
24065}
24066
24067/*
24068* cvc-simple-type
24069*/
24070static int
24071xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24072 xmlNodePtr node,
24073 xmlSchemaTypePtr type,
24074 const xmlChar *value,
24075 xmlSchemaValPtr *retVal,
24076 int fireErrors,
24077 int normalize,
24078 int isNormalized)
24079{
24080 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24081 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024082 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024083 xmlChar *normValue = NULL;
24084
24085#define NORMALIZE(atype) \
24086 if ((! isNormalized) && \
24087 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24088 normValue = xmlSchemaNormalizeValue(atype, value); \
24089 if (normValue != NULL) \
24090 value = normValue; \
24091 isNormalized = 1; \
24092 }
24093
24094 if ((retVal != NULL) && (*retVal != NULL)) {
24095 xmlSchemaFreeValue(*retVal);
24096 *retVal = NULL;
24097 }
24098 /*
24099 * 3.14.4 Simple Type Definition Validation Rules
24100 * Validation Rule: String Valid
24101 */
24102 /*
24103 * 1 It is schema-valid with respect to that definition as defined
24104 * by Datatype Valid in [XML Schemas: Datatypes].
24105 */
24106 /*
24107 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
24108 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
24109 * the string must be a ·declared entity name·.
24110 */
24111 /*
24112 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
24113 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24114 * then every whitespace-delimited substring of the string must be a ·declared
24115 * entity name·.
24116 */
24117 /*
24118 * 2.3 otherwise no further condition applies.
24119 */
24120 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24121 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024122 if (value == NULL)
24123 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024124 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024125 xmlSchemaTypePtr biType; /* The built-in type. */
24126 /*
24127 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24128 * a literal in the ·lexical space· of {base type definition}"
24129 */
24130 /*
24131 * Whitespace-normalize.
24132 */
24133 NORMALIZE(type);
24134 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24135 /*
24136 * Get the built-in type.
24137 */
24138 biType = type->baseType;
24139 while ((biType != NULL) &&
24140 (biType->type != XML_SCHEMA_TYPE_BASIC))
24141 biType = biType->baseType;
24142
24143 if (biType == NULL) {
24144 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24145 "could not get the built-in type");
24146 goto internal_error;
24147 }
24148 } else
24149 biType = type;
24150 /*
24151 * NOTATIONs need to be processed here, since they need
24152 * to lookup in the hashtable of NOTATION declarations of the schema.
24153 */
24154 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24155 switch (biType->builtInType) {
24156 case XML_SCHEMAS_NOTATION:
24157 ret = xmlSchemaValidateNotation(
24158 (xmlSchemaValidCtxtPtr) actxt,
24159 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24160 NULL, value, &val, valNeeded);
24161 break;
24162 case XML_SCHEMAS_QNAME:
24163 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24164 value, &val, valNeeded);
24165 break;
24166 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024167 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024168 if (valNeeded)
24169 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24170 value, &val, NULL);
24171 else
24172 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24173 value, NULL, NULL);
24174 break;
24175 }
24176 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24177 switch (biType->builtInType) {
24178 case XML_SCHEMAS_NOTATION:
24179 ret = xmlSchemaValidateNotation(NULL,
24180 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24181 value, &val, valNeeded);
24182 break;
24183 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024184 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024185 if (valNeeded)
24186 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24187 value, &val, node);
24188 else
24189 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24190 value, NULL, node);
24191 break;
24192 }
24193 } else {
24194 /*
24195 * Validation via a public API is not implemented yet.
24196 */
24197 TODO
24198 goto internal_error;
24199 }
24200 if (ret != 0) {
24201 if (ret < 0) {
24202 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24203 "validating against a built-in type");
24204 goto internal_error;
24205 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024206 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024207 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24208 else
24209 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24210 }
24211 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24212 /*
24213 * Check facets.
24214 */
24215 ret = xmlSchemaValidateFacets(actxt, node, type,
24216 (xmlSchemaValType) biType->builtInType, value, val,
24217 0, fireErrors);
24218 if (ret != 0) {
24219 if (ret < 0) {
24220 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24221 "validating facets of atomic simple type");
24222 goto internal_error;
24223 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024224 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024225 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24226 else
24227 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24228 }
24229 }
24230 if (fireErrors && (ret > 0))
24231 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024232 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024233
24234 xmlSchemaTypePtr itemType;
24235 const xmlChar *cur, *end;
24236 xmlChar *tmpValue = NULL;
24237 unsigned long len = 0;
24238 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24239 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24240 * of white space separated tokens, each of which ·match·es a literal
24241 * in the ·lexical space· of {item type definition}
24242 */
24243 /*
24244 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24245 * the list type has an enum or pattern facet.
24246 */
24247 NORMALIZE(type);
24248 /*
24249 * VAL TODO: Optimize validation of empty values.
24250 * VAL TODO: We do not have computed values for lists.
24251 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024252 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024253 cur = value;
24254 do {
24255 while (IS_BLANK_CH(*cur))
24256 cur++;
24257 end = cur;
24258 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24259 end++;
24260 if (end == cur)
24261 break;
24262 tmpValue = xmlStrndup(cur, end - cur);
24263 len++;
24264
24265 if (valNeeded)
24266 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24267 tmpValue, &curVal, fireErrors, 0, 1);
24268 else
24269 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24270 tmpValue, NULL, fireErrors, 0, 1);
24271 FREE_AND_NULL(tmpValue);
24272 if (curVal != NULL) {
24273 /*
24274 * Add to list of computed values.
24275 */
24276 if (val == NULL)
24277 val = curVal;
24278 else
24279 xmlSchemaValueAppend(prevVal, curVal);
24280 prevVal = curVal;
24281 curVal = NULL;
24282 }
24283 if (ret != 0) {
24284 if (ret < 0) {
24285 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24286 "validating an item of list simple type");
24287 goto internal_error;
24288 }
24289 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24290 break;
24291 }
24292 cur = end;
24293 } while (*cur != 0);
24294 FREE_AND_NULL(tmpValue);
24295 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24296 /*
24297 * Apply facets (pattern, enumeration).
24298 */
24299 ret = xmlSchemaValidateFacets(actxt, node, type,
24300 XML_SCHEMAS_UNKNOWN, value, val,
24301 len, fireErrors);
24302 if (ret != 0) {
24303 if (ret < 0) {
24304 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24305 "validating facets of list simple type");
24306 goto internal_error;
24307 }
24308 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24309 }
24310 }
24311 if (fireErrors && (ret > 0)) {
24312 /*
24313 * Report the normalized value.
24314 */
24315 normalize = 1;
24316 NORMALIZE(type);
24317 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24318 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024319 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024320 xmlSchemaTypeLinkPtr memberLink;
24321 /*
24322 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24323 * not apply directly; however, the normalization behavior of ·union·
24324 * types is controlled by the value of whiteSpace on that one of the
24325 * ·memberTypes· against which the ·union· is successfully validated.
24326 *
24327 * This means that the value is normalized by the first validating
24328 * member type, then the facets of the union type are applied. This
24329 * needs changing of the value!
24330 */
24331
24332 /*
24333 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24334 * literal in the ·lexical space· of at least one member of
24335 * {member type definitions}
24336 */
24337 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24338 if (memberLink == NULL) {
24339 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24340 "union simple type has no member types");
24341 goto internal_error;
24342 }
24343 /*
24344 * Always normalize union type values, since we currently
24345 * cannot store the whitespace information with the value
24346 * itself; otherwise a later value-comparison would be
24347 * not possible.
24348 */
24349 while (memberLink != NULL) {
24350 if (valNeeded)
24351 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24352 memberLink->type, value, &val, 0, 1, 0);
24353 else
24354 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24355 memberLink->type, value, NULL, 0, 1, 0);
24356 if (ret <= 0)
24357 break;
24358 memberLink = memberLink->next;
24359 }
24360 if (ret != 0) {
24361 if (ret < 0) {
24362 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24363 "validating members of union simple type");
24364 goto internal_error;
24365 }
24366 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24367 }
24368 /*
24369 * Apply facets (pattern, enumeration).
24370 */
24371 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24372 /*
24373 * The normalization behavior of ·union· types is controlled by
24374 * the value of whiteSpace on that one of the ·memberTypes·
24375 * against which the ·union· is successfully validated.
24376 */
24377 NORMALIZE(memberLink->type);
24378 ret = xmlSchemaValidateFacets(actxt, node, type,
24379 XML_SCHEMAS_UNKNOWN, value, val,
24380 0, fireErrors);
24381 if (ret != 0) {
24382 if (ret < 0) {
24383 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24384 "validating facets of union simple type");
24385 goto internal_error;
24386 }
24387 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24388 }
24389 }
24390 if (fireErrors && (ret > 0))
24391 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24392 }
24393
24394 if (normValue != NULL)
24395 xmlFree(normValue);
24396 if (ret == 0) {
24397 if (retVal != NULL)
24398 *retVal = val;
24399 else if (val != NULL)
24400 xmlSchemaFreeValue(val);
24401 } else if (val != NULL)
24402 xmlSchemaFreeValue(val);
24403 return (ret);
24404internal_error:
24405 if (normValue != NULL)
24406 xmlFree(normValue);
24407 if (val != NULL)
24408 xmlSchemaFreeValue(val);
24409 return (-1);
24410}
24411
24412static int
24413xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24414 const xmlChar *value,
24415 const xmlChar **nsName,
24416 const xmlChar **localName)
24417{
24418 int ret = 0;
24419
24420 if ((nsName == NULL) || (localName == NULL))
24421 return (-1);
24422 *nsName = NULL;
24423 *localName = NULL;
24424
24425 ret = xmlValidateQName(value, 1);
24426 if (ret == -1)
24427 return (-1);
24428 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024429 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024430 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24431 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24432 return (1);
24433 }
24434 {
24435 xmlChar *local = NULL;
24436 xmlChar *prefix;
24437
24438 /*
24439 * NOTE: xmlSplitQName2 will return a duplicated
24440 * string.
24441 */
24442 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024443 if (local == NULL)
24444 *localName = xmlDictLookup(vctxt->dict, value, -1);
24445 else {
24446 *localName = xmlDictLookup(vctxt->dict, local, -1);
24447 xmlFree(local);
24448 }
24449
24450 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24451
24452 if (prefix != NULL) {
24453 xmlFree(prefix);
24454 /*
24455 * A namespace must be found if the prefix is NOT NULL.
24456 */
24457 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024458 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024459 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024460 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024461 "The QName value '%s' has no "
24462 "corresponding namespace declaration in scope",
24463 value, NULL);
24464 return (2);
24465 }
24466 }
24467 }
24468 return (0);
24469}
24470
24471static int
24472xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24473 xmlSchemaAttrInfoPtr iattr,
24474 xmlSchemaTypePtr *localType,
24475 xmlSchemaElementPtr elemDecl)
24476{
24477 int ret = 0;
24478 /*
24479 * cvc-elt (3.3.4) : (4)
24480 * AND
24481 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24482 * (1.2.1.2.1) - (1.2.1.2.4)
24483 * Handle 'xsi:type'.
24484 */
24485 if (localType == NULL)
24486 return (-1);
24487 *localType = NULL;
24488 if (iattr == NULL)
24489 return (0);
24490 else {
24491 const xmlChar *nsName = NULL, *local = NULL;
24492 /*
24493 * TODO: We should report a *warning* that the type was overriden
24494 * by the instance.
24495 */
24496 ACTIVATE_ATTRIBUTE(iattr);
24497 /*
24498 * (cvc-elt) (3.3.4) : (4.1)
24499 * (cvc-assess-elt) (1.2.1.2.2)
24500 */
24501 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24502 &nsName, &local);
24503 if (ret != 0) {
24504 if (ret < 0) {
24505 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24506 "calling xmlSchemaQNameExpand() to validate the "
24507 "attribute 'xsi:type'");
24508 goto internal_error;
24509 }
24510 goto exit;
24511 }
24512 /*
24513 * (cvc-elt) (3.3.4) : (4.2)
24514 * (cvc-assess-elt) (1.2.1.2.3)
24515 */
24516 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24517 if (*localType == NULL) {
24518 xmlChar *str = NULL;
24519
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024520 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024521 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024522 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024523 "The QName value '%s' of the xsi:type attribute does not "
24524 "resolve to a type definition",
24525 xmlSchemaFormatQName(&str, nsName, local), NULL);
24526 FREE_AND_NULL(str);
24527 ret = vctxt->err;
24528 goto exit;
24529 }
24530 if (elemDecl != NULL) {
24531 int set = 0;
24532
24533 /*
24534 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24535 * "The ·local type definition· must be validly
24536 * derived from the {type definition} given the union of
24537 * the {disallowed substitutions} and the {type definition}'s
24538 * {prohibited substitutions}, as defined in
24539 * Type Derivation OK (Complex) (§3.4.6)
24540 * (if it is a complex type definition),
24541 * or given {disallowed substitutions} as defined in Type
24542 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24543 * definition)."
24544 *
24545 * {disallowed substitutions}: the "block" on the element decl.
24546 * {prohibited substitutions}: the "block" on the type def.
24547 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024548 /*
24549 * OPTIMIZE TODO: We could map types already evaluated
24550 * to be validly derived from other types to avoid checking
24551 * this over and over for the same types.
24552 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024553 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24554 (elemDecl->subtypes->flags &
24555 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24556 set |= SUBSET_EXTENSION;
24557
24558 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24559 (elemDecl->subtypes->flags &
24560 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24561 set |= SUBSET_RESTRICTION;
24562
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024563 /*
24564 * REMOVED and CHANGED since this produced a parser context
24565 * which adds to the string dict of the schema. So this would
24566 * change the schema and we don't want this. We don't need
24567 * the parser context anymore.
24568 *
24569 * if ((vctxt->pctxt == NULL) &&
24570 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24571 * return (-1);
24572 */
24573
24574 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024575 elemDecl->subtypes, set) != 0) {
24576 xmlChar *str = NULL;
24577
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024578 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024579 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24580 "The type definition '%s', specified by xsi:type, is "
24581 "blocked or not validly derived from the type definition "
24582 "of the element declaration",
24583 xmlSchemaFormatQName(&str,
24584 (*localType)->targetNamespace,
24585 (*localType)->name),
24586 NULL);
24587 FREE_AND_NULL(str);
24588 ret = vctxt->err;
24589 *localType = NULL;
24590 }
24591 }
24592 }
24593exit:
24594 ACTIVATE_ELEM;
24595 return (ret);
24596internal_error:
24597 ACTIVATE_ELEM;
24598 return (-1);
24599}
24600
24601static int
24602xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24603{
24604 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Daniel Veillard14b56432006-03-09 18:41:40 +000024605 xmlSchemaTypePtr actualType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024606
24607 /*
24608 * cvc-elt (3.3.4) : 1
24609 */
24610 if (elemDecl == NULL) {
24611 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24612 "No matching declaration available");
24613 return (vctxt->err);
24614 }
Daniel Veillard14b56432006-03-09 18:41:40 +000024615 actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024616 /*
24617 * cvc-elt (3.3.4) : 2
24618 */
24619 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24620 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24621 "The element declaration is abstract");
24622 return (vctxt->err);
24623 }
24624 if (actualType == NULL) {
24625 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24626 "The type definition is absent");
24627 return (XML_SCHEMAV_CVC_TYPE_1);
24628 }
24629 if (vctxt->nbAttrInfos != 0) {
24630 int ret;
24631 xmlSchemaAttrInfoPtr iattr;
24632 /*
24633 * cvc-elt (3.3.4) : 3
24634 * Handle 'xsi:nil'.
24635 */
24636 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24637 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24638 if (iattr) {
24639 ACTIVATE_ATTRIBUTE(iattr);
24640 /*
24641 * Validate the value.
24642 */
24643 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024644 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024645 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24646 iattr->value, &(iattr->val), 1, 0, 0);
24647 ACTIVATE_ELEM;
24648 if (ret < 0) {
24649 VERROR_INT("xmlSchemaValidateElemDecl",
24650 "calling xmlSchemaVCheckCVCSimpleType() to "
24651 "validate the attribute 'xsi:nil'");
24652 return (-1);
24653 }
24654 if (ret == 0) {
24655 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24656 /*
24657 * cvc-elt (3.3.4) : 3.1
24658 */
24659 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24660 "The element is not 'nillable'");
24661 /* Does not return an error on purpose. */
24662 } else {
24663 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24664 /*
24665 * cvc-elt (3.3.4) : 3.2.2
24666 */
24667 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24668 (elemDecl->value != NULL)) {
24669 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24670 "The element cannot be 'nilled' because "
24671 "there is a fixed value constraint defined "
24672 "for it");
24673 /* Does not return an error on purpose. */
24674 } else
24675 vctxt->inode->flags |=
24676 XML_SCHEMA_ELEM_INFO_NILLED;
24677 }
24678 }
24679 }
24680 }
24681 /*
24682 * cvc-elt (3.3.4) : 4
24683 * Handle 'xsi:type'.
24684 */
24685 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24686 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24687 if (iattr) {
24688 xmlSchemaTypePtr localType = NULL;
24689
24690 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24691 elemDecl);
24692 if (ret != 0) {
24693 if (ret == -1) {
24694 VERROR_INT("xmlSchemaValidateElemDecl",
24695 "calling xmlSchemaProcessXSIType() to "
24696 "process the attribute 'xsi:type'");
24697 return (-1);
24698 }
24699 /* Does not return an error on purpose. */
24700 }
24701 if (localType != NULL) {
24702 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24703 actualType = localType;
24704 }
24705 }
24706 }
24707 /*
24708 * IDC: Register identity-constraint XPath matchers.
24709 */
24710 if ((elemDecl->idcs != NULL) &&
24711 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24712 return (-1);
24713 /*
24714 * No actual type definition.
24715 */
24716 if (actualType == NULL) {
24717 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24718 "The type definition is absent");
24719 return (XML_SCHEMAV_CVC_TYPE_1);
24720 }
24721 /*
24722 * Remember the actual type definition.
24723 */
24724 vctxt->inode->typeDef = actualType;
24725
24726 return (0);
24727}
24728
24729static int
24730xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24731{
24732 xmlSchemaAttrInfoPtr iattr;
24733 int ret = 0, i;
24734
24735 /*
24736 * SPEC cvc-type (3.1.1)
24737 * "The attributes of must be empty, excepting those whose namespace
24738 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24739 * whose local name is one of type, nil, schemaLocation or
24740 * noNamespaceSchemaLocation."
24741 */
24742 if (vctxt->nbAttrInfos == 0)
24743 return (0);
24744 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24745 iattr = vctxt->attrInfos[i];
24746 if (! iattr->metaType) {
24747 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024748 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024749 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24750 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24751 }
24752 }
24753 ACTIVATE_ELEM
24754 return (ret);
24755}
24756
24757/*
24758* Cleanup currently used attribute infos.
24759*/
24760static void
24761xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24762{
24763 int i;
24764 xmlSchemaAttrInfoPtr attr;
24765
24766 if (vctxt->nbAttrInfos == 0)
24767 return;
24768 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24769 attr = vctxt->attrInfos[i];
24770 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24771 if (attr->localName != NULL)
24772 xmlFree((xmlChar *) attr->localName);
24773 if (attr->nsName != NULL)
24774 xmlFree((xmlChar *) attr->nsName);
24775 }
24776 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24777 if (attr->value != NULL)
24778 xmlFree((xmlChar *) attr->value);
24779 }
24780 if (attr->val != NULL) {
24781 xmlSchemaFreeValue(attr->val);
24782 attr->val = NULL;
24783 }
24784 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24785 }
24786 vctxt->nbAttrInfos = 0;
24787}
24788
24789/*
24790* 3.4.4 Complex Type Definition Validation Rules
24791* Element Locally Valid (Complex Type) (cvc-complex-type)
24792* 3.2.4 Attribute Declaration Validation Rules
24793* Validation Rule: Attribute Locally Valid (cvc-attribute)
24794* Attribute Locally Valid (Use) (cvc-au)
24795*
24796* Only "assessed" attribute information items will be visible to
24797* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24798*/
24799static int
24800xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24801{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024802 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24803 xmlSchemaItemListPtr attrUseList;
24804 xmlSchemaAttributeUsePtr attrUse = NULL;
24805 xmlSchemaAttributePtr attrDecl = NULL;
24806 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24807 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024808 int xpathRes = 0, res, wildIDs = 0, fixed;
24809
24810 /*
24811 * SPEC (cvc-attribute)
24812 * (1) "The declaration must not be ·absent· (see Missing
24813 * Sub-components (§5.3) for how this can fail to be
24814 * the case)."
24815 * (2) "Its {type definition} must not be absent."
24816 *
24817 * NOTE (1) + (2): This is not handled here, since we currently do not
24818 * allow validation against schemas which have missing sub-components.
24819 *
24820 * SPEC (cvc-complex-type)
24821 * (3) "For each attribute information item in the element information
24822 * item's [attributes] excepting those whose [namespace name] is
24823 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24824 * [local name] is one of type, nil, schemaLocation or
24825 * noNamespaceSchemaLocation, the appropriate case among the following
24826 * must be true:
24827 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024828 */
24829 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24830 /*
24831 * @nbAttrs is the number of attributes present in the instance.
24832 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024833 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024834 if (attrUseList != NULL)
24835 nbUses = attrUseList->nbItems;
24836 else
24837 nbUses = 0;
24838 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024839 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024840 attrUse = attrUseList->items[i];
24841 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24842 for (j = 0; j < nbAttrs; j++) {
24843 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024844 /*
24845 * SPEC (cvc-complex-type) (3)
24846 * Skip meta attributes.
24847 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024848 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024849 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024850 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024851 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024852 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024853 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024854 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024855 continue;
24856 found = 1;
24857 /*
24858 * SPEC (cvc-complex-type)
24859 * (3.1) "If there is among the {attribute uses} an attribute
24860 * use with an {attribute declaration} whose {name} matches
24861 * the attribute information item's [local name] and whose
24862 * {target namespace} is identical to the attribute information
24863 * item's [namespace name] (where an ·absent· {target namespace}
24864 * is taken to be identical to a [namespace name] with no value),
24865 * then the attribute information must be ·valid· with respect
24866 * to that attribute use as per Attribute Locally Valid (Use)
24867 * (§3.5.4). In this case the {attribute declaration} of that
24868 * attribute use is the ·context-determined declaration· for the
24869 * attribute information item with respect to Schema-Validity
24870 * Assessment (Attribute) (§3.2.4) and
24871 * Assessment Outcome (Attribute) (§3.2.5).
24872 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024873 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24874 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024875 /*
24876 * Context-determined declaration.
24877 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024878 iattr->decl = attrDecl;
24879 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024880 break;
24881 }
24882
24883 if (found)
24884 continue;
24885
24886 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24887 /*
24888 * Handle non-existent, required attributes.
24889 *
24890 * SPEC (cvc-complex-type)
24891 * (4) "The {attribute declaration} of each attribute use in
24892 * the {attribute uses} whose {required} is true matches one
24893 * of the attribute information items in the element information
24894 * item's [attributes] as per clause 3.1 above."
24895 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024896 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24897 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024898 VERROR_INT(
24899 "xmlSchemaVAttributesComplex",
24900 "calling xmlSchemaGetFreshAttrInfo()");
24901 return (-1);
24902 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024903 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24904 tmpiattr->use = attrUse;
24905 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024906 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24907 ((attrUse->defValue != NULL) ||
24908 (attrDecl->defValue != NULL))) {
24909 /*
24910 * Handle non-existent, optional, default/fixed attributes.
24911 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024912 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24913 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024914 VERROR_INT(
24915 "xmlSchemaVAttributesComplex",
24916 "calling xmlSchemaGetFreshAttrInfo()");
24917 return (-1);
24918 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024919 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24920 tmpiattr->use = attrUse;
24921 tmpiattr->decl = attrDecl;
24922 tmpiattr->typeDef = attrDecl->subtypes;
24923 tmpiattr->localName = attrDecl->name;
24924 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024925 }
24926 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024927
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024928 if (vctxt->nbAttrInfos == 0)
24929 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024930 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024931 /*
24932 * Validate against the wildcard.
24933 */
24934 if (type->attributeWildcard != NULL) {
24935 /*
24936 * SPEC (cvc-complex-type)
24937 * (3.2.1) "There must be an {attribute wildcard}."
24938 */
24939 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024940 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024941 /*
24942 * SPEC (cvc-complex-type) (3)
24943 * Skip meta attributes.
24944 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024945 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024946 continue;
24947 /*
24948 * SPEC (cvc-complex-type)
24949 * (3.2.2) "The attribute information item must be ·valid· with
24950 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24951 *
24952 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24953 * "... its [namespace name] must be ·valid· with respect to
24954 * the wildcard constraint, as defined in Wildcard allows
24955 * Namespace Name (§3.10.4)."
24956 */
24957 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024958 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024959 /*
24960 * Handle processContents.
24961 *
24962 * SPEC (cvc-wildcard):
24963 * processContents | context-determined declaration:
24964 * "strict" "mustFind"
24965 * "lax" "none"
24966 * "skip" "skip"
24967 */
24968 if (type->attributeWildcard->processContents ==
24969 XML_SCHEMAS_ANY_SKIP) {
24970 /*
24971 * context-determined declaration = "skip"
24972 *
24973 * SPEC PSVI Assessment Outcome (Attribute)
24974 * [validity] = "notKnown"
24975 * [validation attempted] = "none"
24976 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024977 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024978 continue;
24979 }
24980 /*
24981 * Find an attribute declaration.
24982 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024983 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24984 iattr->localName, iattr->nsName);
24985 if (iattr->decl != NULL) {
24986 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024987 /*
24988 * SPEC (cvc-complex-type)
24989 * (5) "Let [Definition:] the wild IDs be the set of
24990 * all attribute information item to which clause 3.2
24991 * applied and whose ·validation· resulted in a
24992 * ·context-determined declaration· of mustFind or no
24993 * ·context-determined declaration· at all, and whose
24994 * [local name] and [namespace name] resolve (as
24995 * defined by QName resolution (Instance) (§3.15.4)) to
24996 * an attribute declaration whose {type definition} is
24997 * or is derived from ID. Then all of the following
24998 * must be true:"
24999 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025000 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025001 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025002 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025003 /*
25004 * SPEC (5.1) "There must be no more than one
25005 * item in ·wild IDs·."
25006 */
25007 if (wildIDs != 0) {
25008 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025009 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025010 TODO
25011 continue;
25012 }
25013 wildIDs++;
25014 /*
25015 * SPEC (cvc-complex-type)
25016 * (5.2) "If ·wild IDs· is non-empty, there must not
25017 * be any attribute uses among the {attribute uses}
25018 * whose {attribute declaration}'s {type definition}
25019 * is or is derived from ID."
25020 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025021 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025022 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025023 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025024 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025025 /* URGENT VAL TODO: implement */
25026 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025027 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025028 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025029 }
25030 }
25031 }
25032 } else if (type->attributeWildcard->processContents ==
25033 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025034 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025035 /*
25036 * SPEC PSVI Assessment Outcome (Attribute)
25037 * [validity] = "notKnown"
25038 * [validation attempted] = "none"
25039 */
25040 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025041 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025042 }
25043 }
25044 }
25045 }
25046
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025047 if (vctxt->nbAttrInfos == 0)
25048 return (0);
25049
25050 /*
25051 * Validate values, create default attributes, evaluate IDCs.
25052 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025053 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025054 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025055 /*
25056 * VAL TODO: Note that we won't try to resolve IDCs to
25057 * "lax" and "skip" validated attributes. Check what to
25058 * do in this case.
25059 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025060 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25061 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025062 continue;
25063 /*
25064 * VAL TODO: What to do if the type definition is missing?
25065 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025066 if (iattr->typeDef == NULL) {
25067 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025068 continue;
25069 }
25070
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025071 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025072 fixed = 0;
25073 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025074
25075 if (vctxt->xpathStates != NULL) {
25076 /*
25077 * Evaluate IDCs.
25078 */
25079 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25080 XML_ATTRIBUTE_NODE);
25081 if (xpathRes == -1) {
25082 VERROR_INT("xmlSchemaVAttributesComplex",
25083 "calling xmlSchemaXPathEvaluate()");
25084 goto internal_error;
25085 }
25086 }
25087
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025088 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025089 /*
25090 * Default/fixed attributes.
25091 */
25092 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025093 if (iattr->use->defValue != NULL) {
25094 iattr->value = (xmlChar *) iattr->use->defValue;
25095 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025096 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025097 iattr->value = (xmlChar *) iattr->decl->defValue;
25098 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025099 }
25100 /*
25101 * IDCs will consume the precomputed default value,
25102 * so we need to clone it.
25103 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025104 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025105 VERROR_INT("xmlSchemaVAttributesComplex",
25106 "default/fixed value on an attribute use was "
25107 "not precomputed");
25108 goto internal_error;
25109 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025110 iattr->val = xmlSchemaCopyValue(iattr->val);
25111 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025112 VERROR_INT("xmlSchemaVAttributesComplex",
25113 "calling xmlSchemaCopyValue()");
25114 goto internal_error;
25115 }
25116 }
25117 /*
25118 * PSVI: Add the default attribute to the current element.
25119 * VAL TODO: Should we use the *normalized* value? This currently
25120 * uses the *initial* value.
25121 */
25122 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025123 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025124 xmlChar *normValue;
25125 const xmlChar *value;
25126
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025127 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025128 /*
25129 * Normalize the value.
25130 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025131 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25132 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025133 if (normValue != NULL)
25134 value = BAD_CAST normValue;
25135
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025136 if (iattr->nsName == NULL) {
25137 if (xmlNewProp(iattr->node->parent,
25138 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025139 VERROR_INT("xmlSchemaVAttributesComplex",
25140 "callling xmlNewProp()");
25141 if (normValue != NULL)
25142 xmlFree(normValue);
25143 goto internal_error;
25144 }
25145 } else {
25146 xmlNsPtr ns;
25147
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025148 ns = xmlSearchNsByHref(iattr->node->doc,
25149 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025150 if (ns == NULL) {
25151 xmlChar prefix[12];
25152 int counter = 0;
25153
25154 /*
25155 * Create a namespace declaration on the validation
25156 * root node if no namespace declaration is in scope.
25157 */
25158 do {
25159 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025160 ns = xmlSearchNs(iattr->node->doc,
25161 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025162 if (counter > 1000) {
25163 VERROR_INT(
25164 "xmlSchemaVAttributesComplex",
25165 "could not compute a ns prefix for a "
25166 "default/fixed attribute");
25167 if (normValue != NULL)
25168 xmlFree(normValue);
25169 goto internal_error;
25170 }
25171 } while (ns != NULL);
25172 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025173 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025174 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025175 /*
25176 * TODO:
25177 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25178 * If we have QNames: do we need to ensure there's a
25179 * prefix defined for the QName?
25180 */
25181 xmlNewNsProp(iattr->node->parent, ns,
25182 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025183 }
25184 if (normValue != NULL)
25185 xmlFree(normValue);
25186 }
25187 /*
25188 * Go directly to IDC evaluation.
25189 */
25190 goto eval_idcs;
25191 }
25192 /*
25193 * Validate the value.
25194 */
25195 if (vctxt->value != NULL) {
25196 /*
25197 * Free last computed value; just for safety reasons.
25198 */
25199 xmlSchemaFreeValue(vctxt->value);
25200 vctxt->value = NULL;
25201 }
25202 /*
25203 * Note that the attribute *use* can be unavailable, if
25204 * the attribute was a wild attribute.
25205 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025206 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25207 ((iattr->use != NULL) &&
25208 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025209 fixed = 1;
25210 else
25211 fixed = 0;
25212 /*
25213 * SPEC (cvc-attribute)
25214 * (3) "The item's ·normalized value· must be locally ·valid·
25215 * with respect to that {type definition} as per
25216 * String Valid (§3.14.4)."
25217 *
25218 * VAL TODO: Do we already have the
25219 * "normalized attribute value" here?
25220 */
25221 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025222 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025223 /*
25224 * Request a computed value.
25225 */
25226 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025227 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025228 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025229 1, 1, 0);
25230 } else {
25231 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025232 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025233 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025234 1, 0, 0);
25235 }
25236
25237 if (res != 0) {
25238 if (res == -1) {
25239 VERROR_INT("xmlSchemaVAttributesComplex",
25240 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25241 goto internal_error;
25242 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025243 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025244 /*
25245 * SPEC PSVI Assessment Outcome (Attribute)
25246 * [validity] = "invalid"
25247 */
25248 goto eval_idcs;
25249 }
25250
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025251 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025252 /*
25253 * SPEC Attribute Locally Valid (Use) (cvc-au)
25254 * "For an attribute information item to be·valid·
25255 * with respect to an attribute use its *normalized*
25256 * value· must match the *canonical* lexical
25257 * representation of the attribute use's {value
25258 * constraint}value, if it is present and fixed."
25259 *
25260 * VAL TODO: The requirement for the *canonical* value
25261 * will be removed in XML Schema 1.1.
25262 */
25263 /*
25264 * SPEC Attribute Locally Valid (cvc-attribute)
25265 * (4) "The item's *actual* value· must match the *value* of
25266 * the {value constraint}, if it is present and fixed."
25267 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025268 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025269 /* VAL TODO: A value was not precomputed. */
25270 TODO
25271 goto eval_idcs;
25272 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025273 if ((iattr->use != NULL) &&
25274 (iattr->use->defValue != NULL)) {
25275 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025276 /* VAL TODO: A default value was not precomputed. */
25277 TODO
25278 goto eval_idcs;
25279 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025280 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025281 /*
25282 if (xmlSchemaCompareValuesWhtsp(attr->val,
25283 (xmlSchemaWhitespaceValueType) ws,
25284 attr->use->defVal,
25285 (xmlSchemaWhitespaceValueType) ws) != 0) {
25286 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025287 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25288 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025289 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025290 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025291 /* VAL TODO: A default value was not precomputed. */
25292 TODO
25293 goto eval_idcs;
25294 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025295 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025296 /*
25297 if (xmlSchemaCompareValuesWhtsp(attr->val,
25298 (xmlSchemaWhitespaceValueType) ws,
25299 attrDecl->defVal,
25300 (xmlSchemaWhitespaceValueType) ws) != 0) {
25301 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025302 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25303 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025304 }
25305 /*
25306 * [validity] = "valid"
25307 */
25308 }
25309eval_idcs:
25310 /*
25311 * Evaluate IDCs.
25312 */
25313 if (xpathRes) {
25314 if (xmlSchemaXPathProcessHistory(vctxt,
25315 vctxt->depth +1) == -1) {
25316 VERROR_INT("xmlSchemaVAttributesComplex",
25317 "calling xmlSchemaXPathEvaluate()");
25318 goto internal_error;
25319 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025320 } else if (vctxt->xpathStates != NULL)
25321 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025322 }
25323
25324 /*
25325 * Report errors.
25326 */
25327 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025328 iattr = vctxt->attrInfos[i];
25329 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25330 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25331 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25332 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025333 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025334 ACTIVATE_ATTRIBUTE(iattr);
25335 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025336 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25337 xmlChar *str = NULL;
25338 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025339 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025340 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25341 "The attribute '%s' is required but missing",
25342 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025343 iattr->decl->targetNamespace,
25344 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025345 NULL);
25346 FREE_AND_NULL(str)
25347 break;
25348 }
25349 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25350 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25351 "The type definition is absent");
25352 break;
25353 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025354 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025355 XML_SCHEMAV_CVC_AU, NULL, NULL,
25356 "The value '%s' does not match the fixed "
25357 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025358 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025359 break;
25360 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25361 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25362 "No matching global attribute declaration available, but "
25363 "demanded by the strict wildcard");
25364 break;
25365 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025366 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025367 break;
25368 /*
25369 * MAYBE VAL TODO: One might report different error messages
25370 * for the following errors.
25371 */
25372 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025373 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025374 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025375 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025376 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025377 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025378 }
25379 break;
25380 default:
25381 break;
25382 }
25383 }
25384
25385 ACTIVATE_ELEM;
25386 return (0);
25387internal_error:
25388 ACTIVATE_ELEM;
25389 return (-1);
25390}
25391
25392static int
25393xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25394 int *skip)
25395{
25396 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25397 /*
25398 * The namespace of the element was already identified to be
25399 * matching the wildcard.
25400 */
25401 if ((skip == NULL) || (wild == NULL) ||
25402 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25403 VERROR_INT("xmlSchemaValidateElemWildcard",
25404 "bad arguments");
25405 return (-1);
25406 }
25407 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025408 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25409 /*
25410 * URGENT VAL TODO: Either we need to position the stream to the
25411 * next sibling, or walk the whole subtree.
25412 */
25413 *skip = 1;
25414 return (0);
25415 }
25416 {
25417 xmlSchemaElementPtr decl = NULL;
25418
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025419 decl = xmlSchemaGetElem(vctxt->schema,
25420 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025421 if (decl != NULL) {
25422 vctxt->inode->decl = decl;
25423 return (0);
25424 }
25425 }
25426 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25427 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025428 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025429 "No matching global element declaration available, but "
25430 "demanded by the strict wildcard");
25431 return (vctxt->err);
25432 }
25433 if (vctxt->nbAttrInfos != 0) {
25434 xmlSchemaAttrInfoPtr iattr;
25435 /*
25436 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25437 * (1.2.1.2.1) - (1.2.1.2.3 )
25438 *
25439 * Use the xsi:type attribute for the type definition.
25440 */
25441 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25442 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25443 if (iattr != NULL) {
25444 if (xmlSchemaProcessXSIType(vctxt, iattr,
25445 &(vctxt->inode->typeDef), NULL) == -1) {
25446 VERROR_INT("xmlSchemaValidateElemWildcard",
25447 "calling xmlSchemaProcessXSIType() to "
25448 "process the attribute 'xsi:nil'");
25449 return (-1);
25450 }
25451 /*
25452 * Don't return an error on purpose.
25453 */
25454 return (0);
25455 }
25456 }
25457 /*
25458 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25459 *
25460 * Fallback to "anyType".
25461 */
25462 vctxt->inode->typeDef =
25463 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25464 return (0);
25465}
25466
25467/*
25468* xmlSchemaCheckCOSValidDefault:
25469*
25470* This will be called if: not nilled, no content and a default/fixed
25471* value is provided.
25472*/
25473
25474static int
25475xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25476 const xmlChar *value,
25477 xmlSchemaValPtr *val)
25478{
25479 int ret = 0;
25480 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25481
25482 /*
25483 * cos-valid-default:
25484 * Schema Component Constraint: Element Default Valid (Immediate)
25485 * For a string to be a valid default with respect to a type
25486 * definition the appropriate case among the following must be true:
25487 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025488 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025489 /*
25490 * Complex type.
25491 *
25492 * SPEC (2.1) "its {content type} must be a simple type definition
25493 * or mixed."
25494 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25495 * type}'s particle must be ·emptiable· as defined by
25496 * Particle Emptiable (§3.9.6)."
25497 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025498 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25499 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25500 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025501 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25502 /* NOTE that this covers (2.2.2) as well. */
25503 VERROR(ret, NULL,
25504 "For a string to be a valid default, the type definition "
25505 "must be a simple type or a complex type with simple content "
25506 "or mixed content and a particle emptiable");
25507 return(ret);
25508 }
25509 }
25510 /*
25511 * 1 If the type definition is a simple type definition, then the string
25512 * must be ·valid· with respect to that definition as defined by String
25513 * Valid (§3.14.4).
25514 *
25515 * AND
25516 *
25517 * 2.2.1 If the {content type} is a simple type definition, then the
25518 * string must be ·valid· with respect to that simple type definition
25519 * as defined by String Valid (§3.14.4).
25520 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025521 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025522
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025523 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025524 NULL, inode->typeDef, value, val, 1, 1, 0);
25525
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025526 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025527
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025528 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025529 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25530 }
25531 if (ret < 0) {
25532 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25533 "calling xmlSchemaVCheckCVCSimpleType()");
25534 }
25535 return (ret);
25536}
25537
25538static void
25539xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25540 const xmlChar * name ATTRIBUTE_UNUSED,
25541 xmlSchemaElementPtr item,
25542 xmlSchemaNodeInfoPtr inode)
25543{
25544 inode->decl = item;
25545#ifdef DEBUG_CONTENT
25546 {
25547 xmlChar *str = NULL;
25548
25549 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25550 xmlGenericError(xmlGenericErrorContext,
25551 "AUTOMATON callback for '%s' [declaration]\n",
25552 xmlSchemaFormatQName(&str,
25553 inode->localName, inode->nsName));
25554 } else {
25555 xmlGenericError(xmlGenericErrorContext,
25556 "AUTOMATON callback for '%s' [wildcard]\n",
25557 xmlSchemaFormatQName(&str,
25558 inode->localName, inode->nsName));
25559
25560 }
25561 FREE_AND_NULL(str)
25562 }
25563#endif
25564}
25565
25566static int
25567xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025568{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025569 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25570 if (vctxt->inode == NULL) {
25571 VERROR_INT("xmlSchemaValidatorPushElem",
25572 "calling xmlSchemaGetFreshElemInfo()");
25573 return (-1);
25574 }
25575 vctxt->nbAttrInfos = 0;
25576 return (0);
25577}
25578
25579static int
25580xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25581 xmlSchemaNodeInfoPtr inode,
25582 xmlSchemaTypePtr type,
25583 const xmlChar *value)
25584{
25585 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25586 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025587 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025588 type, value, &(inode->val), 1, 1, 0));
25589 else
25590 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025591 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025592 type, value, NULL, 1, 0, 0));
25593}
25594
25595
25596
25597/*
25598* Process END of element.
25599*/
25600static int
25601xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25602{
25603 int ret = 0;
25604 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25605
25606 if (vctxt->nbAttrInfos != 0)
25607 xmlSchemaClearAttrInfos(vctxt);
25608 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25609 /*
25610 * This element was not expected;
25611 * we will not validate child elements of broken parents.
25612 * Skip validation of all content of the parent.
25613 */
25614 vctxt->skipDepth = vctxt->depth -1;
25615 goto end_elem;
25616 }
25617 if ((inode->typeDef == NULL) ||
25618 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25619 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025620 * 1. the type definition might be missing if the element was
25621 * error prone
25622 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025623 */
25624 goto end_elem;
25625 }
25626 /*
25627 * Check the content model.
25628 */
25629 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25630 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25631
25632 /*
25633 * Workaround for "anyType".
25634 */
25635 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25636 goto character_content;
25637
25638 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25639 xmlChar *values[10];
25640 int terminal, nbval = 10, nbneg;
25641
25642 if (inode->regexCtxt == NULL) {
25643 /*
25644 * Create the regex context.
25645 */
25646 inode->regexCtxt =
25647 xmlRegNewExecCtxt(inode->typeDef->contModel,
25648 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25649 vctxt);
25650 if (inode->regexCtxt == NULL) {
25651 VERROR_INT("xmlSchemaValidatorPopElem",
25652 "failed to create a regex context");
25653 goto internal_error;
25654 }
25655#ifdef DEBUG_AUTOMATA
25656 xmlGenericError(xmlGenericErrorContext,
25657 "AUTOMATON create on '%s'\n", inode->localName);
25658#endif
25659 }
25660 /*
25661 * Get hold of the still expected content, since a further
25662 * call to xmlRegExecPushString() will loose this information.
25663 */
25664 xmlRegExecNextValues(inode->regexCtxt,
25665 &nbval, &nbneg, &values[0], &terminal);
25666 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25667 if (ret <= 0) {
25668 /*
25669 * Still missing something.
25670 */
25671 ret = 1;
25672 inode->flags |=
25673 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025674 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025675 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25676 "Missing child element(s)",
25677 nbval, nbneg, values);
25678#ifdef DEBUG_AUTOMATA
25679 xmlGenericError(xmlGenericErrorContext,
25680 "AUTOMATON missing ERROR on '%s'\n",
25681 inode->localName);
25682#endif
25683 } else {
25684 /*
25685 * Content model is satisfied.
25686 */
25687 ret = 0;
25688#ifdef DEBUG_AUTOMATA
25689 xmlGenericError(xmlGenericErrorContext,
25690 "AUTOMATON succeeded on '%s'\n",
25691 inode->localName);
25692#endif
25693 }
25694
25695 }
25696 }
25697 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25698 goto end_elem;
25699
25700character_content:
25701
25702 if (vctxt->value != NULL) {
25703 xmlSchemaFreeValue(vctxt->value);
25704 vctxt->value = NULL;
25705 }
25706 /*
25707 * Check character content.
25708 */
25709 if (inode->decl == NULL) {
25710 /*
25711 * Speedup if no declaration exists.
25712 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025713 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025714 ret = xmlSchemaVCheckINodeDataType(vctxt,
25715 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025716 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025717 ret = xmlSchemaVCheckINodeDataType(vctxt,
25718 inode, inode->typeDef->contentTypeDef,
25719 inode->value);
25720 }
25721 if (ret < 0) {
25722 VERROR_INT("xmlSchemaValidatorPopElem",
25723 "calling xmlSchemaVCheckCVCSimpleType()");
25724 goto internal_error;
25725 }
25726 goto end_elem;
25727 }
25728 /*
25729 * cvc-elt (3.3.4) : 5
25730 * The appropriate case among the following must be true:
25731 */
25732 /*
25733 * cvc-elt (3.3.4) : 5.1
25734 * If the declaration has a {value constraint},
25735 * the item has neither element nor character [children] and
25736 * clause 3.2 has not applied, then all of the following must be true:
25737 */
25738 if ((inode->decl->value != NULL) &&
25739 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25740 (! INODE_NILLED(inode))) {
25741 /*
25742 * cvc-elt (3.3.4) : 5.1.1
25743 * If the ·actual type definition· is a ·local type definition·
25744 * then the canonical lexical representation of the {value constraint}
25745 * value must be a valid default for the ·actual type definition· as
25746 * defined in Element Default Valid (Immediate) (§3.3.6).
25747 */
25748 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025749 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025750 * NOTE: Although the *canonical* value is stated, it is not
25751 * relevant if canonical or not. Additionally XML Schema 1.1
25752 * will removed this requirement as well.
25753 */
25754 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25755
25756 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25757 inode->decl->value, &(inode->val));
25758 if (ret != 0) {
25759 if (ret < 0) {
25760 VERROR_INT("xmlSchemaValidatorPopElem",
25761 "calling xmlSchemaCheckCOSValidDefault()");
25762 goto internal_error;
25763 }
25764 goto end_elem;
25765 }
25766 /*
25767 * Stop here, to avoid redundant validation of the value
25768 * (see following).
25769 */
25770 goto default_psvi;
25771 }
25772 /*
25773 * cvc-elt (3.3.4) : 5.1.2
25774 * The element information item with the canonical lexical
25775 * representation of the {value constraint} value used as its
25776 * ·normalized value· must be ·valid· with respect to the
25777 * ·actual type definition· as defined by Element Locally Valid (Type)
25778 * (§3.3.4).
25779 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025780 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025781 ret = xmlSchemaVCheckINodeDataType(vctxt,
25782 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025783 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025784 ret = xmlSchemaVCheckINodeDataType(vctxt,
25785 inode, inode->typeDef->contentTypeDef,
25786 inode->decl->value);
25787 }
25788 if (ret != 0) {
25789 if (ret < 0) {
25790 VERROR_INT("xmlSchemaValidatorPopElem",
25791 "calling xmlSchemaVCheckCVCSimpleType()");
25792 goto internal_error;
25793 }
25794 goto end_elem;
25795 }
25796
25797default_psvi:
25798 /*
25799 * PSVI: Create a text node on the instance element.
25800 */
25801 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25802 (inode->node != NULL)) {
25803 xmlNodePtr textChild;
25804 xmlChar *normValue;
25805 /*
25806 * VAL TODO: Normalize the value.
25807 */
25808 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25809 inode->decl->value);
25810 if (normValue != NULL) {
25811 textChild = xmlNewText(BAD_CAST normValue);
25812 xmlFree(normValue);
25813 } else
25814 textChild = xmlNewText(inode->decl->value);
25815 if (textChild == NULL) {
25816 VERROR_INT("xmlSchemaValidatorPopElem",
25817 "calling xmlNewText()");
25818 goto internal_error;
25819 } else
25820 xmlAddChild(inode->node, textChild);
25821 }
25822
25823 } else if (! INODE_NILLED(inode)) {
25824 /*
25825 * 5.2.1 The element information item must be ·valid· with respect
25826 * to the ·actual type definition· as defined by Element Locally
25827 * Valid (Type) (§3.3.4).
25828 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025829 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025830 /*
25831 * SPEC (cvc-type) (3.1)
25832 * "If the type definition is a simple type definition, ..."
25833 * (3.1.3) "If clause 3.2 of Element Locally Valid
25834 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25835 * must be ·valid· with respect to the type definition as defined
25836 * by String Valid (§3.14.4).
25837 */
25838 ret = xmlSchemaVCheckINodeDataType(vctxt,
25839 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025840 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025841 /*
25842 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25843 * definition, then the element information item must be
25844 * ·valid· with respect to the type definition as per
25845 * Element Locally Valid (Complex Type) (§3.4.4);"
25846 *
25847 * SPEC (cvc-complex-type) (2.2)
25848 * "If the {content type} is a simple type definition, ...
25849 * the ·normalized value· of the element information item is
25850 * ·valid· with respect to that simple type definition as
25851 * defined by String Valid (§3.14.4)."
25852 */
25853 ret = xmlSchemaVCheckINodeDataType(vctxt,
25854 inode, inode->typeDef->contentTypeDef, inode->value);
25855 }
25856 if (ret != 0) {
25857 if (ret < 0) {
25858 VERROR_INT("xmlSchemaValidatorPopElem",
25859 "calling xmlSchemaVCheckCVCSimpleType()");
25860 goto internal_error;
25861 }
25862 goto end_elem;
25863 }
25864 /*
25865 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25866 * not applied, all of the following must be true:
25867 */
25868 if ((inode->decl->value != NULL) &&
25869 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25870
25871 /*
25872 * TODO: We will need a computed value, when comparison is
25873 * done on computed values.
25874 */
25875 /*
25876 * 5.2.2.1 The element information item must have no element
25877 * information item [children].
25878 */
25879 if (inode->flags &
25880 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25881 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25882 VERROR(ret, NULL,
25883 "The content must not containt element nodes since "
25884 "there is a fixed value constraint");
25885 goto end_elem;
25886 } else {
25887 /*
25888 * 5.2.2.2 The appropriate case among the following must
25889 * be true:
25890 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025891 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025892 /*
25893 * 5.2.2.2.1 If the {content type} of the ·actual type
25894 * definition· is mixed, then the *initial value* of the
25895 * item must match the canonical lexical representation
25896 * of the {value constraint} value.
25897 *
25898 * ... the *initial value* of an element information
25899 * item is the string composed of, in order, the
25900 * [character code] of each character information item in
25901 * the [children] of that element information item.
25902 */
25903 if (! xmlStrEqual(inode->value, inode->decl->value)){
25904 /*
25905 * VAL TODO: Report invalid & expected values as well.
25906 * VAL TODO: Implement the canonical stuff.
25907 */
25908 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025909 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025910 ret, NULL, NULL,
25911 "The initial value '%s' does not match the fixed "
25912 "value constraint '%s'",
25913 inode->value, inode->decl->value);
25914 goto end_elem;
25915 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025916 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025917 /*
25918 * 5.2.2.2.2 If the {content type} of the ·actual type
25919 * definition· is a simple type definition, then the
25920 * *actual value* of the item must match the canonical
25921 * lexical representation of the {value constraint} value.
25922 */
25923 /*
25924 * VAL TODO: *actual value* is the normalized value, impl.
25925 * this.
25926 * VAL TODO: Report invalid & expected values as well.
25927 * VAL TODO: Implement a comparison with the computed values.
25928 */
25929 if (! xmlStrEqual(inode->value,
25930 inode->decl->value)) {
25931 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025932 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025933 ret, NULL, NULL,
25934 "The actual value '%s' does not match the fixed "
25935 "value constraint '%s'",
25936 inode->value,
25937 inode->decl->value);
25938 goto end_elem;
25939 }
25940 }
25941 }
25942 }
25943 }
25944
25945end_elem:
25946 if (vctxt->depth < 0) {
25947 /* TODO: raise error? */
25948 return (0);
25949 }
25950 if (vctxt->depth == vctxt->skipDepth)
25951 vctxt->skipDepth = -1;
25952 /*
25953 * Evaluate the history of XPath state objects.
25954 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025955 if (inode->appliedXPath &&
25956 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025957 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025958 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025959 * MAYBE TODO:
25960 * SPEC (6) "The element information item must be ·valid· with
25961 * respect to each of the {identity-constraint definitions} as per
25962 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025963 */
25964 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025965 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25966 * need to be built in any case.
25967 * We will currently build IDC node-tables and bubble them only if
25968 * keyrefs do exist.
25969 */
25970
25971 /*
25972 * Add the current IDC target-nodes to the IDC node-tables.
25973 */
25974 if ((inode->idcMatchers != NULL) &&
25975 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25976 {
25977 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25978 goto internal_error;
25979 }
25980 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025981 * Validate IDC keyrefs.
25982 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025983 if (vctxt->inode->hasKeyrefs)
25984 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25985 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025986 /*
25987 * Merge/free the IDC table.
25988 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025989 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025990#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025991 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025992 inode->nsName,
25993 inode->localName,
25994 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025995#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025996 if ((vctxt->depth > 0) &&
25997 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25998 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025999 /*
26000 * Merge the IDC node table with the table of the parent node.
26001 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026002 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
26003 goto internal_error;
26004 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026005 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026006 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026007 * Clear the current ielem.
26008 * VAL TODO: Don't free the PSVI IDC tables if they are
26009 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026010 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026011 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026012 /*
26013 * Skip further processing if we are on the validation root.
26014 */
26015 if (vctxt->depth == 0) {
26016 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026017 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026018 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026019 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026020 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026021 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026022 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026023 if (vctxt->aidcs != NULL) {
26024 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
26025 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026026 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026027 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026028 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026029 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026030 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026031 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026032 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026033 }
26034 aidc = aidc->next;
26035 } while (aidc != NULL);
26036 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026037 vctxt->depth--;
26038 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000026039 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026040 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026041 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
26042 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026043 return (ret);
26044
26045internal_error:
26046 vctxt->err = -1;
26047 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026048}
26049
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026050/*
26051* 3.4.4 Complex Type Definition Validation Rules
26052* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26053*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026054static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026055xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026056{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026057 xmlSchemaNodeInfoPtr pielem;
26058 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026059 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026060
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026061 if (vctxt->depth <= 0) {
26062 VERROR_INT("xmlSchemaValidateChildElem",
26063 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026064 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026065 }
26066 pielem = vctxt->elemInfos[vctxt->depth -1];
26067 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26068 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026069 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026070 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026071 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026072 if (INODE_NILLED(pielem)) {
26073 /*
26074 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26075 */
26076 ACTIVATE_PARENT_ELEM;
26077 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26078 VERROR(ret, NULL,
26079 "Neither character nor element content is allowed, "
26080 "because the element was 'nilled'");
26081 ACTIVATE_ELEM;
26082 goto unexpected_elem;
26083 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026084
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026085 ptype = pielem->typeDef;
26086
26087 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26088 /*
26089 * Workaround for "anyType": we have currently no content model
26090 * assigned for "anyType", so handle it explicitely.
26091 * "anyType" has an unbounded, lax "any" wildcard.
26092 */
26093 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26094 vctxt->inode->localName,
26095 vctxt->inode->nsName);
26096
26097 if (vctxt->inode->decl == NULL) {
26098 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026099 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026100 * Process "xsi:type".
26101 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026102 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026103 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26104 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26105 if (iattr != NULL) {
26106 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26107 &(vctxt->inode->typeDef), NULL);
26108 if (ret != 0) {
26109 if (ret == -1) {
26110 VERROR_INT("xmlSchemaValidateChildElem",
26111 "calling xmlSchemaProcessXSIType() to "
26112 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026113 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026114 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026115 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026116 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026117 } else {
26118 /*
26119 * Fallback to "anyType".
26120 *
26121 * SPEC (cvc-assess-elt)
26122 * "If the item cannot be ·strictly assessed·, [...]
26123 * an element information item's schema validity may be laxly
26124 * assessed if its ·context-determined declaration· is not
26125 * skip by ·validating· with respect to the ·ur-type
26126 * definition· as per Element Locally Valid (Type) (§3.3.4)."
26127 */
26128 vctxt->inode->typeDef =
26129 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026130 }
26131 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026132 return (0);
26133 }
26134
26135 switch (ptype->contentType) {
26136 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026137 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026138 * SPEC (2.1) "If the {content type} is empty, then the
26139 * element information item has no character or element
26140 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026141 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026142 ACTIVATE_PARENT_ELEM
26143 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26144 VERROR(ret, NULL,
26145 "Element content is not allowed, "
26146 "because the content type is empty");
26147 ACTIVATE_ELEM
26148 goto unexpected_elem;
26149 break;
26150
26151 case XML_SCHEMA_CONTENT_MIXED:
26152 case XML_SCHEMA_CONTENT_ELEMENTS: {
26153 xmlRegExecCtxtPtr regexCtxt;
26154 xmlChar *values[10];
26155 int terminal, nbval = 10, nbneg;
26156
26157 /* VAL TODO: Optimized "anyType" validation.*/
26158
26159 if (ptype->contModel == NULL) {
26160 VERROR_INT("xmlSchemaValidateChildElem",
26161 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026162 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026163 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026164 /*
26165 * Safety belf for evaluation if the cont. model was already
26166 * examined to be invalid.
26167 */
26168 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26169 VERROR_INT("xmlSchemaValidateChildElem",
26170 "validating elem, but elem content is already invalid");
26171 return (-1);
26172 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026173
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026174 regexCtxt = pielem->regexCtxt;
26175 if (regexCtxt == NULL) {
26176 /*
26177 * Create the regex context.
26178 */
26179 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26180 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26181 vctxt);
26182 if (regexCtxt == NULL) {
26183 VERROR_INT("xmlSchemaValidateChildElem",
26184 "failed to create a regex context");
26185 return (-1);
26186 }
26187 pielem->regexCtxt = regexCtxt;
26188#ifdef DEBUG_AUTOMATA
26189 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26190 pielem->localName);
26191#endif
26192 }
26193
26194 /*
26195 * SPEC (2.4) "If the {content type} is element-only or mixed,
26196 * then the sequence of the element information item's
26197 * element information item [children], if any, taken in
26198 * order, is ·valid· with respect to the {content type}'s
26199 * particle, as defined in Element Sequence Locally Valid
26200 * (Particle) (§3.9.4)."
26201 */
26202 ret = xmlRegExecPushString2(regexCtxt,
26203 vctxt->inode->localName,
26204 vctxt->inode->nsName,
26205 vctxt->inode);
26206#ifdef DEBUG_AUTOMATA
26207 if (ret < 0)
26208 xmlGenericError(xmlGenericErrorContext,
26209 "AUTOMATON push ERROR for '%s' on '%s'\n",
26210 vctxt->inode->localName, pielem->localName);
26211 else
26212 xmlGenericError(xmlGenericErrorContext,
26213 "AUTOMATON push OK for '%s' on '%s'\n",
26214 vctxt->inode->localName, pielem->localName);
26215#endif
26216 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26217 VERROR_INT("xmlSchemaValidateChildElem",
26218 "calling xmlRegExecPushString2()");
26219 return (-1);
26220 }
26221 if (ret < 0) {
26222 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26223 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026224 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026225 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26226 "This element is not expected",
26227 nbval, nbneg, values);
26228 ret = vctxt->err;
26229 goto unexpected_elem;
26230 } else
26231 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026232 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026233 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026234 case XML_SCHEMA_CONTENT_SIMPLE:
26235 case XML_SCHEMA_CONTENT_BASIC:
26236 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026237 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026238 /*
26239 * SPEC (cvc-complex-type) (2.2)
26240 * "If the {content type} is a simple type definition, then
26241 * the element information item has no element information
26242 * item [children], ..."
26243 */
26244 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26245 VERROR(ret, NULL, "Element content is not allowed, "
26246 "because the content type is a simple type definition");
26247 } else {
26248 /*
26249 * SPEC (cvc-type) (3.1.2) "The element information item must
26250 * have no element information item [children]."
26251 */
26252 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26253 VERROR(ret, NULL, "Element content is not allowed, "
26254 "because the type definition is simple");
26255 }
26256 ACTIVATE_ELEM
26257 ret = vctxt->err;
26258 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026259 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026260
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026261 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026262 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026263 }
26264 return (ret);
26265unexpected_elem:
26266 /*
26267 * Pop this element and set the skipDepth to skip
26268 * all further content of the parent element.
26269 */
26270 vctxt->skipDepth = vctxt->depth;
26271 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26272 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26273 return (ret);
26274}
26275
26276#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26277#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26278#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26279
26280static int
26281xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26282 int nodeType, const xmlChar *value, int len,
26283 int mode, int *consumed)
26284{
26285 /*
26286 * Unfortunately we have to duplicate the text sometimes.
26287 * OPTIMIZE: Maybe we could skip it, if:
26288 * 1. content type is simple
26289 * 2. whitespace is "collapse"
26290 * 3. it consists of whitespace only
26291 *
26292 * Process character content.
26293 */
26294 if (consumed != NULL)
26295 *consumed = 0;
26296 if (INODE_NILLED(vctxt->inode)) {
26297 /*
26298 * SPEC cvc-elt (3.3.4 - 3.2.1)
26299 * "The element information item must have no character or
26300 * element information item [children]."
26301 */
26302 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26303 "Neither character nor element content is allowed "
26304 "because the element is 'nilled'");
26305 return (vctxt->err);
26306 }
26307 /*
26308 * SPEC (2.1) "If the {content type} is empty, then the
26309 * element information item has no character or element
26310 * information item [children]."
26311 */
26312 if (vctxt->inode->typeDef->contentType ==
26313 XML_SCHEMA_CONTENT_EMPTY) {
26314 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26315 "Character content is not allowed, "
26316 "because the content type is empty");
26317 return (vctxt->err);
26318 }
26319
26320 if (vctxt->inode->typeDef->contentType ==
26321 XML_SCHEMA_CONTENT_ELEMENTS) {
26322 if ((nodeType != XML_TEXT_NODE) ||
26323 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26324 /*
26325 * SPEC cvc-complex-type (2.3)
26326 * "If the {content type} is element-only, then the
26327 * element information item has no character information
26328 * item [children] other than those whose [character
26329 * code] is defined as a white space in [XML 1.0 (Second
26330 * Edition)]."
26331 */
26332 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26333 "Character content other than whitespace is not allowed "
26334 "because the content type is 'element-only'");
26335 return (vctxt->err);
26336 }
26337 return (0);
26338 }
26339
26340 if ((value == NULL) || (value[0] == 0))
26341 return (0);
26342 /*
26343 * Save the value.
26344 * NOTE that even if the content type is *mixed*, we need the
26345 * *initial value* for default/fixed value constraints.
26346 */
26347 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26348 ((vctxt->inode->decl == NULL) ||
26349 (vctxt->inode->decl->value == NULL)))
26350 return (0);
26351
26352 if (vctxt->inode->value == NULL) {
26353 /*
26354 * Set the value.
26355 */
26356 switch (mode) {
26357 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26358 /*
26359 * When working on a tree.
26360 */
26361 vctxt->inode->value = value;
26362 break;
26363 case XML_SCHEMA_PUSH_TEXT_CREATED:
26364 /*
26365 * When working with the reader.
26366 * The value will be freed by the element info.
26367 */
26368 vctxt->inode->value = value;
26369 if (consumed != NULL)
26370 *consumed = 1;
26371 vctxt->inode->flags |=
26372 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26373 break;
26374 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26375 /*
26376 * When working with SAX.
26377 * The value will be freed by the element info.
26378 */
26379 if (len != -1)
26380 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26381 else
26382 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26383 vctxt->inode->flags |=
26384 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26385 break;
26386 default:
26387 break;
26388 }
Kasimier T. Buchcik5bb0c082005-12-20 10:48:33 +000026389 } else {
26390 if (len < 0)
26391 len = xmlStrlen(value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026392 /*
26393 * Concat the value.
26394 */
26395 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026396 vctxt->inode->value = BAD_CAST xmlStrncat(
26397 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026398 } else {
26399 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026400 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026401 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26402 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026403 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026404
26405 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026406}
26407
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026408static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026409xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026410{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026411 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026412
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026413 if ((vctxt->skipDepth != -1) &&
26414 (vctxt->depth >= vctxt->skipDepth)) {
26415 VERROR_INT("xmlSchemaValidateElem",
26416 "in skip-state");
26417 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026418 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026419 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026420 /*
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026421 * We will stop validation if there was an error during
26422 * dynamic schema construction.
26423 * Note that we simply set @skipDepth to 0, this could
26424 * mean that a streaming document via SAX would be
26425 * still read to the end but it won't be validated any more.
26426 * TODO: If we are sure how to stop the validation at once
26427 * for all input scenarios, then this should be changed to
26428 * instantly stop the validation.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026429 */
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026430 ret = xmlSchemaAssembleByXSI(vctxt);
26431 if (ret != 0) {
26432 if (ret == -1)
26433 goto internal_error;
26434 vctxt->skipDepth = 0;
26435 return(ret);
26436 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026437 }
26438 if (vctxt->depth > 0) {
26439 /*
26440 * Validate this element against the content model
26441 * of the parent.
26442 */
26443 ret = xmlSchemaValidateChildElem(vctxt);
26444 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026445 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026446 VERROR_INT("xmlSchemaValidateElem",
26447 "calling xmlSchemaStreamValidateChildElement()");
26448 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026449 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026450 goto exit;
26451 }
26452 if (vctxt->depth == vctxt->skipDepth)
26453 goto exit;
26454 if ((vctxt->inode->decl == NULL) &&
26455 (vctxt->inode->typeDef == NULL)) {
26456 VERROR_INT("xmlSchemaValidateElem",
26457 "the child element was valid but neither the "
26458 "declaration nor the type was set");
26459 goto internal_error;
26460 }
26461 } else {
26462 /*
26463 * Get the declaration of the validation root.
26464 */
26465 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26466 vctxt->inode->localName,
26467 vctxt->inode->nsName);
26468 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026469 ret = XML_SCHEMAV_CVC_ELT_1;
26470 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026471 "No matching global declaration available "
26472 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026473 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026474 }
26475 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026476
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026477 if (vctxt->inode->decl == NULL)
26478 goto type_validation;
26479
26480 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26481 int skip;
26482 /*
26483 * Wildcards.
26484 */
26485 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26486 if (ret != 0) {
26487 if (ret < 0) {
26488 VERROR_INT("xmlSchemaValidateElem",
26489 "calling xmlSchemaValidateElemWildcard()");
26490 goto internal_error;
26491 }
26492 goto exit;
26493 }
26494 if (skip) {
26495 vctxt->skipDepth = vctxt->depth;
26496 goto exit;
26497 }
26498 /*
26499 * The declaration might be set by the wildcard validation,
26500 * when the processContents is "lax" or "strict".
26501 */
26502 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26503 /*
26504 * Clear the "decl" field to not confuse further processing.
26505 */
26506 vctxt->inode->decl = NULL;
26507 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026508 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026509 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026510 /*
26511 * Validate against the declaration.
26512 */
26513 ret = xmlSchemaValidateElemDecl(vctxt);
26514 if (ret != 0) {
26515 if (ret < 0) {
26516 VERROR_INT("xmlSchemaValidateElem",
26517 "calling xmlSchemaValidateElemDecl()");
26518 goto internal_error;
26519 }
26520 goto exit;
26521 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026522 /*
26523 * Validate against the type definition.
26524 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026525type_validation:
26526
26527 if (vctxt->inode->typeDef == NULL) {
26528 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26529 ret = XML_SCHEMAV_CVC_TYPE_1;
26530 VERROR(ret, NULL,
26531 "The type definition is absent");
26532 goto exit;
26533 }
26534 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26535 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26536 ret = XML_SCHEMAV_CVC_TYPE_2;
26537 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026538 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026539 goto exit;
26540 }
26541 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026542 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026543 * during validation against the declaration. This must be done
26544 * _before_ attribute validation.
26545 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026546 if (vctxt->xpathStates != NULL) {
26547 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026548 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026549 if (ret == -1) {
26550 VERROR_INT("xmlSchemaValidateElem",
26551 "calling xmlSchemaXPathEvaluate()");
26552 goto internal_error;
26553 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026554 }
26555 /*
26556 * Validate attributes.
26557 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026558 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026559 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026560 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026561
26562 ret = xmlSchemaVAttributesComplex(vctxt);
26563 }
26564 } else if (vctxt->nbAttrInfos != 0) {
26565
26566 ret = xmlSchemaVAttributesSimple(vctxt);
26567 }
26568 /*
26569 * Clear registered attributes.
26570 */
26571 if (vctxt->nbAttrInfos != 0)
26572 xmlSchemaClearAttrInfos(vctxt);
26573 if (ret == -1) {
26574 VERROR_INT("xmlSchemaValidateElem",
26575 "calling attributes validation");
26576 goto internal_error;
26577 }
26578 /*
26579 * Don't return an error if attributes are invalid on purpose.
26580 */
26581 ret = 0;
26582
26583exit:
26584 if (ret != 0)
26585 vctxt->skipDepth = vctxt->depth;
26586 return (ret);
26587internal_error:
26588 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026589}
26590
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026591#ifdef XML_SCHEMA_READER_ENABLED
26592static int
26593xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026594{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026595 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26596 int depth, nodeType, ret = 0, consumed;
26597 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026598
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026599 vctxt->depth = -1;
26600 ret = xmlTextReaderRead(vctxt->reader);
26601 /*
26602 * Move to the document element.
26603 */
26604 while (ret == 1) {
26605 nodeType = xmlTextReaderNodeType(vctxt->reader);
26606 if (nodeType == XML_ELEMENT_NODE)
26607 goto root_found;
26608 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026609 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026610 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026611
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026612root_found:
26613
26614 do {
26615 depth = xmlTextReaderDepth(vctxt->reader);
26616 nodeType = xmlTextReaderNodeType(vctxt->reader);
26617
26618 if (nodeType == XML_ELEMENT_NODE) {
26619
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026620 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026621 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26622 VERROR_INT("xmlSchemaVReaderWalk",
26623 "calling xmlSchemaValidatorPushElem()");
26624 goto internal_error;
26625 }
26626 ielem = vctxt->inode;
26627 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26628 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26629 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26630 /*
26631 * Is the element empty?
26632 */
26633 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26634 if (ret == -1) {
26635 VERROR_INT("xmlSchemaVReaderWalk",
26636 "calling xmlTextReaderIsEmptyElement()");
26637 goto internal_error;
26638 }
26639 if (ret) {
26640 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26641 }
26642 /*
26643 * Register attributes.
26644 */
26645 vctxt->nbAttrInfos = 0;
26646 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26647 if (ret == -1) {
26648 VERROR_INT("xmlSchemaVReaderWalk",
26649 "calling xmlTextReaderMoveToFirstAttribute()");
26650 goto internal_error;
26651 }
26652 if (ret == 1) {
26653 do {
26654 /*
26655 * VAL TODO: How do we know that the reader works on a
26656 * node tree, to be able to pass a node here?
26657 */
26658 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26659 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26660 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26661 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26662
26663 VERROR_INT("xmlSchemaVReaderWalk",
26664 "calling xmlSchemaValidatorPushAttribute()");
26665 goto internal_error;
26666 }
26667 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26668 if (ret == -1) {
26669 VERROR_INT("xmlSchemaVReaderWalk",
26670 "calling xmlTextReaderMoveToFirstAttribute()");
26671 goto internal_error;
26672 }
26673 } while (ret == 1);
26674 /*
26675 * Back to element position.
26676 */
26677 ret = xmlTextReaderMoveToElement(vctxt->reader);
26678 if (ret == -1) {
26679 VERROR_INT("xmlSchemaVReaderWalk",
26680 "calling xmlTextReaderMoveToElement()");
26681 goto internal_error;
26682 }
26683 }
26684 /*
26685 * Validate the element.
26686 */
26687 ret= xmlSchemaValidateElem(vctxt);
26688 if (ret != 0) {
26689 if (ret == -1) {
26690 VERROR_INT("xmlSchemaVReaderWalk",
26691 "calling xmlSchemaValidateElem()");
26692 goto internal_error;
26693 }
26694 goto exit;
26695 }
26696 if (vctxt->depth == vctxt->skipDepth) {
26697 int curDepth;
26698 /*
26699 * Skip all content.
26700 */
26701 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26702 ret = xmlTextReaderRead(vctxt->reader);
26703 curDepth = xmlTextReaderDepth(vctxt->reader);
26704 while ((ret == 1) && (curDepth != depth)) {
26705 ret = xmlTextReaderRead(vctxt->reader);
26706 curDepth = xmlTextReaderDepth(vctxt->reader);
26707 }
26708 if (ret < 0) {
26709 /*
26710 * VAL TODO: A reader error occured; what to do here?
26711 */
26712 ret = 1;
26713 goto exit;
26714 }
26715 }
26716 goto leave_elem;
26717 }
26718 /*
26719 * READER VAL TODO: Is an END_ELEM really never called
26720 * if the elem is empty?
26721 */
26722 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26723 goto leave_elem;
26724 } else if (nodeType == END_ELEM) {
26725 /*
26726 * Process END of element.
26727 */
26728leave_elem:
26729 ret = xmlSchemaValidatorPopElem(vctxt);
26730 if (ret != 0) {
26731 if (ret < 0) {
26732 VERROR_INT("xmlSchemaVReaderWalk",
26733 "calling xmlSchemaValidatorPopElem()");
26734 goto internal_error;
26735 }
26736 goto exit;
26737 }
26738 if (vctxt->depth >= 0)
26739 ielem = vctxt->inode;
26740 else
26741 ielem = NULL;
26742 } else if ((nodeType == XML_TEXT_NODE) ||
26743 (nodeType == XML_CDATA_SECTION_NODE) ||
26744 (nodeType == WHTSP) ||
26745 (nodeType == SIGN_WHTSP)) {
26746 /*
26747 * Process character content.
26748 */
26749 xmlChar *value;
26750
26751 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26752 nodeType = XML_TEXT_NODE;
26753
26754 value = xmlTextReaderValue(vctxt->reader);
26755 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26756 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26757 if (! consumed)
26758 xmlFree(value);
26759 if (ret == -1) {
26760 VERROR_INT("xmlSchemaVReaderWalk",
26761 "calling xmlSchemaVPushText()");
26762 goto internal_error;
26763 }
26764 } else if ((nodeType == XML_ENTITY_NODE) ||
26765 (nodeType == XML_ENTITY_REF_NODE)) {
26766 /*
26767 * VAL TODO: What to do with entities?
26768 */
26769 TODO
26770 }
26771 /*
26772 * Read next node.
26773 */
26774 ret = xmlTextReaderRead(vctxt->reader);
26775 } while (ret == 1);
26776
26777exit:
26778 return (ret);
26779internal_error:
26780 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026781}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026782#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026783
26784/************************************************************************
26785 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026786 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026787 * *
26788 ************************************************************************/
26789
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026790/*
26791* Process text content.
26792*/
26793static void
26794xmlSchemaSAXHandleText(void *ctx,
26795 const xmlChar * ch,
26796 int len)
26797{
26798 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26799
26800 if (vctxt->depth < 0)
26801 return;
26802 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26803 return;
26804 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26805 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26806 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26807 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26808 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26809 "calling xmlSchemaVPushText()");
26810 vctxt->err = -1;
26811 xmlStopParser(vctxt->parserCtxt);
26812 }
26813}
26814
26815/*
26816* Process CDATA content.
26817*/
26818static void
26819xmlSchemaSAXHandleCDataSection(void *ctx,
26820 const xmlChar * ch,
26821 int len)
26822{
26823 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26824
26825 if (vctxt->depth < 0)
26826 return;
26827 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26828 return;
26829 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26830 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26831 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26832 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26833 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26834 "calling xmlSchemaVPushText()");
26835 vctxt->err = -1;
26836 xmlStopParser(vctxt->parserCtxt);
26837 }
26838}
26839
26840static void
26841xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26842 const xmlChar * name ATTRIBUTE_UNUSED)
26843{
26844 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26845
26846 if (vctxt->depth < 0)
26847 return;
26848 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26849 return;
26850 /* SAX VAL TODO: What to do here? */
26851 TODO
26852}
26853
26854static void
26855xmlSchemaSAXHandleStartElementNs(void *ctx,
26856 const xmlChar * localname,
26857 const xmlChar * prefix ATTRIBUTE_UNUSED,
26858 const xmlChar * URI,
26859 int nb_namespaces,
26860 const xmlChar ** namespaces,
26861 int nb_attributes,
26862 int nb_defaulted ATTRIBUTE_UNUSED,
26863 const xmlChar ** attributes)
26864{
26865 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26866 int ret;
26867 xmlSchemaNodeInfoPtr ielem;
26868 int i, j;
26869
26870 /*
26871 * SAX VAL TODO: What to do with nb_defaulted?
26872 */
26873 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026874 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026875 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026876 vctxt->depth++;
26877 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026878 return;
26879 /*
26880 * Push the element.
26881 */
26882 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26883 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26884 "calling xmlSchemaValidatorPushElem()");
26885 goto internal_error;
26886 }
26887 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026888 /*
26889 * TODO: Is this OK?
26890 */
26891 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026892 ielem->localName = localname;
26893 ielem->nsName = URI;
26894 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26895 /*
26896 * Register namespaces on the elem info.
26897 */
26898 if (nb_namespaces != 0) {
26899 /*
26900 * Although the parser builds its own namespace list,
26901 * we have no access to it, so we'll use an own one.
26902 */
26903 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26904 /*
26905 * Store prefix and namespace name.
26906 */
26907 if (ielem->nsBindings == NULL) {
26908 ielem->nsBindings =
26909 (const xmlChar **) xmlMalloc(10 *
26910 sizeof(const xmlChar *));
26911 if (ielem->nsBindings == NULL) {
26912 xmlSchemaVErrMemory(vctxt,
26913 "allocating namespace bindings for SAX validation",
26914 NULL);
26915 goto internal_error;
26916 }
26917 ielem->nbNsBindings = 0;
26918 ielem->sizeNsBindings = 5;
26919 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26920 ielem->sizeNsBindings *= 2;
26921 ielem->nsBindings =
26922 (const xmlChar **) xmlRealloc(
26923 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026924 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026925 if (ielem->nsBindings == NULL) {
26926 xmlSchemaVErrMemory(vctxt,
26927 "re-allocating namespace bindings for SAX validation",
26928 NULL);
26929 goto internal_error;
26930 }
26931 }
26932
26933 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26934 if (namespaces[j+1][0] == 0) {
26935 /*
26936 * Handle xmlns="".
26937 */
26938 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26939 } else
26940 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26941 namespaces[j+1];
26942 ielem->nbNsBindings++;
26943 }
26944 }
26945 /*
26946 * Register attributes.
26947 * SAX VAL TODO: We are not adding namespace declaration
26948 * attributes yet.
26949 */
26950 if (nb_attributes != 0) {
26951 xmlChar *value;
26952
26953 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26954 /*
26955 * Duplicate the value.
26956 */
26957 value = xmlStrndup(attributes[j+3],
26958 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026959 /*
26960 * TODO: Set the node line.
26961 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026962 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026963 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026964 value, 1);
26965 if (ret == -1) {
26966 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26967 "calling xmlSchemaValidatorPushAttribute()");
26968 goto internal_error;
26969 }
26970 }
26971 }
26972 /*
26973 * Validate the element.
26974 */
26975 ret = xmlSchemaValidateElem(vctxt);
26976 if (ret != 0) {
26977 if (ret == -1) {
26978 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26979 "calling xmlSchemaValidateElem()");
26980 goto internal_error;
26981 }
26982 goto exit;
26983 }
26984
26985exit:
26986 return;
26987internal_error:
26988 vctxt->err = -1;
26989 xmlStopParser(vctxt->parserCtxt);
26990 return;
26991}
26992
26993static void
26994xmlSchemaSAXHandleEndElementNs(void *ctx,
26995 const xmlChar * localname ATTRIBUTE_UNUSED,
26996 const xmlChar * prefix ATTRIBUTE_UNUSED,
26997 const xmlChar * URI ATTRIBUTE_UNUSED)
26998{
26999 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27000 int res;
27001
27002 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027003 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027004 */
27005 if (vctxt->skipDepth != -1) {
27006 if (vctxt->depth > vctxt->skipDepth) {
27007 vctxt->depth--;
27008 return;
27009 } else
27010 vctxt->skipDepth = -1;
27011 }
27012 /*
27013 * SAX VAL TODO: Just a temporary check.
27014 */
27015 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
27016 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
27017 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27018 "elem pop mismatch");
27019 }
27020 res = xmlSchemaValidatorPopElem(vctxt);
27021 if (res != 0) {
27022 if (res < 0) {
27023 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27024 "calling xmlSchemaValidatorPopElem()");
27025 goto internal_error;
27026 }
27027 goto exit;
27028 }
27029exit:
27030 return;
27031internal_error:
27032 vctxt->err = -1;
27033 xmlStopParser(vctxt->parserCtxt);
27034 return;
27035}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027036
Daniel Veillard4255d502002-04-16 15:50:10 +000027037/************************************************************************
27038 * *
27039 * Validation interfaces *
27040 * *
27041 ************************************************************************/
27042
27043/**
27044 * xmlSchemaNewValidCtxt:
27045 * @schema: a precompiled XML Schemas
27046 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027047 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000027048 *
27049 * Returns the validation context or NULL in case of error
27050 */
27051xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027052xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
27053{
Daniel Veillard4255d502002-04-16 15:50:10 +000027054 xmlSchemaValidCtxtPtr ret;
27055
27056 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
27057 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027058 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000027059 return (NULL);
27060 }
27061 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027062 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027063 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027064 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027065 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027066 return (ret);
27067}
27068
27069/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027070 * xmlSchemaClearValidCtxt:
27071 * @ctxt: the schema validation context
27072 *
27073 * Free the resources associated to the schema validation context;
27074 * leaves some fields alive intended for reuse of the context.
27075 */
27076static void
27077xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27078{
27079 if (vctxt == NULL)
27080 return;
27081
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027082 /*
27083 * TODO: Should we clear the flags?
27084 * Might be problematic if one reuses the context
27085 * and assumes that the options remain the same.
27086 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027087 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027088 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027089 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027090#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027091 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027092#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027093 vctxt->hasKeyrefs = 0;
27094
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027095 if (vctxt->value != NULL) {
27096 xmlSchemaFreeValue(vctxt->value);
27097 vctxt->value = NULL;
27098 }
27099 /*
27100 * Augmented IDC information.
27101 */
27102 if (vctxt->aidcs != NULL) {
27103 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27104 do {
27105 next = cur->next;
27106 xmlFree(cur);
27107 cur = next;
27108 } while (cur != NULL);
27109 vctxt->aidcs = NULL;
27110 }
27111 if (vctxt->idcNodes != NULL) {
27112 int i;
27113 xmlSchemaPSVIIDCNodePtr item;
27114
27115 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027116 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027117 xmlFree(item->keys);
27118 xmlFree(item);
27119 }
27120 xmlFree(vctxt->idcNodes);
27121 vctxt->idcNodes = NULL;
27122 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027123 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027124 * Note that we won't delete the XPath state pool here.
27125 */
27126 if (vctxt->xpathStates != NULL) {
27127 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27128 vctxt->xpathStates = NULL;
27129 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027130 /*
27131 * Attribute info.
27132 */
27133 if (vctxt->nbAttrInfos != 0) {
27134 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027135 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027136 /*
27137 * Element info.
27138 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027139 if (vctxt->elemInfos != NULL) {
27140 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027141 xmlSchemaNodeInfoPtr ei;
27142
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027143 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027144 ei = vctxt->elemInfos[i];
27145 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027146 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027147 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027148 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027149 }
27150 xmlSchemaItemListClear(vctxt->nodeQNames);
27151 /* Recreate the dict. */
27152 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027153 /*
27154 * TODO: Is is save to recreate it? Do we have a scenario
27155 * where the user provides the dict?
27156 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027157 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027158}
27159
27160/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027161 * xmlSchemaFreeValidCtxt:
27162 * @ctxt: the schema validation context
27163 *
27164 * Free the resources associated to the schema validation context
27165 */
27166void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027167xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27168{
Daniel Veillard4255d502002-04-16 15:50:10 +000027169 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027170 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027171 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027172 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027173 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027174 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027175 if (ctxt->idcNodes != NULL) {
27176 int i;
27177 xmlSchemaPSVIIDCNodePtr item;
27178
27179 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027180 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027181 xmlFree(item->keys);
27182 xmlFree(item);
27183 }
27184 xmlFree(ctxt->idcNodes);
27185 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027186 if (ctxt->idcKeys != NULL) {
27187 int i;
27188 for (i = 0; i < ctxt->nbIdcKeys; i++)
27189 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27190 xmlFree(ctxt->idcKeys);
27191 }
27192
27193 if (ctxt->xpathStates != NULL)
27194 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27195 if (ctxt->xpathStatePool != NULL)
27196 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27197
27198 /*
27199 * Augmented IDC information.
27200 */
27201 if (ctxt->aidcs != NULL) {
27202 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27203 do {
27204 next = cur->next;
27205 xmlFree(cur);
27206 cur = next;
27207 } while (cur != NULL);
27208 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027209 if (ctxt->attrInfos != NULL) {
27210 int i;
27211 xmlSchemaAttrInfoPtr attr;
27212
27213 /* Just a paranoid call to the cleanup. */
27214 if (ctxt->nbAttrInfos != 0)
27215 xmlSchemaClearAttrInfos(ctxt);
27216 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27217 attr = ctxt->attrInfos[i];
27218 xmlFree(attr);
27219 }
27220 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027221 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027222 if (ctxt->elemInfos != NULL) {
27223 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027224 xmlSchemaNodeInfoPtr ei;
27225
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027226 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027227 ei = ctxt->elemInfos[i];
27228 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027229 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027230 xmlSchemaClearElemInfo(ei);
27231 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027232 }
27233 xmlFree(ctxt->elemInfos);
27234 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027235 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027236 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027237 if (ctxt->dict != NULL)
27238 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027239 xmlFree(ctxt);
27240}
27241
27242/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027243 * xmlSchemaIsValid:
27244 * @ctxt: the schema validation context
27245 *
27246 * Check if any error was detected during validation.
27247 *
27248 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27249 * of internal error.
27250 */
27251int
27252xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27253{
27254 if (ctxt == NULL)
27255 return(-1);
27256 return(ctxt->err == 0);
27257}
27258
27259/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027260 * xmlSchemaSetValidErrors:
27261 * @ctxt: a schema validation context
27262 * @err: the error function
27263 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027264 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027265 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027266 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027267 */
27268void
27269xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027270 xmlSchemaValidityErrorFunc err,
27271 xmlSchemaValidityWarningFunc warn, void *ctx)
27272{
Daniel Veillard4255d502002-04-16 15:50:10 +000027273 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027274 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027275 ctxt->error = err;
27276 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027277 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027278 if (ctxt->pctxt != NULL)
27279 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027280}
27281
27282/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027283 * xmlSchemaSetValidStructuredErrors:
27284 * @ctxt: a schema validation context
27285 * @serror: the structured error function
27286 * @ctx: the functions context
27287 *
27288 * Set the structured error callback
27289 */
27290void
27291xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027292 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027293{
27294 if (ctxt == NULL)
27295 return;
27296 ctxt->serror = serror;
27297 ctxt->error = NULL;
27298 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027299 ctxt->errCtxt = ctx;
27300 if (ctxt->pctxt != NULL)
27301 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027302}
27303
27304/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027305 * xmlSchemaGetValidErrors:
27306 * @ctxt: a XML-Schema validation context
27307 * @err: the error function result
27308 * @warn: the warning function result
27309 * @ctx: the functions context result
27310 *
27311 * Get the error and warning callback informations
27312 *
27313 * Returns -1 in case of error and 0 otherwise
27314 */
27315int
27316xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027317 xmlSchemaValidityErrorFunc * err,
27318 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027319{
27320 if (ctxt == NULL)
27321 return (-1);
27322 if (err != NULL)
27323 *err = ctxt->error;
27324 if (warn != NULL)
27325 *warn = ctxt->warning;
27326 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027327 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027328 return (0);
27329}
27330
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027331
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027332/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027333 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027334 * @ctxt: a schema validation context
27335 * @options: a combination of xmlSchemaValidOption
27336 *
27337 * Sets the options to be used during the validation.
27338 *
27339 * Returns 0 in case of success, -1 in case of an
27340 * API error.
27341 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027342int
27343xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27344 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027345
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027346{
27347 int i;
27348
27349 if (ctxt == NULL)
27350 return (-1);
27351 /*
27352 * WARNING: Change the start value if adding to the
27353 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027354 * TODO: Is there an other, more easy to maintain,
27355 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027356 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027357 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027358 if (options & 1<<i)
27359 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027360 }
27361 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027362 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027363}
27364
27365/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027366 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027367 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027368 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027369 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027370 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027371 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027372 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027373int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027374xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027375
27376{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027377 if (ctxt == NULL)
27378 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027379 else
27380 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027381}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027382
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027383static int
27384xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27385{
27386 xmlAttrPtr attr;
27387 int ret = 0;
27388 xmlSchemaNodeInfoPtr ielem = NULL;
27389 xmlNodePtr node, valRoot;
27390 const xmlChar *nsName;
27391
27392 /* DOC VAL TODO: Move this to the start function. */
27393 valRoot = xmlDocGetRootElement(vctxt->doc);
27394 if (valRoot == NULL) {
27395 /* VAL TODO: Error code? */
27396 VERROR(1, NULL, "The document has no document element");
27397 return (1);
27398 }
27399 vctxt->depth = -1;
27400 vctxt->validationRoot = valRoot;
27401 node = valRoot;
27402 while (node != NULL) {
27403 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27404 goto next_sibling;
27405 if (node->type == XML_ELEMENT_NODE) {
27406
27407 /*
27408 * Init the node-info.
27409 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027410 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027411 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27412 goto internal_error;
27413 ielem = vctxt->inode;
27414 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027415 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027416 ielem->localName = node->name;
27417 if (node->ns != NULL)
27418 ielem->nsName = node->ns->href;
27419 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27420 /*
27421 * Register attributes.
27422 * DOC VAL TODO: We do not register namespace declaration
27423 * attributes yet.
27424 */
27425 vctxt->nbAttrInfos = 0;
27426 if (node->properties != NULL) {
27427 attr = node->properties;
27428 do {
27429 if (attr->ns != NULL)
27430 nsName = attr->ns->href;
27431 else
27432 nsName = NULL;
27433 ret = xmlSchemaValidatorPushAttribute(vctxt,
27434 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027435 /*
27436 * Note that we give it the line number of the
27437 * parent element.
27438 */
27439 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027440 attr->name, nsName, 0,
27441 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27442 if (ret == -1) {
27443 VERROR_INT("xmlSchemaDocWalk",
27444 "calling xmlSchemaValidatorPushAttribute()");
27445 goto internal_error;
27446 }
27447 attr = attr->next;
27448 } while (attr);
27449 }
27450 /*
27451 * Validate the element.
27452 */
27453 ret = xmlSchemaValidateElem(vctxt);
27454 if (ret != 0) {
27455 if (ret == -1) {
27456 VERROR_INT("xmlSchemaDocWalk",
27457 "calling xmlSchemaValidateElem()");
27458 goto internal_error;
27459 }
27460 /*
27461 * Don't stop validation; just skip the content
27462 * of this element.
27463 */
27464 goto leave_node;
27465 }
27466 if ((vctxt->skipDepth != -1) &&
27467 (vctxt->depth >= vctxt->skipDepth))
27468 goto leave_node;
27469 } else if ((node->type == XML_TEXT_NODE) ||
27470 (node->type == XML_CDATA_SECTION_NODE)) {
27471 /*
27472 * Process character content.
27473 */
Daniel Veillard14b56432006-03-09 18:41:40 +000027474 if ((ielem != NULL) && (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027475 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27476 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27477 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27478 if (ret < 0) {
27479 VERROR_INT("xmlSchemaVDocWalk",
27480 "calling xmlSchemaVPushText()");
27481 goto internal_error;
27482 }
27483 /*
27484 * DOC VAL TODO: Should we skip further validation of the
27485 * element content here?
27486 */
27487 } else if ((node->type == XML_ENTITY_NODE) ||
27488 (node->type == XML_ENTITY_REF_NODE)) {
27489 /*
27490 * DOC VAL TODO: What to do with entities?
27491 */
27492 TODO
27493 } else {
27494 goto leave_node;
27495 /*
27496 * DOC VAL TODO: XInclude nodes, etc.
27497 */
27498 }
27499 /*
27500 * Walk the doc.
27501 */
27502 if (node->children != NULL) {
27503 node = node->children;
27504 continue;
27505 }
27506leave_node:
27507 if (node->type == XML_ELEMENT_NODE) {
27508 /*
27509 * Leaving the scope of an element.
27510 */
27511 if (node != vctxt->inode->node) {
27512 VERROR_INT("xmlSchemaVDocWalk",
27513 "element position mismatch");
27514 goto internal_error;
27515 }
27516 ret = xmlSchemaValidatorPopElem(vctxt);
27517 if (ret != 0) {
27518 if (ret < 0) {
27519 VERROR_INT("xmlSchemaVDocWalk",
27520 "calling xmlSchemaValidatorPopElem()");
27521 goto internal_error;
27522 }
27523 }
27524 if (node == valRoot)
27525 goto exit;
27526 }
27527next_sibling:
27528 if (node->next != NULL)
27529 node = node->next;
27530 else {
27531 node = node->parent;
27532 goto leave_node;
27533 }
27534 }
27535
27536exit:
27537 return (ret);
27538internal_error:
27539 return (-1);
27540}
27541
27542static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027543xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027544 /*
27545 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027546 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027547 vctxt->err = 0;
27548 vctxt->nberrors = 0;
27549 vctxt->depth = -1;
27550 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027551 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027552 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000027553#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027554 vctxt->createIDCNodeTables = 1;
27555#else
27556 vctxt->createIDCNodeTables = 0;
27557#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027558 /*
27559 * Create a schema + parser if necessary.
27560 */
27561 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027562 xmlSchemaParserCtxtPtr pctxt;
27563
27564 vctxt->xsiAssemble = 1;
27565 /*
27566 * If not schema was given then we will create a schema
27567 * dynamically using XSI schema locations.
27568 *
27569 * Create the schema parser context.
27570 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027571 if ((vctxt->pctxt == NULL) &&
27572 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27573 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027574 pctxt = vctxt->pctxt;
27575 pctxt->xsiAssemble = 1;
27576 /*
27577 * Create the schema.
27578 */
27579 vctxt->schema = xmlSchemaNewSchema(pctxt);
27580 if (vctxt->schema == NULL)
27581 return (-1);
27582 /*
27583 * Create the schema construction context.
27584 */
27585 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27586 if (pctxt->constructor == NULL)
27587 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027588 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027589 /*
27590 * Take ownership of the constructor to be able to free it.
27591 */
27592 pctxt->ownsConstructor = 1;
27593 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027594 /*
27595 * Augment the IDC definitions.
27596 */
27597 if (vctxt->schema->idcDef != NULL) {
27598 xmlHashScan(vctxt->schema->idcDef,
27599 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27600 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027601 return(0);
27602}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027603
Daniel Veillardf10ae122005-07-10 19:03:16 +000027604static void
27605xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027606 if (vctxt->xsiAssemble) {
27607 if (vctxt->schema != NULL) {
27608 xmlSchemaFree(vctxt->schema);
27609 vctxt->schema = NULL;
27610 }
27611 }
27612 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027613}
27614
27615static int
27616xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27617{
27618 int ret = 0;
27619
27620 if (xmlSchemaPreRun(vctxt) < 0)
27621 return(-1);
27622
27623 if (vctxt->doc != NULL) {
27624 /*
27625 * Tree validation.
27626 */
27627 ret = xmlSchemaVDocWalk(vctxt);
27628#ifdef LIBXML_READER_ENABLED
27629 } else if (vctxt->reader != NULL) {
27630 /*
27631 * XML Reader validation.
27632 */
27633#ifdef XML_SCHEMA_READER_ENABLED
27634 ret = xmlSchemaVReaderWalk(vctxt);
27635#endif
27636#endif
27637 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27638 /*
27639 * SAX validation.
27640 */
27641 ret = xmlParseDocument(vctxt->parserCtxt);
27642 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027643 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027644 "no instance to validate");
27645 ret = -1;
27646 }
27647
27648 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027649 if (ret == 0)
27650 ret = vctxt->err;
27651 return (ret);
27652}
27653
27654/**
27655 * xmlSchemaValidateOneElement:
27656 * @ctxt: a schema validation context
27657 * @elem: an element node
27658 *
27659 * Validate a branch of a tree, starting with the given @elem.
27660 *
27661 * Returns 0 if the element and its subtree is valid, a positive error
27662 * code number otherwise and -1 in case of an internal or API error.
27663 */
27664int
27665xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27666{
27667 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27668 return (-1);
27669
27670 if (ctxt->schema == NULL)
27671 return (-1);
27672
27673 ctxt->doc = elem->doc;
27674 ctxt->node = elem;
27675 ctxt->validationRoot = elem;
27676 return(xmlSchemaVStart(ctxt));
27677}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027678
Daniel Veillard259f0df2004-08-18 09:13:18 +000027679/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027680 * xmlSchemaValidateDoc:
27681 * @ctxt: a schema validation context
27682 * @doc: a parsed document tree
27683 *
27684 * Validate a document tree in memory.
27685 *
27686 * Returns 0 if the document is schemas valid, a positive error code
27687 * number otherwise and -1 in case of internal or API error.
27688 */
27689int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027690xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27691{
Daniel Veillard4255d502002-04-16 15:50:10 +000027692 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027693 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027694
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027695 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027696 ctxt->node = xmlDocGetRootElement(doc);
27697 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027698 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027699 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27700 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027701 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027702 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027703 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027704 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027705 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027706}
27707
Daniel Veillardcdc82732005-07-08 15:04:06 +000027708
27709/************************************************************************
27710 * *
27711 * Function and data for SAX streaming API *
27712 * *
27713 ************************************************************************/
27714typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27715typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27716
27717struct _xmlSchemaSplitSAXData {
27718 xmlSAXHandlerPtr user_sax;
27719 void *user_data;
27720 xmlSchemaValidCtxtPtr ctxt;
27721 xmlSAXHandlerPtr schemas_sax;
27722};
27723
Daniel Veillard971771e2005-07-09 17:32:57 +000027724#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27725
27726struct _xmlSchemaSAXPlug {
27727 unsigned int magic;
27728
27729 /* the original callbacks informations */
27730 xmlSAXHandlerPtr *user_sax_ptr;
27731 xmlSAXHandlerPtr user_sax;
27732 void **user_data_ptr;
27733 void *user_data;
27734
27735 /* the block plugged back and validation informations */
27736 xmlSAXHandler schemas_sax;
27737 xmlSchemaValidCtxtPtr ctxt;
27738};
27739
Daniel Veillardcdc82732005-07-08 15:04:06 +000027740/* All those functions just bounces to the user provided SAX handlers */
27741static void
27742internalSubsetSplit(void *ctx, const xmlChar *name,
27743 const xmlChar *ExternalID, const xmlChar *SystemID)
27744{
Daniel Veillard971771e2005-07-09 17:32:57 +000027745 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027746 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27747 (ctxt->user_sax->internalSubset != NULL))
27748 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27749 SystemID);
27750}
27751
27752static int
27753isStandaloneSplit(void *ctx)
27754{
Daniel Veillard971771e2005-07-09 17:32:57 +000027755 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027756 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27757 (ctxt->user_sax->isStandalone != NULL))
27758 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27759 return(0);
27760}
27761
27762static int
27763hasInternalSubsetSplit(void *ctx)
27764{
Daniel Veillard971771e2005-07-09 17:32:57 +000027765 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027766 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27767 (ctxt->user_sax->hasInternalSubset != NULL))
27768 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27769 return(0);
27770}
27771
27772static int
27773hasExternalSubsetSplit(void *ctx)
27774{
Daniel Veillard971771e2005-07-09 17:32:57 +000027775 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027776 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27777 (ctxt->user_sax->hasExternalSubset != NULL))
27778 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27779 return(0);
27780}
27781
27782static void
27783externalSubsetSplit(void *ctx, const xmlChar *name,
27784 const xmlChar *ExternalID, const xmlChar *SystemID)
27785{
Daniel Veillard971771e2005-07-09 17:32:57 +000027786 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027787 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27788 (ctxt->user_sax->internalSubset != NULL))
27789 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27790 SystemID);
27791}
27792
27793static xmlParserInputPtr
27794resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27795{
Daniel Veillard971771e2005-07-09 17:32:57 +000027796 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027797 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27798 (ctxt->user_sax->resolveEntity != NULL))
27799 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27800 systemId));
27801 return(NULL);
27802}
27803
27804static xmlEntityPtr
27805getEntitySplit(void *ctx, const xmlChar *name)
27806{
Daniel Veillard971771e2005-07-09 17:32:57 +000027807 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027808 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27809 (ctxt->user_sax->getEntity != NULL))
27810 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27811 return(NULL);
27812}
27813
27814static xmlEntityPtr
27815getParameterEntitySplit(void *ctx, const xmlChar *name)
27816{
Daniel Veillard971771e2005-07-09 17:32:57 +000027817 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027818 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27819 (ctxt->user_sax->getParameterEntity != NULL))
27820 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27821 return(NULL);
27822}
27823
27824
27825static void
27826entityDeclSplit(void *ctx, const xmlChar *name, int type,
27827 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27828{
Daniel Veillard971771e2005-07-09 17:32:57 +000027829 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027830 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27831 (ctxt->user_sax->entityDecl != NULL))
27832 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27833 systemId, content);
27834}
27835
27836static void
27837attributeDeclSplit(void *ctx, const xmlChar * elem,
27838 const xmlChar * name, int type, int def,
27839 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27840{
Daniel Veillard971771e2005-07-09 17:32:57 +000027841 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027842 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27843 (ctxt->user_sax->attributeDecl != NULL)) {
27844 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27845 def, defaultValue, tree);
27846 } else {
27847 xmlFreeEnumeration(tree);
27848 }
27849}
27850
27851static void
27852elementDeclSplit(void *ctx, const xmlChar *name, int type,
27853 xmlElementContentPtr content)
27854{
Daniel Veillard971771e2005-07-09 17:32:57 +000027855 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027856 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27857 (ctxt->user_sax->elementDecl != NULL))
27858 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27859}
27860
27861static void
27862notationDeclSplit(void *ctx, const xmlChar *name,
27863 const xmlChar *publicId, const xmlChar *systemId)
27864{
Daniel Veillard971771e2005-07-09 17:32:57 +000027865 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027866 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27867 (ctxt->user_sax->notationDecl != NULL))
27868 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27869 systemId);
27870}
27871
27872static void
27873unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27874 const xmlChar *publicId, const xmlChar *systemId,
27875 const xmlChar *notationName)
27876{
Daniel Veillard971771e2005-07-09 17:32:57 +000027877 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027878 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27879 (ctxt->user_sax->unparsedEntityDecl != NULL))
27880 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27881 systemId, notationName);
27882}
27883
27884static void
27885setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27886{
Daniel Veillard971771e2005-07-09 17:32:57 +000027887 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027888 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27889 (ctxt->user_sax->setDocumentLocator != NULL))
27890 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27891}
27892
27893static void
27894startDocumentSplit(void *ctx)
27895{
Daniel Veillard971771e2005-07-09 17:32:57 +000027896 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027897 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27898 (ctxt->user_sax->startDocument != NULL))
27899 ctxt->user_sax->startDocument(ctxt->user_data);
27900}
27901
27902static void
27903endDocumentSplit(void *ctx)
27904{
Daniel Veillard971771e2005-07-09 17:32:57 +000027905 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027906 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27907 (ctxt->user_sax->endDocument != NULL))
27908 ctxt->user_sax->endDocument(ctxt->user_data);
27909}
27910
27911static void
27912processingInstructionSplit(void *ctx, const xmlChar *target,
27913 const xmlChar *data)
27914{
Daniel Veillard971771e2005-07-09 17:32:57 +000027915 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027916 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27917 (ctxt->user_sax->processingInstruction != NULL))
27918 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27919}
27920
27921static void
27922commentSplit(void *ctx, const xmlChar *value)
27923{
Daniel Veillard971771e2005-07-09 17:32:57 +000027924 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027925 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27926 (ctxt->user_sax->comment != NULL))
27927 ctxt->user_sax->comment(ctxt->user_data, value);
27928}
27929
27930/*
27931 * Varargs error callbacks to the user application, harder ...
27932 */
27933
Daniel Veillardffa3c742005-07-21 13:24:09 +000027934static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027935warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027936 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027937 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27938 (ctxt->user_sax->warning != NULL)) {
27939 TODO
27940 }
27941}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027942static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027943errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027944 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027945 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27946 (ctxt->user_sax->error != NULL)) {
27947 TODO
27948 }
27949}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027950static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027951fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027952 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027953 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27954 (ctxt->user_sax->fatalError != NULL)) {
27955 TODO
27956 }
27957}
27958
27959/*
27960 * Those are function where both the user handler and the schemas handler
27961 * need to be called.
27962 */
27963static void
27964charactersSplit(void *ctx, const xmlChar *ch, int len)
27965{
Daniel Veillard971771e2005-07-09 17:32:57 +000027966 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027967 if (ctxt == NULL)
27968 return;
27969 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27970 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27971 if (ctxt->ctxt != NULL)
27972 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27973}
27974
27975static void
27976ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27977{
Daniel Veillard971771e2005-07-09 17:32:57 +000027978 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027979 if (ctxt == NULL)
27980 return;
27981 if ((ctxt->user_sax != NULL) &&
27982 (ctxt->user_sax->ignorableWhitespace != NULL))
27983 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27984 if (ctxt->ctxt != NULL)
27985 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27986}
27987
27988static void
27989cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27990{
Daniel Veillard971771e2005-07-09 17:32:57 +000027991 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027992 if (ctxt == NULL)
27993 return;
27994 if ((ctxt->user_sax != NULL) &&
27995 (ctxt->user_sax->ignorableWhitespace != NULL))
27996 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27997 if (ctxt->ctxt != NULL)
27998 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27999}
28000
28001static void
28002referenceSplit(void *ctx, const xmlChar *name)
28003{
Daniel Veillard971771e2005-07-09 17:32:57 +000028004 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillard14b56432006-03-09 18:41:40 +000028005 if (ctxt == NULL)
28006 return;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028007 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28008 (ctxt->user_sax->reference != NULL))
28009 ctxt->user_sax->reference(ctxt->user_data, name);
28010 if (ctxt->ctxt != NULL)
28011 xmlSchemaSAXHandleReference(ctxt->user_data, name);
28012}
28013
28014static void
28015startElementNsSplit(void *ctx, const xmlChar * localname,
28016 const xmlChar * prefix, const xmlChar * URI,
28017 int nb_namespaces, const xmlChar ** namespaces,
28018 int nb_attributes, int nb_defaulted,
28019 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028020 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028021 if (ctxt == NULL)
28022 return;
28023 if ((ctxt->user_sax != NULL) &&
28024 (ctxt->user_sax->startElementNs != NULL))
28025 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
28026 URI, nb_namespaces, namespaces,
28027 nb_attributes, nb_defaulted,
28028 attributes);
28029 if (ctxt->ctxt != NULL)
28030 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
28031 URI, nb_namespaces, namespaces,
28032 nb_attributes, nb_defaulted,
28033 attributes);
28034}
28035
28036static void
28037endElementNsSplit(void *ctx, const xmlChar * localname,
28038 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028039 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028040 if (ctxt == NULL)
28041 return;
28042 if ((ctxt->user_sax != NULL) &&
28043 (ctxt->user_sax->endElementNs != NULL))
28044 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
28045 if (ctxt->ctxt != NULL)
28046 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
28047}
28048
Daniel Veillard4255d502002-04-16 15:50:10 +000028049/**
Daniel Veillard971771e2005-07-09 17:32:57 +000028050 * xmlSchemaSAXPlug:
28051 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000028052 * @sax: a pointer to the original xmlSAXHandlerPtr
28053 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000028054 *
28055 * Plug a SAX based validation layer in a SAX parsing event flow.
28056 * The original @saxptr and @dataptr data are replaced by new pointers
28057 * but the calls to the original will be maintained.
28058 *
28059 * Returns a pointer to a data structure needed to unplug the validation layer
28060 * or NULL in case of errors.
28061 */
28062xmlSchemaSAXPlugPtr
28063xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28064 xmlSAXHandlerPtr *sax, void **user_data)
28065{
28066 xmlSchemaSAXPlugPtr ret;
28067 xmlSAXHandlerPtr old_sax;
28068
28069 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28070 return(NULL);
28071
28072 /*
28073 * We only allow to plug into SAX2 event streams
28074 */
28075 old_sax = *sax;
28076 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28077 return(NULL);
28078 if ((old_sax != NULL) &&
28079 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28080 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28081 return(NULL);
28082
28083 /*
28084 * everything seems right allocate the local data needed for that layer
28085 */
28086 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28087 if (ret == NULL) {
28088 return(NULL);
28089 }
28090 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28091 ret->magic = XML_SAX_PLUG_MAGIC;
28092 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28093 ret->ctxt = ctxt;
28094 ret->user_sax_ptr = sax;
28095 ret->user_sax = old_sax;
28096 if (old_sax == NULL) {
28097 /*
28098 * go direct, no need for the split block and functions.
28099 */
28100 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28101 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28102 /*
28103 * Note that we use the same text-function for both, to prevent
28104 * the parser from testing for ignorable whitespace.
28105 */
28106 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28107 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28108
28109 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28110 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28111
28112 ret->user_data = ctxt;
28113 *user_data = ctxt;
28114 } else {
28115 /*
28116 * for each callback unused by Schemas initialize it to the Split
28117 * routine only if non NULL in the user block, this can speed up
28118 * things at the SAX level.
28119 */
28120 if (old_sax->internalSubset != NULL)
28121 ret->schemas_sax.internalSubset = internalSubsetSplit;
28122 if (old_sax->isStandalone != NULL)
28123 ret->schemas_sax.isStandalone = isStandaloneSplit;
28124 if (old_sax->hasInternalSubset != NULL)
28125 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28126 if (old_sax->hasExternalSubset != NULL)
28127 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28128 if (old_sax->resolveEntity != NULL)
28129 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28130 if (old_sax->getEntity != NULL)
28131 ret->schemas_sax.getEntity = getEntitySplit;
28132 if (old_sax->entityDecl != NULL)
28133 ret->schemas_sax.entityDecl = entityDeclSplit;
28134 if (old_sax->notationDecl != NULL)
28135 ret->schemas_sax.notationDecl = notationDeclSplit;
28136 if (old_sax->attributeDecl != NULL)
28137 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28138 if (old_sax->elementDecl != NULL)
28139 ret->schemas_sax.elementDecl = elementDeclSplit;
28140 if (old_sax->unparsedEntityDecl != NULL)
28141 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28142 if (old_sax->setDocumentLocator != NULL)
28143 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28144 if (old_sax->startDocument != NULL)
28145 ret->schemas_sax.startDocument = startDocumentSplit;
28146 if (old_sax->endDocument != NULL)
28147 ret->schemas_sax.endDocument = endDocumentSplit;
28148 if (old_sax->processingInstruction != NULL)
28149 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28150 if (old_sax->comment != NULL)
28151 ret->schemas_sax.comment = commentSplit;
28152 if (old_sax->warning != NULL)
28153 ret->schemas_sax.warning = warningSplit;
28154 if (old_sax->error != NULL)
28155 ret->schemas_sax.error = errorSplit;
28156 if (old_sax->fatalError != NULL)
28157 ret->schemas_sax.fatalError = fatalErrorSplit;
28158 if (old_sax->getParameterEntity != NULL)
28159 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28160 if (old_sax->externalSubset != NULL)
28161 ret->schemas_sax.externalSubset = externalSubsetSplit;
28162
28163 /*
28164 * the 6 schemas callback have to go to the splitter functions
28165 * Note that we use the same text-function for ignorableWhitespace
28166 * if possible, to prevent the parser from testing for ignorable
28167 * whitespace.
28168 */
28169 ret->schemas_sax.characters = charactersSplit;
28170 if ((old_sax->ignorableWhitespace != NULL) &&
28171 (old_sax->ignorableWhitespace != old_sax->characters))
28172 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28173 else
28174 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28175 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28176 ret->schemas_sax.reference = referenceSplit;
28177 ret->schemas_sax.startElementNs = startElementNsSplit;
28178 ret->schemas_sax.endElementNs = endElementNsSplit;
28179
28180 ret->user_data_ptr = user_data;
28181 ret->user_data = *user_data;
28182 *user_data = ret;
28183 }
28184
28185 /*
28186 * plug the pointers back.
28187 */
28188 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028189 ctxt->sax = *sax;
28190 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28191 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028192 return(ret);
28193}
28194
28195/**
28196 * xmlSchemaSAXUnplug:
28197 * @plug: a data structure returned by xmlSchemaSAXPlug
28198 *
28199 * Unplug a SAX based validation layer in a SAX parsing event flow.
28200 * The original pointers used in the call are restored.
28201 *
28202 * Returns 0 in case of success and -1 in case of failure.
28203 */
28204int
28205xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28206{
28207 xmlSAXHandlerPtr *sax;
28208 void **user_data;
28209
28210 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28211 return(-1);
28212 plug->magic = 0;
28213
Daniel Veillardf10ae122005-07-10 19:03:16 +000028214 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028215 /* restore the data */
28216 sax = plug->user_sax_ptr;
28217 *sax = plug->user_sax;
28218 if (plug->user_sax != NULL) {
28219 user_data = plug->user_data_ptr;
28220 *user_data = plug->user_data;
28221 }
28222
28223 /* free and return */
28224 xmlFree(plug);
28225 return(0);
28226}
28227
28228/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028229 * xmlSchemaValidateStream:
28230 * @ctxt: a schema validation context
28231 * @input: the input to use for reading the data
28232 * @enc: an optional encoding information
28233 * @sax: a SAX handler for the resulting events
28234 * @user_data: the context to provide to the SAX handler.
28235 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028236 * Validate an input based on a flow of SAX event from the parser
28237 * and forward the events to the @sax handler with the provided @user_data
28238 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028239 *
28240 * Returns 0 if the document is schemas valid, a positive error code
28241 * number otherwise and -1 in case of internal or API error.
28242 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028243int
Daniel Veillard4255d502002-04-16 15:50:10 +000028244xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028245 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28246 xmlSAXHandlerPtr sax, void *user_data)
28247{
Daniel Veillard971771e2005-07-09 17:32:57 +000028248 xmlSchemaSAXPlugPtr plug = NULL;
28249 xmlSAXHandlerPtr old_sax = NULL;
28250 xmlParserCtxtPtr pctxt = NULL;
28251 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028252 int ret;
28253
Daniel Veillard4255d502002-04-16 15:50:10 +000028254 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028255 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028256
Daniel Veillardcdc82732005-07-08 15:04:06 +000028257 /*
28258 * prepare the parser
28259 */
28260 pctxt = xmlNewParserCtxt();
28261 if (pctxt == NULL)
28262 return (-1);
28263 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028264 pctxt->sax = sax;
28265 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028266#if 0
28267 if (options)
28268 xmlCtxtUseOptions(pctxt, options);
28269#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028270 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028271
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028272 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028273 if (inputStream == NULL) {
28274 ret = -1;
28275 goto done;
28276 }
28277 inputPush(pctxt, inputStream);
28278 ctxt->parserCtxt = pctxt;
28279 ctxt->input = input;
28280
28281 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028282 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028283 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028284 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28285 if (plug == NULL) {
28286 ret = -1;
28287 goto done;
28288 }
28289 ctxt->input = input;
28290 ctxt->enc = enc;
28291 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028292 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28293 ret = xmlSchemaVStart(ctxt);
28294
28295 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28296 ret = ctxt->parserCtxt->errNo;
28297 if (ret == 0)
28298 ret = 1;
28299 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028300
28301done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028302 ctxt->parserCtxt = NULL;
28303 ctxt->sax = NULL;
28304 ctxt->input = NULL;
28305 if (plug != NULL) {
28306 xmlSchemaSAXUnplug(plug);
28307 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028308 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028309 if (pctxt != NULL) {
28310 pctxt->sax = old_sax;
28311 xmlFreeParserCtxt(pctxt);
28312 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028313 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028314}
28315
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028316/**
28317 * xmlSchemaValidateFile:
28318 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028319 * @filename: the URI of the instance
28320 * @options: a future set of options, currently unused
28321 *
28322 * Do a schemas validation of the given resource, it will use the
28323 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028324 *
28325 * Returns 0 if the document is valid, a positive error code
28326 * number otherwise and -1 in case of an internal or API error.
28327 */
28328int
28329xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028330 const char * filename,
28331 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028332{
28333 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028334 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028335
28336 if ((ctxt == NULL) || (filename == NULL))
28337 return (-1);
28338
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028339 input = xmlParserInputBufferCreateFilename(filename,
28340 XML_CHAR_ENCODING_NONE);
28341 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028342 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028343 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28344 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028345 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028346}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028347
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028348#define bottom_xmlschemas
28349#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028350#endif /* LIBXML_SCHEMAS_ENABLED */