blob: 3baae056216abe36539255fa07ec1ba76332a9c2 [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. Buchcikb63d2fa2006-04-19 11:20:49 +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;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001777 int res, found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001778
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;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001791 found = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001792 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1793 ws, &value);
1794 if (res == -1) {
1795 xmlSchemaInternalErr(actxt,
1796 "xmlSchemaFormatFacetEnumSet",
1797 "compute the canonical lexical representation");
1798 if (*buf != NULL)
1799 xmlFree(*buf);
1800 *buf = NULL;
1801 return (NULL);
1802 }
1803 if (*buf == NULL)
1804 *buf = xmlStrdup(BAD_CAST "'");
1805 else
1806 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1807 *buf = xmlStrcat(*buf, BAD_CAST value);
1808 *buf = xmlStrcat(*buf, BAD_CAST "'");
1809 if (value != NULL) {
1810 xmlFree((xmlChar *)value);
1811 value = NULL;
1812 }
1813 }
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001814 /*
1815 * The enumeration facet of a type restricts the enumeration
1816 * facet of the ancestor type; i.e., such restricted enumerations
1817 * do not belong to the set of the given type. Thus we break
1818 * on the first found enumeration.
1819 */
1820 if (found)
1821 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001822 type = type->baseType;
1823 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1824
1825 return ((const xmlChar *) *buf);
1826}
1827
1828/************************************************************************
1829 * *
1830 * Error functions *
1831 * *
1832 ************************************************************************/
1833
1834#if 0
1835static void
1836xmlSchemaErrMemory(const char *msg)
1837{
1838 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1839 msg);
1840}
1841#endif
1842
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001843static void
1844xmlSchemaPSimpleErr(const char *msg)
1845{
1846 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1847 msg);
1848}
1849
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001850/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001851 * xmlSchemaPErrMemory:
1852 * @node: a context node
1853 * @extra: extra informations
1854 *
1855 * Handle an out of memory condition
1856 */
1857static void
1858xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1859 const char *extra, xmlNodePtr node)
1860{
1861 if (ctxt != NULL)
1862 ctxt->nberrors++;
1863 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1864 extra);
1865}
1866
1867/**
1868 * xmlSchemaPErr:
1869 * @ctxt: the parsing context
1870 * @node: the context node
1871 * @error: the error code
1872 * @msg: the error message
1873 * @str1: extra data
1874 * @str2: extra data
1875 *
1876 * Handle a parser error
1877 */
1878static void
1879xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1880 const char *msg, const xmlChar * str1, const xmlChar * str2)
1881{
1882 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001883 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001884 void *data = NULL;
1885
1886 if (ctxt != NULL) {
1887 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001888 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001889 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001890 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001891 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001892 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001893 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001894 error, XML_ERR_ERROR, NULL, 0,
1895 (const char *) str1, (const char *) str2, NULL, 0, 0,
1896 msg, str1, str2);
1897}
1898
1899/**
1900 * xmlSchemaPErr2:
1901 * @ctxt: the parsing context
1902 * @node: the context node
1903 * @node: the current child
1904 * @error: the error code
1905 * @msg: the error message
1906 * @str1: extra data
1907 * @str2: extra data
1908 *
1909 * Handle a parser error
1910 */
1911static void
1912xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1913 xmlNodePtr child, int error,
1914 const char *msg, const xmlChar * str1, const xmlChar * str2)
1915{
1916 if (child != NULL)
1917 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1918 else
1919 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1920}
1921
Daniel Veillard01fa6152004-06-29 17:04:39 +00001922
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001923/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001924 * xmlSchemaPErrExt:
1925 * @ctxt: the parsing context
1926 * @node: the context node
1927 * @error: the error code
1928 * @strData1: extra data
1929 * @strData2: extra data
1930 * @strData3: extra data
1931 * @msg: the message
1932 * @str1: extra parameter for the message display
1933 * @str2: extra parameter for the message display
1934 * @str3: extra parameter for the message display
1935 * @str4: extra parameter for the message display
1936 * @str5: extra parameter for the message display
1937 *
1938 * Handle a parser error
1939 */
1940static void
1941xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1942 const xmlChar * strData1, const xmlChar * strData2,
1943 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1944 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1945 const xmlChar * str5)
1946{
1947
1948 xmlGenericErrorFunc channel = NULL;
1949 xmlStructuredErrorFunc schannel = NULL;
1950 void *data = NULL;
1951
1952 if (ctxt != NULL) {
1953 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001954 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001955 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001956 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001957 schannel = ctxt->serror;
1958 }
1959 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1960 error, XML_ERR_ERROR, NULL, 0,
1961 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001962 (const char *) strData3, 0, 0, msg, str1, str2,
1963 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001964}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001965
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001966/************************************************************************
1967 * *
1968 * Allround error functions *
1969 * *
1970 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001971
1972/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001973 * xmlSchemaVTypeErrMemory:
1974 * @node: a context node
1975 * @extra: extra informations
1976 *
1977 * Handle an out of memory condition
1978 */
1979static void
1980xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1981 const char *extra, xmlNodePtr node)
1982{
1983 if (ctxt != NULL) {
1984 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001985 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001986 }
1987 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1988 extra);
1989}
1990
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001991static void
1992xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1993 const char *msg, const xmlChar *str)
1994{
1995 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1996 msg, (const char *) str);
1997}
1998
1999#define WXS_ERROR_TYPE_ERROR 1
2000#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002001/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002002 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002003 * @ctxt: the validation context
2004 * @node: the context node
2005 * @error: the error code
2006 * @msg: the error message
2007 * @str1: extra data
2008 * @str2: extra data
2009 * @str3: extra data
2010 *
2011 * Handle a validation error
2012 */
2013static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002014xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002015 xmlErrorLevel errorLevel,
2016 int error, xmlNodePtr node, int line, const char *msg,
2017 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002018 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002019{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002020 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002021 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002022 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002023
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002024 if (ctxt != NULL) {
2025 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2026 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002027 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002028 if (errorLevel != XML_ERR_WARNING) {
2029 vctxt->nberrors++;
2030 vctxt->err = error;
2031 channel = vctxt->error;
2032 } else {
2033 channel = vctxt->warning;
2034 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002035 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002036 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002037
2038 /*
2039 * Error node. If we specify a line number, then
2040 * do not channel any node to the error function.
2041 */
2042 if (line == 0) {
2043 if ((node == NULL) &&
2044 (vctxt->depth >= 0) &&
2045 (vctxt->inode != NULL)) {
2046 node = vctxt->inode->node;
2047 }
2048 /*
2049 * Get filename and line if no node-tree.
2050 */
2051 if ((node == NULL) &&
2052 (vctxt->parserCtxt != NULL) &&
2053 (vctxt->parserCtxt->input != NULL)) {
2054 file = vctxt->parserCtxt->input->filename;
2055 line = vctxt->parserCtxt->input->line;
2056 }
2057 } else {
2058 /*
2059 * Override the given node's (if any) position
2060 * and channel only the given line number.
2061 */
2062 node = NULL;
2063 /*
2064 * Get filename.
2065 */
2066 if (vctxt->doc != NULL)
2067 file = (const char *) vctxt->doc->URL;
2068 else if ((vctxt->parserCtxt != NULL) &&
2069 (vctxt->parserCtxt->input != NULL))
2070 file = vctxt->parserCtxt->input->filename;
2071 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002072 __xmlRaiseError(schannel, channel, data, ctxt,
2073 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002074 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002075 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002076 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002077
2078 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2079 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002080 if (errorLevel != XML_ERR_WARNING) {
2081 pctxt->nberrors++;
2082 pctxt->err = error;
2083 channel = pctxt->error;
2084 } else {
2085 channel = pctxt->warning;
2086 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002087 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002088 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002089 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002090 node, XML_FROM_SCHEMASP, error,
2091 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002092 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002093 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002094 } else {
2095 TODO
2096 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002097 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002098}
2099
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002100/**
2101 * xmlSchemaErr3:
2102 * @ctxt: the validation context
2103 * @node: the context node
2104 * @error: the error code
2105 * @msg: the error message
2106 * @str1: extra data
2107 * @str2: extra data
2108 * @str3: extra data
2109 *
2110 * Handle a validation error
2111 */
2112static void
2113xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2114 int error, xmlNodePtr node, const char *msg,
2115 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2116{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002117 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2118 msg, str1, str2, str3, NULL);
2119}
2120
2121static void
2122xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2123 int error, xmlNodePtr node, const char *msg,
2124 const xmlChar *str1, const xmlChar *str2,
2125 const xmlChar *str3, const xmlChar *str4)
2126{
2127 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2128 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002129}
2130
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002131static void
2132xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2133 int error, xmlNodePtr node, const char *msg,
2134 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002135{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002136 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002137}
2138
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002139static xmlChar *
2140xmlSchemaFormatNodeForError(xmlChar ** msg,
2141 xmlSchemaAbstractCtxtPtr actxt,
2142 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002143{
2144 xmlChar *str = NULL;
2145
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002146 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002147 if ((node != NULL) &&
2148 (node->type != XML_ELEMENT_NODE) &&
2149 (node->type != XML_ATTRIBUTE_NODE))
2150 {
2151 /*
2152 * Don't try to format other nodes than element and
2153 * attribute nodes.
2154 * Play save and return an empty string.
2155 */
2156 *msg = xmlStrdup(BAD_CAST "");
2157 return(*msg);
2158 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002159 if (node != NULL) {
2160 /*
2161 * Work on tree nodes.
2162 */
2163 if (node->type == XML_ATTRIBUTE_NODE) {
2164 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002165
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002166 *msg = xmlStrdup(BAD_CAST "Element '");
2167 if (elem->ns != NULL)
2168 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2169 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002170 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002171 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2172 NULL, elem->name));
2173 FREE_AND_NULL(str);
2174 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2175 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2176 } else {
2177 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002178 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002179 if (node->ns != NULL)
2180 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2181 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002182 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002183 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2184 NULL, node->name));
2185 FREE_AND_NULL(str);
2186 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2187 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2188 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2189 /*
2190 * Work on node infos.
2191 */
2192 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2193 xmlSchemaNodeInfoPtr ielem =
2194 vctxt->elemInfos[vctxt->depth];
2195
2196 *msg = xmlStrdup(BAD_CAST "Element '");
2197 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2198 ielem->nsName, ielem->localName));
2199 FREE_AND_NULL(str);
2200 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2201 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2202 } else {
2203 *msg = xmlStrdup(BAD_CAST "Element '");
2204 }
2205 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2206 vctxt->inode->nsName, vctxt->inode->localName));
2207 FREE_AND_NULL(str);
2208 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002209 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2210 /*
2211 * Hmm, no node while parsing?
2212 * Return an empty string, in case NULL will break something.
2213 */
2214 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002215 } else {
2216 TODO
2217 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002218 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002219 /*
2220 * VAL TODO: The output of the given schema component is currently
2221 * disabled.
2222 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002223#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002224 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2225 *msg = xmlStrcat(*msg, BAD_CAST " [");
2226 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2227 NULL, type, NULL, 0));
2228 FREE_AND_NULL(str)
2229 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002230 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002231#endif
2232 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002233}
2234
Daniel Veillardc0826a72004-08-10 14:17:33 +00002235static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002236xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002237 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002238 const char *message,
2239 const xmlChar *str1,
2240 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002241{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002242 xmlChar *msg = NULL;
2243
Daniel Veillard14b56432006-03-09 18:41:40 +00002244 if (actxt == NULL)
2245 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246 msg = xmlStrdup(BAD_CAST "Internal error: ");
2247 msg = xmlStrcat(msg, BAD_CAST funcName);
2248 msg = xmlStrcat(msg, BAD_CAST ", ");
2249 msg = xmlStrcat(msg, BAD_CAST message);
2250 msg = xmlStrcat(msg, BAD_CAST ".\n");
2251
2252 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2253 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002254 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002255
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002256 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002257 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002258 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002259
2260 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002261}
2262
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002263static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002264xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2265 const char *funcName,
2266 const char *message)
2267{
2268 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2269}
2270
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002271#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002272static void
2273xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2274 const char *funcName,
2275 const char *message,
2276 const xmlChar *str1,
2277 const xmlChar *str2)
2278{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002279 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002280 str1, str2);
2281}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002282#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002283
2284static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002285xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2286 xmlParserErrors error,
2287 xmlNodePtr node,
2288 xmlSchemaBasicItemPtr item,
2289 const char *message,
2290 const xmlChar *str1, const xmlChar *str2,
2291 const xmlChar *str3, const xmlChar *str4)
2292{
2293 xmlChar *msg = NULL;
2294
2295 if ((node == NULL) && (item != NULL) &&
2296 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2297 node = WXS_ITEM_NODE(item);
2298 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2299 msg = xmlStrcat(msg, BAD_CAST ": ");
2300 } else
2301 xmlSchemaFormatNodeForError(&msg, actxt, node);
2302 msg = xmlStrcat(msg, (const xmlChar *) message);
2303 msg = xmlStrcat(msg, BAD_CAST ".\n");
2304 xmlSchemaErr4(actxt, error, node,
2305 (const char *) msg, str1, str2, str3, str4);
2306 FREE_AND_NULL(msg)
2307}
2308
2309static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002310xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2311 xmlParserErrors error,
2312 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002313 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002314 const char *message,
2315 const xmlChar *str1,
2316 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002317{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002318 xmlSchemaCustomErr4(actxt, error, node, item,
2319 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002320}
2321
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002322
2323
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002324static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002325xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2326 xmlParserErrors error,
2327 xmlNodePtr node,
2328 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2329 const char *message,
2330 const xmlChar *str1,
2331 const xmlChar *str2,
2332 const xmlChar *str3)
2333{
2334 xmlChar *msg = NULL;
2335
2336 xmlSchemaFormatNodeForError(&msg, actxt, node);
2337 msg = xmlStrcat(msg, (const xmlChar *) message);
2338 msg = xmlStrcat(msg, BAD_CAST ".\n");
2339
2340 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002341 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2342 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002343
2344 FREE_AND_NULL(msg)
2345}
2346
2347
2348
2349static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002350xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2351 xmlParserErrors error,
2352 xmlSchemaPSVIIDCNodePtr idcNode,
2353 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2354 const char *message,
2355 const xmlChar *str1,
2356 const xmlChar *str2)
2357{
2358 xmlChar *msg = NULL, *qname = NULL;
2359
2360 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2361 msg = xmlStrcat(msg, (const xmlChar *) message);
2362 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002363 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002364 error, NULL, idcNode->nodeLine, (const char *) msg,
2365 xmlSchemaFormatQName(&qname,
2366 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2367 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002368 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002369 FREE_AND_NULL(qname);
2370 FREE_AND_NULL(msg);
2371}
2372
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002373static int
2374xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2375 xmlNodePtr node)
2376{
2377 if (node != NULL)
2378 return (node->type);
2379 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2380 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2381 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2382 return (-1);
2383}
2384
2385static int
2386xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2387{
2388 switch (item->type) {
2389 case XML_SCHEMA_TYPE_COMPLEX:
2390 case XML_SCHEMA_TYPE_SIMPLE:
2391 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2392 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002393 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002394 case XML_SCHEMA_TYPE_GROUP:
2395 return (1);
2396 case XML_SCHEMA_TYPE_ELEMENT:
2397 if ( ((xmlSchemaElementPtr) item)->flags &
2398 XML_SCHEMAS_ELEM_GLOBAL)
2399 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002400 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002401 case XML_SCHEMA_TYPE_ATTRIBUTE:
2402 if ( ((xmlSchemaAttributePtr) item)->flags &
2403 XML_SCHEMAS_ATTR_GLOBAL)
2404 return(1);
2405 break;
2406 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002407 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002408 return(1);
2409 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002410 return (0);
2411}
2412
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002413static void
2414xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2415 xmlParserErrors error,
2416 xmlNodePtr node,
2417 const xmlChar *value,
2418 xmlSchemaTypePtr type,
2419 int displayValue)
2420{
2421 xmlChar *msg = NULL;
2422
2423 xmlSchemaFormatNodeForError(&msg, actxt, node);
2424
2425 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2426 XML_ATTRIBUTE_NODE))
2427 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2428 else
2429 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2430 "value of ");
2431
2432 if (! xmlSchemaIsGlobalItem(type))
2433 msg = xmlStrcat(msg, BAD_CAST "the local ");
2434 else
2435 msg = xmlStrcat(msg, BAD_CAST "the ");
2436
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002437 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002438 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002439 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002440 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002441 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002442 msg = xmlStrcat(msg, BAD_CAST "union type");
2443
2444 if (xmlSchemaIsGlobalItem(type)) {
2445 xmlChar *str = NULL;
2446 msg = xmlStrcat(msg, BAD_CAST " '");
2447 if (type->builtInType != 0) {
2448 msg = xmlStrcat(msg, BAD_CAST "xs:");
2449 msg = xmlStrcat(msg, type->name);
2450 } else
2451 msg = xmlStrcat(msg,
2452 xmlSchemaFormatQName(&str,
2453 type->targetNamespace, type->name));
2454 msg = xmlStrcat(msg, BAD_CAST "'");
2455 FREE_AND_NULL(str);
2456 }
2457 msg = xmlStrcat(msg, BAD_CAST ".\n");
2458 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2459 XML_ATTRIBUTE_NODE))
2460 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2461 else
2462 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2463 FREE_AND_NULL(msg)
2464}
2465
Daniel Veillardc0826a72004-08-10 14:17:33 +00002466static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002467xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2468 xmlSchemaNodeInfoPtr ni,
2469 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002470{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002471 if (node != NULL) {
2472 if (node->ns != NULL)
2473 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2474 else
2475 return (xmlSchemaFormatQName(str, NULL, node->name));
2476 } else if (ni != NULL)
2477 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2478 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002479}
2480
Daniel Veillardc0826a72004-08-10 14:17:33 +00002481static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002482xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2483 xmlParserErrors error,
2484 xmlSchemaAttrInfoPtr ni,
2485 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002486{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002487 xmlChar *msg = NULL, *str = NULL;
2488
2489 xmlSchemaFormatNodeForError(&msg, actxt, node);
2490 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2491 xmlSchemaErr(actxt, error, node, (const char *) msg,
2492 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2493 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002494 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002495 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002496}
2497
Daniel Veillardc0826a72004-08-10 14:17:33 +00002498static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002499xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2500 xmlParserErrors error,
2501 xmlNodePtr node,
2502 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002503 const char *message,
2504 int nbval,
2505 int nbneg,
2506 xmlChar **values)
2507{
2508 xmlChar *str = NULL, *msg = NULL;
2509 xmlChar *localName, *nsName;
2510 const xmlChar *cur, *end;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00002511 int i;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002512
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002513 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002514 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002515 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002516 /*
2517 * Note that is does not make sense to report that we have a
2518 * wildcard here, since the wildcard might be unfolded into
2519 * multiple transitions.
2520 */
2521 if (nbval + nbneg > 0) {
2522 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002523 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002524 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002525 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002526 nsName = NULL;
2527
2528 for (i = 0; i < nbval + nbneg; i++) {
2529 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002530 if (cur == NULL)
2531 continue;
2532 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2533 (cur[3] == ' ')) {
Daniel Veillard77005e62005-07-19 16:26:18 +00002534 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002535 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002536 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002537 /*
2538 * Get the local name.
2539 */
2540 localName = NULL;
2541
2542 end = cur;
2543 if (*end == '*') {
2544 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002545 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002546 } else {
2547 while ((*end != 0) && (*end != '|'))
2548 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002549 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002550 }
2551 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002552 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002553 /*
2554 * Skip "*|*" if they come with negated expressions, since
2555 * they represent the same negated wildcard.
2556 */
2557 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2558 /*
2559 * Get the namespace name.
2560 */
2561 cur = end;
2562 if (*end == '*') {
2563 nsName = xmlStrdup(BAD_CAST "{*}");
2564 } else {
2565 while (*end != 0)
2566 end++;
2567
2568 if (i >= nbval)
2569 nsName = xmlStrdup(BAD_CAST "{##other:");
2570 else
2571 nsName = xmlStrdup(BAD_CAST "{");
2572
2573 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2574 nsName = xmlStrcat(nsName, BAD_CAST "}");
2575 }
2576 str = xmlStrcat(str, BAD_CAST nsName);
2577 FREE_AND_NULL(nsName)
2578 } else {
2579 FREE_AND_NULL(localName);
2580 continue;
2581 }
2582 }
2583 str = xmlStrcat(str, BAD_CAST localName);
2584 FREE_AND_NULL(localName);
2585
2586 if (i < nbval + nbneg -1)
2587 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002588 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002589 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002590 msg = xmlStrcat(msg, BAD_CAST str);
2591 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002592 } else
2593 msg = xmlStrcat(msg, BAD_CAST "\n");
2594 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002595 xmlFree(msg);
2596}
2597
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002598static void
2599xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2600 xmlParserErrors error,
2601 xmlNodePtr node,
2602 const xmlChar *value,
2603 unsigned long length,
2604 xmlSchemaTypePtr type,
2605 xmlSchemaFacetPtr facet,
2606 const char *message,
2607 const xmlChar *str1,
2608 const xmlChar *str2)
2609{
2610 xmlChar *str = NULL, *msg = NULL;
2611 xmlSchemaTypeType facetType;
2612 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2613
2614 xmlSchemaFormatNodeForError(&msg, actxt, node);
2615 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2616 facetType = XML_SCHEMA_FACET_ENUMERATION;
2617 /*
2618 * If enumerations are validated, one must not expect the
2619 * facet to be given.
2620 */
2621 } else
2622 facetType = facet->type;
2623 msg = xmlStrcat(msg, BAD_CAST "[");
2624 msg = xmlStrcat(msg, BAD_CAST "facet '");
2625 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2626 msg = xmlStrcat(msg, BAD_CAST "'] ");
2627 if (message == NULL) {
2628 /*
2629 * Use a default message.
2630 */
2631 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2632 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2633 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2634
2635 char len[25], actLen[25];
2636
2637 /* FIXME, TODO: What is the max expected string length of the
2638 * this value?
2639 */
2640 if (nodeType == XML_ATTRIBUTE_NODE)
2641 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2642 else
2643 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2644
2645 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2646 snprintf(actLen, 24, "%lu", length);
2647
2648 if (facetType == XML_SCHEMA_FACET_LENGTH)
2649 msg = xmlStrcat(msg,
2650 BAD_CAST "this differs from the allowed length of '%s'.\n");
2651 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2652 msg = xmlStrcat(msg,
2653 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2654 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2655 msg = xmlStrcat(msg,
2656 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2657
2658 if (nodeType == XML_ATTRIBUTE_NODE)
2659 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2660 value, (const xmlChar *) actLen, (const xmlChar *) len);
2661 else
2662 xmlSchemaErr(actxt, error, node, (const char *) msg,
2663 (const xmlChar *) actLen, (const xmlChar *) len);
2664
2665 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2666 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2667 "of the set {%s}.\n");
2668 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2669 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2670 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2671 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2672 "by the pattern '%s'.\n");
2673 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2674 facet->value);
2675 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2676 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2677 "minimum value allowed ('%s').\n");
2678 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2679 facet->value);
2680 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2681 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2682 "maximum value allowed ('%s').\n");
2683 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2684 facet->value);
2685 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002686 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002687 "'%s'.\n");
2688 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2689 facet->value);
2690 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002691 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002692 "'%s'.\n");
2693 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2694 facet->value);
2695 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2696 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2697 "digits than are allowed ('%s').\n");
2698 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2699 facet->value);
2700 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2701 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2702 "digits than are allowed ('%s').\n");
2703 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2704 facet->value);
2705 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2706 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2707 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2708 } else {
2709 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2710 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2711 }
2712 } else {
2713 msg = xmlStrcat(msg, (const xmlChar *) message);
2714 msg = xmlStrcat(msg, BAD_CAST ".\n");
2715 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2716 }
2717 FREE_AND_NULL(str)
2718 xmlFree(msg);
2719}
2720
2721#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002722 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002723
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002724#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002725
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002726#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2727#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002728
2729#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2730
2731
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002732/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002733 * xmlSchemaPMissingAttrErr:
2734 * @ctxt: the schema validation context
2735 * @ownerDes: the designation of the owner
2736 * @ownerName: the name of the owner
2737 * @ownerItem: the owner as a schema object
2738 * @ownerElem: the owner as an element node
2739 * @node: the parent element node of the missing attribute node
2740 * @type: the corresponding type of the attribute node
2741 *
2742 * Reports an illegal attribute.
2743 */
2744static void
2745xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002746 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002747 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002748 xmlNodePtr ownerElem,
2749 const char *name,
2750 const char *message)
2751{
2752 xmlChar *des = NULL;
2753
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002754 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2755
Daniel Veillardc0826a72004-08-10 14:17:33 +00002756 if (message != NULL)
2757 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002758 else
2759 xmlSchemaPErr(ctxt, ownerElem, error,
2760 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002761 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002762 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002763}
2764
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002765
Daniel Veillardc0826a72004-08-10 14:17:33 +00002766/**
2767 * xmlSchemaPResCompAttrErr:
2768 * @ctxt: the schema validation context
2769 * @error: the error code
2770 * @ownerDes: the designation of the owner
2771 * @ownerItem: the owner as a schema object
2772 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002773 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002774 * @refName: the referenced local name
2775 * @refURI: the referenced namespace URI
2776 * @message: optional message
2777 *
2778 * Used to report QName attribute values that failed to resolve
2779 * to schema components.
2780 */
2781static void
2782xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002783 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002784 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002785 xmlNodePtr ownerElem,
2786 const char *name,
2787 const xmlChar *refName,
2788 const xmlChar *refURI,
2789 xmlSchemaTypeType refType,
2790 const char *refTypeStr)
2791{
2792 xmlChar *des = NULL, *strA = NULL;
2793
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002794 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002795 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002796 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002797 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002798 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002799 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2800 "%s.\n", BAD_CAST des, BAD_CAST name,
2801 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002802 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002803 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002804 FREE_AND_NULL(strA)
2805}
2806
William M. Brack2f2a6632004-08-20 23:09:47 +00002807/**
2808 * xmlSchemaPCustomAttrErr:
2809 * @ctxt: the schema parser context
2810 * @error: the error code
2811 * @ownerDes: the designation of the owner
2812 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002813 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002814 *
2815 * Reports an illegal attribute during the parse.
2816 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002817static void
2818xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002819 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002820 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002821 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002822 xmlAttrPtr attr,
2823 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002824{
2825 xmlChar *des = NULL;
2826
2827 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002828 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002829 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002830 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002831 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002832 } else
2833 des = *ownerDes;
Daniel Veillard14b56432006-03-09 18:41:40 +00002834 if (attr == NULL) {
2835 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2836 "%s, attribute '%s': %s.\n",
Daniel Veillardaac7c682006-03-10 13:40:16 +00002837 BAD_CAST des, (const xmlChar *) "Unknown",
2838 (const xmlChar *) msg, NULL, NULL);
Daniel Veillard14b56432006-03-09 18:41:40 +00002839 } else {
2840 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2841 "%s, attribute '%s': %s.\n",
2842 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2843 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002844 if (ownerDes == NULL)
2845 FREE_AND_NULL(des);
2846}
2847
2848/**
2849 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002850 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002851 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002852 * @ownerDes: the designation of the attribute's owner
2853 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002854 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002855 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002856 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002857 */
2858static void
2859xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002860 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002861 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002862 xmlAttrPtr attr)
2863{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002864 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002865
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002866 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2867 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2868 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2869 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2870 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002871 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002872 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002873}
2874
William M. Brack2f2a6632004-08-20 23:09:47 +00002875/**
2876 * xmlSchemaPCustomErr:
2877 * @ctxt: the schema parser context
2878 * @error: the error code
2879 * @itemDes: the designation of the schema item
2880 * @item: the schema item
2881 * @itemElem: the node of the schema item
2882 * @message: the error message
2883 * @str1: an optional param for the error message
2884 * @str2: an optional param for the error message
2885 * @str3: an optional param for the error message
2886 *
2887 * Reports an error during parsing.
2888 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002889static void
2890xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002891 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002892 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002893 xmlNodePtr itemElem,
2894 const char *message,
2895 const xmlChar *str1,
2896 const xmlChar *str2,
2897 const xmlChar *str3)
2898{
2899 xmlChar *des = NULL, *msg = NULL;
2900
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002901 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002902 msg = xmlStrdup(BAD_CAST "%s: ");
2903 msg = xmlStrcat(msg, (const xmlChar *) message);
2904 msg = xmlStrcat(msg, BAD_CAST ".\n");
2905 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002906 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002907 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002908 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002909 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002910 FREE_AND_NULL(msg);
2911}
2912
William M. Brack2f2a6632004-08-20 23:09:47 +00002913/**
2914 * xmlSchemaPCustomErr:
2915 * @ctxt: the schema parser context
2916 * @error: the error code
2917 * @itemDes: the designation of the schema item
2918 * @item: the schema item
2919 * @itemElem: the node of the schema item
2920 * @message: the error message
2921 * @str1: the optional param for the error message
2922 *
2923 * Reports an error during parsing.
2924 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002925static void
2926xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002927 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002928 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002929 xmlNodePtr itemElem,
2930 const char *message,
2931 const xmlChar *str1)
2932{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002933 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002934 str1, NULL, NULL);
2935}
2936
William M. Brack2f2a6632004-08-20 23:09:47 +00002937/**
2938 * xmlSchemaPAttrUseErr:
2939 * @ctxt: the schema parser context
2940 * @error: the error code
2941 * @itemDes: the designation of the schema type
2942 * @item: the schema type
2943 * @itemElem: the node of the schema type
2944 * @attr: the invalid schema attribute
2945 * @message: the error message
2946 * @str1: the optional param for the error message
2947 *
2948 * Reports an attribute use error during parsing.
2949 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002950static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002951xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002952 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002953 xmlNodePtr node,
2954 xmlSchemaBasicItemPtr ownerItem,
2955 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002956 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002957 const xmlChar *str1, const xmlChar *str2,
2958 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002959{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002960 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002961
2962 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2963 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002964 msg = xmlStrcat(msg,
2965 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002966 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002967 FREE_AND_NULL(str);
2968 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002969 msg = xmlStrcat(msg, (const xmlChar *) message);
2970 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002971 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2972 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002973 xmlFree(msg);
2974}
2975
William M. Brack2f2a6632004-08-20 23:09:47 +00002976/**
2977 * xmlSchemaPIllegalFacetAtomicErr:
2978 * @ctxt: the schema parser context
2979 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002980 * @type: the schema type
2981 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002982 * @facet: the illegal facet
2983 *
2984 * Reports an illegal facet for atomic simple types.
2985 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002986static void
2987xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002988 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002989 xmlSchemaTypePtr type,
2990 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002991 xmlSchemaFacetPtr facet)
2992{
2993 xmlChar *des = NULL, *strT = NULL;
2994
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002995 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2996 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002997 "%s: The facet '%s' is not allowed on types derived from the "
2998 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002999 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003000 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00003001 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003002 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003003 FREE_AND_NULL(strT);
3004}
3005
William M. Brack2f2a6632004-08-20 23:09:47 +00003006/**
3007 * xmlSchemaPIllegalFacetListUnionErr:
3008 * @ctxt: the schema parser context
3009 * @error: the error code
3010 * @itemDes: the designation of the schema item involved
3011 * @item: the schema item involved
3012 * @facet: the illegal facet
3013 *
3014 * Reports an illegal facet for <list> and <union>.
3015 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003016static void
3017xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003018 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003019 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003020 xmlSchemaFacetPtr facet)
3021{
Daniel Veillardb2947172006-03-27 09:45:01 +00003022 xmlChar *des = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003023
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003024 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3025 type->node);
3026 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003027 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003028 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003029 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003030}
3031
3032/**
3033 * xmlSchemaPMutualExclAttrErr:
3034 * @ctxt: the schema validation context
3035 * @error: the error code
3036 * @elemDes: the designation of the parent element node
3037 * @attr: the bad attribute node
3038 * @type: the corresponding type of the attribute node
3039 *
3040 * Reports an illegal attribute.
3041 */
3042static void
3043xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3044 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003045 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003046 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003047 const char *name1,
3048 const char *name2)
3049{
3050 xmlChar *des = NULL;
3051
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003052 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003053 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003054 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003055 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003056 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003057}
3058
3059/**
3060 * xmlSchemaPSimpleTypeErr:
3061 * @ctxt: the schema validation context
3062 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003063 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003064 * @ownerDes: the designation of the owner
3065 * @ownerItem: the schema object if existent
3066 * @node: the validated node
3067 * @value: the validated value
3068 *
3069 * Reports a simple type validation error.
3070 * TODO: Should this report the value of an element as well?
3071 */
3072static void
3073xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3074 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003075 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003076 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003077 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003078 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003079 const xmlChar *value,
3080 const char *message,
3081 const xmlChar *str1,
3082 const xmlChar *str2)
3083{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003084 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003085
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003086 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003087 if (message == NULL) {
3088 /*
3089 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003090 */
3091 if (type != NULL) {
3092 if (node->type == XML_ATTRIBUTE_NODE)
3093 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3094 else
3095 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3096 "valid value of ");
3097 if (! xmlSchemaIsGlobalItem(type))
3098 msg = xmlStrcat(msg, BAD_CAST "the local ");
3099 else
3100 msg = xmlStrcat(msg, BAD_CAST "the ");
3101
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003102 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003103 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003104 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003105 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003106 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003107 msg = xmlStrcat(msg, BAD_CAST "union type");
3108
3109 if (xmlSchemaIsGlobalItem(type)) {
3110 xmlChar *str = NULL;
3111 msg = xmlStrcat(msg, BAD_CAST " '");
3112 if (type->builtInType != 0) {
3113 msg = xmlStrcat(msg, BAD_CAST "xs:");
3114 msg = xmlStrcat(msg, type->name);
3115 } else
3116 msg = xmlStrcat(msg,
3117 xmlSchemaFormatQName(&str,
3118 type->targetNamespace, type->name));
3119 msg = xmlStrcat(msg, BAD_CAST "'.");
3120 FREE_AND_NULL(str);
3121 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003122 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003123 if (node->type == XML_ATTRIBUTE_NODE)
3124 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3125 else
3126 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3127 "valid.");
3128 }
3129 if (expected) {
3130 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3131 msg = xmlStrcat(msg, BAD_CAST expected);
3132 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3133 } else
3134 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003135 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003136 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3137 else
3138 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3139 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003140 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003141 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003142 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003143 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003144 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003145 /* Cleanup. */
3146 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003147}
3148
William M. Brack2f2a6632004-08-20 23:09:47 +00003149/**
3150 * xmlSchemaPContentErr:
3151 * @ctxt: the schema parser context
3152 * @error: the error code
3153 * @onwerDes: the designation of the holder of the content
3154 * @ownerItem: the owner item of the holder of the content
3155 * @ownerElem: the node of the holder of the content
3156 * @child: the invalid child node
3157 * @message: the optional error message
3158 * @content: the optional string describing the correct content
3159 *
3160 * Reports an error concerning the content of a schema element.
3161 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003162static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003163xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003164 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003165 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003166 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003167 xmlNodePtr child,
3168 const char *message,
3169 const char *content)
3170{
3171 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003172
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003173 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003174 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003175 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3176 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003177 BAD_CAST des, BAD_CAST message);
3178 else {
3179 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003180 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3181 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003182 BAD_CAST des, BAD_CAST content);
3183 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003184 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3185 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003186 BAD_CAST des, NULL);
3187 }
3188 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003189 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003190}
3191
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003192/************************************************************************
3193 * *
3194 * Streamable error functions *
3195 * *
3196 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003197
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003198
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003199
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003200
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003201/************************************************************************
3202 * *
3203 * Validation helper functions *
3204 * *
3205 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003206
Daniel Veillardc0826a72004-08-10 14:17:33 +00003207
Daniel Veillard4255d502002-04-16 15:50:10 +00003208/************************************************************************
3209 * *
3210 * Allocation functions *
3211 * *
3212 ************************************************************************/
3213
3214/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003215 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003216 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 *
3218 * Allocate a new Schema structure.
3219 *
3220 * Returns the newly allocated structure or NULL in case or error
3221 */
3222static xmlSchemaPtr
3223xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3224{
3225 xmlSchemaPtr ret;
3226
3227 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3228 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003229 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003230 return (NULL);
3231 }
3232 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003233 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003234 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003235
3236 return (ret);
3237}
3238
3239/**
3240 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003241 *
3242 * Allocate a new Facet structure.
3243 *
3244 * Returns the newly allocated structure or NULL in case or error
3245 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003246xmlSchemaFacetPtr
3247xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003248{
3249 xmlSchemaFacetPtr ret;
3250
3251 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3252 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003253 return (NULL);
3254 }
3255 memset(ret, 0, sizeof(xmlSchemaFacet));
3256
3257 return (ret);
3258}
3259
3260/**
3261 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003262 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003263 * @node: a node
3264 *
3265 * Allocate a new annotation structure.
3266 *
3267 * Returns the newly allocated structure or NULL in case or error
3268 */
3269static xmlSchemaAnnotPtr
3270xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3271{
3272 xmlSchemaAnnotPtr ret;
3273
3274 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3275 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003276 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003277 return (NULL);
3278 }
3279 memset(ret, 0, sizeof(xmlSchemaAnnot));
3280 ret->content = node;
3281 return (ret);
3282}
3283
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003284static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003285xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003286{
3287 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003288
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003289 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3290 if (ret == NULL) {
3291 xmlSchemaPErrMemory(NULL,
3292 "allocating an item list structure", NULL);
3293 return (NULL);
3294 }
3295 memset(ret, 0, sizeof(xmlSchemaItemList));
3296 return (ret);
3297}
3298
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003299static void
3300xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3301{
3302 if (list->items != NULL) {
3303 xmlFree(list->items);
3304 list->items = NULL;
3305 }
3306 list->nbItems = 0;
3307 list->sizeItems = 0;
3308}
3309
3310static int
3311xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3312{
3313 if (list->items == NULL) {
3314 list->items = (void **) xmlMalloc(
3315 20 * sizeof(void *));
3316 if (list->items == NULL) {
3317 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3318 return(-1);
3319 }
3320 list->sizeItems = 20;
3321 } else if (list->sizeItems <= list->nbItems) {
3322 list->sizeItems *= 2;
3323 list->items = (void **) xmlRealloc(list->items,
3324 list->sizeItems * sizeof(void *));
3325 if (list->items == NULL) {
3326 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3327 list->sizeItems = 0;
3328 return(-1);
3329 }
3330 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003331 list->items[list->nbItems++] = item;
3332 return(0);
3333}
3334
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003335static int
3336xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3337 int initialSize,
3338 void *item)
3339{
3340 if (list->items == NULL) {
3341 if (initialSize <= 0)
3342 initialSize = 1;
3343 list->items = (void **) xmlMalloc(
3344 initialSize * sizeof(void *));
3345 if (list->items == NULL) {
3346 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3347 return(-1);
3348 }
3349 list->sizeItems = initialSize;
3350 } else if (list->sizeItems <= list->nbItems) {
3351 list->sizeItems *= 2;
3352 list->items = (void **) xmlRealloc(list->items,
3353 list->sizeItems * sizeof(void *));
3354 if (list->items == NULL) {
3355 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3356 list->sizeItems = 0;
3357 return(-1);
3358 }
3359 }
3360 list->items[list->nbItems++] = item;
3361 return(0);
3362}
3363
3364static int
3365xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3366{
3367 if (list->items == NULL) {
3368 list->items = (void **) xmlMalloc(
3369 20 * sizeof(void *));
3370 if (list->items == NULL) {
3371 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3372 return(-1);
3373 }
3374 list->sizeItems = 20;
3375 } else if (list->sizeItems <= list->nbItems) {
3376 list->sizeItems *= 2;
3377 list->items = (void **) xmlRealloc(list->items,
3378 list->sizeItems * sizeof(void *));
3379 if (list->items == NULL) {
3380 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3381 list->sizeItems = 0;
3382 return(-1);
3383 }
3384 }
3385 /*
3386 * Just append if the index is greater/equal than the item count.
3387 */
3388 if (idx >= list->nbItems) {
3389 list->items[list->nbItems++] = item;
3390 } else {
3391 int i;
3392 for (i = list->nbItems; i > idx; i--)
3393 list->items[i] = list->items[i-1];
3394 list->items[idx] = item;
3395 list->nbItems++;
3396 }
3397 return(0);
3398}
3399
3400#if 0 /* enable if ever needed */
3401static int
3402xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3403 int initialSize,
3404 void *item,
3405 int idx)
3406{
3407 if (list->items == NULL) {
3408 if (initialSize <= 0)
3409 initialSize = 1;
3410 list->items = (void **) xmlMalloc(
3411 initialSize * sizeof(void *));
3412 if (list->items == NULL) {
3413 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3414 return(-1);
3415 }
3416 list->sizeItems = initialSize;
3417 } else if (list->sizeItems <= list->nbItems) {
3418 list->sizeItems *= 2;
3419 list->items = (void **) xmlRealloc(list->items,
3420 list->sizeItems * sizeof(void *));
3421 if (list->items == NULL) {
3422 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3423 list->sizeItems = 0;
3424 return(-1);
3425 }
3426 }
3427 /*
3428 * Just append if the index is greater/equal than the item count.
3429 */
3430 if (idx >= list->nbItems) {
3431 list->items[list->nbItems++] = item;
3432 } else {
3433 int i;
3434 for (i = list->nbItems; i > idx; i--)
3435 list->items[i] = list->items[i-1];
3436 list->items[idx] = item;
3437 list->nbItems++;
3438 }
3439 return(0);
3440}
3441#endif
3442
3443static int
3444xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3445{
3446 int i;
3447 if ((list->items == NULL) || (idx >= list->nbItems)) {
3448 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3449 "index error.\n");
3450 return(-1);
3451 }
3452
3453 if (list->nbItems == 1) {
3454 /* TODO: Really free the list? */
3455 xmlFree(list->items);
3456 list->items = NULL;
3457 list->nbItems = 0;
3458 list->sizeItems = 0;
3459 } else if (list->nbItems -1 == idx) {
3460 list->nbItems--;
3461 } else {
3462 for (i = idx; i < list->nbItems -1; i++)
3463 list->items[i] = list->items[i+1];
3464 list->nbItems--;
3465 }
3466 return(0);
3467}
3468
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003469/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003470 * xmlSchemaItemListFree:
3471 * @annot: a schema type structure
3472 *
3473 * Deallocate a annotation structure
3474 */
3475static void
3476xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3477{
3478 if (list == NULL)
3479 return;
3480 if (list->items != NULL)
3481 xmlFree(list->items);
3482 xmlFree(list);
3483}
3484
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003485static void
3486xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003487{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003488 if (bucket == NULL)
3489 return;
3490 if (bucket->globals != NULL) {
3491 xmlSchemaComponentListFree(bucket->globals);
3492 xmlSchemaItemListFree(bucket->globals);
3493 }
3494 if (bucket->locals != NULL) {
3495 xmlSchemaComponentListFree(bucket->locals);
3496 xmlSchemaItemListFree(bucket->locals);
3497 }
3498 if (bucket->relations != NULL) {
3499 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3500 do {
3501 prev = cur;
3502 cur = cur->next;
3503 xmlFree(prev);
3504 } while (cur != NULL);
3505 }
3506 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3507 xmlFreeDoc(bucket->doc);
3508 }
3509 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003510 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3511 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003512 }
3513 xmlFree(bucket);
3514}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003515
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003516static xmlSchemaBucketPtr
3517xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003518 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003519{
3520 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003521 int size;
3522 xmlSchemaPtr mainSchema;
3523
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003524 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003525 PERROR_INT("xmlSchemaBucketCreate",
3526 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003527 return(NULL);
3528 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003529 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003530 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003531 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003532 size = sizeof(xmlSchemaInclude);
3533 else
3534 size = sizeof(xmlSchemaImport);
3535 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3536 if (ret == NULL) {
3537 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3538 return(NULL);
3539 }
3540 memset(ret, 0, size);
3541 ret->targetNamespace = targetNamespace;
3542 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003543 ret->globals = xmlSchemaItemListCreate();
3544 if (ret->globals == NULL) {
3545 xmlFree(ret);
3546 return(NULL);
3547 }
3548 ret->locals = xmlSchemaItemListCreate();
3549 if (ret->locals == NULL) {
3550 xmlFree(ret);
3551 return(NULL);
3552 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003553 /*
3554 * The following will assure that only the first bucket is marked as
3555 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3556 * For each following import buckets an xmlSchema will be created.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003557 * An xmlSchema will be created for every distinct targetNamespace.
3558 * We assign the targetNamespace to the schemata here.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003559 */
3560 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003561 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003562 PERROR_INT("xmlSchemaBucketCreate",
3563 "first bucket but it's an include or redefine");
3564 xmlSchemaBucketFree(ret);
3565 return(NULL);
3566 }
3567 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3568 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3569 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003570 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3571 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003572 /*
3573 * Ensure that the main schema gets a targetNamespace.
3574 */
3575 mainSchema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003576 } else {
3577 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3578 PERROR_INT("xmlSchemaBucketCreate",
3579 "main bucket but it's not the first one");
3580 xmlSchemaBucketFree(ret);
3581 return(NULL);
3582 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3583 /*
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003584 * Create a schema for imports and assign the
3585 * targetNamespace.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003586 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003587 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3588 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003589 xmlSchemaBucketFree(ret);
3590 return(NULL);
3591 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003592 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003593 }
3594 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003595 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003596 int res;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003597 /*
3598 * Imports go into the "schemasImports" slot of the main *schema*.
3599 * Note that we create an import entry for the main schema as well; i.e.,
3600 * even if there's only one schema, we'll get an import.
3601 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003602 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003603 mainSchema->schemasImports = xmlHashCreateDict(5,
3604 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003605 if (mainSchema->schemasImports == NULL) {
3606 xmlSchemaBucketFree(ret);
3607 return(NULL);
3608 }
3609 }
3610 if (targetNamespace == NULL)
3611 res = xmlHashAddEntry(mainSchema->schemasImports,
3612 XML_SCHEMAS_NO_NAMESPACE, ret);
3613 else
3614 res = xmlHashAddEntry(mainSchema->schemasImports,
3615 targetNamespace, ret);
3616 if (res != 0) {
3617 PERROR_INT("xmlSchemaBucketCreate",
3618 "failed to add the schema bucket to the hash");
3619 xmlSchemaBucketFree(ret);
3620 return(NULL);
3621 }
3622 } else {
3623 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003624 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3625 WXS_INCBUCKET(ret)->ownerImport =
3626 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003627 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003628 WXS_INCBUCKET(ret)->ownerImport =
3629 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003630
3631 /* Includes got into the "includes" slot of the *main* schema. */
3632 if (mainSchema->includes == NULL) {
3633 mainSchema->includes = xmlSchemaItemListCreate();
3634 if (mainSchema->includes == NULL) {
3635 xmlSchemaBucketFree(ret);
3636 return(NULL);
3637 }
3638 }
3639 xmlSchemaItemListAdd(mainSchema->includes, ret);
3640 }
3641 /*
3642 * Add to list of all buckets; this is used for lookup
3643 * during schema construction time only.
3644 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003645 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003646 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003647 return(ret);
3648}
3649
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003650static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003651xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003652{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003653 if (*list == NULL) {
3654 *list = xmlSchemaItemListCreate();
3655 if (*list == NULL)
3656 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003657 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003658 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003659 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003660}
3661
3662/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003663 * xmlSchemaFreeAnnot:
3664 * @annot: a schema type structure
3665 *
3666 * Deallocate a annotation structure
3667 */
3668static void
3669xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3670{
3671 if (annot == NULL)
3672 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003673 if (annot->next == NULL) {
3674 xmlFree(annot);
3675 } else {
3676 xmlSchemaAnnotPtr prev;
3677
3678 do {
3679 prev = annot;
3680 annot = annot->next;
3681 xmlFree(prev);
3682 } while (annot != NULL);
3683 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003684}
3685
3686/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003687 * xmlSchemaFreeNotation:
3688 * @schema: a schema notation structure
3689 *
3690 * Deallocate a Schema Notation structure.
3691 */
3692static void
3693xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3694{
3695 if (nota == NULL)
3696 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003697 xmlFree(nota);
3698}
3699
3700/**
3701 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003702 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003703 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003704 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003705 */
3706static void
3707xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3708{
3709 if (attr == NULL)
3710 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003711 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003712 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003713 if (attr->defVal != NULL)
3714 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003715 xmlFree(attr);
3716}
3717
3718/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003719 * xmlSchemaFreeAttributeUse:
3720 * @use: an attribute use
3721 *
3722 * Deallocates an attribute use structure.
3723 */
3724static void
3725xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3726{
3727 if (use == NULL)
3728 return;
3729 if (use->annot != NULL)
3730 xmlSchemaFreeAnnot(use->annot);
3731 if (use->defVal != NULL)
3732 xmlSchemaFreeValue(use->defVal);
3733 xmlFree(use);
3734}
3735
3736/**
3737 * xmlSchemaFreeAttributeUseProhib:
3738 * @prohib: an attribute use prohibition
3739 *
3740 * Deallocates an attribute use structure.
3741 */
3742static void
3743xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3744{
3745 if (prohib == NULL)
3746 return;
3747 xmlFree(prohib);
3748}
3749
3750/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003751 * xmlSchemaFreeWildcardNsSet:
3752 * set: a schema wildcard namespace
3753 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003754 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003755 */
3756static void
3757xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3758{
3759 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003760
Daniel Veillard3646d642004-06-02 19:19:14 +00003761 while (set != NULL) {
3762 next = set->next;
3763 xmlFree(set);
3764 set = next;
3765 }
3766}
3767
3768/**
3769 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003770 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003771 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003772 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003773 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003774void
Daniel Veillard3646d642004-06-02 19:19:14 +00003775xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3776{
3777 if (wildcard == NULL)
3778 return;
3779 if (wildcard->annot != NULL)
3780 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003781 if (wildcard->nsSet != NULL)
3782 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3783 if (wildcard->negNsSet != NULL)
3784 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003785 xmlFree(wildcard);
3786}
3787
3788/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003789 * xmlSchemaFreeAttributeGroup:
3790 * @schema: a schema attribute group structure
3791 *
3792 * Deallocate a Schema Attribute Group structure.
3793 */
3794static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003795xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003796{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003797 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003798 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003799 if (attrGr->annot != NULL)
3800 xmlSchemaFreeAnnot(attrGr->annot);
3801 if (attrGr->attrUses != NULL)
3802 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3803 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003804}
3805
3806/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003807 * xmlSchemaFreeQNameRef:
3808 * @item: a QName reference structure
3809 *
3810 * Deallocatea a QName reference structure.
3811 */
3812static void
3813xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3814{
3815 xmlFree(item);
3816}
3817
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003818/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003819 * xmlSchemaFreeTypeLinkList:
3820 * @alink: a type link
3821 *
3822 * Deallocate a list of types.
3823 */
3824static void
3825xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3826{
3827 xmlSchemaTypeLinkPtr next;
3828
3829 while (link != NULL) {
3830 next = link->next;
3831 xmlFree(link);
3832 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003833 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003834}
3835
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003836static void
3837xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3838{
3839 xmlSchemaIDCStateObjPtr next;
3840 while (sto != NULL) {
3841 next = sto->next;
3842 if (sto->history != NULL)
3843 xmlFree(sto->history);
3844 if (sto->xpathCtxt != NULL)
3845 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3846 xmlFree(sto);
3847 sto = next;
3848 }
3849}
3850
3851/**
3852 * xmlSchemaFreeIDC:
3853 * @idc: a identity-constraint definition
3854 *
3855 * Deallocates an identity-constraint definition.
3856 */
3857static void
3858xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3859{
3860 xmlSchemaIDCSelectPtr cur, prev;
3861
3862 if (idcDef == NULL)
3863 return;
3864 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003865 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003866 /* Selector */
3867 if (idcDef->selector != NULL) {
3868 if (idcDef->selector->xpathComp != NULL)
3869 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3870 xmlFree(idcDef->selector);
3871 }
3872 /* Fields */
3873 if (idcDef->fields != NULL) {
3874 cur = idcDef->fields;
3875 do {
3876 prev = cur;
3877 cur = cur->next;
3878 if (prev->xpathComp != NULL)
3879 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003880 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003881 } while (cur != NULL);
3882 }
3883 xmlFree(idcDef);
3884}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003885
Daniel Veillard01fa6152004-06-29 17:04:39 +00003886/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003887 * xmlSchemaFreeElement:
3888 * @schema: a schema element structure
3889 *
3890 * Deallocate a Schema Element structure.
3891 */
3892static void
3893xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3894{
3895 if (elem == NULL)
3896 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003897 if (elem->annot != NULL)
3898 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003900 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003901 if (elem->defVal != NULL)
3902 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003903 xmlFree(elem);
3904}
3905
3906/**
3907 * xmlSchemaFreeFacet:
3908 * @facet: a schema facet structure
3909 *
3910 * Deallocate a Schema Facet structure.
3911 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003912void
Daniel Veillard4255d502002-04-16 15:50:10 +00003913xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3914{
3915 if (facet == NULL)
3916 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003917 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003918 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003919 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003920 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003921 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003922 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003923 xmlFree(facet);
3924}
3925
3926/**
3927 * xmlSchemaFreeType:
3928 * @type: a schema type structure
3929 *
3930 * Deallocate a Schema Type structure.
3931 */
3932void
3933xmlSchemaFreeType(xmlSchemaTypePtr type)
3934{
3935 if (type == NULL)
3936 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003937 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003938 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003939 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003940 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003941
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003942 facet = type->facets;
3943 while (facet != NULL) {
3944 next = facet->next;
3945 xmlSchemaFreeFacet(facet);
3946 facet = next;
3947 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003948 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003949 if (type->attrUses != NULL)
3950 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003951 if (type->memberTypes != NULL)
3952 xmlSchemaFreeTypeLinkList(type->memberTypes);
3953 if (type->facetSet != NULL) {
3954 xmlSchemaFacetLinkPtr next, link;
3955
3956 link = type->facetSet;
3957 do {
3958 next = link->next;
3959 xmlFree(link);
3960 link = next;
3961 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003962 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003963 if (type->contModel != NULL)
3964 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003965 xmlFree(type);
3966}
3967
3968/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003969 * xmlSchemaFreeModelGroupDef:
3970 * @item: a schema model group definition
3971 *
3972 * Deallocates a schema model group definition.
3973 */
3974static void
3975xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3976{
3977 if (item->annot != NULL)
3978 xmlSchemaFreeAnnot(item->annot);
3979 xmlFree(item);
3980}
3981
3982/**
3983 * xmlSchemaFreeModelGroup:
3984 * @item: a schema model group
3985 *
3986 * Deallocates a schema model group structure.
3987 */
3988static void
3989xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3990{
3991 if (item->annot != NULL)
3992 xmlSchemaFreeAnnot(item->annot);
3993 xmlFree(item);
3994}
3995
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003996static void
3997xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3998{
3999 if ((list == NULL) || (list->nbItems == 0))
4000 return;
4001 {
4002 xmlSchemaTreeItemPtr item;
4003 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
4004 int i;
4005
4006 for (i = 0; i < list->nbItems; i++) {
4007 item = items[i];
4008 if (item == NULL)
4009 continue;
4010 switch (item->type) {
4011 case XML_SCHEMA_TYPE_SIMPLE:
4012 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004013 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004014 break;
4015 case XML_SCHEMA_TYPE_ATTRIBUTE:
4016 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004017 break;
4018 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
4019 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
4020 break;
4021 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
4022 xmlSchemaFreeAttributeUseProhib(
4023 (xmlSchemaAttributeUseProhibPtr) item);
4024 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004025 case XML_SCHEMA_TYPE_ELEMENT:
4026 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4027 break;
4028 case XML_SCHEMA_TYPE_PARTICLE:
4029 if (item->annot != NULL)
4030 xmlSchemaFreeAnnot(item->annot);
4031 xmlFree(item);
4032 break;
4033 case XML_SCHEMA_TYPE_SEQUENCE:
4034 case XML_SCHEMA_TYPE_CHOICE:
4035 case XML_SCHEMA_TYPE_ALL:
4036 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4037 break;
4038 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4039 xmlSchemaFreeAttributeGroup(
4040 (xmlSchemaAttributeGroupPtr) item);
4041 break;
4042 case XML_SCHEMA_TYPE_GROUP:
4043 xmlSchemaFreeModelGroupDef(
4044 (xmlSchemaModelGroupDefPtr) item);
4045 break;
4046 case XML_SCHEMA_TYPE_ANY:
4047 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4048 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4049 break;
4050 case XML_SCHEMA_TYPE_IDC_KEY:
4051 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4052 case XML_SCHEMA_TYPE_IDC_KEYREF:
4053 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4054 break;
4055 case XML_SCHEMA_TYPE_NOTATION:
4056 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4057 break;
4058 case XML_SCHEMA_EXTRA_QNAMEREF:
4059 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4060 break;
4061 default: {
4062 /* TODO: This should never be hit. */
4063 xmlSchemaPSimpleInternalErr(NULL,
4064 "Internal error: xmlSchemaComponentListFree, "
4065 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004066 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004067 }
4068 break;
4069 }
4070 }
4071 list->nbItems = 0;
4072 }
4073}
4074
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004075/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004076 * xmlSchemaFree:
4077 * @schema: a schema structure
4078 *
4079 * Deallocate a Schema structure.
4080 */
4081void
4082xmlSchemaFree(xmlSchemaPtr schema)
4083{
4084 if (schema == NULL)
4085 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004086 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004087 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004088 TODO
4089 /*
4090 * Note that those slots are not responsible for freeing
4091 * schema components anymore; this will now be done by
4092 * the schema buckets.
4093 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004094 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004095 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004096 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004097 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004098 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004099 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004100 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004101 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004102 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004103 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004104 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004105 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004106 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004107 xmlHashFree(schema->idcDef, NULL);
4108
Daniel Veillard1d913862003-11-21 00:28:39 +00004109 if (schema->schemasImports != NULL)
4110 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004111 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004112 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004113 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4114 int i;
4115 for (i = 0; i < list->nbItems; i++) {
4116 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4117 }
4118 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004119 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004120 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004121 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004122 /* Never free the doc here, since this will be done by the buckets. */
4123
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004124 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004125 xmlFree(schema);
4126}
4127
4128/************************************************************************
4129 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004130 * Debug functions *
4131 * *
4132 ************************************************************************/
4133
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004134#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004135
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004136static void
4137xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4138
Daniel Veillard4255d502002-04-16 15:50:10 +00004139/**
4140 * xmlSchemaElementDump:
4141 * @elem: an element
4142 * @output: the file output
4143 *
4144 * Dump the element
4145 */
4146static void
4147xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004148 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004149 const xmlChar * namespace ATTRIBUTE_UNUSED,
4150 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004151{
4152 if (elem == NULL)
4153 return;
4154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004155
4156 fprintf(output, "Element");
4157 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4158 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004159 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004160 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004161 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004162 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004163#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004164 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004165 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004166 if (elem->maxOccurs >= UNBOUNDED)
4167 fprintf(output, "max: unbounded\n");
4168 else if (elem->maxOccurs != 1)
4169 fprintf(output, "max: %d\n", elem->maxOccurs);
4170 else
4171 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004172 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004173#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004174 /*
4175 * Misc other properties.
4176 */
4177 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4178 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4179 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004180 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004181 fprintf(output, " props: ");
4182 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4183 fprintf(output, "[fixed] ");
4184 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4185 fprintf(output, "[default] ");
4186 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4187 fprintf(output, "[abstract] ");
4188 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4189 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004190 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004191 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004192 /*
4193 * Default/fixed value.
4194 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004195 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004196 fprintf(output, " value: '%s'\n", elem->value);
4197 /*
4198 * Type.
4199 */
4200 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004201 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004202 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004203 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004204 else
4205 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004206 } else if (elem->subtypes != NULL) {
4207 /*
4208 * Dump local types.
4209 */
4210 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004211 }
4212 /*
4213 * Substitution group.
4214 */
4215 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004216 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004217 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004218 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004219 else
4220 fprintf(output, "\n");
4221 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004222}
4223
4224/**
4225 * xmlSchemaAnnotDump:
4226 * @output: the file output
4227 * @annot: a annotation
4228 *
4229 * Dump the annotation
4230 */
4231static void
4232xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4233{
4234 xmlChar *content;
4235
4236 if (annot == NULL)
4237 return;
4238
4239 content = xmlNodeGetContent(annot->content);
4240 if (content != NULL) {
4241 fprintf(output, " Annot: %s\n", content);
4242 xmlFree(content);
4243 } else
4244 fprintf(output, " Annot: empty\n");
4245}
4246
4247/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004248 * xmlSchemaContentModelDump:
4249 * @particle: the schema particle
4250 * @output: the file output
4251 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004252 *
4253 * Dump a SchemaType structure
4254 */
4255static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004256xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4257{
4258 xmlChar *str = NULL;
4259 xmlSchemaTreeItemPtr term;
4260 char shift[100];
4261 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004262
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004263 if (particle == NULL)
4264 return;
4265 for (i = 0;((i < depth) && (i < 25));i++)
4266 shift[2 * i] = shift[2 * i + 1] = ' ';
4267 shift[2 * i] = shift[2 * i + 1] = 0;
4268 fprintf(output, shift);
4269 if (particle->children == NULL) {
4270 fprintf(output, "MISSING particle term\n");
4271 return;
4272 }
4273 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004274 if (term == NULL) {
4275 fprintf(output, "(NULL)");
4276 } else {
4277 switch (term->type) {
4278 case XML_SCHEMA_TYPE_ELEMENT:
4279 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4280 ((xmlSchemaElementPtr)term)->targetNamespace,
4281 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004282 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004283 break;
4284 case XML_SCHEMA_TYPE_SEQUENCE:
4285 fprintf(output, "SEQUENCE");
4286 break;
4287 case XML_SCHEMA_TYPE_CHOICE:
4288 fprintf(output, "CHOICE");
4289 break;
4290 case XML_SCHEMA_TYPE_ALL:
4291 fprintf(output, "ALL");
4292 break;
4293 case XML_SCHEMA_TYPE_ANY:
4294 fprintf(output, "ANY");
4295 break;
4296 default:
4297 fprintf(output, "UNKNOWN\n");
4298 return;
4299 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004300 }
4301 if (particle->minOccurs != 1)
4302 fprintf(output, " min: %d", particle->minOccurs);
4303 if (particle->maxOccurs >= UNBOUNDED)
4304 fprintf(output, " max: unbounded");
4305 else if (particle->maxOccurs != 1)
4306 fprintf(output, " max: %d", particle->maxOccurs);
4307 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004308 if (term &&
4309 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4310 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4311 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4312 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004313 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4314 output, depth +1);
4315 }
4316 if (particle->next != NULL)
4317 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4318 output, depth);
4319}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004320
4321/**
4322 * xmlSchemaAttrUsesDump:
4323 * @uses: attribute uses list
4324 * @output: the file output
4325 *
4326 * Dumps a list of attribute use components.
4327 */
4328static void
4329xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4330{
4331 xmlSchemaAttributeUsePtr use;
4332 xmlSchemaAttributeUseProhibPtr prohib;
4333 xmlSchemaQNameRefPtr ref;
4334 const xmlChar *name, *tns;
4335 xmlChar *str = NULL;
4336 int i;
4337
4338 if ((uses == NULL) || (uses->nbItems == 0))
4339 return;
4340
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004341 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004342 for (i = 0; i < uses->nbItems; i++) {
4343 use = uses->items[i];
4344 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4345 fprintf(output, " [prohibition] ");
4346 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4347 name = prohib->name;
4348 tns = prohib->targetNamespace;
4349 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4350 fprintf(output, " [reference] ");
4351 ref = (xmlSchemaQNameRefPtr) use;
4352 name = ref->name;
4353 tns = ref->targetNamespace;
4354 } else {
4355 fprintf(output, " [use] ");
4356 name = WXS_ATTRUSE_DECL_NAME(use);
4357 tns = WXS_ATTRUSE_DECL_TNS(use);
4358 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004359 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004360 (const char *) xmlSchemaFormatQName(&str, tns, name));
4361 FREE_AND_NULL(str);
4362 }
4363}
4364
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004365/**
4366 * xmlSchemaTypeDump:
4367 * @output: the file output
4368 * @type: a type structure
4369 *
4370 * Dump a SchemaType structure
4371 */
4372static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004373xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4374{
4375 if (type == NULL) {
4376 fprintf(output, "Type: NULL\n");
4377 return;
4378 }
4379 fprintf(output, "Type: ");
4380 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004381 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004382 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004383 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004384 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004385 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004386 switch (type->type) {
4387 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004388 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004389 break;
4390 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004391 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004392 break;
4393 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004394 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004395 break;
4396 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004397 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 break;
4399 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004401 break;
4402 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 break;
4405 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004407 break;
4408 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004409 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004410 break;
4411 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 break;
4414 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004415 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004416 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004417 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004418 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004419 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004420 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004421 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004422 break;
4423 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004424 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004425 break;
4426 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004427 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004428 break;
4429 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004430 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004431 break;
4432 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004433 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004434 break;
4435 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004436 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004437 break;
4438 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004439 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004440 break;
4441 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004442 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004443 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004444 }
4445 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004446 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004447 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004448 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004449 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004450 else
4451 fprintf(output, "\n");
4452 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004453 if (type->attrUses != NULL)
4454 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004455 if (type->annot != NULL)
4456 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004457#ifdef DUMP_CONTENT_MODEL
4458 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4459 (type->subtypes != NULL)) {
4460 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4461 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004462 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004463#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004464}
4465
4466/**
4467 * xmlSchemaDump:
4468 * @output: the file output
4469 * @schema: a schema structure
4470 *
4471 * Dump a Schema structure.
4472 */
4473void
4474xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4475{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004476 if (output == NULL)
4477 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004478 if (schema == NULL) {
4479 fprintf(output, "Schemas: NULL\n");
4480 return;
4481 }
4482 fprintf(output, "Schemas: ");
4483 if (schema->name != NULL)
4484 fprintf(output, "%s, ", schema->name);
4485 else
4486 fprintf(output, "no name, ");
4487 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004488 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004489 else
4490 fprintf(output, "no target namespace");
4491 fprintf(output, "\n");
4492 if (schema->annot != NULL)
4493 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004494 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4495 output);
4496 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004497 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004498}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004499
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004500#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004501/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004502 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004503 * @vctxt: the WXS validation context
4504 *
4505 * Displays the current IDC table for debug purposes.
4506 */
4507static void
4508xmlSchemaDebugDumpIDCTable(FILE * output,
4509 const xmlChar *namespaceName,
4510 const xmlChar *localName,
4511 xmlSchemaPSVIIDCBindingPtr bind)
4512{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004513 xmlChar *str = NULL;
4514 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004515 xmlSchemaPSVIIDCNodePtr tab;
4516 xmlSchemaPSVIIDCKeyPtr key;
4517 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004518
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004519 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004520 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004521 FREE_AND_NULL(str)
4522
4523 if (bind == NULL)
4524 return;
4525 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004526 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004527 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004528 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004529 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004530 for (i = 0; i < bind->nbNodes; i++) {
4531 tab = bind->nodeTable[i];
4532 fprintf(output, " ( ");
4533 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004534 key = tab->keys[j];
4535 if ((key != NULL) && (key->val != NULL)) {
4536 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004537 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004538 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004539 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004540 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004541 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004542 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004543 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004544 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004545 else
4546 fprintf(output, "(key missing), ");
4547 }
4548 fprintf(output, ")\n");
4549 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004550 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004551 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004552 for (i = 0; i < bind->dupls->nbItems; i++) {
4553 tab = bind->dupls->items[i];
4554 fprintf(output, " ( ");
4555 for (j = 0; j < bind->definition->nbFields; j++) {
4556 key = tab->keys[j];
4557 if ((key != NULL) && (key->val != NULL)) {
4558 res = xmlSchemaGetCanonValue(key->val, &value);
4559 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004560 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004561 else
4562 fprintf(output, "CANON-VALUE-FAILED ");
4563 if (res == 0)
4564 FREE_AND_NULL(value)
4565 } else if (key != NULL)
4566 fprintf(output, "(no val), ");
4567 else
4568 fprintf(output, "(key missing), ");
4569 }
4570 fprintf(output, ")\n");
4571 }
4572 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004573 bind = bind->next;
4574 } while (bind != NULL);
4575}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004576#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004577#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004578
4579/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004580 * *
4581 * Utilities *
4582 * *
4583 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004584
Daniel Veillardc0826a72004-08-10 14:17:33 +00004585/**
4586 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004587 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004588 * @name: the name of the attribute
4589 *
4590 * Seeks an attribute with a name of @name in
4591 * no namespace.
4592 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004593 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004594 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004595static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004596xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004597{
4598 xmlAttrPtr prop;
4599
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004600 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004601 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004602 prop = node->properties;
4603 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004604 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004605 return(prop);
4606 prop = prop->next;
4607 }
4608 return (NULL);
4609}
4610
4611/**
4612 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004613 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004614 * @uri: the uri
4615 * @name: the name of the attribute
4616 *
4617 * Seeks an attribute with a local name of @name and
4618 * a namespace URI of @uri.
4619 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004620 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004621 */
4622static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004623xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004624{
4625 xmlAttrPtr prop;
4626
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004627 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004628 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004629 prop = node->properties;
4630 while (prop != NULL) {
4631 if ((prop->ns != NULL) &&
4632 xmlStrEqual(prop->name, BAD_CAST name) &&
4633 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004634 return(prop);
4635 prop = prop->next;
4636 }
4637 return (NULL);
4638}
4639
4640static const xmlChar *
4641xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4642{
4643 xmlChar *val;
4644 const xmlChar *ret;
4645
4646 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004647 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004648 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004649 ret = xmlDictLookup(ctxt->dict, val, -1);
4650 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004651 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004652}
4653
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004654static const xmlChar *
4655xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4656{
4657 return((const xmlChar*) xmlNodeGetContent(node));
4658}
4659
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004660/**
4661 * xmlSchemaGetProp:
4662 * @ctxt: the parser context
4663 * @node: the node
4664 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004665 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004666 * Read a attribute value and internalize the string
4667 *
4668 * Returns the string or NULL if not present.
4669 */
4670static const xmlChar *
4671xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4672 const char *name)
4673{
4674 xmlChar *val;
4675 const xmlChar *ret;
4676
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004677 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004678 if (val == NULL)
4679 return(NULL);
4680 ret = xmlDictLookup(ctxt->dict, val, -1);
4681 xmlFree(val);
4682 return(ret);
4683}
4684
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004685/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004686 * *
4687 * Parsing functions *
4688 * *
4689 ************************************************************************/
4690
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004691#define WXS_FIND_GLOBAL_ITEM(slot) \
4692 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4693 ret = xmlHashLookup(schema->slot, name); \
4694 if (ret != NULL) goto exit; \
4695 } \
4696 if (xmlHashSize(schema->schemasImports) > 1) { \
4697 xmlSchemaImportPtr import; \
4698 if (nsName == NULL) \
4699 import = xmlHashLookup(schema->schemasImports, \
4700 XML_SCHEMAS_NO_NAMESPACE); \
4701 else \
4702 import = xmlHashLookup(schema->schemasImports, nsName); \
4703 if (import == NULL) \
4704 goto exit; \
4705 ret = xmlHashLookup(import->schema->slot, name); \
4706 }
4707
Daniel Veillard4255d502002-04-16 15:50:10 +00004708/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004709 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004710 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004711 * @name: the element name
4712 * @ns: the element namespace
4713 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004714 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004715 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004716 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004717 */
4718static xmlSchemaElementPtr
4719xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004720 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004721{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004722 xmlSchemaElementPtr ret = NULL;
4723
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004724 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004725 return(NULL);
4726 if (schema != NULL) {
4727 WXS_FIND_GLOBAL_ITEM(elemDecl)
4728 }
4729exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004730#ifdef DEBUG
4731 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004732 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004733 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004734 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004735 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004736 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004737 }
4738#endif
4739 return (ret);
4740}
4741
4742/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004743 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004744 * @schema: the main schema
4745 * @name: the type's name
4746 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004747 *
4748 * Lookup a type in the schemas or the predefined types
4749 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004750 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004751 */
4752static xmlSchemaTypePtr
4753xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004754 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004755{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004756 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004757
4758 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004759 return (NULL);
4760 /* First try the built-in types. */
4761 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4762 ret = xmlSchemaGetPredefinedType(name, nsName);
4763 if (ret != NULL)
4764 goto exit;
4765 /*
4766 * Note that we try the parsed schemas as well here
4767 * since one might have parsed the S4S, which contain more
4768 * than the built-in types.
4769 * TODO: Can we optimize this?
4770 */
4771 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004772 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004773 WXS_FIND_GLOBAL_ITEM(typeDecl)
4774 }
4775exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004776
Daniel Veillard4255d502002-04-16 15:50:10 +00004777#ifdef DEBUG
4778 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004779 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004780 fprintf(stderr, "Unable to lookup type %s", name);
4781 else
4782 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004783 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004784 }
4785#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004786 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004787}
4788
Daniel Veillard3646d642004-06-02 19:19:14 +00004789/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004790 * xmlSchemaGetAttributeDecl:
4791 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004792 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004793 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004794 *
4795 * Lookup a an attribute in the schema or imported schemas
4796 *
4797 * Returns the attribute declaration or NULL if not found.
4798 */
4799static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004800xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004801 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004802{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004803 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004804
4805 if ((name == NULL) || (schema == NULL))
4806 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004807 if (schema != NULL) {
4808 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004809 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004810exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004811#ifdef DEBUG
4812 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004813 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004814 fprintf(stderr, "Unable to lookup attribute %s", name);
4815 else
4816 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004817 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 }
4819#endif
4820 return (ret);
4821}
4822
4823/**
4824 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004825 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004826 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004827 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004828 *
4829 * Lookup a an attribute group in the schema or imported schemas
4830 *
4831 * Returns the attribute group definition or NULL if not found.
4832 */
4833static xmlSchemaAttributeGroupPtr
4834xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004835 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004836{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004837 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004838
4839 if ((name == NULL) || (schema == NULL))
4840 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004841 if (schema != NULL) {
4842 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4843 }
4844exit:
4845 /* TODO:
4846 if ((ret != NULL) && (ret->redef != NULL)) {
4847 * Return the last redefinition. *
4848 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004849 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004850 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004851#ifdef DEBUG
4852 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004853 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004854 fprintf(stderr, "Unable to lookup attribute group %s", name);
4855 else
4856 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004857 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004858 }
4859#endif
4860 return (ret);
4861}
4862
4863/**
4864 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004865 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004866 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004867 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004868 *
4869 * Lookup a group in the schema or imported schemas
4870 *
4871 * Returns the group definition or NULL if not found.
4872 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004873static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004874xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004875 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004876{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004877 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004878
4879 if ((name == NULL) || (schema == NULL))
4880 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004881 if (schema != NULL) {
4882 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004883 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004884exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004885
Daniel Veillard3646d642004-06-02 19:19:14 +00004886#ifdef DEBUG
4887 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004888 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004889 fprintf(stderr, "Unable to lookup group %s", name);
4890 else
4891 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004892 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004893 }
4894#endif
4895 return (ret);
4896}
4897
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004898static xmlSchemaNotationPtr
4899xmlSchemaGetNotation(xmlSchemaPtr schema,
4900 const xmlChar *name,
4901 const xmlChar *nsName)
4902{
4903 xmlSchemaNotationPtr ret = NULL;
4904
4905 if ((name == NULL) || (schema == NULL))
4906 return (NULL);
4907 if (schema != NULL) {
4908 WXS_FIND_GLOBAL_ITEM(notaDecl)
4909 }
4910exit:
4911 return (ret);
4912}
4913
4914static xmlSchemaIDCPtr
4915xmlSchemaGetIDC(xmlSchemaPtr schema,
4916 const xmlChar *name,
4917 const xmlChar *nsName)
4918{
4919 xmlSchemaIDCPtr ret = NULL;
4920
4921 if ((name == NULL) || (schema == NULL))
4922 return (NULL);
4923 if (schema != NULL) {
4924 WXS_FIND_GLOBAL_ITEM(idcDef)
4925 }
4926exit:
4927 return (ret);
4928}
4929
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004930/**
4931 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004932 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004933 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004934 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004935 *
4936 * Lookup a group in the schema or imported schemas
4937 *
4938 * Returns the group definition or NULL if not found.
4939 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004940static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004941xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4942 xmlSchemaTypeType itemType,
4943 const xmlChar *name,
4944 const xmlChar *targetNs)
4945{
4946 switch (itemType) {
4947 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004948 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004949 name, targetNs));
4950 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004951 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004952 name, targetNs));
4953 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004954 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004955 return (NULL);
4956 }
4957}
4958
Daniel Veillard4255d502002-04-16 15:50:10 +00004959/************************************************************************
4960 * *
4961 * Parsing functions *
4962 * *
4963 ************************************************************************/
4964
4965#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004966 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004967
4968/**
4969 * xmlSchemaIsBlank:
4970 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004971 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004972 *
4973 * Check if a string is ignorable
4974 *
4975 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4976 */
4977static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004978xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004979{
Daniel Veillard4255d502002-04-16 15:50:10 +00004980 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004981 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004982 if (len < 0) {
4983 while (*str != 0) {
4984 if (!(IS_BLANK_CH(*str)))
4985 return (0);
4986 str++;
4987 }
4988 } else while ((*str != 0) && (len != 0)) {
4989 if (!(IS_BLANK_CH(*str)))
4990 return (0);
4991 str++;
4992 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004993 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004994
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004995 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004996}
4997
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004998#define WXS_COMP_NAME(c, t) ((t) (c))->name
4999#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
5000/*
5001* xmlSchemaFindRedefCompInGraph:
5002* ATTENTION TODO: This uses pointer comp. for strings.
5003*/
5004static xmlSchemaBasicItemPtr
5005xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
5006 xmlSchemaTypeType type,
5007 const xmlChar *name,
5008 const xmlChar *nsName)
5009{
5010 xmlSchemaBasicItemPtr ret;
5011 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005012
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005013 if ((bucket == NULL) || (name == NULL))
5014 return(NULL);
5015 if ((bucket->globals == NULL) ||
5016 (bucket->globals->nbItems == 0))
5017 goto subschemas;
5018 /*
5019 * Search in global components.
5020 */
5021 for (i = 0; i < bucket->globals->nbItems; i++) {
5022 ret = bucket->globals->items[i];
5023 if (ret->type == type) {
5024 switch (type) {
5025 case XML_SCHEMA_TYPE_COMPLEX:
5026 case XML_SCHEMA_TYPE_SIMPLE:
5027 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5028 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5029 nsName))
5030 {
5031 return(ret);
5032 }
5033 break;
5034 case XML_SCHEMA_TYPE_GROUP:
5035 if ((WXS_COMP_NAME(ret,
5036 xmlSchemaModelGroupDefPtr) == name) &&
5037 (WXS_COMP_TNS(ret,
5038 xmlSchemaModelGroupDefPtr) == nsName))
5039 {
5040 return(ret);
5041 }
5042 break;
5043 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5044 if ((WXS_COMP_NAME(ret,
5045 xmlSchemaAttributeGroupPtr) == name) &&
5046 (WXS_COMP_TNS(ret,
5047 xmlSchemaAttributeGroupPtr) == nsName))
5048 {
5049 return(ret);
5050 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005051 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005052 default:
5053 /* Should not be hit. */
5054 return(NULL);
5055 }
5056 }
5057 }
5058subschemas:
5059 /*
5060 * Process imported/included schemas.
5061 */
5062 if (bucket->relations != NULL) {
5063 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5064
5065 /*
5066 * TODO: Marking the bucket will not avoid multiple searches
5067 * in the same schema, but avoids at least circularity.
5068 */
5069 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5070 do {
5071 if ((rel->bucket != NULL) &&
5072 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5073 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5074 type, name, nsName);
5075 if (ret != NULL)
5076 return(ret);
5077 }
5078 rel = rel->next;
5079 } while (rel != NULL);
5080 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5081 }
5082 return(NULL);
5083}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005084
5085/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005086 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005087 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005088 * @schema: the schema being built
5089 * @name: the item name
5090 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005091 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005092 * *WARNING* this interface is highly subject to change
5093 *
5094 * Returns the new struture or NULL in case of error
5095 */
5096static xmlSchemaNotationPtr
5097xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005098 const xmlChar *name, const xmlChar *nsName,
5099 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005100{
5101 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005102
5103 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5104 return (NULL);
5105
Daniel Veillard4255d502002-04-16 15:50:10 +00005106 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5107 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005108 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 return (NULL);
5110 }
5111 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005112 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005113 ret->name = name;
5114 ret->targetNamespace = nsName;
5115 /* TODO: do we need the node to be set?
5116 * ret->node = node;*/
5117 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 return (ret);
5119}
5120
Daniel Veillard4255d502002-04-16 15:50:10 +00005121/**
5122 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005123 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005124 * @schema: the schema being built
5125 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005126 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005127 *
5128 * Add an XML schema Attrribute declaration
5129 * *WARNING* this interface is highly subject to change
5130 *
5131 * Returns the new struture or NULL in case of error
5132 */
5133static xmlSchemaAttributePtr
5134xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005135 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005136 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005137{
5138 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005139
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005140 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005141 return (NULL);
5142
5143 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5144 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005145 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005146 return (NULL);
5147 }
5148 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005149 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5150 ret->node = node;
5151 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005152 ret->targetNamespace = nsName;
5153
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005154 if (topLevel)
5155 WXS_ADD_GLOBAL(ctxt, ret);
5156 else
5157 WXS_ADD_LOCAL(ctxt, ret);
5158 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005159 return (ret);
5160}
5161
5162/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005163 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005164 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005165 * @schema: the schema being built
5166 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005167 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005168 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005169 * Add an XML schema Attrribute declaration
5170 * *WARNING* this interface is highly subject to change
5171 *
5172 * Returns the new struture or NULL in case of error
5173 */
5174static xmlSchemaAttributeUsePtr
5175xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5176 xmlNodePtr node)
5177{
5178 xmlSchemaAttributeUsePtr ret = NULL;
5179
5180 if (pctxt == NULL)
5181 return (NULL);
5182
5183 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5184 if (ret == NULL) {
5185 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5186 return (NULL);
5187 }
5188 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5189 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5190 ret->node = node;
5191
5192 WXS_ADD_LOCAL(pctxt, ret);
5193 return (ret);
5194}
5195
5196/*
5197* xmlSchemaAddRedef:
5198*
5199* Adds a redefinition information. This is used at a later stage to:
5200* resolve references to the redefined components and to check constraints.
5201*/
5202static xmlSchemaRedefPtr
5203xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5204 xmlSchemaBucketPtr targetBucket,
5205 void *item,
5206 const xmlChar *refName,
5207 const xmlChar *refTargetNs)
5208{
5209 xmlSchemaRedefPtr ret;
5210
5211 ret = (xmlSchemaRedefPtr)
5212 xmlMalloc(sizeof(xmlSchemaRedef));
5213 if (ret == NULL) {
5214 xmlSchemaPErrMemory(pctxt,
5215 "allocating redefinition info", NULL);
5216 return (NULL);
5217 }
5218 memset(ret, 0, sizeof(xmlSchemaRedef));
5219 ret->item = item;
5220 ret->targetBucket = targetBucket;
5221 ret->refName = refName;
5222 ret->refTargetNs = refTargetNs;
5223 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5224 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5225 else
5226 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5227 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5228
5229 return (ret);
5230}
5231
5232/**
5233 * xmlSchemaAddAttributeGroupDefinition:
5234 * @ctxt: a schema parser context
5235 * @schema: the schema being built
5236 * @name: the item name
5237 * @nsName: the target namespace
5238 * @node: the corresponding node
5239 *
5240 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005241 *
5242 * Returns the new struture or NULL in case of error
5243 */
5244static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005245xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5246 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5247 const xmlChar *name,
5248 const xmlChar *nsName,
5249 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005250{
5251 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005253 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005254 return (NULL);
5255
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005256 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005257 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005258 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005259 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005260 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005261 }
5262 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005263 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005264 ret->name = name;
5265 ret->targetNamespace = nsName;
5266 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005267
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005268 /* TODO: Remove the flag. */
5269 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5270 if (pctxt->isRedefine) {
5271 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5272 ret, name, nsName);
5273 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005274 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005275 return(NULL);
5276 }
5277 pctxt->redefCounter = 0;
5278 }
5279 WXS_ADD_GLOBAL(pctxt, ret);
5280 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005281 return (ret);
5282}
5283
5284/**
5285 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005286 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 * @schema: the schema being built
5288 * @name: the type name
5289 * @namespace: the type namespace
5290 *
5291 * Add an XML schema Element declaration
5292 * *WARNING* this interface is highly subject to change
5293 *
5294 * Returns the new struture or NULL in case of error
5295 */
5296static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005297xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005298 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005299 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005300{
5301 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005302
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005303 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005304 return (NULL);
5305
Daniel Veillard4255d502002-04-16 15:50:10 +00005306 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5307 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005308 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 return (NULL);
5310 }
5311 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005312 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5313 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005314 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005315 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005316
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005317 if (topLevel)
5318 WXS_ADD_GLOBAL(ctxt, ret);
5319 else
5320 WXS_ADD_LOCAL(ctxt, ret);
5321 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005322 return (ret);
5323}
5324
5325/**
5326 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005327 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005328 * @schema: the schema being built
5329 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005330 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005331 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005332 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005333 * *WARNING* this interface is highly subject to change
5334 *
5335 * Returns the new struture or NULL in case of error
5336 */
5337static xmlSchemaTypePtr
5338xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005339 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005340 const xmlChar * name, const xmlChar * nsName,
5341 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005342{
5343 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005344
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005345 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005346 return (NULL);
5347
5348 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5349 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005350 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005351 return (NULL);
5352 }
5353 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005354 ret->type = type;
5355 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005356 ret->targetNamespace = nsName;
5357 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005358 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005359 if (ctxt->isRedefine) {
5360 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5361 ret, name, nsName);
5362 if (ctxt->redef == NULL) {
5363 xmlFree(ret);
5364 return(NULL);
5365 }
5366 ctxt->redefCounter = 0;
5367 }
5368 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005369 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005370 WXS_ADD_LOCAL(ctxt, ret);
5371 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005372 return (ret);
5373}
5374
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005375static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005376xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005377 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005378 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005379 const xmlChar *refNs)
5380{
5381 xmlSchemaQNameRefPtr ret;
5382
5383 ret = (xmlSchemaQNameRefPtr)
5384 xmlMalloc(sizeof(xmlSchemaQNameRef));
5385 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005386 xmlSchemaPErrMemory(pctxt,
5387 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005388 return (NULL);
5389 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005390 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005391 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5392 ret->name = refName;
5393 ret->targetNamespace = refNs;
5394 ret->item = NULL;
5395 ret->itemType = refType;
5396 /*
5397 * Store the reference item in the schema.
5398 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005399 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005400 return (ret);
5401}
5402
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005403static xmlSchemaAttributeUseProhibPtr
5404xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5405{
5406 xmlSchemaAttributeUseProhibPtr ret;
5407
5408 ret = (xmlSchemaAttributeUseProhibPtr)
5409 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5410 if (ret == NULL) {
5411 xmlSchemaPErrMemory(pctxt,
5412 "allocating attribute use prohibition", NULL);
5413 return (NULL);
5414 }
5415 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5416 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5417 WXS_ADD_LOCAL(pctxt, ret);
5418 return (ret);
5419}
5420
5421
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005422/**
5423 * xmlSchemaAddModelGroup:
5424 * @ctxt: a schema parser context
5425 * @schema: the schema being built
5426 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005427 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005428 *
5429 * Adds a schema model group
5430 * *WARNING* this interface is highly subject to change
5431 *
5432 * Returns the new struture or NULL in case of error
5433 */
5434static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005435xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5436 xmlSchemaPtr schema,
5437 xmlSchemaTypeType type,
5438 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005439{
5440 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005441
5442 if ((ctxt == NULL) || (schema == NULL))
5443 return (NULL);
5444
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005445 ret = (xmlSchemaModelGroupPtr)
5446 xmlMalloc(sizeof(xmlSchemaModelGroup));
5447 if (ret == NULL) {
5448 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5449 NULL);
5450 return (NULL);
5451 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005452 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005453 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005454 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005455 WXS_ADD_LOCAL(ctxt, ret);
5456 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5457 (type == XML_SCHEMA_TYPE_CHOICE))
5458 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005459 return (ret);
5460}
5461
5462
5463/**
5464 * xmlSchemaAddParticle:
5465 * @ctxt: a schema parser context
5466 * @schema: the schema being built
5467 * @node: the corresponding node in the schema doc
5468 * @min: the minOccurs
5469 * @max: the maxOccurs
5470 *
5471 * Adds an XML schema particle component.
5472 * *WARNING* this interface is highly subject to change
5473 *
5474 * Returns the new struture or NULL in case of error
5475 */
5476static xmlSchemaParticlePtr
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005477xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005478 xmlNodePtr node, int min, int max)
5479{
5480 xmlSchemaParticlePtr ret = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005481 if (ctxt == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005482 return (NULL);
5483
5484#ifdef DEBUG
5485 fprintf(stderr, "Adding particle component\n");
5486#endif
5487 ret = (xmlSchemaParticlePtr)
5488 xmlMalloc(sizeof(xmlSchemaParticle));
5489 if (ret == NULL) {
5490 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5491 NULL);
5492 return (NULL);
5493 }
5494 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5495 ret->annot = NULL;
5496 ret->node = node;
5497 ret->minOccurs = min;
5498 ret->maxOccurs = max;
5499 ret->next = NULL;
5500 ret->children = NULL;
5501
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005502 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005503 /*
5504 * Note that addition to pending components will be done locally
5505 * to the specific parsing function, since the most particles
5506 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005507 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005508 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005509 return (ret);
5510}
5511
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005512/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005513 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005514 * @ctxt: a schema validation context
5515 * @schema: the schema being built
5516 * @name: the group name
5517 *
5518 * Add an XML schema Group definition
5519 *
5520 * Returns the new struture or NULL in case of error
5521 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005522static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005523xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5524 xmlSchemaPtr schema,
5525 const xmlChar *name,
5526 const xmlChar *nsName,
5527 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005528{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005529 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005530
5531 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5532 return (NULL);
5533
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005534 ret = (xmlSchemaModelGroupDefPtr)
5535 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005536 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005537 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005538 return (NULL);
5539 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005540 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005541 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005542 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005543 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005544 ret->targetNamespace = nsName;
5545
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005546 if (ctxt->isRedefine) {
5547 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5548 ret, name, nsName);
5549 if (ctxt->redef == NULL) {
5550 xmlFree(ret);
5551 return(NULL);
5552 }
5553 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005554 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005555 WXS_ADD_GLOBAL(ctxt, ret);
5556 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005557 return (ret);
5558}
5559
Daniel Veillard3646d642004-06-02 19:19:14 +00005560/**
5561 * xmlSchemaNewWildcardNs:
5562 * @ctxt: a schema validation context
5563 *
5564 * Creates a new wildcard namespace constraint.
5565 *
5566 * Returns the new struture or NULL in case of error
5567 */
5568static xmlSchemaWildcardNsPtr
5569xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5570{
5571 xmlSchemaWildcardNsPtr ret;
5572
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005573 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005574 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5575 if (ret == NULL) {
5576 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005577 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005578 }
5579 ret->value = NULL;
5580 ret->next = NULL;
5581 return (ret);
5582}
5583
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005584static xmlSchemaIDCPtr
5585xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5586 const xmlChar *name, const xmlChar *nsName,
5587 int category, xmlNodePtr node)
5588{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005589 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005590
5591 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5592 return (NULL);
5593
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005594 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5595 if (ret == NULL) {
5596 xmlSchemaPErrMemory(ctxt,
5597 "allocating an identity-constraint definition", NULL);
5598 return (NULL);
5599 }
5600 memset(ret, 0, sizeof(xmlSchemaIDC));
5601 /* The target namespace of the parent element declaration. */
5602 ret->targetNamespace = nsName;
5603 ret->name = name;
5604 ret->type = category;
5605 ret->node = node;
5606
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005607 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005608 /*
5609 * Only keyrefs need to be fixup up.
5610 */
5611 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005612 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005613 return (ret);
5614}
5615
Daniel Veillard3646d642004-06-02 19:19:14 +00005616/**
5617 * xmlSchemaAddWildcard:
5618 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005619 * @schema: a schema
5620 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005621 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005622 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005623 *
5624 * Returns the new struture or NULL in case of error
5625 */
5626static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005627xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5628 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005629{
5630 xmlSchemaWildcardPtr ret = NULL;
5631
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005632 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005633 return (NULL);
5634
5635 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5636 if (ret == NULL) {
5637 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5638 return (NULL);
5639 }
5640 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005641 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005642 ret->node = node;
5643 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005644 return (ret);
5645}
5646
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005647static void
5648xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5649{
5650 if (group == NULL)
5651 return;
5652 if (group->members != NULL)
5653 xmlSchemaItemListFree(group->members);
5654 xmlFree(group);
5655}
5656
5657static xmlSchemaSubstGroupPtr
5658xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5659 xmlSchemaElementPtr head)
5660{
5661 xmlSchemaSubstGroupPtr ret;
5662
5663 /* Init subst group hash. */
5664 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5665 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5666 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5667 return(NULL);
5668 }
5669 /* Create a new substitution group. */
5670 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5671 if (ret == NULL) {
5672 xmlSchemaPErrMemory(NULL,
5673 "allocating a substitution group container", NULL);
5674 return(NULL);
5675 }
5676 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5677 ret->head = head;
5678 /* Create list of members. */
5679 ret->members = xmlSchemaItemListCreate();
5680 if (ret->members == NULL) {
5681 xmlSchemaSubstGroupFree(ret);
5682 return(NULL);
5683 }
5684 /* Add subst group to hash. */
5685 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5686 head->name, head->targetNamespace, ret) != 0) {
5687 PERROR_INT("xmlSchemaSubstGroupAdd",
5688 "failed to add a new substitution container");
5689 xmlSchemaSubstGroupFree(ret);
5690 return(NULL);
5691 }
5692 return(ret);
5693}
5694
5695static xmlSchemaSubstGroupPtr
5696xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5697 xmlSchemaElementPtr head)
5698{
5699 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5700 return(NULL);
5701 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5702 head->name, head->targetNamespace));
5703
5704}
5705
5706/**
5707 * xmlSchemaAddElementSubstitutionMember:
5708 * @pctxt: a schema parser context
5709 * @head: the head of the substitution group
5710 * @member: the new member of the substitution group
5711 *
5712 * Allocate a new annotation structure.
5713 *
5714 * Returns the newly allocated structure or NULL in case or error
5715 */
5716static int
5717xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5718 xmlSchemaElementPtr head,
5719 xmlSchemaElementPtr member)
5720{
5721 xmlSchemaSubstGroupPtr substGroup = NULL;
5722
5723 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5724 return (-1);
5725
5726 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5727 if (substGroup == NULL)
5728 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5729 if (substGroup == NULL)
5730 return(-1);
5731 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5732 return(-1);
5733 return(0);
5734}
5735
Daniel Veillard4255d502002-04-16 15:50:10 +00005736/************************************************************************
5737 * *
5738 * Utilities for parsing *
5739 * *
5740 ************************************************************************/
5741
Daniel Veillard4255d502002-04-16 15:50:10 +00005742/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005743 * xmlSchemaPValAttrNodeQNameValue:
5744 * @ctxt: a schema parser context
5745 * @schema: the schema context
5746 * @ownerDes: the designation of the parent element
5747 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005748 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005749 * @local: the resulting local part if found, the attribute value otherwise
5750 * @uri: the resulting namespace URI if found
5751 *
5752 * Extracts the local name and the URI of a QName value and validates it.
5753 * This one is intended to be used on attribute values that
5754 * should resolve to schema components.
5755 *
5756 * Returns 0, in case the QName is valid, a positive error code
5757 * if not valid and -1 if an internal error occurs.
5758 */
5759static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005760xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005761 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005762 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005763 xmlAttrPtr attr,
5764 const xmlChar *value,
5765 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005766 const xmlChar **local)
5767{
5768 const xmlChar *pref;
5769 xmlNsPtr ns;
5770 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005771
Daniel Veillardc0826a72004-08-10 14:17:33 +00005772 *uri = NULL;
5773 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005774 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005775 if (ret > 0) {
5776 xmlSchemaPSimpleTypeErr(ctxt,
5777 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5778 ownerItem, (xmlNodePtr) attr,
5779 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5780 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005781 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005782 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 } else if (ret < 0)
5784 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005785
5786 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005787 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005788 if (ns)
5789 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5790 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005791 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5792 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005793 /*
5794 * This one takes care of included schemas with no
5795 * target namespace.
5796 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005797 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005798 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005799 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005800 return (0);
5801 }
5802 /*
5803 * At this point xmlSplitQName3 has to return a local name.
5804 */
5805 *local = xmlSplitQName3(value, &len);
5806 *local = xmlDictLookup(ctxt->dict, *local, -1);
5807 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005808 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5809 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005810 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005811 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005812 ownerItem, (xmlNodePtr) attr,
5813 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5814 "The value '%s' of simple type 'xs:QName' has no "
5815 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005816 return (ctxt->err);
5817 } else {
5818 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005819 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005820 return (0);
5821}
5822
5823/**
5824 * xmlSchemaPValAttrNodeQName:
5825 * @ctxt: a schema parser context
5826 * @schema: the schema context
5827 * @ownerDes: the designation of the owner element
5828 * @ownerItem: the owner as a schema object
5829 * @attr: the attribute node
5830 * @local: the resulting local part if found, the attribute value otherwise
5831 * @uri: the resulting namespace URI if found
5832 *
5833 * Extracts and validates the QName of an attribute value.
5834 * This one is intended to be used on attribute values that
5835 * should resolve to schema components.
5836 *
5837 * Returns 0, in case the QName is valid, a positive error code
5838 * if not valid and -1 if an internal error occurs.
5839 */
5840static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005841xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005842 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005843 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005844 xmlAttrPtr attr,
5845 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005846 const xmlChar **local)
5847{
5848 const xmlChar *value;
5849
5850 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005851 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005852 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005853}
5854
5855/**
5856 * xmlSchemaPValAttrQName:
5857 * @ctxt: a schema parser context
5858 * @schema: the schema context
5859 * @ownerDes: the designation of the parent element
5860 * @ownerItem: the owner as a schema object
5861 * @ownerElem: the parent node of the attribute
5862 * @name: the name of the attribute
5863 * @local: the resulting local part if found, the attribute value otherwise
5864 * @uri: the resulting namespace URI if found
5865 *
5866 * Extracts and validates the QName of an attribute value.
5867 *
5868 * Returns 0, in case the QName is valid, a positive error code
5869 * if not valid and -1 if an internal error occurs.
5870 */
5871static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005872xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5873 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005874 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005875 xmlNodePtr ownerElem,
5876 const char *name,
5877 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005878 const xmlChar **local)
5879{
5880 xmlAttrPtr attr;
5881
5882 attr = xmlSchemaGetPropNode(ownerElem, name);
5883 if (attr == NULL) {
5884 *local = NULL;
5885 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005886 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005887 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005888 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005889 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005890}
5891
5892/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005893 * xmlSchemaPValAttrID:
5894 * @ctxt: a schema parser context
5895 * @schema: the schema context
5896 * @ownerDes: the designation of the parent element
5897 * @ownerItem: the owner as a schema object
5898 * @ownerElem: the parent node of the attribute
5899 * @name: the name of the attribute
5900 *
5901 * Extracts and validates the ID of an attribute value.
5902 *
5903 * Returns 0, in case the ID is valid, a positive error code
5904 * if not valid and -1 if an internal error occurs.
5905 */
5906static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005907xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005908{
5909 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005910 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005911
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005912 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005913 return(0);
5914 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5915 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005916 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005917 /*
5918 * NOTE: the IDness might have already be declared in the DTD
5919 */
5920 if (attr->atype != XML_ATTRIBUTE_ID) {
5921 xmlIDPtr res;
5922 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005923
5924 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005925 * TODO: Use xmlSchemaStrip here; it's not exported at this
5926 * moment.
5927 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005928 strip = xmlSchemaCollapseString(value);
5929 if (strip != NULL) {
5930 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005931 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005932 }
5933 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005934 if (res == NULL) {
5935 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005936 xmlSchemaPSimpleTypeErr(ctxt,
5937 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005938 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005939 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5940 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005941 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005942 } else
5943 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005944 }
5945 } else if (ret > 0) {
5946 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005947 xmlSchemaPSimpleTypeErr(ctxt,
5948 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005949 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005950 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5951 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5952 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005953 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005954 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005955 if (value != NULL)
5956 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005957
5958 return (ret);
5959}
5960
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005961static int
5962xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5963 xmlNodePtr ownerElem,
5964 const xmlChar *name)
5965{
5966 xmlAttrPtr attr;
5967
5968 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5969 if (attr == NULL)
5970 return(0);
5971 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5972
5973}
5974
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005975/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005976 * xmlGetMaxOccurs:
5977 * @ctxt: a schema validation context
5978 * @node: a subtree containing XML Schema informations
5979 *
5980 * Get the maxOccurs property
5981 *
5982 * Returns the default if not found, or the value
5983 */
5984static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005985xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5986 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005987{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005988 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005989 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005990 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005991
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005992 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5993 if (attr == NULL)
5994 return (def);
5995 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005996
5997 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005998 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005999 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006000 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6001 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006002 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006003 val, NULL, NULL, NULL);
6004 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006005 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006006 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00006007 }
6008
6009 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006010 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006011 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006012 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006013 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006014 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6015 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006016 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006017 val, NULL, NULL, NULL);
6018 return (def);
6019 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006020 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006021 ret = ret * 10 + (*cur - '0');
6022 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006023 }
William M. Brack76e95df2003-10-18 16:20:14 +00006024 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006025 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006026 /*
6027 * TODO: Restrict the maximal value to Integer.
6028 */
6029 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006030 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006031 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6032 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006033 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006034 val, NULL, NULL, NULL);
6035 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006036 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006037 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006038}
6039
6040/**
6041 * xmlGetMinOccurs:
6042 * @ctxt: a schema validation context
6043 * @node: a subtree containing XML Schema informations
6044 *
6045 * Get the minOccurs property
6046 *
6047 * Returns the default if not found, or the value
6048 */
6049static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006050xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006051 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006052{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006053 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006054 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006055 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006056
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006057 attr = xmlSchemaGetPropNode(node, "minOccurs");
6058 if (attr == NULL)
6059 return (def);
6060 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006061 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006062 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006063 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006064 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006065 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006066 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6067 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006068 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006069 val, NULL, NULL, NULL);
6070 return (def);
6071 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006072 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006073 ret = ret * 10 + (*cur - '0');
6074 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006075 }
William M. Brack76e95df2003-10-18 16:20:14 +00006076 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006077 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006078 /*
6079 * TODO: Restrict the maximal value to Integer.
6080 */
6081 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006082 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006083 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6084 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006085 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006086 val, NULL, NULL, NULL);
6087 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006088 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006089 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006090}
6091
6092/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006093 * xmlSchemaPGetBoolNodeValue:
6094 * @ctxt: a schema validation context
6095 * @ownerDes: owner designation
6096 * @ownerItem: the owner as a schema item
6097 * @node: the node holding the value
6098 *
6099 * Converts a boolean string value into 1 or 0.
6100 *
6101 * Returns 0 or 1.
6102 */
6103static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006104xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006105 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006106 xmlNodePtr node)
6107{
6108 xmlChar *value = NULL;
6109 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006110
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006111 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006112 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006113 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006114 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006115 * can have the following legal literals {true, false, 1, 0}.
6116 */
6117 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6118 res = 1;
6119 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6120 res = 0;
6121 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6122 res = 1;
6123 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006124 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006125 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006126 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006127 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006128 ownerItem, node,
6129 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6130 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006131 NULL, NULL, NULL);
6132 }
6133 if (value != NULL)
6134 xmlFree(value);
6135 return (res);
6136}
6137
6138/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006139 * xmlGetBooleanProp:
6140 * @ctxt: a schema validation context
6141 * @node: a subtree containing XML Schema informations
6142 * @name: the attribute name
6143 * @def: the default value
6144 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006145 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006146 *
6147 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006148 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006149 */
6150static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006151xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006152 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006153 const char *name, int def)
6154{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006155 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006156
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006157 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006158 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006159 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006160 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006161 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006162 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006163 * can have the following legal literals {true, false, 1, 0}.
6164 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006165 if (xmlStrEqual(val, BAD_CAST "true"))
6166 def = 1;
6167 else if (xmlStrEqual(val, BAD_CAST "false"))
6168 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006169 else if (xmlStrEqual(val, BAD_CAST "1"))
6170 def = 1;
6171 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006172 def = 0;
6173 else {
6174 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006175 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006176 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006177 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006178 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6179 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006180 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006181 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006182}
6183
6184/************************************************************************
6185 * *
6186 * Shema extraction from an Infoset *
6187 * *
6188 ************************************************************************/
6189static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6190 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006191 xmlNodePtr node,
6192 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006193static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6194 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006195 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006196 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006197 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006198static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6199 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006200 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006201 xmlNodePtr node,
6202 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006203static xmlSchemaBasicItemPtr
6204xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6205 xmlSchemaPtr schema,
6206 xmlNodePtr node,
6207 xmlSchemaItemListPtr uses,
6208 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006209static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6210 xmlSchemaPtr schema,
6211 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006212static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006213xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6214 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006215
6216/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006217 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006218 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006219 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006220 * @ownerDes: the designation of the parent element
6221 * @ownerItem: the schema object owner if existent
6222 * @attr: the schema attribute node being validated
6223 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006224 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006225 *
6226 * Validates a value against the given built-in type.
6227 * This one is intended to be used internally for validation
6228 * of schema attribute values during parsing of the schema.
6229 *
6230 * Returns 0 if the value is valid, a positive error code
6231 * number otherwise and -1 in case of an internal or API error.
6232 */
6233static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006234xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006235 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006236 xmlAttrPtr attr,
6237 const xmlChar *value,
6238 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006239{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006240
6241 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006242
6243 /*
6244 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6245 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006246 */
6247 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006248 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006249 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6250 PERROR_INT("xmlSchemaPValAttrNodeValue",
6251 "the given type is not a built-in type");
6252 return (-1);
6253 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006254 switch (type->builtInType) {
6255 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006256 case XML_SCHEMAS_QNAME:
6257 case XML_SCHEMAS_ANYURI:
6258 case XML_SCHEMAS_TOKEN:
6259 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006260 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6261 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006262 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006263 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006264 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006265 "validation using the given type is not supported while "
6266 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006267 return (-1);
6268 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006269 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006270 /*
6271 * TODO: Should we use the S4S error codes instead?
6272 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006273 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006274 PERROR_INT("xmlSchemaPValAttrNodeValue",
6275 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006276 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006277 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006278 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006279 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6280 else
6281 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6282 xmlSchemaPSimpleTypeErr(pctxt,
6283 ret, ownerItem, (xmlNodePtr) attr,
6284 type, NULL, value, NULL, NULL, NULL);
6285 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006286 return (ret);
6287}
6288
6289/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006290 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006291 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006292 * @ctxt: a schema parser context
6293 * @ownerDes: the designation of the parent element
6294 * @ownerItem: the schema object owner if existent
6295 * @attr: the schema attribute node being validated
6296 * @type: the built-in type to be validated against
6297 * @value: the resulting value if any
6298 *
6299 * Extracts and validates a value against the given built-in type.
6300 * This one is intended to be used internally for validation
6301 * of schema attribute values during parsing of the schema.
6302 *
6303 * Returns 0 if the value is valid, a positive error code
6304 * number otherwise and -1 in case of an internal or API error.
6305 */
6306static int
6307xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006308 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006309 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006310 xmlSchemaTypePtr type,
6311 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006312{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006313 const xmlChar *val;
6314
6315 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006316 return (-1);
6317
Daniel Veillardc0826a72004-08-10 14:17:33 +00006318 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6319 if (value != NULL)
6320 *value = val;
6321
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006322 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006323 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006324}
6325
6326/**
6327 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006328 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006329 * @ctxt: a schema parser context
6330 * @node: the element node of the attribute
6331 * @ownerDes: the designation of the parent element
6332 * @ownerItem: the schema object owner if existent
6333 * @ownerElem: the owner element node
6334 * @name: the name of the schema attribute node
6335 * @type: the built-in type to be validated against
6336 * @value: the resulting value if any
6337 *
6338 * Extracts and validates a value against the given built-in type.
6339 * This one is intended to be used internally for validation
6340 * of schema attribute values during parsing of the schema.
6341 *
6342 * Returns 0 if the value is valid, a positive error code
6343 * number otherwise and -1 in case of an internal or API error.
6344 */
6345static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006346xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6347 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006348 xmlNodePtr ownerElem,
6349 const char *name,
6350 xmlSchemaTypePtr type,
6351 const xmlChar **value)
6352{
6353 xmlAttrPtr attr;
6354
6355 if ((ctxt == NULL) || (type == NULL)) {
6356 if (value != NULL)
6357 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006358 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006359 }
6360 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6361 if (value != NULL)
6362 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006363 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006364 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006365 "Internal error: xmlSchemaPValAttr, the given "
6366 "type '%s' is not a built-in type.\n",
6367 type->name, NULL);
6368 return (-1);
6369 }
6370 attr = xmlSchemaGetPropNode(ownerElem, name);
6371 if (attr == NULL) {
6372 if (value != NULL)
6373 *value = NULL;
6374 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006375 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006376 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006377 type, value));
6378}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006379
6380static int
6381xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006382 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006383 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006384 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006385 const xmlChar *namespaceName)
6386{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006387 /* TODO: Pointer comparison instead? */
6388 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006389 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006390 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006391 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006392 /*
6393 * Check if the referenced namespace was <import>ed.
6394 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006395 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006396 xmlSchemaSchemaRelationPtr rel;
6397
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006398 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006399 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006400 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006401 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006402 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006403 rel = rel->next;
6404 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006405 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006406 /*
6407 * No matching <import>ed namespace found.
6408 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006409 {
6410 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6411
6412 if (namespaceName == NULL)
6413 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6414 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6415 "References from this schema to components in no "
6416 "namespace are not allowed, since not indicated by an "
6417 "import statement", NULL, NULL);
6418 else
6419 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6420 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6421 "References from this schema to components in the "
6422 "namespace '%s' are not allowed, since not indicated by an "
6423 "import statement", namespaceName, NULL);
6424 }
6425 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006426}
6427
Daniel Veillardc0826a72004-08-10 14:17:33 +00006428/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006429 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006430 * @ctxt: a schema validation context
6431 * @schema: the schema being built
6432 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006433 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006434 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006435 * Parses attribute uses and attribute declarations and
6436 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006437 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006438static int
6439xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6440 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6441 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006442{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006443 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006444
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006445 while ((IS_SCHEMA((*child), "attribute")) ||
6446 (IS_SCHEMA((*child), "attributeGroup"))) {
6447 if (IS_SCHEMA((*child), "attribute")) {
6448 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6449 *list, parentType);
6450 } else {
6451 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6452 if ((item != NULL) && (hasRefs != NULL))
6453 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006454 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006455 if (item != NULL) {
6456 if (*list == NULL) {
6457 /* TODO: Customize grow factor. */
6458 *list = xmlSchemaItemListCreate();
6459 if (*list == NULL)
6460 return(-1);
6461 }
6462 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6463 return(-1);
6464 }
6465 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006466 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006467 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006468}
6469
6470/**
6471 * xmlSchemaParseAnnotation:
6472 * @ctxt: a schema validation context
6473 * @schema: the schema being built
6474 * @node: a subtree containing XML Schema informations
6475 *
6476 * parse a XML schema Attrribute declaration
6477 * *WARNING* this interface is highly subject to change
6478 *
William M. Bracke7091952004-05-11 15:09:58 +00006479 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006480 * 1 in case of success.
6481 */
6482static xmlSchemaAnnotPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006483xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006484{
6485 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006486 xmlNodePtr child = NULL;
6487 xmlAttrPtr attr;
6488 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006489
Daniel Veillardc0826a72004-08-10 14:17:33 +00006490 /*
6491 * INFO: S4S completed.
6492 */
6493 /*
6494 * id = ID
6495 * {any attributes with non-schema namespace . . .}>
6496 * Content: (appinfo | documentation)*
6497 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006498 if ((ctxt == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00006499 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006500 if (needed)
6501 ret = xmlSchemaNewAnnot(ctxt, node);
6502 else
6503 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006504 attr = node->properties;
6505 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006506 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006507 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006508 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006509 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006510
6511 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006512 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006513 }
6514 attr = attr->next;
6515 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006516 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006517 /*
6518 * And now for the children...
6519 */
6520 child = node->children;
6521 while (child != NULL) {
6522 if (IS_SCHEMA(child, "appinfo")) {
6523 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006524 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006525 * source = anyURI
6526 * {any attributes with non-schema namespace . . .}>
6527 * Content: ({any})*
6528 */
6529 attr = child->properties;
6530 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006531 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006532 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006533 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006534 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006535
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006536 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006537 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006538 }
6539 attr = attr->next;
6540 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006541 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006542 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006543 child = child->next;
6544 } else if (IS_SCHEMA(child, "documentation")) {
6545 /* TODO: make available the content of "documentation". */
6546 /*
6547 * source = anyURI
6548 * {any attributes with non-schema namespace . . .}>
6549 * Content: ({any})*
6550 */
6551 attr = child->properties;
6552 while (attr != NULL) {
6553 if (attr->ns == NULL) {
6554 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006555 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006556 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006557 }
6558 } else {
6559 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6560 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6561 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006562
6563 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006564 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006565 }
6566 }
6567 attr = attr->next;
6568 }
6569 /*
6570 * Attribute "xml:lang".
6571 */
6572 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6573 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006574 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006575 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006576 child = child->next;
6577 } else {
6578 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006579 xmlSchemaPContentErr(ctxt,
6580 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006581 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006582 barked = 1;
6583 child = child->next;
6584 }
6585 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006586
Daniel Veillard4255d502002-04-16 15:50:10 +00006587 return (ret);
6588}
6589
6590/**
6591 * xmlSchemaParseFacet:
6592 * @ctxt: a schema validation context
6593 * @schema: the schema being built
6594 * @node: a subtree containing XML Schema informations
6595 *
6596 * parse a XML schema Facet declaration
6597 * *WARNING* this interface is highly subject to change
6598 *
6599 * Returns the new type structure or NULL in case of error
6600 */
6601static xmlSchemaFacetPtr
6602xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006603 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006604{
6605 xmlSchemaFacetPtr facet;
6606 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006607 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006608
6609 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6610 return (NULL);
6611
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006612 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006613 if (facet == NULL) {
6614 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6615 return (NULL);
6616 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006617 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006618 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006619 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006620 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6621 "Facet %s has no value\n", node->name, NULL);
6622 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006623 return (NULL);
6624 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006625 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006627 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006628 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006629 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006630 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006631 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006632 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006633 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006634 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006635 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006636 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006637 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006638 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006639 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006640 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006641 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006642 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006643 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006644 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006645 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006646 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6647 } else if (IS_SCHEMA(node, "minLength")) {
6648 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6649 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006650 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6651 "Unknown facet type %s\n", node->name, NULL);
6652 xmlSchemaFreeFacet(facet);
6653 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006654 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006655 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006656 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006657 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6658 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6659 const xmlChar *fixed;
6660
6661 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6662 if (fixed != NULL) {
6663 if (xmlStrEqual(fixed, BAD_CAST "true"))
6664 facet->fixed = 1;
6665 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006666 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006667 child = node->children;
6668
6669 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006670 facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006671 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006672 }
6673 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006674 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6675 "Facet %s has unexpected child content\n",
6676 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006677 }
6678 return (facet);
6679}
6680
6681/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006682 * xmlSchemaParseWildcardNs:
6683 * @ctxt: a schema parser context
6684 * @wildc: the wildcard, already created
6685 * @node: a subtree containing XML Schema informations
6686 *
6687 * Parses the attribute "processContents" and "namespace"
6688 * of a xsd:anyAttribute and xsd:any.
6689 * *WARNING* this interface is highly subject to change
6690 *
6691 * Returns 0 if everything goes fine, a positive error code
6692 * if something is not valid and -1 if an internal error occurs.
6693 */
6694static int
6695xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006696 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006697 xmlSchemaWildcardPtr wildc,
6698 xmlNodePtr node)
6699{
6700 const xmlChar *pc, *ns, *dictnsItem;
6701 int ret = 0;
6702 xmlChar *nsItem;
6703 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6704 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006705
Daniel Veillardc0826a72004-08-10 14:17:33 +00006706 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6707 if ((pc == NULL)
6708 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6709 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6710 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6711 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6712 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6713 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6714 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006715 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006716 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006717 NULL, node,
6718 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006719 NULL, NULL, NULL);
6720 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006721 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006722 }
6723 /*
6724 * Build the namespace constraints.
6725 */
6726 attr = xmlSchemaGetPropNode(node, "namespace");
6727 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006728 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006729 wildc->any = 1;
6730 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6731 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006732 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006733 return (-1);
6734 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006735 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006736 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006737 const xmlChar *end, *cur;
6738
6739 cur = ns;
6740 do {
6741 while (IS_BLANK_CH(*cur))
6742 cur++;
6743 end = cur;
6744 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6745 end++;
6746 if (end == cur)
6747 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006748 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006749 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6750 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006751 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006752 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006753 NULL, (xmlNodePtr) attr,
6754 NULL,
6755 "((##any | ##other) | List of (xs:anyURI | "
6756 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006757 nsItem, NULL, NULL, NULL);
6758 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6759 } else {
6760 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006761 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006762 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6763 dictnsItem = NULL;
6764 } else {
6765 /*
6766 * Validate the item (anyURI).
6767 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006768 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006769 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6770 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6771 }
6772 /*
6773 * Avoid dublicate namespaces.
6774 */
6775 tmp = wildc->nsSet;
6776 while (tmp != NULL) {
6777 if (dictnsItem == tmp->value)
6778 break;
6779 tmp = tmp->next;
6780 }
6781 if (tmp == NULL) {
6782 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6783 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006784 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006785 return (-1);
6786 }
6787 tmp->value = dictnsItem;
6788 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006789 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006790 wildc->nsSet = tmp;
6791 else
6792 lastNs->next = tmp;
6793 lastNs = tmp;
6794 }
6795
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006796 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006797 xmlFree(nsItem);
6798 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006799 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006800 }
6801 return (ret);
6802}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006803
6804static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006805xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6806 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006807 xmlNodePtr node,
6808 int minOccurs,
6809 int maxOccurs) {
6810
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006811 if ((maxOccurs == 0) && ( minOccurs == 0))
6812 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006813 if (maxOccurs != UNBOUNDED) {
6814 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006815 * TODO: Maybe we should better not create the particle,
6816 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006817 * content model.
6818 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006819 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006820 * 3.9.6 Schema Component Constraint: Particle Correct
6821 *
6822 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006823 if (maxOccurs < 1) {
6824 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006825 * 2.2 {max occurs} must be greater than or equal to 1.
6826 */
6827 xmlSchemaPCustomAttrErr(ctxt,
6828 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006829 NULL, NULL,
6830 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006831 "The value must be greater than or equal to 1");
6832 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6833 } else if (minOccurs > maxOccurs) {
6834 /*
6835 * 2.1 {min occurs} must not be greater than {max occurs}.
6836 */
6837 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006838 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006839 NULL, NULL,
6840 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006841 "The value must not be greater than the value of 'maxOccurs'");
6842 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6843 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006844 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006845 return (0);
6846}
6847
Daniel Veillardc0826a72004-08-10 14:17:33 +00006848/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006849 * xmlSchemaParseAny:
6850 * @ctxt: a schema validation context
6851 * @schema: the schema being built
6852 * @node: a subtree containing XML Schema informations
6853 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006854 * Parsea a XML schema <any> element. A particle and wildcard
6855 * will be created (except if minOccurs==maxOccurs==0, in this case
6856 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006857 * *WARNING* this interface is highly subject to change
6858 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006859 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006860 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006861static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006862xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6863 xmlNodePtr node)
6864{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006865 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006866 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006867 xmlSchemaWildcardPtr wild;
6868 int min, max;
6869 xmlAttrPtr attr;
6870 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006871
6872 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6873 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006874 /*
6875 * Check for illegal attributes.
6876 */
6877 attr = node->properties;
6878 while (attr != NULL) {
6879 if (attr->ns == NULL) {
6880 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6881 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6882 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6883 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6884 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006885 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006886 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006887 }
6888 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006889 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006890 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006891 }
6892 attr = attr->next;
6893 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006894 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006895 /*
6896 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006897 */
6898 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6899 "(xs:nonNegativeInteger | unbounded)");
6900 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6901 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006902 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6903 /*
6904 * Create & parse the wildcard.
6905 */
6906 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6907 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006908 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006909 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006910 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006911 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006912 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006913 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006914 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006915 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006916 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006917 }
6918 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006919 xmlSchemaPContentErr(ctxt,
6920 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006921 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006922 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006923 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006924 /*
6925 * No component if minOccurs==maxOccurs==0.
6926 */
6927 if ((min == 0) && (max == 0)) {
6928 /* Don't free the wildcard, since it's already on the list. */
6929 return (NULL);
6930 }
6931 /*
6932 * Create the particle.
6933 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00006934 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006935 if (particle == NULL)
6936 return (NULL);
6937 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006938 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006939
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006940 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006941}
6942
6943/**
6944 * xmlSchemaParseNotation:
6945 * @ctxt: a schema validation context
6946 * @schema: the schema being built
6947 * @node: a subtree containing XML Schema informations
6948 *
6949 * parse a XML schema Notation declaration
6950 *
6951 * Returns the new structure or NULL in case of error
6952 */
6953static xmlSchemaNotationPtr
6954xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006955 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006956{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006957 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006958 xmlSchemaNotationPtr ret;
6959 xmlNodePtr child = NULL;
6960
6961 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6962 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006963 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006964 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006965 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6966 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006967 return (NULL);
6968 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006969 ret = xmlSchemaAddNotation(ctxt, schema, name,
6970 ctxt->targetNamespace, node);
6971 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006972 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006973 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006974
Daniel Veillard4255d502002-04-16 15:50:10 +00006975 child = node->children;
6976 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006977 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006978 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006979 }
6980 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006981 xmlSchemaPContentErr(ctxt,
6982 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006983 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006984 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006985 }
6986
6987 return (ret);
6988}
6989
6990/**
6991 * xmlSchemaParseAnyAttribute:
6992 * @ctxt: a schema validation context
6993 * @schema: the schema being built
6994 * @node: a subtree containing XML Schema informations
6995 *
6996 * parse a XML schema AnyAttrribute declaration
6997 * *WARNING* this interface is highly subject to change
6998 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006999 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00007000 */
Daniel Veillard3646d642004-06-02 19:19:14 +00007001static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007002xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
7003 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007004{
Daniel Veillard3646d642004-06-02 19:19:14 +00007005 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007006 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007007 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007008
7009 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7010 return (NULL);
7011
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007012 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
7013 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00007014 if (ret == NULL) {
7015 return (NULL);
7016 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007017 /*
7018 * Check for illegal attributes.
7019 */
7020 attr = node->properties;
7021 while (attr != NULL) {
7022 if (attr->ns == NULL) {
7023 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7024 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7025 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007030 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007031 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007032 }
7033 attr = attr->next;
7034 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007035 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007036 /*
7037 * Parse the namespace list.
7038 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007039 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007040 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007041 /*
7042 * And now for the children...
7043 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007044 child = node->children;
7045 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007046 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007047 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007048 }
7049 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007050 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007051 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007052 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007053 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007054 }
7055
7056 return (ret);
7057}
7058
7059
7060/**
7061 * xmlSchemaParseAttribute:
7062 * @ctxt: a schema validation context
7063 * @schema: the schema being built
7064 * @node: a subtree containing XML Schema informations
7065 *
7066 * parse a XML schema Attrribute declaration
7067 * *WARNING* this interface is highly subject to change
7068 *
William M. Bracke7091952004-05-11 15:09:58 +00007069 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007070 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007071static xmlSchemaBasicItemPtr
7072xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7073 xmlSchemaPtr schema,
7074 xmlNodePtr node,
7075 xmlSchemaItemListPtr uses,
7076 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007077{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007078 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7079 xmlSchemaAttributeUsePtr use = NULL;
7080 xmlNodePtr child = NULL;
7081 xmlAttrPtr attr;
7082 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7083 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7084 int nberrors, hasForm = 0, defValueType = 0;
7085
7086#define WXS_ATTR_DEF_VAL_DEFAULT 1
7087#define WXS_ATTR_DEF_VAL_FIXED 2
7088
7089 /*
7090 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7091 */
7092
7093 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7094 return (NULL);
7095 attr = xmlSchemaGetPropNode(node, "ref");
7096 if (attr != NULL) {
7097 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7098 NULL, attr, &tmpNs, &tmpName) != 0) {
7099 return (NULL);
7100 }
7101 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7102 return(NULL);
7103 isRef = 1;
7104 }
7105 nberrors = pctxt->nberrors;
7106 /*
7107 * Check for illegal attributes.
7108 */
7109 attr = node->properties;
7110 while (attr != NULL) {
7111 if (attr->ns == NULL) {
7112 if (isRef) {
7113 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7114 xmlSchemaPValAttrNodeID(pctxt, attr);
7115 goto attr_next;
7116 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7117 goto attr_next;
7118 }
7119 } else {
7120 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7121 goto attr_next;
7122 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7123 xmlSchemaPValAttrNodeID(pctxt, attr);
7124 goto attr_next;
7125 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7126 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7127 attr, &tmpNs, &tmpName);
7128 goto attr_next;
7129 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7130 /*
7131 * Evaluate the target namespace
7132 */
7133 hasForm = 1;
7134 attrValue = xmlSchemaGetNodeContent(pctxt,
7135 (xmlNodePtr) attr);
7136 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7137 ns = pctxt->targetNamespace;
7138 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7139 {
7140 xmlSchemaPSimpleTypeErr(pctxt,
7141 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7142 NULL, (xmlNodePtr) attr,
7143 NULL, "(qualified | unqualified)",
7144 attrValue, NULL, NULL, NULL);
7145 }
7146 goto attr_next;
7147 }
7148 }
7149 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7150
7151 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7152 /* TODO: Maybe we need to normalize the value beforehand. */
7153 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7154 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7155 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7156 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7157 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7158 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7159 else {
7160 xmlSchemaPSimpleTypeErr(pctxt,
7161 XML_SCHEMAP_INVALID_ATTR_USE,
7162 NULL, (xmlNodePtr) attr,
7163 NULL, "(optional | prohibited | required)",
7164 attrValue, NULL, NULL, NULL);
7165 }
7166 goto attr_next;
7167 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7168 /*
7169 * 3.2.3 : 1
7170 * default and fixed must not both be present.
7171 */
7172 if (defValue) {
7173 xmlSchemaPMutualExclAttrErr(pctxt,
7174 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7175 NULL, attr, "default", "fixed");
7176 } else {
7177 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7178 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7179 }
7180 goto attr_next;
7181 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7182 /*
7183 * 3.2.3 : 1
7184 * default and fixed must not both be present.
7185 */
7186 if (defValue) {
7187 xmlSchemaPMutualExclAttrErr(pctxt,
7188 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7189 NULL, attr, "default", "fixed");
7190 } else {
7191 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7192 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7193 }
7194 goto attr_next;
7195 }
7196 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7197 goto attr_next;
7198
7199 xmlSchemaPIllegalAttrErr(pctxt,
7200 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7201
7202attr_next:
7203 attr = attr->next;
7204 }
7205 /*
7206 * 3.2.3 : 2
7207 * If default and use are both present, use must have
7208 * the actual value optional.
7209 */
7210 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7211 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7212 xmlSchemaPSimpleTypeErr(pctxt,
7213 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7214 NULL, node, NULL,
7215 "(optional | prohibited | required)", NULL,
7216 "The value of the attribute 'use' must be 'optional' "
7217 "if the attribute 'default' is present",
7218 NULL, NULL);
7219 }
7220 /*
7221 * We want correct attributes.
7222 */
7223 if (nberrors != pctxt->nberrors)
7224 return(NULL);
7225 if (! isRef) {
7226 xmlSchemaAttributePtr attrDecl;
7227
7228 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7229 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7230 ns = pctxt->targetNamespace;
7231 /*
7232 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7233 * TODO: Move this to the component layer.
7234 */
7235 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7236 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7237 XML_SCHEMAP_NO_XSI,
7238 node, NULL,
7239 "The target namespace must not match '%s'",
7240 xmlSchemaInstanceNs, NULL);
7241 }
7242 attr = xmlSchemaGetPropNode(node, "name");
7243 if (attr == NULL) {
7244 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7245 NULL, node, "name", NULL);
7246 return (NULL);
7247 }
7248 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7249 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7250 return (NULL);
7251 }
7252 /*
7253 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7254 * TODO: Move this to the component layer.
7255 */
7256 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7257 xmlSchemaPSimpleTypeErr(pctxt,
7258 XML_SCHEMAP_NO_XMLNS,
7259 NULL, (xmlNodePtr) attr,
7260 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7261 "The value of the attribute must not match 'xmlns'",
7262 NULL, NULL);
7263 return (NULL);
7264 }
7265 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7266 goto check_children;
7267 /*
7268 * Create the attribute use component.
7269 */
7270 use = xmlSchemaAddAttributeUse(pctxt, node);
7271 if (use == NULL)
7272 return(NULL);
7273 use->occurs = occurs;
7274 /*
7275 * Create the attribute declaration.
7276 */
7277 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7278 if (attrDecl == NULL)
7279 return (NULL);
7280 if (tmpName != NULL) {
7281 attrDecl->typeName = tmpName;
7282 attrDecl->typeNs = tmpNs;
7283 }
7284 use->attrDecl = attrDecl;
7285 /*
7286 * Value constraint.
7287 */
7288 if (defValue != NULL) {
7289 attrDecl->defValue = defValue;
7290 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7291 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7292 }
7293 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7294 xmlSchemaQNameRefPtr ref;
7295
7296 /*
7297 * Create the attribute use component.
7298 */
7299 use = xmlSchemaAddAttributeUse(pctxt, node);
7300 if (use == NULL)
7301 return(NULL);
7302 /*
7303 * We need to resolve the reference at later stage.
7304 */
7305 WXS_ADD_PENDING(pctxt, use);
7306 use->occurs = occurs;
7307 /*
7308 * Create a QName reference to the attribute declaration.
7309 */
7310 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7311 tmpName, tmpNs);
7312 if (ref == NULL)
7313 return(NULL);
7314 /*
7315 * Assign the reference. This will be substituted for the
7316 * referenced attribute declaration when the QName is resolved.
7317 */
7318 use->attrDecl = WXS_ATTR_CAST ref;
7319 /*
7320 * Value constraint.
7321 */
7322 if (defValue != NULL)
7323 use->defValue = defValue;
7324 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7325 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7326 }
7327
7328check_children:
7329 /*
7330 * And now for the children...
7331 */
7332 child = node->children;
7333 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7334 xmlSchemaAttributeUseProhibPtr prohib;
7335
7336 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007337 xmlSchemaParseAnnotation(pctxt, child, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007338 child = child->next;
7339 }
7340 if (child != NULL) {
7341 xmlSchemaPContentErr(pctxt,
7342 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7343 NULL, node, child, NULL,
7344 "(annotation?)");
7345 }
7346 /*
7347 * Check for pointlessness of attribute prohibitions.
7348 */
7349 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7350 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7351 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7352 node, NULL,
7353 "Skipping attribute use prohibition, since it is "
7354 "pointless inside an <attributeGroup>",
7355 NULL, NULL, NULL);
7356 return(NULL);
7357 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7358 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7359 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7360 node, NULL,
7361 "Skipping attribute use prohibition, since it is "
7362 "pointless when extending a type",
7363 NULL, NULL, NULL);
7364 return(NULL);
7365 }
7366 if (! isRef) {
7367 tmpName = name;
7368 tmpNs = ns;
7369 }
7370 /*
7371 * Check for duplicate attribute prohibitions.
7372 */
7373 if (uses) {
7374 int i;
7375
7376 for (i = 0; i < uses->nbItems; i++) {
7377 use = uses->items[i];
7378 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7379 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7380 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7381 {
7382 xmlChar *str = NULL;
7383
7384 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7385 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7386 node, NULL,
7387 "Skipping duplicate attribute use prohibition '%s'",
7388 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7389 NULL, NULL);
7390 FREE_AND_NULL(str)
7391 return(NULL);
7392 }
7393 }
7394 }
7395 /*
7396 * Create the attribute prohibition helper component.
7397 */
7398 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7399 if (prohib == NULL)
7400 return(NULL);
7401 prohib->node = node;
7402 prohib->name = tmpName;
7403 prohib->targetNamespace = tmpNs;
7404 if (isRef) {
7405 /*
7406 * We need at least to resolve to the attribute declaration.
7407 */
7408 WXS_ADD_PENDING(pctxt, prohib);
7409 }
7410 return(WXS_BASIC_CAST prohib);
7411 } else {
7412 if (IS_SCHEMA(child, "annotation")) {
7413 /*
7414 * TODO: Should this go into the attr decl?
7415 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007416 use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007417 child = child->next;
7418 }
7419 if (isRef) {
7420 if (child != NULL) {
7421 if (IS_SCHEMA(child, "simpleType"))
7422 /*
7423 * 3.2.3 : 3.2
7424 * If ref is present, then all of <simpleType>,
7425 * form and type must be absent.
7426 */
7427 xmlSchemaPContentErr(pctxt,
7428 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7429 NULL, node, child, NULL,
7430 "(annotation?)");
7431 else
7432 xmlSchemaPContentErr(pctxt,
7433 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7434 NULL, node, child, NULL,
7435 "(annotation?)");
7436 }
7437 } else {
7438 if (IS_SCHEMA(child, "simpleType")) {
7439 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7440 /*
7441 * 3.2.3 : 4
7442 * type and <simpleType> must not both be present.
7443 */
7444 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7445 NULL, node, child,
7446 "The attribute 'type' and the <simpleType> child "
7447 "are mutually exclusive", NULL);
7448 } else
7449 WXS_ATTRUSE_TYPEDEF(use) =
7450 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7451 child = child->next;
7452 }
7453 if (child != NULL)
7454 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7455 NULL, node, child, NULL,
7456 "(annotation?, simpleType?)");
7457 }
7458 }
7459 return (WXS_BASIC_CAST use);
7460}
7461
7462
7463static xmlSchemaAttributePtr
7464xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7465 xmlSchemaPtr schema,
7466 xmlNodePtr node)
7467{
7468 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007469 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007470 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007471 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007472
7473 /*
7474 * Note that the w3c spec assumes the schema to be validated with schema
7475 * for schemas beforehand.
7476 *
7477 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007478 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007479 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7480 return (NULL);
7481 /*
7482 * 3.2.3 : 3.1
7483 * One of ref or name must be present, but not both
7484 */
7485 attr = xmlSchemaGetPropNode(node, "name");
7486 if (attr == NULL) {
7487 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7488 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007489 return (NULL);
7490 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007491 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7492 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7493 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007494 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007495 /*
7496 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7497 * TODO: Move this to the component layer.
7498 */
7499 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7500 xmlSchemaPSimpleTypeErr(pctxt,
7501 XML_SCHEMAP_NO_XMLNS,
7502 NULL, (xmlNodePtr) attr,
7503 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7504 "The value of the attribute must not match 'xmlns'",
7505 NULL, NULL);
7506 return (NULL);
7507 }
7508 /*
7509 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7510 * TODO: Move this to the component layer.
7511 * Or better leave it here and add it to the component layer
7512 * if we have a schema construction API.
7513 */
7514 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7515 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7516 XML_SCHEMAP_NO_XSI, node, NULL,
7517 "The target namespace must not match '%s'",
7518 xmlSchemaInstanceNs, NULL);
7519 }
7520
7521 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7522 pctxt->targetNamespace, node, 1);
7523 if (ret == NULL)
7524 return (NULL);
7525 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7526
7527 /*
7528 * Check for illegal attributes.
7529 */
7530 attr = node->properties;
7531 while (attr != NULL) {
7532 if (attr->ns == NULL) {
7533 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7534 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7535 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7536 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7537 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7538 {
7539 xmlSchemaPIllegalAttrErr(pctxt,
7540 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7541 }
7542 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7543 xmlSchemaPIllegalAttrErr(pctxt,
7544 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7545 }
7546 attr = attr->next;
7547 }
7548 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7549 node, "type", &ret->typeNs, &ret->typeName);
7550
7551 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007552 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007553 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007554 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007555 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007556 if (ret->defValue != NULL)
7557 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007558 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007559 * Attribute "default".
7560 */
7561 attr = xmlSchemaGetPropNode(node, "default");
7562 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007563 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007564 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007565 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007566 */
7567 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007568 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7569 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007570 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007571 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007572 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007573 /*
7574 * And now for the children...
7575 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007576 child = node->children;
7577 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007578 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007579 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007580 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007581 if (IS_SCHEMA(child, "simpleType")) {
7582 if (ret->typeName != NULL) {
7583 /*
7584 * 3.2.3 : 4
7585 * type and <simpleType> must not both be present.
7586 */
7587 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7588 NULL, node, child,
7589 "The attribute 'type' and the <simpleType> child "
7590 "are mutually exclusive", NULL);
7591 } else
7592 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7593 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007594 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007595 if (child != NULL)
7596 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7597 NULL, node, child, NULL,
7598 "(annotation?, simpleType?)");
7599
Daniel Veillard4255d502002-04-16 15:50:10 +00007600 return (ret);
7601}
7602
7603/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007604 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007605 * @ctxt: a schema validation context
7606 * @schema: the schema being built
7607 * @node: a subtree containing XML Schema informations
7608 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007609 * Parse an attribute group definition reference.
7610 * Note that a reference to an attribute group does not
7611 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007612 * *WARNING* this interface is highly subject to change
7613 *
7614 * Returns the attribute group or NULL in case of error.
7615 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007616static xmlSchemaQNameRefPtr
7617xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7618 xmlSchemaPtr schema,
7619 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007620{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007621 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007622 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007623 xmlAttrPtr attr;
7624 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007625
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007626 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007627 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007628
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007629 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007630 if (attr == NULL) {
7631 xmlSchemaPMissingAttrErr(pctxt,
7632 XML_SCHEMAP_S4S_ATTR_MISSING,
7633 NULL, node, "ref", NULL);
7634 return (NULL);
7635 }
7636 xmlSchemaPValAttrNodeQName(pctxt, schema,
7637 NULL, attr, &refNs, &ref);
7638 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7639 return(NULL);
7640
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007641 /*
7642 * Check for illegal attributes.
7643 */
7644 attr = node->properties;
7645 while (attr != NULL) {
7646 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007647 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007648 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007649 {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007654 xmlSchemaPIllegalAttrErr(pctxt,
7655 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007656 }
7657 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007658 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007659 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007660 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7661
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007662 /*
7663 * And now for the children...
7664 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007665 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007666 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007667 /*
7668 * TODO: We do not have a place to store the annotation, do we?
7669 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007670 xmlSchemaParseAnnotation(pctxt, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007671 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007672 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007673 if (child != NULL) {
7674 xmlSchemaPContentErr(pctxt,
7675 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7676 NULL, node, child, NULL,
7677 "(annotation?)");
7678 }
7679
7680 /*
7681 * Handle attribute group redefinitions.
7682 */
7683 if (pctxt->isRedefine && pctxt->redef &&
7684 (pctxt->redef->item->type ==
7685 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7686 (ref == pctxt->redef->refName) &&
7687 (refNs == pctxt->redef->refTargetNs))
7688 {
7689 /*
7690 * SPEC src-redefine:
7691 * (7.1) "If it has an <attributeGroup> among its contents
7692 * the ·actual value· of whose ref [attribute] is the same
7693 * as the ·actual value· of its own name attribute plus
7694 * target namespace, then it must have exactly one such group."
7695 */
7696 if (pctxt->redefCounter != 0) {
7697 xmlChar *str = NULL;
7698
7699 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7700 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7701 "The redefining attribute group definition "
7702 "'%s' must not contain more than one "
7703 "reference to the redefined definition",
7704 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7705 FREE_AND_NULL(str);
7706 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007707 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007708 pctxt->redefCounter++;
7709 /*
7710 * URGENT TODO: How to ensure that the reference will not be
7711 * handled by the normal component resolution mechanism?
7712 */
7713 ret = xmlSchemaNewQNameRef(pctxt,
7714 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7715 if (ret == NULL)
7716 return(NULL);
7717 ret->node = node;
7718 pctxt->redef->reference = WXS_BASIC_CAST ret;
7719 } else {
7720 /*
7721 * Create a QName-reference helper component. We will substitute this
7722 * component for the attribute uses of the referenced attribute group
7723 * definition.
7724 */
7725 ret = xmlSchemaNewQNameRef(pctxt,
7726 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7727 if (ret == NULL)
7728 return(NULL);
7729 ret->node = node;
7730 /* Add to pending items, to be able to resolve the reference. */
7731 WXS_ADD_PENDING(pctxt, ret);
7732 }
7733 return (ret);
7734}
7735
7736/**
7737 * xmlSchemaParseAttributeGroupDefinition:
7738 * @pctxt: a schema validation context
7739 * @schema: the schema being built
7740 * @node: a subtree containing XML Schema informations
7741 *
7742 * parse a XML schema Attribute Group declaration
7743 * *WARNING* this interface is highly subject to change
7744 *
7745 * Returns the attribute group definition or NULL in case of error.
7746 */
7747static xmlSchemaAttributeGroupPtr
7748xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7749 xmlSchemaPtr schema,
7750 xmlNodePtr node)
7751{
7752 const xmlChar *name;
7753 xmlSchemaAttributeGroupPtr ret;
7754 xmlNodePtr child = NULL;
7755 xmlAttrPtr attr;
7756 int hasRefs = 0;
7757
7758 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7759 return (NULL);
7760
7761 attr = xmlSchemaGetPropNode(node, "name");
7762 if (attr == NULL) {
7763 xmlSchemaPMissingAttrErr(pctxt,
7764 XML_SCHEMAP_S4S_ATTR_MISSING,
7765 NULL, node, "name", NULL);
7766 return (NULL);
7767 }
7768 /*
7769 * The name is crucial, exit if invalid.
7770 */
7771 if (xmlSchemaPValAttrNode(pctxt,
7772 NULL, attr,
7773 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7774 return (NULL);
7775 }
7776 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7777 name, pctxt->targetNamespace, node);
7778 if (ret == NULL)
7779 return (NULL);
7780 /*
7781 * Check for illegal attributes.
7782 */
7783 attr = node->properties;
7784 while (attr != NULL) {
7785 if (attr->ns == NULL) {
7786 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7787 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7788 {
7789 xmlSchemaPIllegalAttrErr(pctxt,
7790 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7791 }
7792 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7793 xmlSchemaPIllegalAttrErr(pctxt,
7794 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7795 }
7796 attr = attr->next;
7797 }
7798 /* Attribute ID */
7799 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7800 /*
7801 * And now for the children...
7802 */
7803 child = node->children;
7804 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007805 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007806 child = child->next;
7807 }
7808 /*
7809 * Parse contained attribute decls/refs.
7810 */
7811 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7812 (xmlSchemaItemListPtr *) &(ret->attrUses),
7813 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7814 return(NULL);
7815 if (hasRefs)
7816 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7817 /*
7818 * Parse the attribute wildcard.
7819 */
7820 if (IS_SCHEMA(child, "anyAttribute")) {
7821 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7822 schema, child);
7823 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007824 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007825 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007826 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007827 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007828 NULL, node, child, NULL,
7829 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007830 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007831 return (ret);
7832}
7833
7834/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007835 * xmlSchemaPValAttrFormDefault:
7836 * @value: the value
7837 * @flags: the flags to be modified
7838 * @flagQualified: the specific flag for "qualified"
7839 *
7840 * Returns 0 if the value is valid, 1 otherwise.
7841 */
7842static int
7843xmlSchemaPValAttrFormDefault(const xmlChar *value,
7844 int *flags,
7845 int flagQualified)
7846{
7847 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7848 if ((*flags & flagQualified) == 0)
7849 *flags |= flagQualified;
7850 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007851 return (1);
7852
William M. Brack2f2a6632004-08-20 23:09:47 +00007853 return (0);
7854}
7855
7856/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007857 * xmlSchemaPValAttrBlockFinal:
7858 * @value: the value
7859 * @flags: the flags to be modified
7860 * @flagAll: the specific flag for "#all"
7861 * @flagExtension: the specific flag for "extension"
7862 * @flagRestriction: the specific flag for "restriction"
7863 * @flagSubstitution: the specific flag for "substitution"
7864 * @flagList: the specific flag for "list"
7865 * @flagUnion: the specific flag for "union"
7866 *
7867 * Validates the value of the attribute "final" and "block". The value
7868 * is converted into the specified flag values and returned in @flags.
7869 *
7870 * Returns 0 if the value is valid, 1 otherwise.
7871 */
7872
7873static int
7874xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007875 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007876 int flagAll,
7877 int flagExtension,
7878 int flagRestriction,
7879 int flagSubstitution,
7880 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007881 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007882{
7883 int ret = 0;
7884
7885 /*
7886 * TODO: This does not check for dublicate entries.
7887 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007888 if ((flags == NULL) || (value == NULL))
7889 return (-1);
7890 if (value[0] == 0)
7891 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007892 if (xmlStrEqual(value, BAD_CAST "#all")) {
7893 if (flagAll != -1)
7894 *flags |= flagAll;
7895 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007896 if (flagExtension != -1)
7897 *flags |= flagExtension;
7898 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007899 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007900 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007902 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007903 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007904 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007905 *flags |= flagUnion;
7906 }
7907 } else {
7908 const xmlChar *end, *cur = value;
7909 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007910
Daniel Veillardc0826a72004-08-10 14:17:33 +00007911 do {
7912 while (IS_BLANK_CH(*cur))
7913 cur++;
7914 end = cur;
7915 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7916 end++;
7917 if (end == cur)
7918 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007919 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007920 if (xmlStrEqual(item, BAD_CAST "extension")) {
7921 if (flagExtension != -1) {
7922 if ((*flags & flagExtension) == 0)
7923 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007924 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007925 ret = 1;
7926 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7927 if (flagRestriction != -1) {
7928 if ((*flags & flagRestriction) == 0)
7929 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007930 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007931 ret = 1;
7932 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7933 if (flagSubstitution != -1) {
7934 if ((*flags & flagSubstitution) == 0)
7935 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007936 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007937 ret = 1;
7938 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7939 if (flagList != -1) {
7940 if ((*flags & flagList) == 0)
7941 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007942 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007943 ret = 1;
7944 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7945 if (flagUnion != -1) {
7946 if ((*flags & flagUnion) == 0)
7947 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007948 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007949 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007950 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007951 ret = 1;
7952 if (item != NULL)
7953 xmlFree(item);
7954 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007955 } while ((ret == 0) && (*cur != 0));
7956 }
7957
Daniel Veillardc0826a72004-08-10 14:17:33 +00007958 return (ret);
7959}
7960
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007961static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007962xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007963 xmlSchemaIDCPtr idc,
7964 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007965 xmlAttrPtr attr,
7966 int isField)
7967{
7968 xmlNodePtr node;
7969
7970 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007971 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007972 * Schema Component Constraint: Selector Value OK
7973 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007974 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007975 * in [XPath].
7976 */
7977 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007978 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007979 XML_SCHEMAP_INTERNAL,
7980 "Internal error: xmlSchemaCheckCSelectorXPath, "
7981 "the selector is not specified.\n", NULL, NULL);
7982 return (-1);
7983 }
7984 if (attr == NULL)
7985 node = idc->node;
7986 else
7987 node = (xmlNodePtr) attr;
7988 if (selector->xpath == NULL) {
7989 xmlSchemaPCustomErr(ctxt,
7990 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007991 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007992 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007993 "The XPath expression of the selector is not valid", NULL);
7994 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7995 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007996 const xmlChar **nsArray = NULL;
7997 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007998 /*
7999 * Compile the XPath expression.
8000 */
8001 /*
8002 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008003 * TODO: Call xmlPatterncompile with different options for selector/
8004 * field.
8005 */
Daniel Veillard14b56432006-03-09 18:41:40 +00008006 if (attr == NULL)
Daniel Veillardaac7c682006-03-10 13:40:16 +00008007 nsList = NULL;
Daniel Veillard14b56432006-03-09 18:41:40 +00008008 else
8009 nsList = xmlGetNsList(attr->doc, attr->parent);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008010 /*
8011 * Build an array of prefixes and namespaces.
8012 */
8013 if (nsList != NULL) {
8014 int i, count = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008015
8016 for (i = 0; nsList[i] != NULL; i++)
8017 count++;
8018
8019 nsArray = (const xmlChar **) xmlMalloc(
8020 (count * 2 + 1) * sizeof(const xmlChar *));
8021 if (nsArray == NULL) {
8022 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8023 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008024 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008025 return (-1);
8026 }
8027 for (i = 0; i < count; i++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008028 nsArray[2 * i] = nsList[i]->href;
8029 nsArray[2 * i + 1] = nsList[i]->prefix;
8030 }
8031 nsArray[count * 2] = NULL;
8032 xmlFree(nsList);
8033 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008034 /*
8035 * TODO: Differentiate between "selector" and "field".
8036 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008037 if (isField)
8038 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008039 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008040 else
8041 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008042 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008043 if (nsArray != NULL)
8044 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008045
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008046 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008047 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008048 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008049 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008050 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008051 "The XPath expression '%s' could not be "
8052 "compiled", selector->xpath);
8053 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008054 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008055 }
8056 return (0);
8057}
8058
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008059#define ADD_ANNOTATION(annot) \
8060 xmlSchemaAnnotPtr cur = item->annot; \
8061 if (item->annot == NULL) { \
8062 item->annot = annot; \
8063 return (annot); \
8064 } \
8065 cur = item->annot; \
8066 if (cur->next != NULL) { \
8067 cur = cur->next; \
8068 } \
8069 cur->next = annot;
8070
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008071/**
8072 * xmlSchemaAssignAnnotation:
8073 * @item: the schema component
8074 * @annot: the annotation
8075 *
8076 * Adds the annotation to the given schema component.
8077 *
8078 * Returns the given annotaion.
8079 */
8080static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008081xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8082 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008083{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008084 if ((annItem == NULL) || (annot == NULL))
8085 return (NULL);
8086 switch (annItem->type) {
8087 case XML_SCHEMA_TYPE_ELEMENT: {
8088 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8089 ADD_ANNOTATION(annot)
8090 }
8091 break;
8092 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8093 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8094 ADD_ANNOTATION(annot)
8095 }
8096 break;
8097 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8098 case XML_SCHEMA_TYPE_ANY: {
8099 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8100 ADD_ANNOTATION(annot)
8101 }
8102 break;
8103 case XML_SCHEMA_TYPE_PARTICLE:
8104 case XML_SCHEMA_TYPE_IDC_KEY:
8105 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008106 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008107 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8108 ADD_ANNOTATION(annot)
8109 }
8110 break;
8111 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8112 xmlSchemaAttributeGroupPtr item =
8113 (xmlSchemaAttributeGroupPtr) annItem;
8114 ADD_ANNOTATION(annot)
8115 }
8116 break;
8117 case XML_SCHEMA_TYPE_NOTATION: {
8118 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8119 ADD_ANNOTATION(annot)
8120 }
8121 break;
8122 case XML_SCHEMA_FACET_MININCLUSIVE:
8123 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8124 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8125 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8126 case XML_SCHEMA_FACET_TOTALDIGITS:
8127 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8128 case XML_SCHEMA_FACET_PATTERN:
8129 case XML_SCHEMA_FACET_ENUMERATION:
8130 case XML_SCHEMA_FACET_WHITESPACE:
8131 case XML_SCHEMA_FACET_LENGTH:
8132 case XML_SCHEMA_FACET_MAXLENGTH:
8133 case XML_SCHEMA_FACET_MINLENGTH: {
8134 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8135 ADD_ANNOTATION(annot)
8136 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008137 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008138 case XML_SCHEMA_TYPE_SIMPLE:
8139 case XML_SCHEMA_TYPE_COMPLEX: {
8140 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8141 ADD_ANNOTATION(annot)
8142 }
8143 break;
8144 case XML_SCHEMA_TYPE_GROUP: {
8145 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8146 ADD_ANNOTATION(annot)
8147 }
8148 break;
8149 case XML_SCHEMA_TYPE_SEQUENCE:
8150 case XML_SCHEMA_TYPE_CHOICE:
8151 case XML_SCHEMA_TYPE_ALL: {
8152 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8153 ADD_ANNOTATION(annot)
8154 }
8155 break;
8156 default:
8157 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008158 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008159 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008160 "Internal error: xmlSchemaAddAnnotation, "
8161 "The item is not a annotated schema component", NULL);
8162 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008163 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008164 return (annot);
8165}
8166
8167/**
8168 * xmlSchemaParseIDCSelectorAndField:
8169 * @ctxt: a schema validation context
8170 * @schema: the schema being built
8171 * @node: a subtree containing XML Schema informations
8172 *
8173 * Parses a XML Schema identity-contraint definition's
8174 * <selector> and <field> elements.
8175 *
8176 * Returns the parsed identity-constraint definition.
8177 */
8178static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008179xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008180 xmlSchemaIDCPtr idc,
8181 xmlNodePtr node,
8182 int isField)
8183{
8184 xmlSchemaIDCSelectPtr item;
8185 xmlNodePtr child = NULL;
8186 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008187
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008188 /*
8189 * Check for illegal attributes.
8190 */
8191 attr = node->properties;
8192 while (attr != NULL) {
8193 if (attr->ns == NULL) {
8194 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8195 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008200 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008201 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008202 }
8203 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008204 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008205 /*
8206 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008207 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008208 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8209 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008210 xmlSchemaPErrMemory(ctxt,
8211 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008212 NULL);
8213 return (NULL);
8214 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008215 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008216 /*
8217 * Attribute "xpath" (mandatory).
8218 */
8219 attr = xmlSchemaGetPropNode(node, "xpath");
8220 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008221 xmlSchemaPMissingAttrErr(ctxt,
8222 XML_SCHEMAP_S4S_ATTR_MISSING,
8223 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008224 "name", NULL);
8225 } else {
8226 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8227 /*
8228 * URGENT TODO: "field"s have an other syntax than "selector"s.
8229 */
8230
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008231 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8232 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008233 xmlSchemaPErr(ctxt,
8234 (xmlNodePtr) attr,
8235 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008236 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008237 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008238 NULL, NULL);
8239 }
8240
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008241 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008242 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008243 /*
8244 * And now for the children...
8245 */
8246 child = node->children;
8247 if (IS_SCHEMA(child, "annotation")) {
8248 /*
8249 * Add the annotation to the parent IDC.
8250 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008251 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008252 xmlSchemaParseAnnotation(ctxt, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008253 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008254 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008255 if (child != NULL) {
8256 xmlSchemaPContentErr(ctxt,
8257 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008258 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008259 NULL, "(annotation?)");
8260 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008261
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008262 return (item);
8263}
8264
8265/**
8266 * xmlSchemaParseIDC:
8267 * @ctxt: a schema validation context
8268 * @schema: the schema being built
8269 * @node: a subtree containing XML Schema informations
8270 *
8271 * Parses a XML Schema identity-contraint definition.
8272 *
8273 * Returns the parsed identity-constraint definition.
8274 */
8275static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008276xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008277 xmlSchemaPtr schema,
8278 xmlNodePtr node,
8279 xmlSchemaTypeType idcCategory,
8280 const xmlChar *targetNamespace)
8281{
8282 xmlSchemaIDCPtr item = NULL;
8283 xmlNodePtr child = NULL;
8284 xmlAttrPtr attr;
8285 const xmlChar *name = NULL;
8286 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008287
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008288 /*
8289 * Check for illegal attributes.
8290 */
8291 attr = node->properties;
8292 while (attr != NULL) {
8293 if (attr->ns == NULL) {
8294 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8295 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8296 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8297 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008302 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008303 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008304 }
8305 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008306 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008307 /*
8308 * Attribute "name" (mandatory).
8309 */
8310 attr = xmlSchemaGetPropNode(node, "name");
8311 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008312 xmlSchemaPMissingAttrErr(ctxt,
8313 XML_SCHEMAP_S4S_ATTR_MISSING,
8314 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008315 "name", NULL);
8316 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008317 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008318 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008319 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8320 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008321 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008322 /* Create the component. */
8323 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8324 idcCategory, node);
8325 if (item == NULL)
8326 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008327
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008328 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008329 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8330 /*
8331 * Attribute "refer" (mandatory).
8332 */
8333 attr = xmlSchemaGetPropNode(node, "refer");
8334 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008335 xmlSchemaPMissingAttrErr(ctxt,
8336 XML_SCHEMAP_S4S_ATTR_MISSING,
8337 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008338 "refer", NULL);
8339 } else {
8340 /*
8341 * Create a reference item.
8342 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008343 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008344 NULL, NULL);
8345 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008346 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008347 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008348 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008349 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008350 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008351 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008352 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008353 }
8354 }
8355 /*
8356 * And now for the children...
8357 */
8358 child = node->children;
8359 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008360 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008361 child = child->next;
8362 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008363 if (child == NULL) {
8364 xmlSchemaPContentErr(ctxt,
8365 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008366 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008367 "A child element is missing",
8368 "(annotation?, (selector, field+))");
8369 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370 /*
8371 * Child element <selector>.
8372 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008373 if (IS_SCHEMA(child, "selector")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008374 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008375 item, child, 0);
8376 child = child->next;
8377 /*
8378 * Child elements <field>.
8379 */
8380 if (IS_SCHEMA(child, "field")) {
8381 do {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008382 field = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008383 item, child, 1);
8384 if (field != NULL) {
8385 field->index = item->nbFields;
8386 item->nbFields++;
8387 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008388 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008389 else
8390 item->fields = field;
8391 lastField = field;
8392 }
8393 child = child->next;
8394 } while (IS_SCHEMA(child, "field"));
8395 } else {
8396 xmlSchemaPContentErr(ctxt,
8397 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008398 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008399 NULL, "(annotation?, (selector, field+))");
8400 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008401 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008402 if (child != NULL) {
8403 xmlSchemaPContentErr(ctxt,
8404 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008405 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008406 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008407 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008408
8409 return (item);
8410}
8411
Daniel Veillardc0826a72004-08-10 14:17:33 +00008412/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008413 * xmlSchemaParseElement:
8414 * @ctxt: a schema validation context
8415 * @schema: the schema being built
8416 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008417 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008418 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008419 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008420 * *WARNING* this interface is highly subject to change
8421 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008422 * Returns the element declaration or a particle; NULL in case
8423 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008424 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008425static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008426xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008427 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008428{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008429 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008430 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008431 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008432 xmlNodePtr child = NULL;
8433 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008434 int min, max, isRef = 0;
8435 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008436
8437 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8438 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008439
Daniel Veillard4255d502002-04-16 15:50:10 +00008440 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008441 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008442
8443 if (isElemRef != NULL)
8444 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008445 /*
8446 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008447 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008448 * robust.
8449 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008450 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008451 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008452 if ((topLevel) || (attr == NULL)) {
8453 if (nameAttr == NULL) {
8454 xmlSchemaPMissingAttrErr(ctxt,
8455 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008456 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008457 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008458 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008459 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008460 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008461
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008462 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008463 child = node->children;
8464 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008465 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008466 child = child->next;
8467 }
8468 /*
8469 * Skip particle part if a global declaration.
8470 */
8471 if (topLevel)
8472 goto declaration_part;
8473 /*
8474 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008475 */
8476 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8477 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8478 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00008479 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008480 if (particle == NULL)
8481 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008482
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008483 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8484
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008485 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008486 const xmlChar *refNs = NULL, *ref = NULL;
8487 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008488 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008489 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008490 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008491 if (isElemRef != NULL)
8492 *isElemRef = 1;
8493
Daniel Veillardc0826a72004-08-10 14:17:33 +00008494 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008495 NULL, attr, &refNs, &ref);
8496 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008497 /*
8498 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008499 */
8500 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008501 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008502 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008504 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008505 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008506 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008507 attr = node->properties;
8508 while (attr != NULL) {
8509 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008510 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8511 xmlStrEqual(attr->name, BAD_CAST "name") ||
8512 xmlStrEqual(attr->name, BAD_CAST "id") ||
8513 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8514 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8515 {
8516 attr = attr->next;
8517 continue;
8518 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008519 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008520 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008521 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008522 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008523 "Only the attributes 'minOccurs', 'maxOccurs' and "
8524 "'id' are allowed in addition to 'ref'");
8525 break;
8526 }
8527 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8528 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008529 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008530 }
8531 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008532 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008533 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008534 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008535 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008536 if (child != NULL) {
8537 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008538 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008539 }
8540 if ((min == 0) && (max == 0))
8541 goto return_null;
8542 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008543 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008544 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008545 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008546 ref, refNs);
8547 if (refer == NULL)
8548 goto return_null;
8549 particle->children = (xmlSchemaTreeItemPtr) refer;
8550 particle->annot = annot;
8551 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008552 * Add the particle to pending components, since the reference
8553 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008554 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008555 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008556 return ((xmlSchemaBasicItemPtr) particle);
8557 }
8558 /*
8559 * The declaration part ===============================================
8560 */
8561declaration_part:
8562 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008563 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008564 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8565
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008566 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008567 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008568 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008569 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008570 * Evaluate the target namespace.
8571 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008572 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008573 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008574 } else {
8575 attr = xmlSchemaGetPropNode(node, "form");
8576 if (attr != NULL) {
8577 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8578 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008579 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008580 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008581 xmlSchemaPSimpleTypeErr(ctxt,
8582 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8583 NULL, (xmlNodePtr) attr,
8584 NULL, "(qualified | unqualified)",
8585 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008586 }
8587 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008588 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008589 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008590 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008591 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008592 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008593 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008594 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008595 * Check for illegal attributes.
8596 */
William M. Bracke7091952004-05-11 15:09:58 +00008597 attr = node->properties;
8598 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008599 if (attr->ns == NULL) {
8600 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8601 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008602 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008603 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008604 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008605 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008606 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8607 {
8608 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008609 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008610 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008611 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008612 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008613 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008614 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008615 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008616 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8617 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008618 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8619
8620 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008621 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008622 }
8623 }
8624 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008625
Daniel Veillardc0826a72004-08-10 14:17:33 +00008626 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008627 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008628 }
8629 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008630 }
William M. Bracke7091952004-05-11 15:09:58 +00008631 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008632 * Extract/validate attributes.
8633 */
8634 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008635 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008636 * Process top attributes of global element declarations here.
8637 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008638 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8639 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008640 xmlSchemaPValAttrQName(ctxt, schema,
8641 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008642 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008643 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008644 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 /*
8646 * Attribute "final".
8647 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008648 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008649 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008650 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8651 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8652 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8653 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008654 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008655 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8656 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008657 -1,
8658 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8659 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008660 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008661 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008662 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008663 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008664 attrValue, NULL, NULL, NULL);
8665 }
8666 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008667 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008668 /*
8669 * Attribute "block".
8670 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008671 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008672 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008673 /*
8674 * Apply default "block" values.
8675 */
8676 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8677 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8678 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8679 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8680 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8681 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008682 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008683 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8684 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008685 -1,
8686 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008687 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008688 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8689 xmlSchemaPSimpleTypeErr(ctxt,
8690 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008691 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008692 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008693 "restriction | substitution))", attrValue,
8694 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008695 }
8696 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008697 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008698 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008699
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008700 attr = xmlSchemaGetPropNode(node, "type");
8701 if (attr != NULL) {
8702 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008703 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008704 &(decl->namedTypeNs), &(decl->namedType));
8705 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008706 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 }
8708 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8709 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008710 if (attr != NULL) {
8711 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008712 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008713 /*
8714 * 3.3.3 : 1
8715 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008716 */
8717 xmlSchemaPMutualExclAttrErr(ctxt,
8718 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008719 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008720 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008721 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8722 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008723 }
William M. Bracke7091952004-05-11 15:09:58 +00008724 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008725 /*
8726 * And now for the children...
8727 */
William M. Bracke7091952004-05-11 15:09:58 +00008728 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008729 /*
8730 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008731 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008732 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008733 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008734 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008735 xmlSchemaPContentErr(ctxt,
8736 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008737 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008738 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008739 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008740 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008741 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008742 child = child->next;
8743 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008744 /*
8745 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008746 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008747 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008748 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008749 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008750 xmlSchemaPContentErr(ctxt,
8751 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008752 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008753 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008754 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008755 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008756 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008757 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008758 }
William M. Bracke7091952004-05-11 15:09:58 +00008759 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008760 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008761 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008762 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008763 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008764 } else if (IS_SCHEMA(child, "key")) {
8765 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008766 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008767 } else if (IS_SCHEMA(child, "keyref")) {
8768 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008769 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008770 }
8771 if (lastIDC != NULL)
8772 lastIDC->next = curIDC;
8773 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008774 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008775 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008776 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008777 }
8778 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008779 xmlSchemaPContentErr(ctxt,
8780 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008781 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008782 NULL, "(annotation?, ((simpleType | complexType)?, "
8783 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008784 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008785 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008786 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008787 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008788 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008789 * different layer.
8790 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008791 FREE_AND_NULL(des)
8792 if (topLevel)
8793 return ((xmlSchemaBasicItemPtr) decl);
8794 else {
8795 particle->children = (xmlSchemaTreeItemPtr) decl;
8796 return ((xmlSchemaBasicItemPtr) particle);
8797 }
8798
8799return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008800 FREE_AND_NULL(des);
8801 if (annot != NULL) {
8802 if (particle != NULL)
8803 particle->annot = NULL;
8804 if (decl != NULL)
8805 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008806 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008807 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008808 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008809}
8810
8811/**
8812 * xmlSchemaParseUnion:
8813 * @ctxt: a schema validation context
8814 * @schema: the schema being built
8815 * @node: a subtree containing XML Schema informations
8816 *
8817 * parse a XML schema Union definition
8818 * *WARNING* this interface is highly subject to change
8819 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008820 * Returns -1 in case of internal error, 0 in case of success and a positive
8821 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008822 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008823static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008824xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008825 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008826{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008827 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008828 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008829 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008830 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008831
8832 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008833 return (-1);
8834 /* Not a component, don't create it. */
8835 type = ctxt->ctxtType;
8836 /*
8837 * Mark the simple type as being of variety "union".
8838 */
8839 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008840 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008841 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8842 * then the ·simple ur-type definition·."
8843 */
8844 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008845 /*
8846 * Check for illegal attributes.
8847 */
8848 attr = node->properties;
8849 while (attr != NULL) {
8850 if (attr->ns == NULL) {
8851 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8852 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008857 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008858 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008859 }
8860 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008861 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008862 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008863 /*
8864 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008865 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008866 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008867 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008868 if (attr != NULL) {
8869 const xmlChar *end;
8870 xmlChar *tmp;
8871 const xmlChar *localName, *nsName;
8872 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8873 xmlSchemaQNameRefPtr ref;
8874
8875 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008876 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008877 do {
8878 while (IS_BLANK_CH(*cur))
8879 cur++;
8880 end = cur;
8881 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8882 end++;
8883 if (end == cur)
8884 break;
8885 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008886 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008887 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008888 /*
8889 * Create the member type link.
8890 */
8891 link = (xmlSchemaTypeLinkPtr)
8892 xmlMalloc(sizeof(xmlSchemaTypeLink));
8893 if (link == NULL) {
8894 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8895 "allocating a type link", NULL);
8896 return (-1);
8897 }
8898 link->type = NULL;
8899 link->next = NULL;
8900 if (lastLink == NULL)
8901 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008902 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008903 lastLink->next = link;
8904 lastLink = link;
8905 /*
8906 * Create a reference item.
8907 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008908 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008909 localName, nsName);
8910 if (ref == NULL) {
8911 FREE_AND_NULL(tmp)
8912 return (-1);
8913 }
8914 /*
8915 * Assign the reference to the link, it will be resolved
8916 * later during fixup of the union simple type.
8917 */
8918 link->type = (xmlSchemaTypePtr) ref;
8919 }
8920 FREE_AND_NULL(tmp)
8921 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008922 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008923
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008924 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008925 /*
8926 * And now for the children...
8927 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008928 child = node->children;
8929 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008930 /*
8931 * Add the annotation to the simple type ancestor.
8932 */
8933 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008934 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008935 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008936 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008937 if (IS_SCHEMA(child, "simpleType")) {
8938 xmlSchemaTypePtr subtype, last = NULL;
8939
8940 /*
8941 * Anchor the member types in the "subtypes" field of the
8942 * simple type.
8943 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008944 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008945 subtype = (xmlSchemaTypePtr)
8946 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8947 if (subtype != NULL) {
8948 if (last == NULL) {
8949 type->subtypes = subtype;
8950 last = subtype;
8951 } else {
8952 last->next = subtype;
8953 last = subtype;
8954 }
8955 last->next = NULL;
8956 }
8957 child = child->next;
8958 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008959 }
8960 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008961 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008962 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008963 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008964 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008965 if ((attr == NULL) && (type->subtypes == NULL)) {
8966 /*
8967 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008968 * Either the memberTypes [attribute] of the <union> element must
8969 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008970 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008971 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008972 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008973 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008974 "Either the attribute 'memberTypes' or "
8975 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008976 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008977 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008978}
8979
8980/**
8981 * xmlSchemaParseList:
8982 * @ctxt: a schema validation context
8983 * @schema: the schema being built
8984 * @node: a subtree containing XML Schema informations
8985 *
8986 * parse a XML schema List definition
8987 * *WARNING* this interface is highly subject to change
8988 *
William M. Bracke7091952004-05-11 15:09:58 +00008989 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008990 * 1 in case of success.
8991 */
8992static xmlSchemaTypePtr
8993xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008994 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008995{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008996 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008997 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008998 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008999
9000 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9001 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009002 /* Not a component, don't create it. */
9003 type = ctxt->ctxtType;
9004 /*
9005 * Mark the type as being of variety "list".
9006 */
9007 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009008 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009009 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
9010 * then the ·simple ur-type definition·."
9011 */
9012 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00009013 /*
9014 * Check for illegal attributes.
9015 */
9016 attr = node->properties;
9017 while (attr != NULL) {
9018 if (attr->ns == NULL) {
9019 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9020 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009025 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009026 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009027 }
9028 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009029 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009030
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009031 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009032
William M. Brack2f2a6632004-08-20 23:09:47 +00009033 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009034 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9035 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009036 *
9037 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9038 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009039 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009040 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9041 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009042 /*
9043 * And now for the children...
9044 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009045 child = node->children;
9046 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009047 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009048 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009049 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009050 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009051 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009052 /*
9053 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009054 * Either the itemType [attribute] or the <simpleType> [child] of
9055 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009056 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009057 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009058 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009059 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009060 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009061 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009062 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009063 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009064 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009065 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009066 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009067 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009068 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009069 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009070 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009071 "Either the attribute 'itemType' or the <simpleType> child "
9072 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009073 }
9074 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009075 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009076 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009077 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009078 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009079 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009080 (type->subtypes == NULL) &&
9081 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009082 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009083 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009084 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009085 "Either the attribute 'itemType' or the <simpleType> child "
9086 "must be present", NULL);
9087 }
9088 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009089}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009090
Daniel Veillard4255d502002-04-16 15:50:10 +00009091/**
9092 * xmlSchemaParseSimpleType:
9093 * @ctxt: a schema validation context
9094 * @schema: the schema being built
9095 * @node: a subtree containing XML Schema informations
9096 *
9097 * parse a XML schema Simple Type definition
9098 * *WARNING* this interface is highly subject to change
9099 *
William M. Bracke7091952004-05-11 15:09:58 +00009100 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009101 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009102 */
9103static xmlSchemaTypePtr
9104xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009105 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009106{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009107 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009108 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009109 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009110 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009111 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009112
9113 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9114 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009115
Daniel Veillardc0826a72004-08-10 14:17:33 +00009116 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009117 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009118 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009119 xmlSchemaPMissingAttrErr(ctxt,
9120 XML_SCHEMAP_S4S_ATTR_MISSING,
9121 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009122 "name", NULL);
9123 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009124 } else {
9125 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009126 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009127 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9128 return (NULL);
9129 /*
9130 * Skip built-in types.
9131 */
9132 if (ctxt->isS4S) {
9133 xmlSchemaTypePtr biType;
9134
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009135 if (ctxt->isRedefine) {
9136 /*
9137 * REDEFINE: Disallow redefinition of built-in-types.
9138 * TODO: It seems that the spec does not say anything
9139 * about this case.
9140 */
9141 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009142 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009143 "Redefinition of built-in simple types is not "
9144 "supported", NULL);
9145 return(NULL);
9146 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009147 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9148 if (biType != NULL)
9149 return (biType);
9150 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009151 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009152 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009153 /*
9154 * TargetNamespace:
9155 * SPEC "The ·actual value· of the targetNamespace [attribute]
9156 * of the <schema> ancestor element information item if present,
9157 * otherwise ·absent·.
9158 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009159 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009160#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009161 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009162#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009163 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009164 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009165 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009166#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009167 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009168 type = xmlSchemaAddType(ctxt, schema,
9169 XML_SCHEMA_TYPE_SIMPLE,
9170 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009171 ctxt->targetNamespace, node, 0);
9172#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009173 type = xmlSchemaAddType(ctxt, schema,
9174 XML_SCHEMA_TYPE_SIMPLE,
9175 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009176#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009177 if (type == NULL)
9178 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009179 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009180 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009181 /*
9182 * Check for illegal attributes.
9183 */
9184 attr = node->properties;
9185 while (attr != NULL) {
9186 if (attr->ns == NULL) {
9187 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
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 Veillard01fa6152004-06-29 17:04:39 +00009190 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009191 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009192 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009193 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009194 }
9195 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009196 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009197 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009198 /*
9199 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009200 *
9201 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009202 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009203 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9204 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009205 if (type == NULL)
9206 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009207 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009208 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009209 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9210 /*
9211 * Check for illegal attributes.
9212 */
9213 attr = node->properties;
9214 while (attr != NULL) {
9215 if (attr->ns == NULL) {
9216 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9217 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009218 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009223 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009224 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009225 }
9226 attr = attr->next;
9227 }
9228 /*
9229 * Attribute "final".
9230 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009231 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009232 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009233 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9234 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9235 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9236 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9237 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9238 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009239 } else {
9240 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009241 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9242 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009243 XML_SCHEMAS_TYPE_FINAL_LIST,
9244 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9245
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009246 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009247 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009248 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009249 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009250 attrValue, NULL, NULL, NULL);
9251 }
9252 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009253 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009254 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009255 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009256 /*
9257 * And now for the children...
9258 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009259 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009260
Daniel Veillardc0826a72004-08-10 14:17:33 +00009261 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009262
Daniel Veillard4255d502002-04-16 15:50:10 +00009263 child = node->children;
9264 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009265 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009266 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009267 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009268 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009269 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009270 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009271 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009272 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009273 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009274 XML_SCHEMA_TYPE_SIMPLE);
9275 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009276 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009277 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009278 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009279 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009280 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009281 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009282 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009283 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009284 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009285 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009286 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009287 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009288 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009289 /*
9290 * REDEFINE: SPEC src-redefine (5)
9291 * "Within the [children], each <simpleType> must have a
9292 * <restriction> among its [children] ... the ·actual value· of whose
9293 * base [attribute] must be the same as the ·actual value· of its own
9294 * name attribute plus target namespace;"
9295 */
9296 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9297 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009298 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009299 "<simpleType> must have a <restriction> child", NULL);
9300 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009301
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009302 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009303 return (type);
9304}
9305
Daniel Veillard4255d502002-04-16 15:50:10 +00009306/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009307 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009308 * @ctxt: the parser context
9309 * @schema: the schema being built
9310 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009311 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009312 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009313 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009314 * We will return a particle component with a qname-component or
9315 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009316 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009317static xmlSchemaTreeItemPtr
9318xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9319 xmlSchemaPtr schema,
9320 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009321{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009322 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009323 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009324 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009325 const xmlChar *ref = NULL, *refNs = NULL;
9326 int min, max;
9327
9328 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009329 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009330
9331 attr = xmlSchemaGetPropNode(node, "ref");
9332 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009333 xmlSchemaPMissingAttrErr(ctxt,
9334 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009335 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009336 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009337 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009338 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009339 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009340 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009341 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009342 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009343 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009344 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009345 /*
9346 * Check for illegal attributes.
9347 */
9348 attr = node->properties;
9349 while (attr != NULL) {
9350 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009351 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009352 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9353 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9354 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009359 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009360 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009361 }
9362 attr = attr->next;
9363 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009364 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00009365 item = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009366 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009367 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009368 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009369 * Create a qname-reference and set as the term; it will be substituted
9370 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009371 */
9372 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009373 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009374 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9375 /*
9376 * And now for the children...
9377 */
9378 child = node->children;
9379 /* TODO: Is annotation even allowed for a model group reference? */
9380 if (IS_SCHEMA(child, "annotation")) {
9381 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009382 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009383 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009384 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009385 child = child->next;
9386 }
9387 if (child != NULL) {
9388 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009389 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009390 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009391 "(annotation?)");
9392 }
9393 /*
9394 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9395 */
9396 if ((min == 0) && (max == 0))
9397 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009398
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009399 return ((xmlSchemaTreeItemPtr) item);
9400}
9401
9402/**
9403 * xmlSchemaParseModelGroupDefinition:
9404 * @ctxt: a schema validation context
9405 * @schema: the schema being built
9406 * @node: a subtree containing XML Schema informations
9407 *
9408 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009409 *
9410 * Note that the contraint src-redefine (6.2) can't be applied until
9411 * references have been resolved. So we will do this at the
9412 * component fixup level.
9413 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009414 * *WARNING* this interface is highly subject to change
9415 *
9416 * Returns -1 in case of error, 0 if the declaration is improper and
9417 * 1 in case of success.
9418 */
9419static xmlSchemaModelGroupDefPtr
9420xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9421 xmlSchemaPtr schema,
9422 xmlNodePtr node)
9423{
9424 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009425 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009426 xmlAttrPtr attr;
9427 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009428
9429 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009430 return (NULL);
9431
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009432 attr = xmlSchemaGetPropNode(node, "name");
9433 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009434 xmlSchemaPMissingAttrErr(ctxt,
9435 XML_SCHEMAP_S4S_ATTR_MISSING,
9436 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009437 "name", NULL);
9438 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009439 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009440 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9441 return (NULL);
9442 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009443 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9444 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009445 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009446 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009447 /*
9448 * Check for illegal attributes.
9449 */
9450 attr = node->properties;
9451 while (attr != NULL) {
9452 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009453 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009454 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
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 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009459 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009460 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009461 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009462 attr = attr->next;
9463 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009464 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009465 /*
9466 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009467 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009468 child = node->children;
9469 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009470 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009471 child = child->next;
9472 }
9473 if (IS_SCHEMA(child, "all")) {
9474 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9475 XML_SCHEMA_TYPE_ALL, 0);
9476 child = child->next;
9477 } else if (IS_SCHEMA(child, "choice")) {
9478 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9479 XML_SCHEMA_TYPE_CHOICE, 0);
9480 child = child->next;
9481 } else if (IS_SCHEMA(child, "sequence")) {
9482 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9483 XML_SCHEMA_TYPE_SEQUENCE, 0);
9484 child = child->next;
9485 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009486
9487
9488
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009489 if (child != NULL) {
9490 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009491 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009492 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009493 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009494 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009495 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009496}
9497
9498/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009499 * xmlSchemaCleanupDoc:
9500 * @ctxt: a schema validation context
9501 * @node: the root of the document.
9502 *
9503 * removes unwanted nodes in a schemas document tree
9504 */
9505static void
9506xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9507{
9508 xmlNodePtr delete, cur;
9509
9510 if ((ctxt == NULL) || (root == NULL)) return;
9511
9512 /*
9513 * Remove all the blank text nodes
9514 */
9515 delete = NULL;
9516 cur = root;
9517 while (cur != NULL) {
9518 if (delete != NULL) {
9519 xmlUnlinkNode(delete);
9520 xmlFreeNode(delete);
9521 delete = NULL;
9522 }
9523 if (cur->type == XML_TEXT_NODE) {
9524 if (IS_BLANK_NODE(cur)) {
9525 if (xmlNodeGetSpacePreserve(cur) != 1) {
9526 delete = cur;
9527 }
9528 }
9529 } else if ((cur->type != XML_ELEMENT_NODE) &&
9530 (cur->type != XML_CDATA_SECTION_NODE)) {
9531 delete = cur;
9532 goto skip_children;
9533 }
9534
9535 /*
9536 * Skip to next node
9537 */
9538 if (cur->children != NULL) {
9539 if ((cur->children->type != XML_ENTITY_DECL) &&
9540 (cur->children->type != XML_ENTITY_REF_NODE) &&
9541 (cur->children->type != XML_ENTITY_NODE)) {
9542 cur = cur->children;
9543 continue;
9544 }
9545 }
9546 skip_children:
9547 if (cur->next != NULL) {
9548 cur = cur->next;
9549 continue;
9550 }
9551
9552 do {
9553 cur = cur->parent;
9554 if (cur == NULL)
9555 break;
9556 if (cur == root) {
9557 cur = NULL;
9558 break;
9559 }
9560 if (cur->next != NULL) {
9561 cur = cur->next;
9562 break;
9563 }
9564 } while (cur != NULL);
9565 }
9566 if (delete != NULL) {
9567 xmlUnlinkNode(delete);
9568 xmlFreeNode(delete);
9569 delete = NULL;
9570 }
9571}
9572
William M. Brack2f2a6632004-08-20 23:09:47 +00009573
William M. Brack2f2a6632004-08-20 23:09:47 +00009574static void
9575xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9576{
9577 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9578 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9579
9580 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9581 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9582
9583 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9584 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9585 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9586 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9587 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9588 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9589 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9590 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9591
9592 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9593 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9594 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9595 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9596 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9597 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9598}
9599
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009600static int
9601xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009602 xmlSchemaPtr schema,
9603 xmlNodePtr node)
9604{
9605 xmlAttrPtr attr;
9606 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009607 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009608
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009609 /*
9610 * Those flags should be moved to the parser context flags,
9611 * since they are not visible at the component level. I.e.
9612 * they are used if processing schema *documents* only.
9613 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009614 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009615 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009616
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009617 /*
9618 * Since the version is of type xs:token, we won't bother to
9619 * check it.
9620 */
9621 /* REMOVED:
9622 attr = xmlSchemaGetPropNode(node, "version");
9623 if (attr != NULL) {
9624 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9625 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9626 HFAILURE;
9627 }
9628 */
9629 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9630 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009631 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009632 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9633 HFAILURE;
9634 if (res != 0) {
9635 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9636 goto exit;
9637 }
9638 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009639 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009640 if (attr != NULL) {
9641 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009642 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9643 XML_SCHEMAS_QUALIF_ELEM);
9644 HFAILURE;
9645 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009646 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009647 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009648 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 "(qualified | unqualified)", val, NULL, NULL, NULL);
9650 }
9651 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009652 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009653 if (attr != NULL) {
9654 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009655 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9656 XML_SCHEMAS_QUALIF_ATTR);
9657 HFAILURE;
9658 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009659 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009660 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009661 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009662 "(qualified | unqualified)", val, NULL, NULL, NULL);
9663 }
9664 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009665 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009666 if (attr != NULL) {
9667 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009668 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009669 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9670 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9671 -1,
9672 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009673 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9674 HFAILURE;
9675 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009676 xmlSchemaPSimpleTypeErr(ctxt,
9677 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009678 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009679 "(#all | List of (extension | restriction | list | union))",
9680 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009681 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009682 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009683 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009684 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009685 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9686 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009687 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9688 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009689 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9690 HFAILURE;
9691 if (res != 0) {
9692 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009693 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009694 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009695 "(#all | List of (extension | restriction | substitution))",
9696 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009697 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009698 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009699
9700exit:
9701 if (oldErrs != ctxt->nberrors)
9702 res = ctxt->err;
9703 return(res);
9704exit_failure:
9705 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009706}
9707
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009708/**
9709 * xmlSchemaParseSchemaTopLevel:
9710 * @ctxt: a schema validation context
9711 * @schema: the schemas
9712 * @nodes: the list of top level nodes
9713 *
9714 * Returns the internal XML Schema structure built from the resource or
9715 * NULL in case of error
9716 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009717static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009718xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9719 xmlSchemaPtr schema, xmlNodePtr nodes)
9720{
9721 xmlNodePtr child;
9722 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009723 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009724
9725 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009726 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009727
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009728 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009729 child = nodes;
9730 while ((IS_SCHEMA(child, "include")) ||
9731 (IS_SCHEMA(child, "import")) ||
9732 (IS_SCHEMA(child, "redefine")) ||
9733 (IS_SCHEMA(child, "annotation"))) {
9734 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009735 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009736 if (schema->annot == NULL)
9737 schema->annot = annot;
9738 else
9739 xmlSchemaFreeAnnot(annot);
9740 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009741 tmpOldErrs = ctxt->nberrors;
9742 res = xmlSchemaParseImport(ctxt, schema, child);
9743 HFAILURE;
9744 HSTOP(ctxt);
9745 if (tmpOldErrs != ctxt->nberrors)
9746 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009747 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009748 tmpOldErrs = ctxt->nberrors;
9749 res = xmlSchemaParseInclude(ctxt, schema, child);
9750 HFAILURE;
9751 HSTOP(ctxt);
9752 if (tmpOldErrs != ctxt->nberrors)
9753 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009754 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009755 tmpOldErrs = ctxt->nberrors;
9756 res = xmlSchemaParseRedefine(ctxt, schema, child);
9757 HFAILURE;
9758 HSTOP(ctxt);
9759 if (tmpOldErrs != ctxt->nberrors)
9760 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009761 }
9762 child = child->next;
9763 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009764 /*
9765 * URGENT TODO: Change the functions to return int results.
9766 * We need especially to catch internal errors.
9767 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009768 while (child != NULL) {
9769 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009770 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009771 child = child->next;
9772 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009773 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009774 child = child->next;
9775 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009776 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009777 child = child->next;
9778 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009779 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009780 child = child->next;
9781 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009782 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009783 child = child->next;
9784 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009785 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009786 child = child->next;
9787 } else if (IS_SCHEMA(child, "notation")) {
9788 xmlSchemaParseNotation(ctxt, schema, child);
9789 child = child->next;
9790 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791 xmlSchemaPContentErr(ctxt,
9792 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009793 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009794 NULL, "((include | import | redefine | annotation)*, "
9795 "(((simpleType | complexType | group | attributeGroup) "
9796 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009797 child = child->next;
9798 }
9799 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009800 /*
9801 * TODO: We should add all annotations.
9802 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009803 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009804 if (schema->annot == NULL)
9805 schema->annot = annot;
9806 else
9807 xmlSchemaFreeAnnot(annot);
9808 child = child->next;
9809 }
9810 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009811exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009812 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009813 if (oldErrs != ctxt->nberrors)
9814 res = ctxt->err;
9815 return(res);
9816exit_failure:
9817 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009818}
9819
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009820static xmlSchemaSchemaRelationPtr
9821xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009822{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009823 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009824
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009825 ret = (xmlSchemaSchemaRelationPtr)
9826 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009827 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009828 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009829 return(NULL);
9830 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009831 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009832 return(ret);
9833}
9834
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009835#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009836static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009837xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009838{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009839 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009840}
9841#endif
9842
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009843static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009844xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9845{
9846 xmlSchemaRedefPtr prev;
9847
9848 while (redef != NULL) {
9849 prev = redef;
9850 redef = redef->next;
9851 xmlFree(prev);
9852 }
9853}
9854
9855static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009856xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9857{
9858 /*
9859 * After the construction context has been freed, there will be
9860 * no schema graph available any more. Only the schema buckets
9861 * will stay alive, which are put into the "schemasImports" and
9862 * "includes" slots of the xmlSchema.
9863 */
9864 if (con->buckets != NULL)
9865 xmlSchemaItemListFree(con->buckets);
9866 if (con->pending != NULL)
9867 xmlSchemaItemListFree(con->pending);
9868 if (con->substGroups != NULL)
9869 xmlHashFree(con->substGroups,
9870 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009871 if (con->redefs != NULL)
9872 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009873 if (con->dict != NULL)
9874 xmlDictFree(con->dict);
9875 xmlFree(con);
9876}
9877
9878static xmlSchemaConstructionCtxtPtr
9879xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9880{
9881 xmlSchemaConstructionCtxtPtr ret;
9882
9883 ret = (xmlSchemaConstructionCtxtPtr)
9884 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9885 if (ret == NULL) {
9886 xmlSchemaPErrMemory(NULL,
9887 "allocating schema construction context", NULL);
9888 return (NULL);
9889 }
9890 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9891
9892 ret->buckets = xmlSchemaItemListCreate();
9893 if (ret->buckets == NULL) {
9894 xmlSchemaPErrMemory(NULL,
9895 "allocating list of schema buckets", NULL);
9896 xmlFree(ret);
9897 return (NULL);
9898 }
9899 ret->pending = xmlSchemaItemListCreate();
9900 if (ret->pending == NULL) {
9901 xmlSchemaPErrMemory(NULL,
9902 "allocating list of pending global components", NULL);
9903 xmlSchemaConstructionCtxtFree(ret);
9904 return (NULL);
9905 }
9906 ret->dict = dict;
9907 xmlDictReference(dict);
9908 return(ret);
9909}
9910
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009911static xmlSchemaParserCtxtPtr
9912xmlSchemaParserCtxtCreate(void)
9913{
9914 xmlSchemaParserCtxtPtr ret;
9915
9916 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9917 if (ret == NULL) {
9918 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9919 NULL);
9920 return (NULL);
9921 }
9922 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9923 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009924 ret->attrProhibs = xmlSchemaItemListCreate();
9925 if (ret->attrProhibs == NULL) {
9926 xmlFree(ret);
9927 return(NULL);
9928 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009929 return(ret);
9930}
9931
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009932/**
9933 * xmlSchemaNewParserCtxtUseDict:
9934 * @URL: the location of the schema
9935 * @dict: the dictionary to be used
9936 *
9937 * Create an XML Schemas parse context for that file/resource expected
9938 * to contain an XML Schemas file.
9939 *
9940 * Returns the parser context or NULL in case of error
9941 */
9942static xmlSchemaParserCtxtPtr
9943xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9944{
9945 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009946
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009947 ret = xmlSchemaParserCtxtCreate();
9948 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009949 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009950 ret->dict = dict;
9951 xmlDictReference(dict);
9952 if (URL != NULL)
9953 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009954 return (ret);
9955}
9956
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009957static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009958xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9959{
9960 if (vctxt->pctxt == NULL) {
9961 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009962 vctxt->pctxt =
9963 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009964 else
9965 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9966 if (vctxt->pctxt == NULL) {
9967 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9968 "failed to create a temp. parser context");
9969 return (-1);
9970 }
9971 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009972 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009973 vctxt->warning, vctxt->errCtxt);
9974 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9975 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009976 }
9977 return (0);
9978}
9979
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009980/**
9981 * xmlSchemaGetSchemaBucket:
9982 * @pctxt: the schema parser context
9983 * @schemaLocation: the URI of the schema document
9984 *
9985 * Returns a schema bucket if it was already parsed.
9986 *
9987 * Returns a schema bucket if it was already parsed from
9988 * @schemaLocation, NULL otherwise.
9989 */
9990static xmlSchemaBucketPtr
9991xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9992 const xmlChar *schemaLocation)
9993{
9994 xmlSchemaBucketPtr cur;
9995 xmlSchemaItemListPtr list;
9996
9997 list = pctxt->constructor->buckets;
9998 if (list->nbItems == 0)
9999 return(NULL);
10000 else {
10001 int i;
10002 for (i = 0; i < list->nbItems; i++) {
10003 cur = (xmlSchemaBucketPtr) list->items[i];
10004 /* Pointer comparison! */
10005 if (cur->schemaLocation == schemaLocation)
10006 return(cur);
10007 }
10008 }
10009 return(NULL);
10010}
10011
10012static xmlSchemaBucketPtr
10013xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10014 const xmlChar *schemaLocation,
10015 const xmlChar *targetNamespace)
10016{
10017 xmlSchemaBucketPtr cur;
10018 xmlSchemaItemListPtr list;
10019
10020 list = pctxt->constructor->buckets;
10021 if (list->nbItems == 0)
10022 return(NULL);
10023 else {
10024 int i;
10025 for (i = 0; i < list->nbItems; i++) {
10026 cur = (xmlSchemaBucketPtr) list->items[i];
10027 /* Pointer comparison! */
10028 if ((cur->origTargetNamespace == NULL) &&
10029 (cur->schemaLocation == schemaLocation) &&
10030 (cur->targetNamespace == targetNamespace))
10031 return(cur);
10032 }
10033 }
10034 return(NULL);
10035}
10036
10037
10038#define IS_BAD_SCHEMA_DOC(b) \
10039 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10040
10041static xmlSchemaBucketPtr
10042xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10043 const xmlChar *targetNamespace,
10044 int imported)
10045{
10046 xmlSchemaBucketPtr cur;
10047 xmlSchemaItemListPtr list;
10048
10049 list = pctxt->constructor->buckets;
10050 if (list->nbItems == 0)
10051 return(NULL);
10052 else {
10053 int i;
10054 for (i = 0; i < list->nbItems; i++) {
10055 cur = (xmlSchemaBucketPtr) list->items[i];
10056 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10057 (cur->origTargetNamespace == targetNamespace) &&
10058 ((imported && cur->imported) ||
10059 ((!imported) && (!cur->imported))))
10060 return(cur);
10061 }
10062 }
10063 return(NULL);
10064}
10065
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010066static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010067xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10068 xmlSchemaPtr schema,
10069 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010070{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010071 int oldFlags;
10072 xmlDocPtr oldDoc;
10073 xmlNodePtr node;
10074 int ret, oldErrs;
10075 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10076
10077 /*
10078 * Save old values; reset the *main* schema.
10079 * URGENT TODO: This is not good; move the per-document information
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010080 * to the parser. Get rid of passing the main schema to the
10081 * parsing functions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010082 */
10083 oldFlags = schema->flags;
10084 oldDoc = schema->doc;
10085 if (schema->flags != 0)
10086 xmlSchemaClearSchemaDefaults(schema);
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010087 schema->doc = bucket->doc;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010088 pctxt->schema = schema;
10089 /*
10090 * Keep the current target namespace on the parser *not* on the
10091 * main schema.
10092 */
10093 pctxt->targetNamespace = bucket->targetNamespace;
10094 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010095
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010096 if ((bucket->targetNamespace != NULL) &&
10097 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010098 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010099 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010100 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010101 pctxt->isS4S = 1;
10102 }
10103 /* Mark it as parsed, even if parsing fails. */
10104 bucket->parsed++;
10105 /* Compile the schema doc. */
10106 node = xmlDocGetRootElement(bucket->doc);
10107 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10108 if (ret != 0)
10109 goto exit;
10110 /* An empty schema; just get out. */
10111 if (node->children == NULL)
10112 goto exit;
10113 oldErrs = pctxt->nberrors;
10114 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10115 if (ret != 0)
10116 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010117 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010118 * TODO: Not nice, but I'm not 100% sure we will get always an error
10119 * as a result of the obove functions; so better rely on pctxt->err
10120 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010121 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010122 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10123 ret = pctxt->err;
10124 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010125 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010126
10127exit:
10128 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10129 /* Restore schema values. */
10130 schema->doc = oldDoc;
10131 schema->flags = oldFlags;
10132 return(ret);
10133}
10134
10135static int
10136xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10137 xmlSchemaPtr schema,
10138 xmlSchemaBucketPtr bucket)
10139{
10140 xmlSchemaParserCtxtPtr newpctxt;
10141 int res = 0;
10142
10143 if (bucket == NULL)
10144 return(0);
10145 if (bucket->parsed) {
10146 PERROR_INT("xmlSchemaParseNewDoc",
10147 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010148 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010149 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010150 if (bucket->doc == NULL) {
10151 PERROR_INT("xmlSchemaParseNewDoc",
10152 "parsing a schema doc, but there's no doc");
10153 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010154 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010155 if (pctxt->constructor == NULL) {
10156 PERROR_INT("xmlSchemaParseNewDoc",
10157 "no constructor");
10158 return(-1);
10159 }
10160 /* Create and init the temporary parser context. */
10161 newpctxt = xmlSchemaNewParserCtxtUseDict(
10162 (const char *) bucket->schemaLocation, pctxt->dict);
10163 if (newpctxt == NULL)
10164 return(-1);
10165 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010166 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010167 * TODO: Can we avoid that the parser knows about the main schema?
10168 * It would be better if he knows about the current schema bucket
10169 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010170 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010171 newpctxt->schema = schema;
10172 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010173 pctxt->errCtxt);
10174 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10175 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010176 newpctxt->counter = pctxt->counter;
10177
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010178
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010179 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10180
10181 /* Channel back errors and cleanup the temporary parser context. */
10182 if (res != 0)
10183 pctxt->err = res;
10184 pctxt->nberrors += newpctxt->nberrors;
10185 pctxt->counter = newpctxt->counter;
10186 newpctxt->constructor = NULL;
10187 /* Free the parser context. */
10188 xmlSchemaFreeParserCtxt(newpctxt);
10189 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010190}
William M. Brack2f2a6632004-08-20 23:09:47 +000010191
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010192static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010193xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10194 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010195{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010196 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010197
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010198 if (cur == NULL) {
10199 bucket->relations = rel;
10200 return;
10201 }
10202 while (cur->next != NULL)
10203 cur = cur->next;
10204 cur->next = rel;
10205}
10206
10207
10208static const xmlChar *
10209xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10210 xmlNodePtr ctxtNode)
10211{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010212 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010213 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010214 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010215 if (location != NULL) {
10216 if (ctxtNode == NULL)
10217 return(location);
10218 else {
10219 xmlChar *base, *URI;
10220 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010221
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010222 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10223 if (base == NULL) {
10224 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10225 } else {
10226 URI = xmlBuildURI(location, base);
10227 xmlFree(base);
10228 }
10229 if (URI != NULL) {
10230 ret = xmlDictLookup(dict, URI, -1);
10231 xmlFree(URI);
10232 return(ret);
10233 }
10234 }
10235 }
10236 return(NULL);
10237}
10238
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010239
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010240
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010241/**
10242 * xmlSchemaAddSchemaDoc:
10243 * @pctxt: a schema validation context
10244 * @schema: the schema being built
10245 * @node: a subtree containing XML Schema informations
10246 *
10247 * Parse an included (and to-be-redefined) XML schema document.
10248 *
10249 * Returns 0 on success, a positive error code on errors and
10250 * -1 in case of an internal or API error.
10251 */
10252
10253static int
10254xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10255 int type, /* import or include or redefine */
10256 const xmlChar *schemaLocation,
10257 xmlDocPtr schemaDoc,
10258 const char *schemaBuffer,
10259 int schemaBufferLen,
10260 xmlNodePtr invokingNode,
10261 const xmlChar *sourceTargetNamespace,
10262 const xmlChar *importNamespace,
10263 xmlSchemaBucketPtr *bucket)
10264{
10265 const xmlChar *targetNamespace = NULL;
10266 xmlSchemaSchemaRelationPtr relation = NULL;
10267 xmlDocPtr doc = NULL;
10268 int res = 0, err = 0, located = 0, preserveDoc = 0;
10269 xmlSchemaBucketPtr bkt = NULL;
10270
10271 if (bucket != NULL)
10272 *bucket = NULL;
10273
10274 switch (type) {
10275 case XML_SCHEMA_SCHEMA_IMPORT:
10276 case XML_SCHEMA_SCHEMA_MAIN:
10277 err = XML_SCHEMAP_SRC_IMPORT;
10278 break;
10279 case XML_SCHEMA_SCHEMA_INCLUDE:
10280 err = XML_SCHEMAP_SRC_INCLUDE;
10281 break;
10282 case XML_SCHEMA_SCHEMA_REDEFINE:
10283 err = XML_SCHEMAP_SRC_REDEFINE;
10284 break;
10285 }
10286
10287
10288 /* Special handling for the main schema:
10289 * skip the location and relation logic and just parse the doc.
10290 * We need just a bucket to be returned in this case.
10291 */
10292 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10293 goto doc_load;
10294
10295 /* Note that we expect the location to be an absulute URI. */
10296 if (schemaLocation != NULL) {
10297 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10298 if ((bkt != NULL) &&
10299 (pctxt->constructor->bucket == bkt)) {
10300 /* Report self-imports/inclusions/redefinitions. */
10301
10302 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10303 invokingNode, NULL,
10304 "The schema must not import/include/redefine itself",
10305 NULL, NULL);
10306 goto exit;
10307 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010308 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010309 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010310 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010311 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010312 relation = xmlSchemaSchemaRelationCreate();
10313 if (relation == NULL)
10314 return(-1);
10315 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10316 relation);
10317 relation->type = type;
10318
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010319 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010320 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010321 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010322 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010323 relation->importNamespace = importNamespace;
10324 if (schemaLocation == NULL) {
10325 /*
10326 * No location; this is just an import of the namespace.
10327 * Note that we don't assign a bucket to the relation
10328 * in this case.
10329 */
10330 goto exit;
10331 }
10332 targetNamespace = importNamespace;
10333 }
10334
10335 /* Did we already fetch the doc? */
10336 if (bkt != NULL) {
10337 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010338 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010339 /* We included/redefined and then try to import a schema. */
10340 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10341 invokingNode, NULL,
10342 "The schema document '%s' cannot be imported, since "
10343 "it was already included or redefined",
10344 schemaLocation, NULL);
10345 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010346 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010347 /* We imported and then try to include/redefine a schema. */
10348 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10349 invokingNode, NULL,
10350 "The schema document '%s' cannot be included or "
10351 "redefined, since it was already imported",
10352 schemaLocation, NULL);
10353 goto exit;
10354 }
10355 }
10356
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010357 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010358 /*
10359 * Given that the schemaLocation [attribute] is only a hint, it is open
10360 * to applications to ignore all but the first <import> for a given
10361 * namespace, regardless of the ·actual value· of schemaLocation, but
10362 * such a strategy risks missing useful information when new
10363 * schemaLocations are offered.
10364 *
10365 * We will use the first <import> that comes with a location.
10366 * Further <import>s *with* a location, will result in an error.
10367 * TODO: Better would be to just report a warning here, but
10368 * we'll try it this way until someone complains.
10369 *
10370 * Schema Document Location Strategy:
10371 * 3 Based on the namespace name, identify an existing schema document,
10372 * either as a resource which is an XML document or a <schema> element
10373 * information item, in some local schema repository;
10374 * 5 Attempt to resolve the namespace name to locate such a resource.
10375 *
10376 * NOTE: (3) and (5) are not supported.
10377 */
10378 if (bkt != NULL) {
10379 relation->bucket = bkt;
10380 goto exit;
10381 }
10382 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10383 importNamespace, 1);
10384
10385 if (bkt != NULL) {
10386 relation->bucket = bkt;
10387 if (bkt->schemaLocation == NULL) {
10388 /* First given location of the schema; load the doc. */
10389 bkt->schemaLocation = schemaLocation;
10390 } else {
10391 if (!xmlStrEqual(schemaLocation,
10392 bkt->schemaLocation)) {
10393 /*
10394 * Additional location given; just skip it.
10395 * URGENT TODO: We should report a warning here.
10396 * res = XML_SCHEMAP_SRC_IMPORT;
10397 */
10398 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10399 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10400 invokingNode, NULL,
10401 "Skipping import of schema located at '%s' for the "
10402 "namespace '%s', since this namespace was already "
10403 "imported with the schema located at '%s'",
10404 schemaLocation, importNamespace, bkt->schemaLocation);
10405 }
10406 goto exit;
10407 }
10408 }
10409 /*
10410 * No bucket + first location: load the doc and create a
10411 * bucket.
10412 */
10413 } else {
10414 /* <include> and <redefine> */
10415 if (bkt != NULL) {
10416
10417 if ((bkt->origTargetNamespace == NULL) &&
10418 (bkt->targetNamespace != sourceTargetNamespace)) {
10419 xmlSchemaBucketPtr chamel;
10420
10421 /*
10422 * Chameleon include/redefine: skip loading only if it was
10423 * aleady build for the targetNamespace of the including
10424 * schema.
10425 */
10426 /*
10427 * URGENT TODO: If the schema is a chameleon-include then copy
10428 * the components into the including schema and modify the
10429 * targetNamespace of those components, do nothing otherwise.
10430 * NOTE: This is currently worked-around by compiling the
10431 * chameleon for every destinct including targetNamespace; thus
10432 * not performant at the moment.
10433 * TODO: Check when the namespace in wildcards for chameleons
10434 * needs to be converted: before we built wildcard intersections
10435 * or after.
10436 * Answer: after!
10437 */
10438 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10439 schemaLocation, sourceTargetNamespace);
10440 if (chamel != NULL) {
10441 /* A fitting chameleon was already parsed; NOP. */
10442 relation->bucket = chamel;
10443 goto exit;
10444 }
10445 /*
10446 * We need to parse the chameleon again for a different
10447 * targetNamespace.
10448 * CHAMELEON TODO: Optimize this by only parsing the
10449 * chameleon once, and then copying the components to
10450 * the new targetNamespace.
10451 */
10452 bkt = NULL;
10453 } else {
10454 relation->bucket = bkt;
10455 goto exit;
10456 }
10457 }
10458 }
10459 if ((bkt != NULL) && (bkt->doc != NULL)) {
10460 PERROR_INT("xmlSchemaAddSchemaDoc",
10461 "trying to load a schema doc, but a doc is already "
10462 "assigned to the schema bucket");
10463 goto exit_failure;
10464 }
10465
10466doc_load:
10467 /*
10468 * Load the document.
10469 */
10470 if (schemaDoc != NULL) {
10471 doc = schemaDoc;
10472 /* Don' free this one, since it was provided by the caller. */
10473 preserveDoc = 1;
10474 /* TODO: Does the context or the doc hold the location? */
10475 if (schemaDoc->URL != NULL)
10476 schemaLocation = xmlDictLookup(pctxt->dict,
10477 schemaDoc->URL, -1);
10478
10479 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10480 xmlParserCtxtPtr parserCtxt;
10481
10482 parserCtxt = xmlNewParserCtxt();
10483 if (parserCtxt == NULL) {
10484 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10485 "allocating a parser context", NULL);
10486 goto exit_failure;
10487 }
10488 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10489 /*
10490 * TODO: Do we have to burden the schema parser dict with all
10491 * the content of the schema doc?
10492 */
10493 xmlDictFree(parserCtxt->dict);
10494 parserCtxt->dict = pctxt->dict;
10495 xmlDictReference(parserCtxt->dict);
10496 }
10497 if (schemaLocation != NULL) {
10498 /* Parse from file. */
10499 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10500 NULL, SCHEMAS_PARSE_OPTIONS);
10501 } else if (schemaBuffer != NULL) {
10502 /* Parse from memory buffer. */
10503 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10504 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10505 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10506 if (doc != NULL)
10507 doc->URL = schemaLocation;
10508 }
10509 /*
10510 * For <import>:
10511 * 2.1 The referent is (a fragment of) a resource which is an
10512 * XML document (see clause 1.1), which in turn corresponds to
10513 * a <schema> element information item in a well-formed information
10514 * set, which in turn corresponds to a valid schema.
10515 * TODO: (2.1) fragments of XML documents are not supported.
10516 *
10517 * 2.2 The referent is a <schema> element information item in
10518 * a well-formed information set, which in turn corresponds
10519 * to a valid schema.
10520 * TODO: (2.2) is not supported.
10521 */
10522 if (doc == NULL) {
10523 xmlErrorPtr lerr;
10524 lerr = xmlGetLastError();
10525 /*
10526 * Check if this a parser error, or if the document could
10527 * just not be located.
10528 * TODO: Try to find specific error codes to react only on
10529 * localisation failures.
10530 */
10531 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10532 /*
10533 * We assume a parser error here.
10534 */
10535 located = 1;
10536 /* TODO: Error code ?? */
10537 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10538 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10539 invokingNode, NULL,
10540 "Failed to parse the XML resource '%s'",
10541 schemaLocation, NULL);
10542 }
10543 }
10544 xmlFreeParserCtxt(parserCtxt);
10545 if ((doc == NULL) && located)
10546 goto exit_error;
10547 } else {
10548 xmlSchemaPErr(pctxt, NULL,
10549 XML_SCHEMAP_NOTHING_TO_PARSE,
10550 "No information for parsing was provided with the "
10551 "given schema parser context.\n",
10552 NULL, NULL);
10553 goto exit_failure;
10554 }
10555 /*
10556 * Preprocess the document.
10557 */
10558 if (doc != NULL) {
10559 xmlNodePtr docElem = NULL;
10560
10561 located = 1;
10562 docElem = xmlDocGetRootElement(doc);
10563 if (docElem == NULL) {
10564 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10565 invokingNode, NULL,
10566 "The document '%s' has no document element",
10567 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010568 goto exit_error;
10569 }
10570 /*
10571 * Remove all the blank text nodes.
10572 */
10573 xmlSchemaCleanupDoc(pctxt, docElem);
10574 /*
10575 * Check the schema's top level element.
10576 */
10577 if (!IS_SCHEMA(docElem, "schema")) {
10578 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10579 invokingNode, NULL,
10580 "The XML document '%s' is not a schema document",
10581 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010582 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),
Daniel Veillardfef73a52006-03-27 09:38:57 +000011504 xmlSchemaFormatQName(&str2, type->targetNamespace,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011505 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];
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012554 /*
12555 * NOTE: This fixes bug #341150. xmlAutomataNewOnceTrans2()
12556 * was incorrectly used instead of xmlAutomataNewTransition2()
12557 * (seems like a copy&paste bug from the XML_SCHEMA_TYPE_ALL
12558 * section in xmlSchemaBuildAContentModel() ).
12559 * TODO: Check if xmlAutomataNewOnceTrans2() was instead
12560 * intended for the above "counter" section originally. I.e.,
12561 * check xs:all with subst-groups.
12562 *
12563 * tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12564 * member->name, member->targetNamespace,
12565 * 1, 1, member);
12566 */
12567 tmp = xmlAutomataNewTransition2(pctxt->am, start, NULL,
12568 member->name, member->targetNamespace, member);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012569 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012570 }
12571 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012572 xmlAutomataStatePtr hop;
12573 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12574 UNBOUNDED : particle->maxOccurs - 1;
12575 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12576
12577 counter =
12578 xmlAutomataNewCounter(pctxt->am, minOccurs,
12579 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012580 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012581
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012582 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012583 xmlAutomataNewTransition2(pctxt->am,
12584 start, NULL,
12585 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012586 hop);
12587 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012588 * Add subst. group members.
12589 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012590 for (i = 0; i < substGroup->members->nbItems; i++) {
12591 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12592 xmlAutomataNewEpsilon(pctxt->am,
12593 xmlAutomataNewTransition2(pctxt->am,
12594 start, NULL,
12595 member->name, member->targetNamespace, member),
12596 hop);
12597 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012598 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12599 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12600 }
12601 if (particle->minOccurs == 0)
12602 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012603 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012604}
12605
12606static void
12607xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12608 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012609{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012610 if (((xmlSchemaElementPtr) particle->children)->flags &
12611 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012612 /*
12613 * Substitution groups.
12614 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012615 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012616 } else {
12617 xmlSchemaElementPtr elemDecl;
12618 xmlAutomataStatePtr start;
12619
12620 elemDecl = (xmlSchemaElementPtr) particle->children;
12621
12622 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012623 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012624 if (particle->maxOccurs == 1) {
12625 start = ctxt->state;
12626 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012627 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12628 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12629 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012630 /* Special case. */
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012631 start = ctxt->state;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012632 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012633 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12634 ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
12635 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012636 } else {
12637 int counter;
12638 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12639 UNBOUNDED : particle->maxOccurs - 1;
12640 int minOccurs = particle->minOccurs < 1 ?
12641 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012642
12643 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012644 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12645 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12646 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12647 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12648 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12649 NULL, counter);
12650 }
12651 if (particle->minOccurs == 0)
12652 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12653 }
12654}
12655
Daniel Veillard4255d502002-04-16 15:50:10 +000012656/**
12657 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012658 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012659 * @particle: the particle component
12660 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012661 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012662 * Create the automaton for the {content type} of a complex type.
12663 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012664 */
12665static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012666xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012667 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012668{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012669 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012670 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012671 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012672 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012673 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012674 /*
12675 * Just return in this case. A missing "term" of the particle
12676 * might arise due to an invalid "term" component.
12677 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012678 return;
12679 }
12680
12681 switch (particle->children->type) {
12682 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012683 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012684 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012685 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012686
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012687 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012688
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012689 start = pctxt->state;
12690 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012691
12692 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012693 if (wild->any == 1) {
12694 /*
12695 * We need to add both transitions:
12696 *
12697 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012698 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012699 pctxt->state =
12700 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012701 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012702 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012703 /*
12704 * 2. the {"*"} for elements in no namespace.
12705 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 pctxt->state =
12707 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012708 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012709 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012710
12711 } else if (wild->nsSet != NULL) {
12712 ns = wild->nsSet;
12713 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012714 pctxt->state = start;
12715 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12716 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12717 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012718 ns = ns->next;
12719 } while (ns != NULL);
12720
12721 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012722 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12723 start, end, BAD_CAST "*", wild->negNsSet->value,
12724 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012725 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012726 } else {
12727 int counter;
12728 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012729 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012730 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012731 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012732 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012733
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012734 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12735 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012736 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012737 pctxt->state =
12738 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012739 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012740 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12741 pctxt->state =
12742 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012743 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012744 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012745 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012746 ns = wild->nsSet;
12747 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012748 pctxt->state =
12749 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012750 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012751 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012752 ns = ns->next;
12753 } while (ns != NULL);
12754
12755 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012756 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012757 start, hop, BAD_CAST "*", wild->negNsSet->value,
12758 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012759 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012760 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12761 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012762 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012763 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012764 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012765 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012766 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012767 break;
12768 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012769 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012770 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012771 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012772 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012773 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012774
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012775 /*
12776 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012777 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012778 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012779 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12780 sub = particle->children->children;
12781 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012782 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012783 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012784 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012785 }
12786 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012787 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012788
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012789 if (particle->maxOccurs >= UNBOUNDED) {
12790 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012791 xmlAutomataStatePtr tmp;
12792 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012793
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012794 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012795 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012796 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012797
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012798 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012799 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012800
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012801 sub = particle->children->children;
12802 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012803 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012804 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012805 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012806 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012807 tmp = pctxt->state;
12808 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012809 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012810 pctxt->state =
12811 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012812 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012813
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012814 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012815 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012816 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012817 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012818
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012819 sub = particle->children->children;
12820 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012821 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012822 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012823 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012824 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012825 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012826 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012827 /*
12828 * epsilon needed to block previous trans from
12829 * being allowed to enter back from another
12830 * construct
12831 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012832 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12833 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012834 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012835 xmlAutomataNewEpsilon(pctxt->am,
12836 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012837 }
12838 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012839 } else if ((particle->maxOccurs > 1)
12840 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012841 xmlAutomataStatePtr tmp;
12842 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012843
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012844 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012845 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012846 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012847
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012848 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012849 particle->minOccurs - 1,
12850 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012851
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012852 sub = particle->children->children;
12853 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012854 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012855 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012856 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012857 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012858 tmp = pctxt->state;
12859 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012860 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012861 pctxt->state =
12862 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012863 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012864 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012865 xmlAutomataNewEpsilon(pctxt->am,
12866 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012867 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012868 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012869 sub = particle->children->children;
12870 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012871 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012872 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012873 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012874 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012875 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012876 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12877 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012878 }
12879 }
12880 }
12881 break;
12882 }
12883 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012884 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012885 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012886
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012887 start = pctxt->state;
12888 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012889
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012890 /*
12891 * iterate over the subtypes and remerge the end with an
12892 * epsilon transition
12893 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012894 if (particle->maxOccurs == 1) {
12895 sub = particle->children->children;
12896 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012897 pctxt->state = start;
12898 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012899 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012900 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012901 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012902 }
12903 } else {
12904 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012905 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012906 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12907 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012908 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012909 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012910
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012911 /*
12912 * use a counter to keep track of the number of transtions
12913 * which went through the choice.
12914 */
12915 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012916 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12917 hop = xmlAutomataNewState(pctxt->am);
12918 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012919
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012920 sub = particle->children->children;
12921 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012922 pctxt->state = base;
12923 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012924 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012925 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012926 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012927 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012928 xmlAutomataNewEpsilon(pctxt->am, start, base);
12929 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12930 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012931 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012932 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012933 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012934 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012935 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012936 break;
12937 }
12938 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012939 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012940 xmlSchemaParticlePtr sub;
12941 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012942 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012943
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012944 sub = (xmlSchemaParticlePtr) particle->children->children;
12945 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012946 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012947 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012948 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012949 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012950
12951 elemDecl = (xmlSchemaElementPtr) sub->children;
12952 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012953 PERROR_INT("xmlSchemaBuildAContentModel",
12954 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012955 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012956 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012957 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012958 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012959 * {particles} of the group must be 0 or 1; this is
12960 * already ensured during the parse of the content of
12961 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012962 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012963 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12964 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012965
Daniel Veillarda980bef2005-07-18 21:34:03 +000012966 /*
12967 * This is an abstract group, we need to share
12968 * the same counter for all the element transitions
12969 * derived from the group
12970 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012971 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012972 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012973 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12974 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012975 } else {
12976 if ((sub->minOccurs == 1) &&
12977 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012978 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12979 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012980 elemDecl->name,
12981 elemDecl->targetNamespace,
12982 1, 1, elemDecl);
12983 } else if ((sub->minOccurs == 0) &&
12984 (sub->maxOccurs == 1)) {
12985
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012986 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12987 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012988 elemDecl->name,
12989 elemDecl->targetNamespace,
12990 0,
12991 1,
12992 elemDecl);
12993 }
12994 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012995 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012996 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012997 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012998 pctxt->state =
12999 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013000 break;
13001 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013002 case XML_SCHEMA_TYPE_GROUP:
13003 /*
13004 * If we hit a model group definition, then this means that
13005 * it was empty, thus was not substituted for the containing
13006 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013007 * TODO: But the group should be substituted and not occur at
13008 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013009 */
13010 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013011 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013012 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
13013 "xmlSchemaBuildAContentModel",
13014 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013015 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013016 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013017 }
13018}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013019
Daniel Veillard4255d502002-04-16 15:50:10 +000013020/**
13021 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000013022 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013023 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013024 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000013025 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013026 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000013027 */
13028static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013029xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013030 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013031{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013032 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13033 (type->contModel != NULL) ||
13034 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13035 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013036 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013037
13038#ifdef DEBUG_CONTENT
13039 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013040 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013041#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013042 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013043 ctxt->am = xmlNewAutomata();
13044 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013045 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013046 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013047 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013048 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013049 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013050 /*
13051 * Build the automaton.
13052 */
13053 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013054 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013055 type->contModel = xmlAutomataCompile(ctxt->am);
13056 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013057 xmlSchemaPCustomErr(ctxt,
13058 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013059 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013060 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013061 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013062 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013063 XML_SCHEMAP_NOT_DETERMINISTIC,
13064 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013065 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013066 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013067 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013068#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013069 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013070 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013071 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013072#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013073 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013074 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013075 xmlFreeAutomata(ctxt->am);
13076 ctxt->am = NULL;
13077}
13078
13079/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013080 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013081 * @elem: the schema element context
13082 * @ctxt: the schema parser context
13083 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013084 * Resolves the references of an element declaration
13085 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013086 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013087 */
13088static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013089xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13090 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013091{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013092 if ((ctxt == NULL) || (elemDecl == NULL) ||
13093 ((elemDecl != NULL) &&
13094 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013095 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013096 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013097
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013098 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013099 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013100
13101 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013102 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013103 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013104 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013105 elemDecl->namedTypeNs);
13106 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013107 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013108 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013109 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013110 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013111 XML_SCHEMA_TYPE_BASIC, "type definition");
13112 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013113 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013114 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013115 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013116 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013117
Daniel Veillardc0826a72004-08-10 14:17:33 +000013118 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013119 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013120 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013121 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013122 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13123 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013124 if (substHead == NULL) {
13125 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013126 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013127 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013128 "substitutionGroup", elemDecl->substGroup,
13129 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013130 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013131 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013132 /*
13133 * Set the "substitution group affiliation".
13134 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013135 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013136 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013137 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013138 * The type definitions is set to:
13139 * SPEC "...the {type definition} of the element
13140 * declaration ·resolved· to by the ·actual value·
13141 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013142 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013143 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013144 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013145 }
13146 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013147 /*
13148 * SPEC "The definition of anyType serves as the default type definition
13149 * for element declarations whose XML representation does not specify one."
13150 */
13151 if ((elemDecl->subtypes == NULL) &&
13152 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013153 (elemDecl->substGroup == NULL))
13154 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013155}
13156
13157/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013158 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013159 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013160 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013161 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013162 * Checks and builds the "member type definitions" property of the union
13163 * simple type. This handles part (1), part (2) is done in
13164 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13165 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013166 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013167 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013168static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013169xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13170 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013171{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013172
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013173 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013174 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013175
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013176 /*
13177 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13178 * define the explicit members as the type definitions ·resolved·
13179 * to by the items in the ·actual value· of the memberTypes [attribute],
13180 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013181 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013182 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013183 /*
13184 * Resolve references.
13185 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013186 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013187 lastLink = NULL;
13188 while (link != NULL) {
13189 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013190
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013191 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13192 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13193
13194 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013195 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013196 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013197 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013198 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13199 /*
13200 * Remove the member type link.
13201 */
13202 if (lastLink == NULL)
13203 type->memberTypes = link->next;
13204 else
13205 lastLink->next = link->next;
13206 newLink = link;
13207 link = link->next;
13208 xmlFree(newLink);
13209 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013210 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013211 lastLink = link;
13212 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013213 }
13214 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013215 /*
13216 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013217 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013218 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013219 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013220 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13221 if (link == NULL) {
13222 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13223 return (-1);
13224 }
13225 link->type = memberType;
13226 link->next = NULL;
13227 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013228 type->memberTypes = link;
13229 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013230 lastLink->next = link;
13231 lastLink = link;
13232 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013233 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013234 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013235}
13236
Daniel Veillard4255d502002-04-16 15:50:10 +000013237/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013238 * xmlSchemaIsDerivedFromBuiltInType:
13239 * @ctxt: the schema parser context
13240 * @type: the type definition
13241 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013242 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013243 *
13244 * Returns 1 if the type has the given value type, or
13245 * is derived from such a type.
13246 */
William M. Brack803812b2004-06-03 02:11:24 +000013247static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013248xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013249{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013250 if (type == NULL)
13251 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013252 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013253 return (0);
13254 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13255 if (type->builtInType == valType)
13256 return(1);
13257 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13258 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13259 return (0);
13260 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013261 }
13262 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013263}
13264
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013265#if 0
13266/**
13267 * xmlSchemaIsDerivedFromBuiltInType:
13268 * @ctxt: the schema parser context
13269 * @type: the type definition
13270 * @valType: the value type
13271 *
13272 *
13273 * Returns 1 if the type has the given value type, or
13274 * is derived from such a type.
13275 */
13276static int
13277xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13278{
13279 if (type == NULL)
13280 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013281 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013282 return (0);
13283 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13284 if (type->builtInType == valType)
13285 return(1);
13286 return (0);
13287 } else
13288 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13289
13290 return (0);
13291}
13292#endif
13293
13294static xmlSchemaTypePtr
13295xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13296{
13297 if (type == NULL)
13298 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013299 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013300 return (NULL);
13301 if (type->type == XML_SCHEMA_TYPE_BASIC)
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013302 return(type);
13303 return(xmlSchemaQueryBuiltInType(type->subtypes));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013304}
13305
Daniel Veillard3646d642004-06-02 19:19:14 +000013306/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013307 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013308 * @type: the simpleType definition
13309 *
13310 * Returns the primitive type of the given type or
13311 * NULL in case of error.
13312 */
13313static xmlSchemaTypePtr
13314xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13315{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013316
Daniel Veillard01fa6152004-06-29 17:04:39 +000013317 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013318 /*
13319 * Note that anySimpleType is actually not a primitive type
13320 * but we need that here.
13321 */
13322 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13323 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013324 return (type);
13325 type = type->baseType;
13326 }
13327
13328 return (NULL);
13329}
13330
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013331#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013332/**
13333 * xmlSchemaGetBuiltInTypeAncestor:
13334 * @type: the simpleType definition
13335 *
13336 * Returns the primitive type of the given type or
13337 * NULL in case of error.
13338 */
13339static xmlSchemaTypePtr
13340xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13341{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013342 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013343 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013344 while (type != NULL) {
13345 if (type->type == XML_SCHEMA_TYPE_BASIC)
13346 return (type);
13347 type = type->baseType;
13348 }
13349
13350 return (NULL);
13351}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013352#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013353
Daniel Veillard01fa6152004-06-29 17:04:39 +000013354/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013355 * xmlSchemaCloneWildcardNsConstraints:
13356 * @ctxt: the schema parser context
13357 * @dest: the destination wildcard
13358 * @source: the source wildcard
13359 *
13360 * Clones the namespace constraints of source
13361 * and assignes them to dest.
13362 * Returns -1 on internal error, 0 otherwise.
13363 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013364static int
13365xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013366 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013367 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013368{
13369 xmlSchemaWildcardNsPtr cur, tmp, last;
13370
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013371 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013372 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013373 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013374 cur = source->nsSet;
13375 last = NULL;
13376 while (cur != NULL) {
13377 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13378 if (tmp == NULL)
13379 return(-1);
13380 tmp->value = cur->value;
13381 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013382 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013383 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013384 last->next = tmp;
13385 last = tmp;
13386 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013387 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013388 if (dest->negNsSet != NULL)
13389 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013390 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013391 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13392 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013393 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013394 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013395 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013396 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013397 return(0);
13398}
13399
Daniel Veillard50355f02004-06-08 17:52:16 +000013400/**
13401 * xmlSchemaUnionWildcards:
13402 * @ctxt: the schema parser context
13403 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013404 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013405 *
13406 * Unions the namespace constraints of the given wildcards.
13407 * @completeWild will hold the resulting union.
13408 * Returns a positive error code on failure, -1 in case of an
13409 * internal error, 0 otherwise.
13410 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013411static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013412xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013413 xmlSchemaWildcardPtr completeWild,
13414 xmlSchemaWildcardPtr curWild)
13415{
13416 xmlSchemaWildcardNsPtr cur, curB, tmp;
13417
13418 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013419 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013420 * value.
13421 */
13422 if ((completeWild->any == curWild->any) &&
13423 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13424 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013425
Daniel Veillard3646d642004-06-02 19:19:14 +000013426 if ((completeWild->negNsSet == NULL) ||
13427 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013428
Daniel Veillard3646d642004-06-02 19:19:14 +000013429 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013430 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013431
13432 /*
13433 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013434 */
13435 cur = completeWild->nsSet;
13436 while (cur != NULL) {
13437 found = 0;
13438 curB = curWild->nsSet;
13439 while (curB != NULL) {
13440 if (cur->value == curB->value) {
13441 found = 1;
13442 break;
13443 }
13444 curB = curB->next;
13445 }
13446 if (!found)
13447 break;
13448 cur = cur->next;
13449 }
13450 if (found)
13451 return(0);
13452 } else
13453 return(0);
13454 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013455 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013456 /*
13457 * 2 If either O1 or O2 is any, then any must be the value
13458 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013459 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013460 if (completeWild->any == 0) {
13461 completeWild->any = 1;
13462 if (completeWild->nsSet != NULL) {
13463 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13464 completeWild->nsSet = NULL;
13465 }
13466 if (completeWild->negNsSet != NULL) {
13467 xmlFree(completeWild->negNsSet);
13468 completeWild->negNsSet = NULL;
13469 }
13470 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013471 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013472 }
13473 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013474 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013475 * then the union of those sets must be the value.
13476 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013477 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013478 int found;
13479 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013480
Daniel Veillard3646d642004-06-02 19:19:14 +000013481 cur = curWild->nsSet;
13482 start = completeWild->nsSet;
13483 while (cur != NULL) {
13484 found = 0;
13485 curB = start;
13486 while (curB != NULL) {
13487 if (cur->value == curB->value) {
13488 found = 1;
13489 break;
13490 }
13491 curB = curB->next;
13492 }
13493 if (!found) {
13494 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013495 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013496 return (-1);
13497 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013498 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013499 completeWild->nsSet = tmp;
13500 }
13501 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013502 }
13503
Daniel Veillard3646d642004-06-02 19:19:14 +000013504 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013505 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013506 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013507 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013508 * or ·absent·), then a pair of not and ·absent· must be the value.
13509 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013510 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013511 (curWild->negNsSet != NULL) &&
13512 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13513 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013514
13515 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013516 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013517 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013518 * 5.
13519 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013520 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013521 (completeWild->negNsSet->value != NULL) &&
13522 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013523 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013524 (curWild->negNsSet->value != NULL) &&
13525 (completeWild->nsSet != NULL))) {
13526
13527 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013528
Daniel Veillard3646d642004-06-02 19:19:14 +000013529 if (completeWild->nsSet != NULL) {
13530 cur = completeWild->nsSet;
13531 curB = curWild->negNsSet;
13532 } else {
13533 cur = curWild->nsSet;
13534 curB = completeWild->negNsSet;
13535 }
13536 nsFound = 0;
13537 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013538 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013539 absentFound = 1;
13540 else if (cur->value == curB->value)
13541 nsFound = 1;
13542 if (nsFound && absentFound)
13543 break;
13544 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013545 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013546
13547 if (nsFound && absentFound) {
13548 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013549 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013550 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013551 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013552 completeWild->any = 1;
13553 if (completeWild->nsSet != NULL) {
13554 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13555 completeWild->nsSet = NULL;
13556 }
13557 if (completeWild->negNsSet != NULL) {
13558 xmlFree(completeWild->negNsSet);
13559 completeWild->negNsSet = NULL;
13560 }
13561 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013562 /*
13563 * 5.2 If the set S includes the negated namespace name
13564 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013565 * be the value.
13566 */
13567 if (completeWild->nsSet != NULL) {
13568 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13569 completeWild->nsSet = NULL;
13570 }
13571 if (completeWild->negNsSet == NULL) {
13572 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13573 if (completeWild->negNsSet == NULL)
13574 return (-1);
13575 }
13576 completeWild->negNsSet->value = NULL;
13577 } else if ((!nsFound) && absentFound) {
13578 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013579 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013580 * namespace name, then the union is not expressible.
13581 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013582 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013583 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013584 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013585 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013586 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013587 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013588 /*
13589 * 5.4 If the set S does not include either the negated namespace
13590 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013591 * and a namespace name must be the value.
13592 */
13593 if (completeWild->negNsSet == NULL) {
13594 if (completeWild->nsSet != NULL) {
13595 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13596 completeWild->nsSet = NULL;
13597 }
13598 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13599 if (completeWild->negNsSet == NULL)
13600 return (-1);
13601 completeWild->negNsSet->value = curWild->negNsSet->value;
13602 }
13603 }
13604 return (0);
13605 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013606 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013607 * 6.
13608 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013609 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013610 (completeWild->negNsSet->value == NULL) &&
13611 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013612 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013613 (curWild->negNsSet->value == NULL) &&
13614 (completeWild->nsSet != NULL))) {
13615
13616 if (completeWild->nsSet != NULL) {
13617 cur = completeWild->nsSet;
13618 } else {
13619 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013620 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013621 while (cur != NULL) {
13622 if (cur->value == NULL) {
13623 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013624 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013625 * value.
13626 */
13627 completeWild->any = 1;
13628 if (completeWild->nsSet != NULL) {
13629 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13630 completeWild->nsSet = NULL;
13631 }
13632 if (completeWild->negNsSet != NULL) {
13633 xmlFree(completeWild->negNsSet);
13634 completeWild->negNsSet = NULL;
13635 }
13636 return (0);
13637 }
13638 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013639 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013640 if (completeWild->negNsSet == NULL) {
13641 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013642 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013643 * and ·absent· must be the value.
13644 */
13645 if (completeWild->nsSet != NULL) {
13646 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13647 completeWild->nsSet = NULL;
13648 }
13649 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13650 if (completeWild->negNsSet == NULL)
13651 return (-1);
13652 completeWild->negNsSet->value = NULL;
13653 }
13654 return (0);
13655 }
13656 return (0);
13657
13658}
13659
Daniel Veillard50355f02004-06-08 17:52:16 +000013660/**
13661 * xmlSchemaIntersectWildcards:
13662 * @ctxt: the schema parser context
13663 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013664 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013665 *
13666 * Intersects the namespace constraints of the given wildcards.
13667 * @completeWild will hold the resulting intersection.
13668 * Returns a positive error code on failure, -1 in case of an
13669 * internal error, 0 otherwise.
13670 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013671static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013672xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013673 xmlSchemaWildcardPtr completeWild,
13674 xmlSchemaWildcardPtr curWild)
13675{
William M. Brack803812b2004-06-03 02:11:24 +000013676 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013677
13678 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013679 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013680 * value.
13681 */
13682 if ((completeWild->any == curWild->any) &&
13683 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13684 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013685
Daniel Veillard3646d642004-06-02 19:19:14 +000013686 if ((completeWild->negNsSet == NULL) ||
13687 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013688
Daniel Veillard3646d642004-06-02 19:19:14 +000013689 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013690 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013691
13692 /*
13693 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013694 */
13695 cur = completeWild->nsSet;
13696 while (cur != NULL) {
13697 found = 0;
13698 curB = curWild->nsSet;
13699 while (curB != NULL) {
13700 if (cur->value == curB->value) {
13701 found = 1;
13702 break;
13703 }
13704 curB = curB->next;
13705 }
13706 if (!found)
13707 break;
13708 cur = cur->next;
13709 }
13710 if (found)
13711 return(0);
13712 } else
13713 return(0);
13714 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013715 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013716 /*
13717 * 2 If either O1 or O2 is any, then the other must be the value.
13718 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013719 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013720 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013721 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013722 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013723 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013724 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013725 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13726 * name or ·absent·) and the other is a set of (namespace names or
13727 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013728 * the set, minus ·absent· if it was in the set, must be the value.
13729 */
13730 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13731 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13732 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013733
Daniel Veillard3646d642004-06-02 19:19:14 +000013734 if (completeWild->nsSet == NULL) {
13735 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013736 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013737 return(-1);
13738 } else
13739 neg = curWild->negNsSet->value;
13740 /*
13741 * Remove absent and negated.
13742 */
13743 prev = NULL;
13744 cur = completeWild->nsSet;
13745 while (cur != NULL) {
13746 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013747 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013748 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013749 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013750 prev->next = cur->next;
13751 xmlFree(cur);
13752 break;
13753 }
13754 prev = cur;
13755 cur = cur->next;
13756 }
13757 if (neg != NULL) {
13758 prev = NULL;
13759 cur = completeWild->nsSet;
13760 while (cur != NULL) {
13761 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013762 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013763 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013764 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013765 prev->next = cur->next;
13766 xmlFree(cur);
13767 break;
13768 }
13769 prev = cur;
13770 cur = cur->next;
13771 }
13772 }
13773
13774 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013775 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013776 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013777 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013778 * then the intersection of those sets must be the value.
13779 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013780 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013781 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013782
Daniel Veillard3646d642004-06-02 19:19:14 +000013783 cur = completeWild->nsSet;
13784 prev = NULL;
13785 while (cur != NULL) {
13786 found = 0;
13787 curB = curWild->nsSet;
13788 while (curB != NULL) {
13789 if (cur->value == curB->value) {
13790 found = 1;
13791 break;
13792 }
13793 curB = curB->next;
13794 }
13795 if (!found) {
13796 if (prev == NULL)
13797 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013798 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013799 prev->next = cur->next;
13800 tmp = cur->next;
13801 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013802 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013803 continue;
13804 }
13805 prev = cur;
13806 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013807 }
13808
Daniel Veillard3646d642004-06-02 19:19:14 +000013809 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013810 }
13811 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013812 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013813 */
13814 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013815 (curWild->negNsSet != NULL) &&
13816 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013817 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013818 (curWild->negNsSet->value != NULL)) {
13819
13820 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013821 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013822 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013823 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013824 }
13825 /*
13826 * 6 If the one is a negation of a namespace name and the other
13827 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013828 * of a namespace name must be the value.
13829 */
13830 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13831 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013832 (completeWild->negNsSet->value == NULL)) {
13833 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013834 }
13835 return(0);
13836}
13837
Daniel Veillard50355f02004-06-08 17:52:16 +000013838/**
13839 * xmlSchemaIsWildcardNsConstraintSubset:
13840 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013841 * @sub: the first wildcard
13842 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013843 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013844 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13845 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013846 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013847 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013848 */
13849static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013850xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13851 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013852{
Daniel Veillard50355f02004-06-08 17:52:16 +000013853 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013854 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013855 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013856 if (super->any)
13857 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013858 /*
13859 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13860 * 2.2 super must be a pair of not and the same value.
13861 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013862 if ((sub->negNsSet != NULL) &&
13863 (super->negNsSet != NULL) &&
13864 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013865 return (0);
13866 /*
13867 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013868 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013869 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013870 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013871 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013872 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013873 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013874 xmlSchemaWildcardNsPtr cur, curB;
13875 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013876
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013877 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013878 while (cur != NULL) {
13879 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013880 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013881 while (curB != NULL) {
13882 if (cur->value == curB->value) {
13883 found = 1;
13884 break;
13885 }
13886 curB = curB->next;
13887 }
13888 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013889 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013890 cur = cur->next;
13891 }
13892 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013893 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013894 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013895 xmlSchemaWildcardNsPtr cur;
13896 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013897 * 3.2.2 super must be a pair of not and a namespace name or
13898 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013899 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013900 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013901 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013902 if (cur->value == super->negNsSet->value)
13903 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013904 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013905 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013906 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013907 }
13908 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013909 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013910}
13911
Daniel Veillard50355f02004-06-08 17:52:16 +000013912static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013913xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013914 int *fixed,
13915 const xmlChar **value,
13916 xmlSchemaValPtr *val)
13917{
13918 *fixed = 0;
13919 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013920 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013921 *val = NULL;
13922
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013923 if (attruse->defValue != NULL) {
13924 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013925 if (val != NULL)
13926 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013927 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013928 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013929 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013930 } else if ((attruse->attrDecl != NULL) &&
13931 (attruse->attrDecl->defValue != NULL)) {
13932 *value = attruse->attrDecl->defValue;
13933 if (val != NULL)
13934 *val = attruse->attrDecl->defVal;
13935 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013936 *fixed = 1;
13937 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013938 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013939 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013940}
Daniel Veillard3646d642004-06-02 19:19:14 +000013941/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013942 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013943 * @wild: the wildcard
13944 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013945 *
13946 * Validation Rule: Wildcard allows Namespace Name
13947 * (cvc-wildcard-namespace)
13948 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013949 * Returns 0 if the given namespace matches the wildcard,
13950 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013951 */
13952static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013953xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13954 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013955{
13956 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013957 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013958
13959 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013960 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013961 else if (wild->nsSet != NULL) {
13962 xmlSchemaWildcardNsPtr cur;
13963
13964 cur = wild->nsSet;
13965 while (cur != NULL) {
13966 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013967 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013968 cur = cur->next;
13969 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013970 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013971 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013972 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013973
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013974 return(1);
13975}
13976
13977#define XML_SCHEMA_ACTION_DERIVE 0
13978#define XML_SCHEMA_ACTION_REDEFINE 1
13979
13980#define WXS_ACTION_STR(a) \
13981((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13982
13983/*
13984* Schema Component Constraint:
13985* Derivation Valid (Restriction, Complex)
13986* derivation-ok-restriction (2) - (4)
13987*
13988* ATTENTION:
13989* In XML Schema 1.1 this will be:
13990* Validation Rule:
13991* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13992*
13993*/
13994static int
13995xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13996 int action,
13997 xmlSchemaBasicItemPtr item,
13998 xmlSchemaBasicItemPtr baseItem,
13999 xmlSchemaItemListPtr uses,
14000 xmlSchemaItemListPtr baseUses,
14001 xmlSchemaWildcardPtr wild,
14002 xmlSchemaWildcardPtr baseWild)
14003{
14004 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014005 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014006 const xmlChar *bEffValue;
14007 int effFixed;
14008
14009 if (uses != NULL) {
14010 for (i = 0; i < uses->nbItems; i++) {
14011 cur = uses->items[i];
14012 found = 0;
14013 if (baseUses == NULL)
14014 goto not_found;
14015 for (j = 0; j < baseUses->nbItems; j++) {
14016 bcur = baseUses->items[j];
14017 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14018 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14019 (WXS_ATTRUSE_DECL_TNS(cur) ==
14020 WXS_ATTRUSE_DECL_TNS(bcur)))
14021 {
14022 /*
14023 * (2.1) "If there is an attribute use in the {attribute
14024 * uses} of the {base type definition} (call this B) whose
14025 * {attribute declaration} has the same {name} and {target
14026 * namespace}, then all of the following must be true:"
14027 */
14028 found = 1;
14029
14030 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14031 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14032 {
14033 xmlChar *str = NULL;
14034 /*
14035 * (2.1.1) "one of the following must be true:"
14036 * (2.1.1.1) "B's {required} is false."
14037 * (2.1.1.2) "R's {required} is true."
14038 */
14039 xmlSchemaPAttrUseErr4(pctxt,
14040 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14041 WXS_ITEM_NODE(item), item, cur,
14042 "The 'optional' attribute use is inconsistent "
14043 "with the corresponding 'required' attribute use of "
14044 "the %s %s",
14045 WXS_ACTION_STR(action),
14046 xmlSchemaGetComponentDesignation(&str, baseItem),
14047 NULL, NULL);
14048 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014049 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014050 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014051 WXS_ATTRUSE_TYPEDEF(cur),
14052 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14053 {
14054 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14055
14056 /*
14057 * SPEC (2.1.2) "R's {attribute declaration}'s
14058 * {type definition} must be validly derived from
14059 * B's {type definition} given the empty set as
14060 * defined in Type Derivation OK (Simple) (§3.14.6)."
14061 */
14062 xmlSchemaPAttrUseErr4(pctxt,
14063 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14064 WXS_ITEM_NODE(item), item, cur,
14065 "The attribute declaration's %s "
14066 "is not validly derived from "
14067 "the corresponding %s of the "
14068 "attribute declaration in the %s %s",
14069 xmlSchemaGetComponentDesignation(&strA,
14070 WXS_ATTRUSE_TYPEDEF(cur)),
14071 xmlSchemaGetComponentDesignation(&strB,
14072 WXS_ATTRUSE_TYPEDEF(bcur)),
14073 WXS_ACTION_STR(action),
14074 xmlSchemaGetComponentDesignation(&strC, baseItem));
14075 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14076 FREE_AND_NULL(strA);
14077 FREE_AND_NULL(strB);
14078 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014079 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014080 } else {
14081 /*
14082 * 2.1.3 [Definition:] Let the effective value
14083 * constraint of an attribute use be its {value
14084 * constraint}, if present, otherwise its {attribute
14085 * declaration}'s {value constraint} .
14086 */
14087 xmlSchemaGetEffectiveValueConstraint(bcur,
14088 &effFixed, &bEffValue, NULL);
14089 /*
14090 * 2.1.3 ... one of the following must be true
14091 *
14092 * 2.1.3.1 B's ·effective value constraint· is
14093 * ·absent· or default.
14094 */
14095 if ((bEffValue != NULL) &&
14096 (effFixed == 1)) {
14097 const xmlChar *rEffValue = NULL;
14098
14099 xmlSchemaGetEffectiveValueConstraint(bcur,
14100 &effFixed, &rEffValue, NULL);
14101 /*
14102 * 2.1.3.2 R's ·effective value constraint· is
14103 * fixed with the same string as B's.
14104 * MAYBE TODO: Compare the computed values.
14105 * Hmm, it says "same string" so
14106 * string-equality might really be sufficient.
14107 */
14108 if ((effFixed == 0) ||
14109 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14110 {
14111 xmlChar *str = NULL;
14112
14113 xmlSchemaPAttrUseErr4(pctxt,
14114 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14115 WXS_ITEM_NODE(item), item, cur,
14116 "The effective value constraint of the "
14117 "attribute use is inconsistent with "
14118 "its correspondent in the %s %s",
14119 WXS_ACTION_STR(action),
14120 xmlSchemaGetComponentDesignation(&str,
14121 baseItem),
14122 NULL, NULL);
14123 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014124 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014125 }
14126 }
14127 }
14128 break;
14129 }
14130 }
14131not_found:
14132 if (!found) {
14133 /*
14134 * (2.2) "otherwise the {base type definition} must have an
14135 * {attribute wildcard} and the {target namespace} of the
14136 * R's {attribute declaration} must be ·valid· with respect
14137 * to that wildcard, as defined in Wildcard allows Namespace
14138 * Name (§3.10.4)."
14139 */
14140 if ((baseWild == NULL) ||
14141 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14142 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14143 {
14144 xmlChar *str = NULL;
14145
14146 xmlSchemaPAttrUseErr4(pctxt,
14147 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14148 WXS_ITEM_NODE(item), item, cur,
14149 "Neither a matching attribute use, "
14150 "nor a matching wildcard exists in the %s %s",
14151 WXS_ACTION_STR(action),
14152 xmlSchemaGetComponentDesignation(&str, baseItem),
14153 NULL, NULL);
14154 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014155 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014156 }
14157 }
14158 }
14159 }
14160 /*
14161 * SPEC derivation-ok-restriction (3):
14162 * (3) "For each attribute use in the {attribute uses} of the {base type
14163 * definition} whose {required} is true, there must be an attribute
14164 * use with an {attribute declaration} with the same {name} and
14165 * {target namespace} as its {attribute declaration} in the {attribute
14166 * uses} of the complex type definition itself whose {required} is true.
14167 */
14168 if (baseUses != NULL) {
14169 for (j = 0; j < baseUses->nbItems; j++) {
14170 bcur = baseUses->items[j];
14171 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14172 continue;
14173 found = 0;
14174 if (uses != NULL) {
14175 for (i = 0; i < uses->nbItems; i++) {
14176 cur = uses->items[i];
14177 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14178 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14179 (WXS_ATTRUSE_DECL_TNS(cur) ==
14180 WXS_ATTRUSE_DECL_TNS(bcur))) {
14181 found = 1;
14182 break;
14183 }
14184 }
14185 }
14186 if (!found) {
14187 xmlChar *strA = NULL, *strB = NULL;
14188
14189 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14190 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14191 NULL, item,
14192 "A matching attribute use for the "
14193 "'required' %s of the %s %s is missing",
14194 xmlSchemaGetComponentDesignation(&strA, bcur),
14195 WXS_ACTION_STR(action),
14196 xmlSchemaGetComponentDesignation(&strB, baseItem),
14197 NULL);
14198 FREE_AND_NULL(strA);
14199 FREE_AND_NULL(strB);
14200 }
14201 }
14202 }
14203 /*
14204 * derivation-ok-restriction (4)
14205 */
14206 if (wild != NULL) {
14207 /*
14208 * (4) "If there is an {attribute wildcard}, all of the
14209 * following must be true:"
14210 */
14211 if (baseWild == NULL) {
14212 xmlChar *str = NULL;
14213
14214 /*
14215 * (4.1) "The {base type definition} must also have one."
14216 */
14217 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14218 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14219 NULL, item,
14220 "The %s has an attribute wildcard, "
14221 "but the %s %s '%s' does not have one",
14222 WXS_ITEM_TYPE_NAME(item),
14223 WXS_ACTION_STR(action),
14224 WXS_ITEM_TYPE_NAME(baseItem),
14225 xmlSchemaGetComponentQName(&str, baseItem));
14226 FREE_AND_NULL(str);
14227 return(pctxt->err);
14228 } else if ((baseWild->any == 0) &&
14229 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14230 {
14231 xmlChar *str = NULL;
14232 /*
14233 * (4.2) "The complex type definition's {attribute wildcard}'s
14234 * {namespace constraint} must be a subset of the {base type
14235 * definition}'s {attribute wildcard}'s {namespace constraint},
14236 * as defined by Wildcard Subset (§3.10.6)."
14237 */
14238 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14239 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14240 NULL, item,
14241 "The attribute wildcard is not a valid "
14242 "subset of the wildcard in the %s %s '%s'",
14243 WXS_ACTION_STR(action),
14244 WXS_ITEM_TYPE_NAME(baseItem),
14245 xmlSchemaGetComponentQName(&str, baseItem),
14246 NULL);
14247 FREE_AND_NULL(str);
14248 return(pctxt->err);
14249 }
14250 /* 4.3 Unless the {base type definition} is the ·ur-type
14251 * definition·, the complex type definition's {attribute
14252 * wildcard}'s {process contents} must be identical to or
14253 * stronger than the {base type definition}'s {attribute
14254 * wildcard}'s {process contents}, where strict is stronger
14255 * than lax is stronger than skip.
14256 */
14257 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14258 (wild->processContents < baseWild->processContents)) {
14259 xmlChar *str = NULL;
14260 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14261 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14262 NULL, baseItem,
14263 "The {process contents} of the attribute wildcard is "
14264 "weaker than the one in the %s %s '%s'",
14265 WXS_ACTION_STR(action),
14266 WXS_ITEM_TYPE_NAME(baseItem),
14267 xmlSchemaGetComponentQName(&str, baseItem),
14268 NULL);
14269 FREE_AND_NULL(str)
14270 return(pctxt->err);
14271 }
14272 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014273 return(0);
14274}
14275
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014276
14277static int
14278xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14279 xmlSchemaBasicItemPtr item,
14280 xmlSchemaWildcardPtr *completeWild,
14281 xmlSchemaItemListPtr list,
14282 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014283/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014284 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014285 * @ctxt: the schema parser context
14286 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014287 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014288 *
14289 * Builds the wildcard and the attribute uses on the given complex type.
14290 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014291 *
14292 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14293 * strings, so recheck this if we start to hardcode some schemata, since
14294 * they might not be in the same dict.
14295 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014296 */
14297static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014298xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014299 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014300{
14301 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014302 xmlSchemaAttributeUsePtr use;
14303 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014304
Daniel Veillard01fa6152004-06-29 17:04:39 +000014305 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014306 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014307 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014308 return (-1);
14309 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014310 baseType = type->baseType;
14311 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014312 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014313 return(-1);
14314
14315 uses = type->attrUses;
14316 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014317 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014318 * Expand attribute group references. And build the 'complete'
14319 * wildcard, i.e. intersect multiple wildcards.
14320 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014321 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014322 if (uses != NULL) {
14323 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014324 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014325 * This one will transfer all attr. prohibitions
14326 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014327 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014328 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14329 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14330 pctxt->attrProhibs) == -1)
14331 {
14332 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14333 "failed to expand attributes");
14334 }
14335 if (pctxt->attrProhibs->nbItems != 0)
14336 prohibs = pctxt->attrProhibs;
14337 } else {
14338 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14339 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14340 NULL) == -1)
14341 {
14342 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14343 "failed to expand attributes");
14344 }
14345 }
14346 }
14347 /*
14348 * Inherit the attribute uses of the base type.
14349 */
14350 if (baseUses != NULL) {
14351 int i, j;
14352 xmlSchemaAttributeUseProhibPtr pro;
14353
14354 if (WXS_IS_RESTRICTION(type)) {
14355 int usesCount;
14356 xmlSchemaAttributeUsePtr tmp;
14357
14358 if (uses != NULL)
14359 usesCount = uses->nbItems;
14360 else
14361 usesCount = 0;
14362
14363 /* Restriction. */
14364 for (i = 0; i < baseUses->nbItems; i++) {
14365 use = baseUses->items[i];
14366 if (prohibs) {
14367 /*
14368 * Filter out prohibited uses.
14369 */
14370 for (j = 0; j < prohibs->nbItems; j++) {
14371 pro = prohibs->items[j];
14372 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14373 (WXS_ATTRUSE_DECL_TNS(use) ==
14374 pro->targetNamespace))
14375 {
14376 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014377 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014378 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014379 }
14380 if (usesCount) {
14381 /*
14382 * Filter out existing uses.
14383 */
14384 for (j = 0; j < usesCount; j++) {
14385 tmp = uses->items[j];
14386 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14387 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14388 (WXS_ATTRUSE_DECL_TNS(use) ==
14389 WXS_ATTRUSE_DECL_TNS(tmp)))
14390 {
14391 goto inherit_next;
14392 }
14393 }
14394 }
14395 if (uses == NULL) {
14396 type->attrUses = xmlSchemaItemListCreate();
14397 if (type->attrUses == NULL)
14398 goto exit_failure;
14399 uses = type->attrUses;
14400 }
14401 xmlSchemaItemListAddSize(uses, 2, use);
14402inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014403 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014404 } else {
14405 /* Extension. */
14406 for (i = 0; i < baseUses->nbItems; i++) {
14407 use = baseUses->items[i];
14408 if (uses == NULL) {
14409 type->attrUses = xmlSchemaItemListCreate();
14410 if (type->attrUses == NULL)
14411 goto exit_failure;
14412 uses = type->attrUses;
14413 }
14414 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14415 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014416 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014417 }
14418 /*
14419 * Shrink attr. uses.
14420 */
14421 if (uses) {
14422 if (uses->nbItems == 0) {
14423 xmlSchemaItemListFree(uses);
14424 type->attrUses = NULL;
14425 }
14426 /*
14427 * TODO: We could shrink the size of the array
14428 * to fit the actual number of items.
14429 */
14430 }
14431 /*
14432 * Compute the complete wildcard.
14433 */
14434 if (WXS_IS_EXTENSION(type)) {
14435 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014436 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014437 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14438 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014439 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014440 if (type->attributeWildcard != NULL) {
14441 /*
14442 * Union the complete wildcard with the base wildcard.
14443 * SPEC {attribute wildcard}
14444 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14445 * and {annotation} are those of the ·complete wildcard·,
14446 * and whose {namespace constraint} is the intensional union
14447 * of the {namespace constraint} of the ·complete wildcard·
14448 * and of the ·base wildcard·, as defined in Attribute
14449 * Wildcard Union (§3.10.6)."
14450 */
14451 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14452 baseType->attributeWildcard) == -1)
14453 goto exit_failure;
14454 } else {
14455 /*
14456 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14457 * then the ·base wildcard·."
14458 */
14459 type->attributeWildcard = baseType->attributeWildcard;
14460 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014461 } else {
14462 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014463 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14464 * ·complete wildcard"
14465 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014466 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014467 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014468 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014469 /*
14470 * SPEC {attribute wildcard}
14471 * (3.1) "If the <restriction> alternative is chosen, then the
14472 * ·complete wildcard·;"
14473 * NOOP
14474 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014475 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014476
Daniel Veillard3646d642004-06-02 19:19:14 +000014477 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014478
14479exit_failure:
14480 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014481}
14482
14483/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014484 * xmlSchemaTypeFinalContains:
14485 * @schema: the schema
14486 * @type: the type definition
14487 * @final: the final
14488 *
14489 * Evaluates if a type definition contains the given "final".
14490 * This does take "finalDefault" into account as well.
14491 *
14492 * Returns 1 if the type does containt the given "final",
14493 * 0 otherwise.
14494 */
14495static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014496xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014497{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014498 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014499 return (0);
14500 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014501 return (1);
14502 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014503 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014504}
14505
14506/**
14507 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14508 * @type: the Union Simple Type
14509 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014510 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014511 * returns NULL otherwise.
14512 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014513static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014514xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14515{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014516 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014517 if (type->memberTypes != NULL)
14518 return (type->memberTypes);
14519 else
14520 type = type->baseType;
14521 }
14522 return (NULL);
14523}
14524
14525/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014526 * xmlSchemaGetParticleTotalRangeMin:
14527 * @particle: the particle
14528 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014529 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014530 * (all and sequence) + (choice)
14531 *
14532 * Returns the minimun Effective Total Range.
14533 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014534static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014535xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014536{
14537 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014538 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014539 return (0);
14540 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014541 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014542 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014543 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014544
14545 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014546 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014547 while (part != NULL) {
14548 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14549 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014550 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014551 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014552 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014553 if (cur == 0)
14554 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014555 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014556 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014557 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014558 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014559 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014560 } else {
14561 /* <all> and <sequence> */
14562 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014563 xmlSchemaParticlePtr part =
14564 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014565
14566 if (part == NULL)
14567 return (0);
14568 do {
14569 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14570 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014571 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014572 else
14573 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014574 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014575 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014576 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014577 }
14578}
14579
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014580/**
14581 * xmlSchemaGetParticleTotalRangeMax:
14582 * @particle: the particle
14583 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014584 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014585 * (all and sequence) + (choice)
14586 *
14587 * Returns the maximum Effective Total Range.
14588 */
14589static int
14590xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14591{
14592 if ((particle->children == NULL) ||
14593 (particle->children->children == NULL))
14594 return (0);
14595 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14596 int max = -1, cur;
14597 xmlSchemaParticlePtr part =
14598 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014599
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014600 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14601 if (part->children == NULL)
14602 continue;
14603 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14604 (part->children->type == XML_SCHEMA_TYPE_ANY))
14605 cur = part->maxOccurs;
14606 else
14607 cur = xmlSchemaGetParticleTotalRangeMax(part);
14608 if (cur == UNBOUNDED)
14609 return (UNBOUNDED);
14610 if ((max < cur) || (max == -1))
14611 max = cur;
14612 }
14613 /* TODO: Handle overflows? */
14614 return (particle->maxOccurs * max);
14615 } else {
14616 /* <all> and <sequence> */
14617 int sum = 0, cur;
14618 xmlSchemaParticlePtr part =
14619 (xmlSchemaParticlePtr) particle->children->children;
14620
14621 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14622 if (part->children == NULL)
14623 continue;
14624 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14625 (part->children->type == XML_SCHEMA_TYPE_ANY))
14626 cur = part->maxOccurs;
14627 else
14628 cur = xmlSchemaGetParticleTotalRangeMax(part);
14629 if (cur == UNBOUNDED)
14630 return (UNBOUNDED);
14631 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14632 return (UNBOUNDED);
14633 sum += cur;
14634 }
14635 /* TODO: Handle overflows? */
14636 return (particle->maxOccurs * sum);
14637 }
14638}
14639
14640/**
14641 * xmlSchemaIsParticleEmptiable:
14642 * @particle: the particle
14643 *
14644 * Schema Component Constraint: Particle Emptiable
14645 * Checks whether the given particle is emptiable.
14646 *
14647 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014648 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014649static int
14650xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14651{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014652 /*
14653 * SPEC (1) "Its {min occurs} is 0."
14654 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014655 if ((particle == NULL) || (particle->minOccurs == 0) ||
14656 (particle->children == NULL))
14657 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014658 /*
14659 * SPEC (2) "Its {term} is a group and the minimum part of the
14660 * effective total range of that group, [...] is 0."
14661 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014662 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014663 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014664 return (1);
14665 }
14666 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014667}
14668
14669/**
14670 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014671 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014672 * @type: the derived simple type definition
14673 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014674 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014675 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014676 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014677 * Type Derivation OK (Simple) (cos-st-derived-OK)
14678 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014679 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014680 * derived from @baseType.
14681 *
14682 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014683 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014684static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014685xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014686 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014687 xmlSchemaTypePtr baseType,
14688 int subset)
14689{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014690 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014691 * 1 They are the same type definition.
14692 * TODO: The identy check might have to be more complex than this.
14693 */
14694 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014695 return (0);
14696 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014697 * 2.1 restriction is not in the subset, or in the {final}
14698 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014699 *
14700 * NOTE that this will be used also via "xsi:type".
14701 *
14702 * TODO: Revise this, it looks strange. How can the "type"
14703 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014704 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014705 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014706 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014707 return(-1);
14708 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014709 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014710 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014711 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014712 (xmlSchemaTypeFinalContains(type->baseType,
14713 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14714 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014715 }
14716 /* 2.2 */
14717 if (type->baseType == baseType) {
14718 /*
14719 * 2.2.1 D's ·base type definition· is B.
14720 */
14721 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014722 }
14723 /*
14724 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14725 * and is validly derived from B given the subset, as defined by this
14726 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014727 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014728 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014729 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014730 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014731 return (0);
14732 }
14733 /*
14734 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014735 * definition·.
14736 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014737 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14738 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014739 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014740 }
14741 /*
14742 * 2.2.4 B's {variety} is union and D is validly derived from a type
14743 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014744 * defined by this constraint.
14745 *
14746 * NOTE: This seems not to involve built-in types, since there is no
14747 * built-in Union Simple Type.
14748 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014749 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014750 xmlSchemaTypeLinkPtr cur;
14751
14752 cur = baseType->memberTypes;
14753 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014754 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014755 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014756 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014757 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014758 type, cur->type, subset) == 0)
14759 {
14760 /*
14761 * It just has to be validly derived from at least one
14762 * member-type.
14763 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014764 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014765 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014766 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014767 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014768 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014769 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14770}
14771
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014772/**
14773 * xmlSchemaCheckTypeDefCircularInternal:
14774 * @pctxt: the schema parser context
14775 * @ctxtType: the type definition
14776 * @ancestor: an ancestor of @ctxtType
14777 *
14778 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014779 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014780 *
14781 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14782 * circular, 0 otherwise.
14783 */
14784static int
14785xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14786 xmlSchemaTypePtr ctxtType,
14787 xmlSchemaTypePtr ancestor)
14788{
14789 int ret;
14790
14791 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14792 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014793
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014794 if (ctxtType == ancestor) {
14795 xmlSchemaPCustomErr(pctxt,
14796 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014797 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014798 "The definition is circular", NULL);
14799 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14800 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014801 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14802 /*
14803 * Avoid inifinite recursion on circular types not yet checked.
14804 */
14805 return (0);
14806 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014807 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14808 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14809 ancestor->baseType);
14810 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14811 return (ret);
14812}
14813
14814/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014815 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014816 * @item: the complex/simple type definition
14817 * @ctxt: the parser context
14818 * @name: the name
14819 *
14820 * Checks for circular type definitions.
14821 */
14822static void
14823xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014824 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014825{
14826 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014827 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14828 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014829 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014830 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14831 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014832}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014833
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014834/*
14835* Simple Type Definition Representation OK (src-simple-type) 4
14836*
14837* "4 Circular union type definition is disallowed. That is, if the
14838* <union> alternative is chosen, there must not be any entries in the
14839* memberTypes [attribute] at any depth which resolve to the component
14840* corresponding to the <simpleType>."
14841*
14842* Note that this should work on the *representation* of a component,
14843* thus assumes any union types in the member types not being yet
14844* substituted. At this stage we need the variety of the types
14845* to be already computed.
14846*/
14847static int
14848xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14849 xmlSchemaTypePtr ctxType,
14850 xmlSchemaTypeLinkPtr members)
14851{
14852 xmlSchemaTypeLinkPtr member;
14853 xmlSchemaTypePtr memberType;
14854
14855 member = members;
14856 while (member != NULL) {
14857 memberType = member->type;
14858 while ((memberType != NULL) &&
14859 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14860 if (memberType == ctxType) {
14861 xmlSchemaPCustomErr(pctxt,
14862 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014863 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014864 "The union type definition is circular", NULL);
14865 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14866 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014867 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014868 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14869 {
14870 int res;
14871 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14872 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14873 ctxType,
14874 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14875 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14876 if (res != 0)
14877 return(res);
14878 }
14879 memberType = memberType->baseType;
14880 }
14881 member = member->next;
14882 }
14883 return(0);
14884}
14885
14886static int
14887xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14888 xmlSchemaTypePtr type)
14889{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014890 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014891 return(0);
14892 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14893 type->memberTypes));
14894}
14895
Daniel Veillard01fa6152004-06-29 17:04:39 +000014896/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014897 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014898 * @item: the complex/simple type definition
14899 * @ctxt: the parser context
14900 * @name: the name
14901 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014902 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014903 */
14904static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014905xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014906 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014907{
14908 if (typeDef == NULL)
14909 return;
14910
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014911 /*
14912 * Resolve the base type.
14913 */
14914 if (typeDef->baseType == NULL) {
14915 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14916 typeDef->base, typeDef->baseNs);
14917 if (typeDef->baseType == NULL) {
14918 xmlSchemaPResCompAttrErr(ctxt,
14919 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014920 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014921 "base", typeDef->base, typeDef->baseNs,
14922 XML_SCHEMA_TYPE_SIMPLE, NULL);
14923 return;
14924 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014925 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014926 if (WXS_IS_SIMPLE(typeDef)) {
14927 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014928 /*
14929 * Resolve the memberTypes.
14930 */
14931 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14932 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014933 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014934 /*
14935 * Resolve the itemType.
14936 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014937 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14938
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014939 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014940 typeDef->base, typeDef->baseNs);
14941
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014942 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014943 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14944 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014945 typeDef->subtypes = NULL;
14946 xmlSchemaPResCompAttrErr(ctxt,
14947 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014948 WXS_BASIC_CAST typeDef, typeDef->node,
14949 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014950 XML_SCHEMA_TYPE_SIMPLE, NULL);
14951 }
14952 }
14953 return;
14954 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014955 }
14956 /*
14957 * The ball of letters below means, that if we have a particle
14958 * which has a QName-helper component as its {term}, we want
14959 * to resolve it...
14960 */
14961 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14962 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14963 XML_SCHEMA_TYPE_PARTICLE) &&
14964 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14965 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14966 XML_SCHEMA_EXTRA_QNAMEREF))
14967 {
14968 xmlSchemaQNameRefPtr ref =
14969 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14970 xmlSchemaModelGroupDefPtr groupDef;
14971
14972 /*
14973 * URGENT TODO: Test this.
14974 */
14975 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14976 /*
14977 * Resolve the MG definition reference.
14978 */
14979 groupDef =
14980 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14981 ref->itemType, ref->name, ref->targetNamespace);
14982 if (groupDef == NULL) {
14983 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14984 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14985 "ref", ref->name, ref->targetNamespace, ref->itemType,
14986 NULL);
14987 /* Remove the particle. */
14988 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14989 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14990 /* Remove the particle. */
14991 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14992 else {
14993 /*
14994 * Assign the MG definition's {model group} to the
14995 * particle's {term}.
14996 */
14997 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14998
14999 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
15000 /*
15001 * SPEC cos-all-limited (1.2)
15002 * "1.2 the {term} property of a particle with
15003 * {max occurs}=1 which is part of a pair which constitutes
15004 * the {content type} of a complex type definition."
15005 */
15006 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
15007 xmlSchemaCustomErr(ACTXT_CAST ctxt,
15008 /* TODO: error code */
15009 XML_SCHEMAP_COS_ALL_LIMITED,
15010 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
15011 "The particle's {max occurs} must be 1, since the "
15012 "reference resolves to an 'all' model group",
15013 NULL, NULL);
15014 }
15015 }
15016 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015017 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015018}
15019
15020
15021
15022/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015023 * xmlSchemaCheckSTPropsCorrect:
15024 * @ctxt: the schema parser context
15025 * @type: the simple type definition
15026 *
15027 * Checks st-props-correct.
15028 *
15029 * Returns 0 if the properties are correct,
15030 * if not, a positive error code and -1 on internal
15031 * errors.
15032 */
15033static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015034xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015035 xmlSchemaTypePtr type)
15036{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015037 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015038 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015039
Daniel Veillardc0826a72004-08-10 14:17:33 +000015040 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015041 /*
15042 * Schema Component Constraint: Simple Type Definition Properties Correct
15043 *
15044 * NOTE: This is somehow redundant, since we actually built a simple type
15045 * to have all the needed information; this acts as an self test.
15046 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015047 /* Base type: If the datatype has been ·derived· by ·restriction·
15048 * then the Simple Type Definition component from which it is ·derived·,
15049 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015050 */
15051 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015052 /*
15053 * TODO: Think about: "modulo the impact of Missing
15054 * Sub-components (§5.3)."
15055 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015056 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015057 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015058 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015059 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015060 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015061
Daniel Veillard01fa6152004-06-29 17:04:39 +000015062 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015063 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015064 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015065 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015066 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015067 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015068 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015069 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015070 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15071 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015072 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015073 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015074 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015075 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015076 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015077 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015078 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015079 "the simple ur-type definition as base type, not '%s'",
15080 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015081 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015082 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15083 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015084 /*
15085 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015086 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015087 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15088 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015089 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015090 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015091 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015092 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015093 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15094 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015095 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015096
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015097 /*
15098 * 3 The {final} of the {base type definition} must not contain restriction.
15099 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015100 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015101 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15102 xmlSchemaPCustomErr(ctxt,
15103 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015104 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015105 "The 'final' of its base type '%s' must not contain "
15106 "'restriction'",
15107 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015108 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015109 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015110 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015111
15112 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015113 * 2 All simple type definitions must be derived ultimately from the ·simple
15114 * ur-type definition (so· circular definitions are disallowed). That is, it
15115 * must be possible to reach a built-in primitive datatype or the ·simple
15116 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015117 *
15118 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015119 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015120 return (0);
15121}
15122
15123/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015124 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015125 * @ctxt: the schema parser context
15126 * @type: the simple type definition
15127 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015128 * Schema Component Constraint:
15129 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15130
15131 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015132 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015133 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015134 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015135 * a positive error code otherwise.
15136 */
15137static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015138xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015139 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015140{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015141 xmlChar *str = NULL;
15142
Daniel Veillard01fa6152004-06-29 17:04:39 +000015143 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015144 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15145 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015146 return (-1);
15147 }
15148
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015149 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015150 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015151 /*
15152 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015153 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015154 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015155 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015156 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015157 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015158 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015159 "The base type '%s' is not an atomic simple type",
15160 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015161 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015162 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15163 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015164 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015165 * restriction.
15166 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015167 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015168 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015169 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015170 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015171 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015172 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015173 "The final of its base type '%s' must not contain 'restriction'",
15174 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015175 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015176 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15177 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015178
15179 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015180 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015181 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 * Primitive datatypes.
15183 */
15184 if (type->facets != NULL) {
15185 xmlSchemaFacetPtr facet;
15186 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015187
Daniel Veillard01fa6152004-06-29 17:04:39 +000015188 primitive = xmlSchemaGetPrimitiveType(type);
15189 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015190 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15191 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015192 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015193 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015194 facet = type->facets;
15195 do {
15196 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015197 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015198 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015199 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015200 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015201 }
15202 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015203 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015204 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015205 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015206 }
15207 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015208 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15209 * of the {base type definition} (call this BF),then the DF's {value}
15210 * must be a valid restriction of BF's {value} as defined in
15211 * [XML Schemas: Datatypes]."
15212 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015213 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015214 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015215 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015216 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015217 xmlSchemaTypePtr itemType = NULL;
15218
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015219 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015220 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015221 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15222 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015223 return (-1);
15224 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015225 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015226 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015227 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015228 * 2.1 The {item type definition} must have a {variety} of atomic or
15229 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015230 * must be atomic).
15231 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015232 if ((! WXS_IS_ATOMIC(itemType)) &&
15233 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015234 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015235 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015236 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015237 "The item type '%s' does not have a variety of atomic or union",
15238 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015239 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015240 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015241 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015242 xmlSchemaTypeLinkPtr member;
15243
15244 member = itemType->memberTypes;
15245 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015246 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015247 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015248 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015249 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015250 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015251 "member type '%s' of this item type is not atomic",
15252 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015253 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015254 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15255 }
15256 member = member->next;
15257 }
15258 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015259
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015260 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015261 xmlSchemaFacetPtr facet;
15262 /*
15263 * This is the case if we have: <simpleType><list ..
15264 */
15265 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015266 * 2.3.1
15267 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015268 * contain list.
15269 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015270 if (xmlSchemaTypeFinalContains(itemType,
15271 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15272 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015273 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015274 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015275 "The final of its item type '%s' must not contain 'list'",
15276 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015277 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015278 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15279 }
15280 /*
15281 * 2.3.1.2 The {facets} must only contain the whiteSpace
15282 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015283 * OPTIMIZE TODO: the S4S already disallows any facet
15284 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015285 */
15286 if (type->facets != NULL) {
15287 facet = type->facets;
15288 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015289 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015290 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015291 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015292 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015293 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15294 }
15295 facet = facet->next;
15296 } while (facet != NULL);
15297 }
15298 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015299 * MAYBE TODO: (Hmm, not really) Datatypes states:
15300 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15301 * whose ·lexical space· allows space (such as string or anyURI)or
15302 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015303 * ·lexical space· allows space.
15304 */
15305 } else {
15306 /*
15307 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015308 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015309 */
15310 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015311 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015312 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15313 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015314 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015315 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015316 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015317 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015318 "The base type '%s' must be a list type",
15319 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015320 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015321 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15322 }
15323 /*
15324 * 2.3.2.2 The {final} of the {base type definition} must not
15325 * contain restriction.
15326 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015327 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015328 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015329 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015330 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015331 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015332 "The 'final' of the base type '%s' must not contain 'restriction'",
15333 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015334 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015335 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15336 }
15337 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015338 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015339 * from the {base type definition}'s {item type definition} given
15340 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15341 */
15342 {
15343 xmlSchemaTypePtr baseItemType;
15344
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015345 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015346 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015347 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15348 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015349 return (-1);
15350 }
15351 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015352 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015353 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015354 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015355 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015356 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015357 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015358 "The item type '%s' is not validly derived from "
15359 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015360 xmlSchemaGetComponentQName(&str, itemType),
15361 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15362 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015363
15364 FREE_AND_NULL(str)
15365 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015366 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015367 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15368 }
15369 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015370
Daniel Veillard01fa6152004-06-29 17:04:39 +000015371 if (type->facets != NULL) {
15372 xmlSchemaFacetPtr facet;
15373 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015374 /*
15375 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015376 * and enumeration facet components are allowed among the {facets}.
15377 */
15378 facet = type->facets;
15379 do {
15380 switch (facet->type) {
15381 case XML_SCHEMA_FACET_LENGTH:
15382 case XML_SCHEMA_FACET_MINLENGTH:
15383 case XML_SCHEMA_FACET_MAXLENGTH:
15384 case XML_SCHEMA_FACET_WHITESPACE:
15385 /*
15386 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015387 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015388 */
15389 case XML_SCHEMA_FACET_PATTERN:
15390 case XML_SCHEMA_FACET_ENUMERATION:
15391 break;
15392 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015393 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015394 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015395 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015396 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015397 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015398 * invalid facets.
15399 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015400 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015401 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015402 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015403 facet = facet->next;
15404 } while (facet != NULL);
15405 if (ok == 0)
15406 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15407 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015408 * SPEC (2.3.2.5) (same as 1.3.2)
15409 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015410 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015411 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015412 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015413 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015414 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015415 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015416 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015417 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015418 * atomic or list.
15419 */
15420 xmlSchemaTypeLinkPtr member;
15421
15422 member = type->memberTypes;
15423 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015424 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015425 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015426
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015427 if ((! WXS_IS_ATOMIC(member->type)) &&
15428 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015429 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015431 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015432 "The member type '%s' is neither an atomic, nor a list type",
15433 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015434 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015435 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15436 }
15437 member = member->next;
15438 }
15439 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015440 * 3.3.1 If the {base type definition} is the ·simple ur-type
15441 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015442 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015443 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015444 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015445 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015446 * {final} which does not contain union.
15447 */
15448 member = type->memberTypes;
15449 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015450 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015451 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015452 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015453 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015454 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015455 "The 'final' of member type '%s' contains 'union'",
15456 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015457 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015458 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15459 }
15460 member = member->next;
15461 }
15462 /*
15463 * 3.3.1.2 The {facets} must be empty.
15464 */
15465 if (type->facetSet != NULL) {
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_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015468 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015469 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015470 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15471 }
15472 } else {
15473 /*
15474 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015475 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015476 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015477 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015478 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015479 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015480 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015481 "The base type '%s' is not a union type",
15482 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015483 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015484 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15485 }
15486 /*
15487 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15488 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015489 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015490 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015491 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015492 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015493 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015494 "The 'final' of its base type '%s' must not contain 'restriction'",
15495 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015496 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015497 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15498 }
15499 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015500 * 3.3.2.3 The {member type definitions}, in order, must be validly
15501 * derived from the corresponding type definitions in the {base
15502 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015503 * as defined in Type Derivation OK (Simple) (§3.14.6).
15504 */
15505 {
15506 xmlSchemaTypeLinkPtr baseMember;
15507
15508 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015509 * OPTIMIZE: if the type is restricting, it has no local defined
15510 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015511 * thus a check for equality can be skipped.
15512 */
15513 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015514 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015515 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015516 * types of it's base type. This check seems not necessary with
15517 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015518 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015519 */
15520 if (type->memberTypes != NULL) {
15521 member = type->memberTypes;
15522 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015523 if ((member == NULL) && (baseMember != NULL)) {
15524 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15525 "different number of member types in base");
15526 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015527 while (member != NULL) {
15528 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015529 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15530 "different number of member types in base");
Daniel Veillard14b56432006-03-09 18:41:40 +000015531 } else if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015532 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015533 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015534 xmlChar *strBMT = NULL, *strBT = NULL;
15535
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015536 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015537 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015538 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015539 "The member type %s is not validly "
15540 "derived from its corresponding member "
15541 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015542 xmlSchemaGetComponentQName(&str, member->type),
15543 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15544 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015545 FREE_AND_NULL(str)
15546 FREE_AND_NULL(strBMT)
15547 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015548 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015549 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015550 member = member->next;
15551 baseMember = baseMember->next;
15552 }
15553 }
15554 }
15555 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015556 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015557 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015558 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015559 if (type->facets != NULL) {
15560 xmlSchemaFacetPtr facet;
15561 int ok = 1;
15562
15563 facet = type->facets;
15564 do {
15565 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15566 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015567 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015568 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015569 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015570 ok = 0;
15571 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015572 facet = facet->next;
15573 } while (facet != NULL);
15574 if (ok == 0)
15575 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015576
Daniel Veillard01fa6152004-06-29 17:04:39 +000015577 }
15578 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015579 * SPEC (3.3.2.5) (same as 1.3.2)
15580 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015581 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015582 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015583 */
15584 }
15585 }
15586
15587 return (0);
15588}
15589
15590/**
15591 * xmlSchemaCheckSRCSimpleType:
15592 * @ctxt: the schema parser context
15593 * @type: the simple type definition
15594 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015595 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015596 *
15597 * Returns 0 if the constraints are satisfied,
15598 * if not a positive error code and -1 on internal
15599 * errors.
15600 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015601#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015602static int
15603xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15604 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015605{
15606 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015607 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015608 * must satisfy the conditions set out in Constraints on Simple Type
15609 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015610 */
15611 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015612 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015613 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015614 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015615 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015616 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015617 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015618 /*
15619 *
15620 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015621 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015622 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015623 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015624 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015625 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015626 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015627 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015628 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015629 /*
15630 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015631 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015632 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015633 return (0);
15634}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015635#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015636
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015637static int
15638xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15639{
15640 if (ctxt->vctxt == NULL) {
15641 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15642 if (ctxt->vctxt == NULL) {
15643 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015644 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015645 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015646 "failed to create a temp. validation context.\n",
15647 NULL, NULL);
15648 return (-1);
15649 }
15650 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015651 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015652 ctxt->error, ctxt->warning, ctxt->errCtxt);
15653 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15654 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015655 }
15656 return (0);
15657}
15658
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015659static int
15660xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15661 xmlNodePtr node,
15662 xmlSchemaTypePtr type,
15663 const xmlChar *value,
15664 xmlSchemaValPtr *retVal,
15665 int fireErrors,
15666 int normalize,
15667 int isNormalized);
15668
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015669/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015670 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015671 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015672 * @type: the simple type definition
15673 * @value: the default value
15674 * @node: an optional node (the holder of the value)
15675 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015676 * Schema Component Constraint: Element Default Valid (Immediate)
15677 * (cos-valid-default)
15678 * This will be used by the parser only. For the validator there's
15679 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015680 *
15681 * Returns 0 if the constraints are satisfied,
15682 * if not, a positive error code and -1 on internal
15683 * errors.
15684 */
15685static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015686xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15687 xmlNodePtr node,
15688 xmlSchemaTypePtr type,
15689 const xmlChar *value,
15690 xmlSchemaValPtr *val)
15691{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015692 int ret = 0;
15693
15694 /*
15695 * cos-valid-default:
15696 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015697 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015698 * definition the appropriate case among the following must be true:
15699 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015700 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015701 /*
15702 * Complex type.
15703 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015704 * SPEC (2.1) "its {content type} must be a simple type definition
15705 * or mixed."
15706 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015707 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015708 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015709 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015710 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15711 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015712 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015713 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015714 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015715 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015716 "For a string to be a valid default, the type definition "
15717 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015718 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015719 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15720 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015721 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015722 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015723 * 1 If the type definition is a simple type definition, then the string
15724 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015725 * Valid (§3.14.4).
15726 *
15727 * AND
15728 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015729 * 2.2.1 If the {content type} is a simple type definition, then the
15730 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015731 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015732 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015733 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015734 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015735 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015736 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015737 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015738 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015739 else
15740 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015741
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015742 if (ret < 0) {
15743 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15744 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015745 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015746
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015747 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015748}
15749
15750/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015751 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015752 * @ctxt: the schema parser context
15753 * @type: the complex type definition
15754 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015755 *.(4.6) Constraints on Complex Type Definition Schema Components
15756 * Schema Component Constraint:
15757 * Complex Type Definition Properties Correct (ct-props-correct)
15758 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015759 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015760 * Returns 0 if the constraints are satisfied, a positive
15761 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015762 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015763static int
15764xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15765 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015766{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015767 /*
15768 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15769 *
15770 * SPEC (1) "The values of the properties of a complex type definition must
15771 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015772 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015773 * Sub-components (§5.3)."
15774 */
15775 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015776 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015777 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015778 /*
15779 * SPEC (2) "If the {base type definition} is a simple type definition,
15780 * the {derivation method} must be extension."
15781 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015782 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015783 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015784 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015785 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015786 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015787 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015788 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015789 /*
15790 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15791 * definition·. That is, it must be possible to reach the ·ur-type
15792 * definition by repeatedly following the {base type definition}."
15793 *
15794 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015795 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015796 /*
15797 * NOTE that (4) and (5) need the following:
15798 * - attribute uses need to be already inherited (apply attr. prohibitions)
15799 * - attribute group references need to be expanded already
15800 * - simple types need to be typefixed already
15801 */
15802 if (type->attrUses &&
15803 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15804 {
15805 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15806 xmlSchemaAttributeUsePtr use, tmp;
15807 int i, j, hasId = 0;
15808
15809 for (i = uses->nbItems -1; i >= 0; i--) {
15810 use = uses->items[i];
15811
15812 /*
15813 * SPEC ct-props-correct
15814 * (4) "Two distinct attribute declarations in the
15815 * {attribute uses} must not have identical {name}s and
15816 * {target namespace}s."
15817 */
15818 if (i > 0) {
15819 for (j = i -1; j >= 0; j--) {
15820 tmp = uses->items[j];
15821 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15822 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15823 (WXS_ATTRUSE_DECL_TNS(use) ==
15824 WXS_ATTRUSE_DECL_TNS(tmp)))
15825 {
15826 xmlChar *str = NULL;
15827
15828 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15829 XML_SCHEMAP_AG_PROPS_CORRECT,
15830 NULL, WXS_BASIC_CAST type,
15831 "Duplicate %s",
15832 xmlSchemaGetComponentDesignation(&str, use),
15833 NULL);
15834 FREE_AND_NULL(str);
15835 /*
15836 * Remove the duplicate.
15837 */
15838 if (xmlSchemaItemListRemove(uses, i) == -1)
15839 goto exit_failure;
15840 goto next_use;
15841 }
15842 }
15843 }
15844 /*
15845 * SPEC ct-props-correct
15846 * (5) "Two distinct attribute declarations in the
15847 * {attribute uses} must not have {type definition}s which
15848 * are or are derived from ID."
15849 */
15850 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15851 if (xmlSchemaIsDerivedFromBuiltInType(
15852 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15853 {
15854 if (hasId) {
15855 xmlChar *str = NULL;
15856
15857 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15858 XML_SCHEMAP_AG_PROPS_CORRECT,
15859 NULL, WXS_BASIC_CAST type,
15860 "There must not exist more than one attribute "
15861 "declaration of type 'xs:ID' "
15862 "(or derived from 'xs:ID'). The %s violates this "
15863 "constraint",
15864 xmlSchemaGetComponentDesignation(&str, use),
15865 NULL);
15866 FREE_AND_NULL(str);
15867 if (xmlSchemaItemListRemove(uses, i) == -1)
15868 goto exit_failure;
15869 }
15870
15871 hasId = 1;
15872 }
15873 }
15874next_use: {}
15875 }
15876 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015877 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015878exit_failure:
15879 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015880}
15881
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015882static int
15883xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15884 xmlSchemaTypePtr typeB)
15885{
15886 /*
15887 * TODO: This should implement component-identity
15888 * in the future.
15889 */
15890 if ((typeA == NULL) || (typeB == NULL))
15891 return (0);
15892 return (typeA == typeB);
15893}
15894
15895/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015896 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015897 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015898 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015899 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015900 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015901 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015902 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015903 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15904 *
15905 * STATUS: completed
15906 *
15907 * Returns 0 if the constraints are satisfied, or 1
15908 * if not.
15909 */
15910static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015911xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015912 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015913 xmlSchemaTypePtr baseType,
15914 int set)
15915{
15916 int equal = xmlSchemaAreEqualTypes(type, baseType);
15917 /* TODO: Error codes. */
15918 /*
15919 * SPEC "For a complex type definition (call it D, for derived)
15920 * to be validly derived from a type definition (call this
15921 * B, for base) given a subset of {extension, restriction}
15922 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015923 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015924 if (! equal) {
15925 /*
15926 * SPEC (1) "If B and D are not the same type definition, then the
15927 * {derivation method} of D must not be in the subset."
15928 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015929 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15930 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015931 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015932 } else {
15933 /*
15934 * SPEC (2.1) "B and D must be the same type definition."
15935 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015936 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015937 }
15938 /*
15939 * SPEC (2.2) "B must be D's {base type definition}."
15940 */
15941 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015942 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015943 /*
15944 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15945 * definition·."
15946 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015947 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015948 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015949
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015950 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015951 /*
15952 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15953 * must be validly derived from B given the subset as defined by this
15954 * constraint."
15955 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015956 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015957 baseType, set));
15958 } else {
15959 /*
15960 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15961 * must be validly derived from B given the subset as defined in Type
15962 * Derivation OK (Simple) (§3.14.6).
15963 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015964 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015965 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015966 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015967}
15968
15969/**
15970 * xmlSchemaCheckCOSDerivedOK:
15971 * @type: the derived simple type definition
15972 * @baseType: the base type definition
15973 *
15974 * Calls:
15975 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015976 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015977 * Checks wheter @type can be validly derived from @baseType.
15978 *
15979 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015980 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015981static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015982xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015983 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015984 xmlSchemaTypePtr baseType,
15985 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015986{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015987 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015988 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015989 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015990 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015991}
15992
William M. Brack2f2a6632004-08-20 23:09:47 +000015993/**
15994 * xmlSchemaCheckCOSCTExtends:
15995 * @ctxt: the schema parser context
15996 * @type: the complex type definition
15997 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015998 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015999 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016000 * Derivation Valid (Extension) (cos-ct-extends)
16001 *
16002 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016003 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016004 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016005 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000016006 *
16007 * Returns 0 if the constraints are satisfied, a positive
16008 * error code if not and -1 if an internal error occured.
16009 */
16010static int
16011xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
16012 xmlSchemaTypePtr type)
16013{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016014 xmlSchemaTypePtr base = type->baseType;
16015 /*
16016 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
16017 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000016018 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016019 /*
16020 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016021 * then all of the following must be true:"
16022 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016023 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016024 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016025 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016026 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016027 */
16028 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16029 xmlSchemaPCustomErr(ctxt,
16030 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016031 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016032 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016033 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016034 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16035 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016036
16037 /*
16038 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16039 * since they are automatically satisfied through the
16040 * inheriting mechanism.
16041 * Note that even if redefining components, the inheriting mechanism
16042 * is used.
16043 */
16044#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016045 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016046 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016047 * uses}
16048 * of the complex type definition itself, that is, for every attribute
16049 * use in the {attribute uses} of the {base type definition}, there
16050 * must be an attribute use in the {attribute uses} of the complex
16051 * type definition itself whose {attribute declaration} has the same
16052 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016053 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016054 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016055 if (base->attrUses != NULL) {
16056 int i, j, found;
16057 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016058
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016059 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16060 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16061 found = 0;
16062 if (type->attrUses != NULL) {
16063 use = (WXS_LIST_CAST type->attrUses)->items[j];
16064 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16065 {
16066 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16067 WXS_ATTRUSE_DECL_NAME(buse)) &&
16068 (WXS_ATTRUSE_DECL_TNS(use) ==
16069 WXS_ATTRUSE_DECL_TNS(buse)) &&
16070 (WXS_ATTRUSE_TYPEDEF(use) ==
16071 WXS_ATTRUSE_TYPEDEF(buse))
16072 {
16073 found = 1;
16074 break;
16075 }
16076 }
16077 }
16078 if (! found) {
16079 xmlChar *str = NULL;
16080
16081 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16082 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16083 NULL, WXS_BASIC_CAST type,
16084 /*
16085 * TODO: The report does not indicate that also the
16086 * type needs to be the same.
16087 */
16088 "This type is missing a matching correspondent "
16089 "for its {base type}'s %s in its {attribute uses}",
16090 xmlSchemaGetComponentDesignation(&str,
16091 buse->children),
16092 NULL);
16093 FREE_AND_NULL(str)
16094 }
16095 }
16096 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016097 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016098 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16099 * definition must also have one, and the base type definition's
16100 * {attribute wildcard}'s {namespace constraint} must be a subset
16101 * of the complex type definition's {attribute wildcard}'s {namespace
16102 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016103 */
16104
16105 /*
16106 * MAYBE TODO: Enable if ever needed. But this will be needed only
16107 * if created the type via a schema construction API.
16108 */
16109 if (base->attributeWildcard != NULL) {
16110 if (type->attributeWilcard == NULL) {
16111 xmlChar *str = NULL;
16112
16113 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16114 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16115 NULL, type,
16116 "The base %s has an attribute wildcard, "
16117 "but this type is missing an attribute wildcard",
16118 xmlSchemaGetComponentDesignation(&str, base));
16119 FREE_AND_NULL(str)
16120
16121 } else if (xmlSchemaCheckCOSNSSubset(
16122 base->attributeWildcard, type->attributeWildcard))
16123 {
16124 xmlChar *str = NULL;
16125
16126 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16127 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16128 NULL, type,
16129 "The attribute wildcard is not a valid "
16130 "superset of the one in the base %s",
16131 xmlSchemaGetComponentDesignation(&str, base));
16132 FREE_AND_NULL(str)
16133 }
16134 }
16135#endif
16136 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016137 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016138 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016139 if ((type->contentTypeDef != NULL) &&
16140 (type->contentTypeDef == base->contentTypeDef)) {
16141 /*
16142 * SPEC (1.4.1) "The {content type} of the {base type definition}
16143 * and the {content type} of the complex type definition itself
16144 * must be the same simple type definition"
16145 * PASS
16146 */
16147 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16148 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16149 /*
16150 * SPEC (1.4.2) "The {content type} of both the {base type
16151 * definition} and the complex type definition itself must
16152 * be empty."
16153 * PASS
16154 */
16155 } else {
16156 /*
16157 * SPEC (1.4.3) "All of the following must be true:"
16158 */
16159 if (type->subtypes == NULL) {
16160 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016161 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016162 * definition itself must specify a particle.
16163 */
16164 xmlSchemaPCustomErr(ctxt,
16165 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016166 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016167 "The content type must specify a particle", NULL);
16168 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16169 }
16170 /*
16171 * SPEC (1.4.3.2) "One of the following must be true:"
16172 */
16173 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16174 /*
16175 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16176 * definition} must be empty.
16177 * PASS
16178 */
16179 } else {
16180 /*
16181 * SPEC (1.4.3.2.2) "All of the following must be true:"
16182 */
16183 if ((type->contentType != base->contentType) ||
16184 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16185 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16186 /*
16187 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16188 * or both must be element-only."
16189 */
16190 xmlSchemaPCustomErr(ctxt,
16191 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016192 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016193 "The content type of both, the type and its base "
16194 "type, must either 'mixed' or 'element-only'", NULL);
16195 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016196 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016197 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016198 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016199 * complex type definition must be a ·valid extension·
16200 * of the {base type definition}'s particle, as defined
16201 * in Particle Valid (Extension) (§3.9.6)."
16202 *
16203 * NOTE that we won't check "Particle Valid (Extension)",
16204 * since it is ensured by the derivation process in
16205 * xmlSchemaTypeFixup(). We need to implement this when heading
16206 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016207 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016208 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016209 }
16210 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016211 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016212 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016213 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016214 } else {
16215 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016216 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016217 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016218 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016219 if (type->contentTypeDef != base) {
16220 /*
16221 * SPEC (2.1) "The {content type} must be the same simple type
16222 * definition."
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 content type must be the simple base type", NULL);
16228 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16229 }
16230 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16231 /*
16232 * SPEC (2.2) "The {final} of the {base type definition} must not
16233 * contain extension"
16234 * NOTE that this is the same as (1.1).
16235 */
16236 xmlSchemaPCustomErr(ctxt,
16237 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016238 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016239 "The 'final' of the base type definition "
16240 "contains 'extension'", NULL);
16241 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016242 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016243 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016244 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016245}
16246
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016247/**
16248 * xmlSchemaCheckDerivationOKRestriction:
16249 * @ctxt: the schema parser context
16250 * @type: the complex type definition
16251 *
16252 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016253 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016254 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16255 *
16256 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016257 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016258 * (5.4.2) ???
16259 *
16260 * ATTENTION:
16261 * In XML Schema 1.1 this will be:
16262 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016263 *
16264 * Returns 0 if the constraints are satisfied, a positive
16265 * error code if not and -1 if an internal error occured.
16266 */
16267static int
16268xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16269 xmlSchemaTypePtr type)
16270{
16271 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016272
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016273 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016274 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016275 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016276 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016277 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016278 if (! WXS_IS_COMPLEX(base)) {
16279 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16280 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16281 type->node, WXS_BASIC_CAST type,
16282 "The base type must be a complex type", NULL, NULL);
16283 return(ctxt->err);
16284 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016285 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16286 /*
16287 * SPEC (1) "The {base type definition} must be a complex type
16288 * definition whose {final} does not contain restriction."
16289 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016290 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16291 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16292 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016293 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016294 "contains 'restriction'", NULL, NULL);
16295 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016296 }
16297 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016298 * SPEC (2), (3) and (4)
16299 * Those are handled in a separate function, since the
16300 * same constraints are needed for redefinition of
16301 * attribute groups as well.
16302 */
16303 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16304 XML_SCHEMA_ACTION_DERIVE,
16305 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16306 type->attrUses, base->attrUses,
16307 type->attributeWildcard,
16308 base->attributeWildcard) == -1)
16309 {
16310 return(-1);
16311 }
16312 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016313 * SPEC (5) "One of the following must be true:"
16314 */
16315 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16316 /*
16317 * SPEC (5.1) "The {base type definition} must be the
16318 * ·ur-type definition·."
16319 * PASS
16320 */
16321 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16322 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16323 /*
16324 * SPEC (5.2.1) "The {content type} of the complex type definition
16325 * must be a simple type definition"
16326 *
16327 * SPEC (5.2.2) "One of the following must be true:"
16328 */
16329 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016330 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16331 {
16332 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016333 /*
16334 * SPEC (5.2.2.1) "The {content type} of the {base type
16335 * definition} must be a simple type definition from which
16336 * the {content type} is validly derived given the empty
16337 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016338 *
16339 * ATTENTION TODO: This seems not needed if the type implicitely
16340 * derived from the base type.
16341 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016342 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016343 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16344 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016345 if (err != 0) {
16346 xmlChar *strA = NULL, *strB = NULL;
16347
16348 if (err == -1)
16349 return(-1);
16350 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16351 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16352 NULL, WXS_BASIC_CAST type,
16353 "The {content type} %s is not validly derived from the "
16354 "base type's {content type} %s",
16355 xmlSchemaGetComponentDesignation(&strA,
16356 type->contentTypeDef),
16357 xmlSchemaGetComponentDesignation(&strB,
16358 base->contentTypeDef));
16359 FREE_AND_NULL(strA);
16360 FREE_AND_NULL(strB);
16361 return(ctxt->err);
16362 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016363 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16364 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016365 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016366 /*
16367 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16368 * and have a particle which is ·emptiable· as defined in
16369 * Particle Emptiable (§3.9.6)."
16370 * PASS
16371 */
16372 } else {
16373 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016374 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16375 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016376 "The content type of the base type must be either "
16377 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016378 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016379 }
16380 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16381 /*
16382 * SPEC (5.3.1) "The {content type} of the complex type itself must
16383 * be empty"
16384 */
16385 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16386 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016387 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016388 * definition} must also be empty."
16389 * PASS
16390 */
16391 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16392 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16393 xmlSchemaIsParticleEmptiable(
16394 (xmlSchemaParticlePtr) base->subtypes)) {
16395 /*
16396 * SPEC (5.3.2.2) "The {content type} of the {base type
16397 * definition} must be elementOnly or mixed and have a particle
16398 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16399 * PASS
16400 */
16401 } else {
16402 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016403 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16404 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016405 "The content type of the base type must be either "
16406 "empty or 'mixed' (or 'elements-only') and an emptiable "
16407 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016408 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016409 }
16410 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016411 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016412 /*
16413 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16414 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016415 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016416 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016417 /*
16418 * SPEC (5.4.1.2) "The {content type} of the complex type
16419 * definition itself and of the {base type definition} must be
16420 * mixed"
16421 */
16422 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016423 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16424 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016425 "If the content type is 'mixed', then the content type of the "
16426 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016427 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016428 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016429 /*
16430 * SPEC (5.4.2) "The particle of the complex type definition itself
16431 * must be a ·valid restriction· of the particle of the {content
16432 * type} of the {base type definition} as defined in Particle Valid
16433 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016434 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016435 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016436 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016437 } else {
16438 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016439 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16440 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016441 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016442 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016443 }
16444 return (0);
16445}
16446
16447/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016448 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016449 * @ctxt: the schema parser context
16450 * @type: the complex type definition
16451 *
16452 * (3.4.6) Constraints on Complex Type Definition Schema Components
16453 *
16454 * Returns 0 if the constraints are satisfied, a positive
16455 * error code if not and -1 if an internal error occured.
16456 */
16457static int
16458xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16459 xmlSchemaTypePtr type)
16460{
16461 int ret;
16462 /*
16463 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016464 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016465 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16466 if (ret != 0)
16467 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016468 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016469 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16470 else
16471 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16472 return (ret);
16473}
16474
16475/**
16476 * xmlSchemaCheckSRCCT:
16477 * @ctxt: the schema parser context
16478 * @type: the complex type definition
16479 *
16480 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016481 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016482 * Complex Type Definition Representation OK (src-ct)
16483 *
16484 * Returns 0 if the constraints are satisfied, a positive
16485 * error code if not and -1 if an internal error occured.
16486 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016487static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016488xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016489 xmlSchemaTypePtr type)
16490{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016491 xmlSchemaTypePtr base;
16492 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016493
16494 /*
16495 * TODO: Adjust the error codes here, as I used
16496 * XML_SCHEMAP_SRC_CT_1 only yet.
16497 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016498 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016499 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016500 /*
16501 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016502 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016503 * must be a complex type definition;
16504 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016505 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016506 xmlChar *str = NULL;
16507 xmlSchemaPCustomErr(ctxt,
16508 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016509 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016510 "If using <complexContent>, the base type is expected to be "
16511 "a complex type. The base type '%s' is a simple type",
16512 xmlSchemaFormatQName(&str, base->targetNamespace,
16513 base->name));
16514 FREE_AND_NULL(str)
16515 return (XML_SCHEMAP_SRC_CT_1);
16516 }
16517 } else {
16518 /*
16519 * SPEC
16520 * 2 If the <simpleContent> alternative is chosen, all of the
16521 * following must be true:
16522 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16523 * base [attribute] must be one of the following:
16524 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016525 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016526 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016527 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016528 /*
16529 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016530 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016531 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016532 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016533 xmlSchemaPCustomErr(ctxt,
16534 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016535 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016536 "If using <simpleContent> and <restriction>, the base "
16537 "type must be a complex type. The base type '%s' is "
16538 "a simple type",
16539 xmlSchemaFormatQName(&str, base->targetNamespace,
16540 base->name));
16541 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016542 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016543 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 } else {
16545 /* Base type is a complex type. */
16546 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16547 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16548 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016549 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016550 * simple type definition;
16551 * PASS
16552 */
16553 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016554 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016555 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016556 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016557 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016558 type->name);
16559 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016560 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016561 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016562 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016563
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016564 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016565 * 2.1.2 only if the <restriction> alternative is also
16566 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016567 * is mixed and a particle emptiable.
16568 */
16569 if (! xmlSchemaIsParticleEmptiable(
16570 (xmlSchemaParticlePtr) base->subtypes)) {
16571 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016572 } else
16573 /*
16574 * Attention: at this point the <simpleType> child is in
16575 * ->contentTypeDef (put there during parsing).
16576 */
16577 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016578 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016579 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016580 * 2.2 If clause 2.1.2 above is satisfied, then there
16581 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016582 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016583 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016584 /* TODO: Change error code to ..._SRC_CT_2_2. */
16585 xmlSchemaPCustomErr(ctxt,
16586 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016587 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016588 "A <simpleType> is expected among the children "
16589 "of <restriction>, if <simpleContent> is used and "
16590 "the base type '%s' is a complex type",
16591 xmlSchemaFormatQName(&str, base->targetNamespace,
16592 base->name));
16593 FREE_AND_NULL(str)
16594 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016595 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016596 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016597 ret = XML_SCHEMAP_SRC_CT_1;
16598 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016599 }
16600 if (ret > 0) {
16601 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016602 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016603 xmlSchemaPCustomErr(ctxt,
16604 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016605 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016606 "If <simpleContent> and <restriction> is used, the "
16607 "base type must be a simple type or a complex type with "
16608 "mixed content and particle emptiable. The base type "
16609 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016610 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016611 base->name));
16612 } else {
16613 xmlSchemaPCustomErr(ctxt,
16614 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016615 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016616 "If <simpleContent> and <extension> is used, the "
16617 "base type must be a simple type. The base type '%s' "
16618 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016619 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016620 base->name));
16621 }
16622 FREE_AND_NULL(str)
16623 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016624 }
16625 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016626 * SPEC (3) "The corresponding complex type definition component must
16627 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016628 * Definition Schema Components (§3.4.6);"
16629 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016630 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016631 /*
16632 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016633 * above for {attribute wildcard} is satisfied, the intensional
16634 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016635 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016636 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016637 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016638 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016639}
William M. Brack2f2a6632004-08-20 23:09:47 +000016640
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016641#ifdef ENABLE_PARTICLE_RESTRICTION
16642/**
16643 * xmlSchemaCheckParticleRangeOK:
16644 * @ctxt: the schema parser context
16645 * @type: the complex type definition
16646 *
16647 * (3.9.6) Constraints on Particle Schema Components
16648 * Schema Component Constraint:
16649 * Occurrence Range OK (range-ok)
16650 *
16651 * STATUS: complete
16652 *
16653 * Returns 0 if the constraints are satisfied, a positive
16654 * error code if not and -1 if an internal error occured.
16655 */
16656static int
16657xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16658 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016659{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016660 if (rmin < bmin)
16661 return (1);
16662 if ((bmax != UNBOUNDED) &&
16663 (rmax > bmax))
16664 return (1);
16665 return (0);
16666}
16667
16668/**
16669 * xmlSchemaCheckRCaseNameAndTypeOK:
16670 * @ctxt: the schema parser context
16671 * @r: the restricting element declaration particle
16672 * @b: the base element declaration particle
16673 *
16674 * (3.9.6) Constraints on Particle Schema Components
16675 * Schema Component Constraint:
16676 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16677 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016678 *
16679 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016680 * MISSING (3.2.3)
16681 * CLARIFY: (3.2.2)
16682 *
16683 * Returns 0 if the constraints are satisfied, a positive
16684 * error code if not and -1 if an internal error occured.
16685 */
16686static int
16687xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16688 xmlSchemaParticlePtr r,
16689 xmlSchemaParticlePtr b)
16690{
16691 xmlSchemaElementPtr elemR, elemB;
16692
16693 /* TODO: Error codes (rcase-NameAndTypeOK). */
16694 elemR = (xmlSchemaElementPtr) r->children;
16695 elemB = (xmlSchemaElementPtr) b->children;
16696 /*
16697 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16698 * the same."
16699 */
16700 if ((elemR != elemB) &&
16701 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16702 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16703 return (1);
16704 /*
16705 * SPEC (2) "R's occurrence range is a valid restriction of B's
16706 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16707 */
16708 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16709 b->minOccurs, b->maxOccurs) != 0)
16710 return (1);
16711 /*
16712 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16713 * {scope} are global."
16714 */
16715 if (elemR == elemB)
16716 return (0);
16717 /*
16718 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16719 */
16720 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16721 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16722 return (1);
16723 /*
16724 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16725 * or is not fixed, or R's declaration's {value constraint} is fixed
16726 * with the same value."
16727 */
16728 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16729 ((elemR->value == NULL) ||
16730 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16731 /* TODO: Equality of the initial value or normalized or canonical? */
16732 (! xmlStrEqual(elemR->value, elemB->value))))
16733 return (1);
16734 /*
16735 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16736 * definitions} is a subset of B's declaration's {identity-constraint
16737 * definitions}, if any."
16738 */
16739 if (elemB->idcs != NULL) {
16740 /* TODO */
16741 }
16742 /*
16743 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16744 * superset of B's declaration's {disallowed substitutions}."
16745 */
16746 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16747 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16748 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16749 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16750 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16751 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16752 return (1);
16753 /*
16754 * SPEC (3.2.5) "R's {type definition} is validly derived given
16755 * {extension, list, union} from B's {type definition}"
16756 *
16757 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16758 * set, if the corresponding constraints handle "restriction" and
16759 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016760 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016761 */
16762 {
16763 int set = 0;
16764
16765 set |= SUBSET_EXTENSION;
16766 set |= SUBSET_LIST;
16767 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016768 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016769 elemB->subtypes, set) != 0)
16770 return (1);
16771 }
16772 return (0);
16773}
16774
16775/**
16776 * xmlSchemaCheckRCaseNSCompat:
16777 * @ctxt: the schema parser context
16778 * @r: the restricting element declaration particle
16779 * @b: the base wildcard particle
16780 *
16781 * (3.9.6) Constraints on Particle Schema Components
16782 * Schema Component Constraint:
16783 * Particle Derivation OK (Elt:Any -- NSCompat)
16784 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016785 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016786 * STATUS: complete
16787 *
16788 * Returns 0 if the constraints are satisfied, a positive
16789 * error code if not and -1 if an internal error occured.
16790 */
16791static int
16792xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16793 xmlSchemaParticlePtr r,
16794 xmlSchemaParticlePtr b)
16795{
16796 /* TODO:Error codes (rcase-NSCompat). */
16797 /*
16798 * SPEC "For an element declaration particle to be a ·valid restriction·
16799 * of a wildcard particle all of the following must be true:"
16800 *
16801 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16802 * with respect to the wildcard's {namespace constraint} as defined by
16803 * Wildcard allows Namespace Name (§3.10.4)."
16804 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016805 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016806 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16807 return (1);
16808 /*
16809 * SPEC (2) "R's occurrence range is a valid restriction of B's
16810 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16811 */
16812 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16813 b->minOccurs, b->maxOccurs) != 0)
16814 return (1);
16815
16816 return (0);
16817}
16818
16819/**
16820 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16821 * @ctxt: the schema parser context
16822 * @r: the restricting element declaration particle
16823 * @b: the base model group particle
16824 *
16825 * (3.9.6) Constraints on Particle Schema Components
16826 * Schema Component Constraint:
16827 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16828 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016829 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016830 * STATUS: TODO
16831 *
16832 * Returns 0 if the constraints are satisfied, a positive
16833 * error code if not and -1 if an internal error occured.
16834 */
16835static int
16836xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16837 xmlSchemaParticlePtr r,
16838 xmlSchemaParticlePtr b)
16839{
16840 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16841 TODO
16842 return (0);
16843}
16844
16845/**
16846 * xmlSchemaCheckRCaseNSSubset:
16847 * @ctxt: the schema parser context
16848 * @r: the restricting wildcard particle
16849 * @b: the base wildcard particle
16850 *
16851 * (3.9.6) Constraints on Particle Schema Components
16852 * Schema Component Constraint:
16853 * Particle Derivation OK (Any:Any -- NSSubset)
16854 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016855 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016856 * STATUS: complete
16857 *
16858 * Returns 0 if the constraints are satisfied, a positive
16859 * error code if not and -1 if an internal error occured.
16860 */
16861static int
16862xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16863 xmlSchemaParticlePtr r,
16864 xmlSchemaParticlePtr b,
16865 int isAnyTypeBase)
16866{
16867 /* TODO: Error codes (rcase-NSSubset). */
16868 /*
16869 * SPEC (1) "R's occurrence range is a valid restriction of B's
16870 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16871 */
16872 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16873 b->minOccurs, b->maxOccurs))
16874 return (1);
16875 /*
16876 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16877 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16878 */
16879 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16880 (xmlSchemaWildcardPtr) b->children))
16881 return (1);
16882 /*
16883 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16884 * definition·, R's {process contents} must be identical to or stronger
16885 * than B's {process contents}, where strict is stronger than lax is
16886 * stronger than skip."
16887 */
16888 if (! isAnyTypeBase) {
16889 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16890 ((xmlSchemaWildcardPtr) b->children)->processContents)
16891 return (1);
16892 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016893
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016894 return (0);
16895}
16896
16897/**
16898 * xmlSchemaCheckCOSParticleRestrict:
16899 * @ctxt: the schema parser context
16900 * @type: the complex type definition
16901 *
16902 * (3.9.6) Constraints on Particle Schema Components
16903 * Schema Component Constraint:
16904 * Particle Valid (Restriction) (cos-particle-restrict)
16905 *
16906 * STATUS: TODO
16907 *
16908 * Returns 0 if the constraints are satisfied, a positive
16909 * error code if not and -1 if an internal error occured.
16910 */
16911static int
16912xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16913 xmlSchemaParticlePtr r,
16914 xmlSchemaParticlePtr b)
16915{
16916 int ret = 0;
16917
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016918 /*part = WXS_TYPE_PARTICLE(type);
16919 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016920 */
16921
16922 TODO
16923
16924 /*
16925 * SPEC (1) "They are the same particle."
16926 */
16927 if (r == b)
16928 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016929
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016930
16931 return (0);
16932}
16933
16934/**
16935 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16936 * @ctxt: the schema parser context
16937 * @r: the model group particle
16938 * @b: the base wildcard particle
16939 *
16940 * (3.9.6) Constraints on Particle Schema Components
16941 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016942 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016943 * NSRecurseCheckCardinality)
16944 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016945 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016946 * STATUS: TODO: subst-groups
16947 *
16948 * Returns 0 if the constraints are satisfied, a positive
16949 * error code if not and -1 if an internal error occured.
16950 */
16951static int
16952xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16953 xmlSchemaParticlePtr r,
16954 xmlSchemaParticlePtr b)
16955{
16956 xmlSchemaParticlePtr part;
16957 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16958 if ((r->children == NULL) || (r->children->children == NULL))
16959 return (-1);
16960 /*
16961 * SPEC "For a group particle to be a ·valid restriction· of a
16962 * wildcard particle..."
16963 *
16964 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016965 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016966 * Particle Valid (Restriction) (§3.9.6)."
16967 */
16968 part = (xmlSchemaParticlePtr) r->children->children;
16969 do {
16970 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16971 return (1);
16972 part = (xmlSchemaParticlePtr) part->next;
16973 } while (part != NULL);
16974 /*
16975 * SPEC (2) "The effective total range of the group [...] is a
16976 * valid restriction of B's occurrence range as defined by
16977 * Occurrence Range OK (§3.9.6)."
16978 */
16979 if (xmlSchemaCheckParticleRangeOK(
16980 xmlSchemaGetParticleTotalRangeMin(r),
16981 xmlSchemaGetParticleTotalRangeMax(r),
16982 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016983 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016984 return (0);
16985}
16986
16987/**
16988 * xmlSchemaCheckRCaseRecurse:
16989 * @ctxt: the schema parser context
16990 * @r: the <all> or <sequence> model group particle
16991 * @b: the base <all> or <sequence> model group particle
16992 *
16993 * (3.9.6) Constraints on Particle Schema Components
16994 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016995 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016996 Recurse)
16997 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016998 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016999 * STATUS: ?
17000 * TODO: subst-groups
17001 *
17002 * Returns 0 if the constraints are satisfied, a positive
17003 * error code if not and -1 if an internal error occured.
17004 */
17005static int
17006xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
17007 xmlSchemaParticlePtr r,
17008 xmlSchemaParticlePtr b)
17009{
17010 /* xmlSchemaParticlePtr part; */
17011 /* TODO: Error codes (rcase-Recurse). */
17012 if ((r->children == NULL) || (b->children == NULL) ||
17013 (r->children->type != b->children->type))
17014 return (-1);
17015 /*
17016 * SPEC "For an all or sequence group particle to be a ·valid
17017 * restriction· of another group particle with the same {compositor}..."
17018 *
17019 * SPEC (1) "R's occurrence range is a valid restriction of B's
17020 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
17021 */
17022 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17023 b->minOccurs, b->maxOccurs))
17024 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017025
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017026
17027 return (0);
17028}
17029
17030#endif
17031
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017032#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17033 xmlSchemaPCustomErrExt(pctxt, \
17034 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017035 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017036 "It is an error for both '%s' and '%s' to be specified on the "\
17037 "same type definition", \
17038 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17039 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17040
17041#define FACET_RESTR_ERR(fac1, msg) \
17042 xmlSchemaPCustomErr(pctxt, \
17043 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017044 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017045 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017046
17047#define FACET_RESTR_FIXED_ERR(fac) \
17048 xmlSchemaPCustomErr(pctxt, \
17049 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017050 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017051 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017052 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017053
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017054static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017055xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17056 xmlSchemaFacetPtr facet1,
17057 xmlSchemaFacetPtr facet2,
17058 int lessGreater,
17059 int orEqual,
17060 int ofBase)
17061{
17062 xmlChar *msg = NULL;
17063
17064 msg = xmlStrdup(BAD_CAST "'");
17065 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17066 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17067 if (lessGreater == 0)
17068 msg = xmlStrcat(msg, BAD_CAST " equal to");
17069 if (lessGreater == 1)
17070 msg = xmlStrcat(msg, BAD_CAST " greater than");
17071 else
17072 msg = xmlStrcat(msg, BAD_CAST " less than");
17073
17074 if (orEqual)
17075 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17076 msg = xmlStrcat(msg, BAD_CAST " '");
17077 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17078 if (ofBase)
17079 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17080 else
17081 msg = xmlStrcat(msg, BAD_CAST "'");
17082
17083 xmlSchemaPCustomErr(pctxt,
17084 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017085 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017086 (const char *) msg, NULL);
17087
17088 if (msg != NULL)
17089 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017090}
17091
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017092/*
17093* xmlSchemaDeriveAndValidateFacets:
17094*
17095* Schema Component Constraint: Simple Type Restriction (Facets)
17096* (st-restrict-facets)
17097*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017098static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017099xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17100 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017101{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017102 xmlSchemaTypePtr base = type->baseType;
17103 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017104 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017105 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17106 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17107 fmininc = NULL, fmaxinc = NULL,
17108 fminexc = NULL, fmaxexc = NULL,
17109 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17110 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17111 bfmininc = NULL, bfmaxinc = NULL,
17112 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017113 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017114
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017115 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017116 * SPEC st-restrict-facets 1:
17117 * "The {variety} of R is the same as that of B."
17118 */
17119 /*
17120 * SPEC st-restrict-facets 2:
17121 * "If {variety} is atomic, the {primitive type definition}
17122 * of R is the same as that of B."
17123 *
17124 * NOTE: we leave 1 & 2 out for now, since this will be
17125 * satisfied by the derivation process.
17126 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17127 */
17128 /*
17129 * SPEC st-restrict-facets 3:
17130 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017131 * of B, eliminating duplicates. To eliminate duplicates,
17132 * when a facet of the same kind occurs in both S and the
17133 * {facets} of B, the one in the {facets} of B is not
17134 * included, with the exception of enumeration and pattern
17135 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017136 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017137 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017138
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017139 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17140 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017141
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017142 last = type->facetSet;
17143 if (last != NULL)
17144 while (last->next != NULL)
17145 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017146
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017147 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17148 facet = cur->facet;
17149 switch (facet->type) {
17150 case XML_SCHEMA_FACET_LENGTH:
17151 flength = facet; break;
17152 case XML_SCHEMA_FACET_MINLENGTH:
17153 fminlen = facet; break;
17154 case XML_SCHEMA_FACET_MININCLUSIVE:
17155 fmininc = facet; break;
17156 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17157 fminexc = facet; break;
17158 case XML_SCHEMA_FACET_MAXLENGTH:
17159 fmaxlen = facet; break;
17160 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17161 fmaxinc = facet; break;
17162 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17163 fmaxexc = facet; break;
17164 case XML_SCHEMA_FACET_TOTALDIGITS:
17165 ftotdig = facet; break;
17166 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17167 ffracdig = facet; break;
17168 default:
17169 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017170 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017171 }
17172 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17173 facet = cur->facet;
17174 switch (facet->type) {
17175 case XML_SCHEMA_FACET_LENGTH:
17176 bflength = facet; break;
17177 case XML_SCHEMA_FACET_MINLENGTH:
17178 bfminlen = facet; break;
17179 case XML_SCHEMA_FACET_MININCLUSIVE:
17180 bfmininc = facet; break;
17181 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17182 bfminexc = facet; break;
17183 case XML_SCHEMA_FACET_MAXLENGTH:
17184 bfmaxlen = facet; break;
17185 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17186 bfmaxinc = facet; break;
17187 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17188 bfmaxexc = facet; break;
17189 case XML_SCHEMA_FACET_TOTALDIGITS:
17190 bftotdig = facet; break;
17191 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17192 bffracdig = facet; break;
17193 default:
17194 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017195 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017196 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017197 /*
17198 * length and minLength or maxLength (2.2) + (3.2)
17199 */
17200 if (flength && (fminlen || fmaxlen)) {
17201 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17202 "either of 'minLength' or 'maxLength' to be specified on "
17203 "the same type definition")
17204 }
17205 /*
17206 * Mutual exclusions in the same derivation step.
17207 */
17208 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017209 /*
17210 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017211 */
17212 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17213 }
17214 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017215 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017216 * SCC "minInclusive and minExclusive"
17217 */
17218 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017219 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017220
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017221 if (flength && bflength) {
17222 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017223 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017224 * The values have to be equal.
17225 */
17226 res = xmlSchemaCompareValues(flength->val, bflength->val);
17227 if (res == -2)
17228 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017229 if (res != 0)
17230 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17231 if ((res != 0) && (bflength->fixed)) {
17232 FACET_RESTR_FIXED_ERR(flength)
17233 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017234
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017235 }
17236 if (fminlen && bfminlen) {
17237 /*
17238 * SCC "minLength valid restriction"
17239 * minLength >= BASE minLength
17240 */
17241 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17242 if (res == -2)
17243 goto internal_error;
17244 if (res == -1)
17245 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17246 if ((res != 0) && (bfminlen->fixed)) {
17247 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017248 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017249 }
17250 if (fmaxlen && bfmaxlen) {
17251 /*
17252 * SCC "maxLength valid restriction"
17253 * maxLength <= BASE minLength
17254 */
17255 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17256 if (res == -2)
17257 goto internal_error;
17258 if (res == 1)
17259 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17260 if ((res != 0) && (bfmaxlen->fixed)) {
17261 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017262 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017263 }
17264 /*
17265 * SCC "length and minLength or maxLength"
17266 */
17267 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017268 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017269 if (flength) {
17270 if (! fminlen)
17271 flength = bflength;
17272 if (fminlen) {
17273 /* (1.1) length >= minLength */
17274 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17275 if (res == -2)
17276 goto internal_error;
17277 if (res == -1)
17278 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17279 }
17280 if (! fmaxlen)
17281 fmaxlen = bfmaxlen;
17282 if (fmaxlen) {
17283 /* (2.1) length <= maxLength */
17284 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17285 if (res == -2)
17286 goto internal_error;
17287 if (res == 1)
17288 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17289 }
17290 }
17291 if (fmaxinc) {
17292 /*
17293 * "maxInclusive"
17294 */
17295 if (fmininc) {
17296 /* SCC "maxInclusive >= minInclusive" */
17297 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17298 if (res == -2)
17299 goto internal_error;
17300 if (res == -1) {
17301 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17302 }
17303 }
17304 /*
17305 * SCC "maxInclusive valid restriction"
17306 */
17307 if (bfmaxinc) {
17308 /* maxInclusive <= BASE maxInclusive */
17309 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17310 if (res == -2)
17311 goto internal_error;
17312 if (res == 1)
17313 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17314 if ((res != 0) && (bfmaxinc->fixed)) {
17315 FACET_RESTR_FIXED_ERR(fmaxinc)
17316 }
17317 }
17318 if (bfmaxexc) {
17319 /* maxInclusive < BASE maxExclusive */
17320 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17321 if (res == -2)
17322 goto internal_error;
17323 if (res != -1) {
17324 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17325 }
17326 }
17327 if (bfmininc) {
17328 /* maxInclusive >= BASE minInclusive */
17329 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17330 if (res == -2)
17331 goto internal_error;
17332 if (res == -1) {
17333 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17334 }
17335 }
17336 if (bfminexc) {
17337 /* maxInclusive > BASE minExclusive */
17338 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17339 if (res == -2)
17340 goto internal_error;
17341 if (res != 1) {
17342 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17343 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017344 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017345 }
17346 if (fmaxexc) {
17347 /*
17348 * "maxExclusive >= minExclusive"
17349 */
17350 if (fminexc) {
17351 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17352 if (res == -2)
17353 goto internal_error;
17354 if (res == -1) {
17355 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17356 }
17357 }
17358 /*
17359 * "maxExclusive valid restriction"
17360 */
17361 if (bfmaxexc) {
17362 /* maxExclusive <= BASE maxExclusive */
17363 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17364 if (res == -2)
17365 goto internal_error;
17366 if (res == 1) {
17367 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17368 }
17369 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017370 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017371 }
17372 }
17373 if (bfmaxinc) {
17374 /* maxExclusive <= BASE maxInclusive */
17375 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17376 if (res == -2)
17377 goto internal_error;
17378 if (res == 1) {
17379 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17380 }
17381 }
17382 if (bfmininc) {
17383 /* maxExclusive > BASE minInclusive */
17384 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17385 if (res == -2)
17386 goto internal_error;
17387 if (res != 1) {
17388 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17389 }
17390 }
17391 if (bfminexc) {
17392 /* maxExclusive > BASE minExclusive */
17393 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17394 if (res == -2)
17395 goto internal_error;
17396 if (res != 1) {
17397 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17398 }
17399 }
17400 }
17401 if (fminexc) {
17402 /*
17403 * "minExclusive < maxInclusive"
17404 */
17405 if (fmaxinc) {
17406 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17407 if (res == -2)
17408 goto internal_error;
17409 if (res != -1) {
17410 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17411 }
17412 }
17413 /*
17414 * "minExclusive valid restriction"
17415 */
17416 if (bfminexc) {
17417 /* minExclusive >= BASE minExclusive */
17418 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17419 if (res == -2)
17420 goto internal_error;
17421 if (res == -1) {
17422 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17423 }
17424 if ((res != 0) && (bfminexc->fixed)) {
17425 FACET_RESTR_FIXED_ERR(fminexc)
17426 }
17427 }
17428 if (bfmaxinc) {
17429 /* minExclusive <= BASE maxInclusive */
17430 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17431 if (res == -2)
17432 goto internal_error;
17433 if (res == 1) {
17434 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17435 }
17436 }
17437 if (bfmininc) {
17438 /* minExclusive >= BASE minInclusive */
17439 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17440 if (res == -2)
17441 goto internal_error;
17442 if (res == -1) {
17443 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17444 }
17445 }
17446 if (bfmaxexc) {
17447 /* minExclusive < BASE maxExclusive */
17448 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17449 if (res == -2)
17450 goto internal_error;
17451 if (res != -1) {
17452 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17453 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017454 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017455 }
17456 if (fmininc) {
17457 /*
17458 * "minInclusive < maxExclusive"
17459 */
17460 if (fmaxexc) {
17461 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17462 if (res == -2)
17463 goto internal_error;
17464 if (res != -1) {
17465 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17466 }
17467 }
17468 /*
17469 * "minExclusive valid restriction"
17470 */
17471 if (bfmininc) {
17472 /* minInclusive >= BASE minInclusive */
17473 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17474 if (res == -2)
17475 goto internal_error;
17476 if (res == -1) {
17477 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17478 }
17479 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017480 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017481 }
17482 }
17483 if (bfmaxinc) {
17484 /* minInclusive <= BASE maxInclusive */
17485 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17486 if (res == -2)
17487 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017488 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017489 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17490 }
17491 }
17492 if (bfminexc) {
17493 /* minInclusive > BASE minExclusive */
17494 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17495 if (res == -2)
17496 goto internal_error;
17497 if (res != 1)
17498 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17499 }
17500 if (bfmaxexc) {
17501 /* minInclusive < BASE maxExclusive */
17502 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17503 if (res == -2)
17504 goto internal_error;
17505 if (res != -1)
17506 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17507 }
17508 }
17509 if (ftotdig && bftotdig) {
17510 /*
17511 * SCC " totalDigits valid restriction"
17512 * totalDigits <= BASE totalDigits
17513 */
17514 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17515 if (res == -2)
17516 goto internal_error;
17517 if (res == 1)
17518 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17519 -1, 1, 1);
17520 if ((res != 0) && (bftotdig->fixed)) {
17521 FACET_RESTR_FIXED_ERR(ftotdig)
17522 }
17523 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017524 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017525 /*
17526 * SCC "fractionDigits valid restriction"
17527 * fractionDigits <= BASE fractionDigits
17528 */
17529 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17530 if (res == -2)
17531 goto internal_error;
17532 if (res == 1)
17533 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17534 -1, 1, 1);
17535 if ((res != 0) && (bffracdig->fixed)) {
17536 FACET_RESTR_FIXED_ERR(ffracdig)
17537 }
17538 }
17539 /*
17540 * SCC "fractionDigits less than or equal to totalDigits"
17541 */
17542 if (! ftotdig)
17543 ftotdig = bftotdig;
17544 if (! ffracdig)
17545 ffracdig = bffracdig;
17546 if (ftotdig && ffracdig) {
17547 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17548 if (res == -2)
17549 goto internal_error;
17550 if (res == 1)
17551 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17552 -1, 1, 0);
17553 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017554 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017555 * *Enumerations* won' be added here, since only the first set
17556 * of enumerations in the ancestor-or-self axis is used
17557 * for validation, plus we need to use the base type of those
17558 * enumerations for whitespace.
17559 *
17560 * *Patterns*: won't be add here, since they are ORed at
17561 * type level and ANDed at ancestor level. This will
17562 * happed during validation by walking the base axis
17563 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017564 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017565 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17566 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017567 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017568 * Special handling of enumerations and patterns.
17569 * TODO: hmm, they should not appear in the set, so remove this.
17570 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017571 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017573 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017574 /*
17575 * Search for a duplicate facet in the current type.
17576 */
17577 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017578 /* err = 0; */
17579 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017580 while (link != NULL) {
17581 facet = link->facet;
17582 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017583 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017584 case XML_SCHEMA_FACET_WHITESPACE:
17585 /*
17586 * The whitespace must be stronger.
17587 */
17588 if (facet->whitespace < bfacet->whitespace) {
17589 FACET_RESTR_ERR(flength,
17590 "The 'whitespace' value has to be equal to "
17591 "or stronger than the 'whitespace' value of "
17592 "the base type")
17593 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017594 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017595 (facet->whitespace != bfacet->whitespace)) {
17596 FACET_RESTR_FIXED_ERR(facet)
17597 }
17598 break;
17599 default:
17600 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017601 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017602 /* Duplicate found. */
17603 break;
17604 }
17605 link = link->next;
17606 }
17607 /*
17608 * If no duplicate was found: add the base types's facet
17609 * to the set.
17610 */
17611 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017612 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017613 xmlMalloc(sizeof(xmlSchemaFacetLink));
17614 if (link == NULL) {
17615 xmlSchemaPErrMemory(pctxt,
17616 "deriving facets, creating a facet link", NULL);
17617 return (-1);
17618 }
17619 link->facet = cur->facet;
17620 link->next = NULL;
17621 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017622 type->facetSet = link;
17623 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017624 last->next = link;
17625 last = link;
17626 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017627
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017628 }
17629
17630 return (0);
17631internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017632 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17633 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017634 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017635}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017636
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017637static int
17638xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17639 xmlSchemaTypePtr type)
17640{
17641 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17642 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017643 * The actual value is then formed by replacing any union type
17644 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017645 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017646 *
17647 * TODO: There's a bug entry at
17648 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17649 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017650 */
17651 link = type->memberTypes;
17652 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017653
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017654 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017655 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017656
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017657 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017658 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017659 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017660 link->type = subLink->type;
17661 if (subLink->next != NULL) {
17662 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017663 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017664 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017665 while (subLink != NULL) {
17666 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017667 xmlMalloc(sizeof(xmlSchemaTypeLink));
17668 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017669 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017670 NULL);
17671 return (-1);
17672 }
17673 newLink->type = subLink->type;
17674 prevLink->next = newLink;
17675 prevLink = newLink;
17676 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017677
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017678 subLink = subLink->next;
17679 }
17680 }
17681 }
17682 }
17683 link = link->next;
17684 }
17685 return (0);
17686}
17687
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017688static void
17689xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17690{
17691 int has = 0, needVal = 0, normVal = 0;
17692
17693 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17694 if (has) {
17695 needVal = (type->baseType->flags &
17696 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17697 normVal = (type->baseType->flags &
17698 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17699 }
17700 if (type->facets != NULL) {
17701 xmlSchemaFacetPtr fac;
17702
17703 for (fac = type->facets; fac != NULL; fac = fac->next) {
17704 switch (fac->type) {
17705 case XML_SCHEMA_FACET_WHITESPACE:
17706 break;
17707 case XML_SCHEMA_FACET_PATTERN:
17708 normVal = 1;
17709 has = 1;
17710 break;
17711 case XML_SCHEMA_FACET_ENUMERATION:
17712 needVal = 1;
17713 normVal = 1;
17714 has = 1;
17715 break;
17716 default:
17717 has = 1;
17718 break;
17719 }
17720 }
17721 }
17722 if (normVal)
17723 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17724 if (needVal)
17725 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17726 if (has)
17727 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17728
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017729 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017730 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17731 /*
17732 * OPTIMIZE VAL TODO: Some facets need a computed value.
17733 */
17734 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17735 (prim->builtInType != XML_SCHEMAS_STRING)) {
17736 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17737 }
17738 }
17739}
17740
17741static int
17742xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17743{
17744
17745
17746 /*
17747 * Evaluate the whitespace-facet value.
17748 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017749 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017750 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17751 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017752 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017753 return (0);
17754
17755 if (type->facetSet != NULL) {
17756 xmlSchemaFacetLinkPtr lin;
17757
17758 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17759 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17760 switch (lin->facet->whitespace) {
17761 case XML_SCHEMAS_FACET_PRESERVE:
17762 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17763 break;
17764 case XML_SCHEMAS_FACET_REPLACE:
17765 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17766 break;
17767 case XML_SCHEMAS_FACET_COLLAPSE:
17768 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17769 break;
17770 default:
17771 return (-1);
17772 }
17773 return (0);
17774 }
17775 }
17776 }
17777 /*
17778 * For all ·atomic· datatypes other than string (and types ·derived·
17779 * by ·restriction· from it) the value of whiteSpace is fixed to
17780 * collapse
17781 */
17782 {
17783 xmlSchemaTypePtr anc;
17784
17785 for (anc = type->baseType; anc != NULL &&
17786 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17787 anc = anc->baseType) {
17788
17789 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17790 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17791 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17792
17793 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17794 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17795 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17796
17797 } else
17798 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17799 break;
17800 }
17801 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017802 }
17803 return (0);
17804}
17805
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017806static int
17807xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17808 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017809{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017810 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17811 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017812 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017813 return(0);
17814 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017815
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017816 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017817 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017818 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017819 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017820 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017821 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017822 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017823 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017824 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017825 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017826 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017827 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017828 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017829 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017830 * Corresponds to <simpleType><union>...
17831 */
17832 if (type->memberTypes == NULL) {
17833 /*
17834 * This one is really needed, so get out.
17835 */
17836 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17837 "union type has no member-types assigned");
17838 return(-1);
17839 }
17840 } else {
17841 /*
17842 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017843 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017844 if (type->baseType == NULL) {
17845 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17846 "type has no base-type assigned");
17847 return(-1);
17848 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017849 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017850 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17851 return(-1);
17852 /*
17853 * Variety
17854 * If the <restriction> alternative is chosen, then the
17855 * {variety} of the {base type definition}.
17856 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017857 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017858 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017859 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017860 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017861 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017862 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017863 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017864 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017865 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017866 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017867 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017868 * NOTE that we won't assign the memberTypes of the base,
17869 * since this will make trouble when freeing them; we will
17870 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017871 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017872 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017873 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017874 return(0);
17875}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017876
Daniel Veillard8651f532002-04-17 09:06:27 +000017877#ifdef DEBUG_TYPE
Daniel Veillard67952602006-01-05 15:29:44 +000017878static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017879xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17880 xmlSchemaTypePtr type)
17881{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017882 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017883 xmlGenericError(xmlGenericErrorContext,
17884 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017885 type->node->doc->URL,
17886 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017887 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017888 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017889 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017890 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017891 switch (type->contentType) {
17892 case XML_SCHEMA_CONTENT_SIMPLE:
17893 xmlGenericError(xmlGenericErrorContext, "simple\n");
17894 break;
17895 case XML_SCHEMA_CONTENT_ELEMENTS:
17896 xmlGenericError(xmlGenericErrorContext, "elements\n");
17897 break;
17898 case XML_SCHEMA_CONTENT_UNKNOWN:
17899 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17900 break;
17901 case XML_SCHEMA_CONTENT_EMPTY:
17902 xmlGenericError(xmlGenericErrorContext, "empty\n");
17903 break;
17904 case XML_SCHEMA_CONTENT_MIXED:
17905 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017906 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017907 xmlGenericError(xmlGenericErrorContext,
17908 "mixed as emptiable particle\n");
17909 else
17910 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17911 break;
17912 /* Removed, since not used. */
17913 /*
17914 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17915 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17916 break;
17917 */
17918 case XML_SCHEMA_CONTENT_BASIC:
17919 xmlGenericError(xmlGenericErrorContext, "basic\n");
17920 break;
17921 default:
17922 xmlGenericError(xmlGenericErrorContext,
17923 "not registered !!!\n");
17924 break;
17925 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017926 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017927}
Daniel Veillard8651f532002-04-17 09:06:27 +000017928#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017929
17930/*
17931* 3.14.6 Constraints on Simple Type Definition Schema Components
17932*/
17933static int
17934xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17935 xmlSchemaTypePtr type)
17936{
17937 int res, olderrs = pctxt->nberrors;
17938
17939 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17940 return(-1);
17941
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017942 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017943 return(0);
17944
17945 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17946 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17947
17948 if (type->baseType == NULL) {
17949 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17950 "missing baseType");
17951 goto exit_failure;
17952 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017953 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017954 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017955 /*
17956 * If a member type of a union is a union itself, we need to substitute
17957 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017958 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17959 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017960 */
17961 if ((type->memberTypes != NULL) &&
17962 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17963 return(-1);
17964 /*
17965 * SPEC src-simple-type 1
17966 * "The corresponding simple type definition, if any, must satisfy
17967 * the conditions set out in Constraints on Simple Type Definition
17968 * Schema Components (§3.14.6)."
17969 */
17970 /*
17971 * Schema Component Constraint: Simple Type Definition Properties Correct
17972 * (st-props-correct)
17973 */
17974 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17975 HFAILURE HERROR
17976 /*
17977 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17978 * (cos-st-restricts)
17979 */
17980 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17981 HFAILURE HERROR
17982 /*
17983 * TODO: Removed the error report, since it got annoying to get an
17984 * extra error report, if anything failed until now.
17985 * Enable this if needed.
17986 *
17987 * xmlSchemaPErr(ctxt, type->node,
17988 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17989 * "Simple type '%s' does not satisfy the constraints "
17990 * "on simple type definitions.\n",
17991 * type->name, NULL);
17992 */
17993 /*
17994 * Schema Component Constraint: Simple Type Restriction (Facets)
17995 * (st-restrict-facets)
17996 */
17997 res = xmlSchemaCheckFacetValues(type, pctxt);
17998 HFAILURE HERROR
17999 if ((type->facetSet != NULL) ||
18000 (type->baseType->facetSet != NULL)) {
18001 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
18002 HFAILURE HERROR
18003 }
18004 /*
18005 * Whitespace value.
18006 */
18007 res = xmlSchemaTypeFixupWhitespace(type);
18008 HFAILURE HERROR
18009 xmlSchemaTypeFixupOptimFacets(type);
18010
18011exit_error:
18012#ifdef DEBUG_TYPE
18013 xmlSchemaDebugFixedType(pctxt, type);
18014#endif
18015 if (olderrs != pctxt->nberrors)
18016 return(pctxt->err);
18017 return(0);
18018
18019exit_failure:
18020#ifdef DEBUG_TYPE
18021 xmlSchemaDebugFixedType(pctxt, type);
18022#endif
18023 return(-1);
18024}
18025
18026static int
18027xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18028 xmlSchemaTypePtr type)
18029{
18030 int res = 0, olderrs = pctxt->nberrors;
18031 xmlSchemaTypePtr baseType = type->baseType;
18032
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018033 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018034 return(0);
18035 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18036 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018037 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018038 "missing baseType");
18039 goto exit_failure;
18040 }
18041 /*
18042 * Fixup the base type.
18043 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018044 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018045 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018046 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18047 /*
18048 * Skip fixup if the base type is invalid.
18049 * TODO: Generate a warning!
18050 */
18051 return(0);
18052 }
18053 /*
18054 * This basically checks if the base type can be derived.
18055 */
18056 res = xmlSchemaCheckSRCCT(pctxt, type);
18057 HFAILURE HERROR
18058 /*
18059 * Fixup the content type.
18060 */
18061 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18062 /*
18063 * Corresponds to <complexType><simpleContent>...
18064 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018065 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018066 (baseType->contentTypeDef != NULL) &&
18067 (WXS_IS_RESTRICTION(type))) {
18068 xmlSchemaTypePtr contentBase, content;
18069#ifdef ENABLE_NAMED_LOCALS
18070 char buf[30];
18071 const xmlChar *tmpname;
18072#endif
18073 /*
18074 * SPEC (1) If <restriction> + base type is <complexType>,
18075 * "whose own {content type} is a simple type..."
18076 */
18077 if (type->contentTypeDef != NULL) {
18078 /*
18079 * SPEC (1.1) "the simple type definition corresponding to the
18080 * <simpleType> among the [children] of <restriction> if there
18081 * is one;"
18082 * Note that this "<simpleType> among the [children]" was put
18083 * into ->contentTypeDef during parsing.
18084 */
18085 contentBase = type->contentTypeDef;
18086 type->contentTypeDef = NULL;
18087 } else {
18088 /*
18089 * (1.2) "...otherwise (<restriction> has no <simpleType>
18090 * among its [children]), the simple type definition which
18091 * is the {content type} of the ... base type."
18092 */
18093 contentBase = baseType->contentTypeDef;
18094 }
18095 /*
18096 * SPEC
18097 * "... a simple type definition which restricts the simple
18098 * type definition identified in clause 1.1 or clause 1.2
18099 * with a set of facet components"
18100 *
18101 * Create the anonymous simple type, which will be the content
18102 * type of the complex type.
18103 */
18104#ifdef ENABLE_NAMED_LOCALS
18105 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18106 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018107 content = xmlSchemaAddType(pctxt, pctxt->schema,
18108 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018109 type->node, 0);
18110#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018111 content = xmlSchemaAddType(pctxt, pctxt->schema,
18112 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018113 type->node, 0);
18114#endif
18115 if (content == NULL)
18116 goto exit_failure;
18117 /*
18118 * We will use the same node as for the <complexType>
18119 * to have it somehow anchored in the schema doc.
18120 */
18121 content->type = XML_SCHEMA_TYPE_SIMPLE;
18122 content->baseType = contentBase;
18123 /*
18124 * Move the facets, previously anchored on the
18125 * complexType during parsing.
18126 */
18127 content->facets = type->facets;
18128 type->facets = NULL;
18129 content->facetSet = type->facetSet;
18130 type->facetSet = NULL;
18131
18132 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018133 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018134 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018135 /*
18136 * Fixup the newly created type. We don't need to check
18137 * for circularity here.
18138 */
18139 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18140 HFAILURE HERROR
18141 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18142 HFAILURE HERROR
18143
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018144 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018145 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18146 (WXS_IS_RESTRICTION(type))) {
18147 /*
18148 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18149 * an emptiable particle, then a simple type definition which
18150 * restricts the <restriction>'s <simpleType> child.
18151 */
18152 if ((type->contentTypeDef == NULL) ||
18153 (type->contentTypeDef->baseType == NULL)) {
18154 /*
18155 * TODO: Check if this ever happens.
18156 */
18157 xmlSchemaPCustomErr(pctxt,
18158 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018159 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018160 "Internal error: xmlSchemaTypeFixup, "
18161 "complex type '%s': the <simpleContent><restriction> "
18162 "is missing a <simpleType> child, but was not catched "
18163 "by xmlSchemaCheckSRCCT()", type->name);
18164 goto exit_failure;
18165 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018166 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018167 /*
18168 * SPEC (3) If <extension> + base is <complexType> with
18169 * <simpleType> content, "...then the {content type} of that
18170 * complex type definition"
18171 */
18172 if (baseType->contentTypeDef == NULL) {
18173 /*
18174 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18175 * should have catched this already.
18176 */
18177 xmlSchemaPCustomErr(pctxt,
18178 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018179 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018180 "Internal error: xmlSchemaTypeFixup, "
18181 "complex type '%s': the <extension>ed base type is "
18182 "a complex type with no simple content type",
18183 type->name);
18184 goto exit_failure;
18185 }
18186 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018187 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018188 /*
18189 * SPEC (4) <extension> + base is <simpleType>
18190 * "... then that simple type definition"
18191 */
18192 type->contentTypeDef = baseType;
18193 } else {
18194 /*
18195 * TODO: Check if this ever happens.
18196 */
18197 xmlSchemaPCustomErr(pctxt,
18198 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018199 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018200 "Internal error: xmlSchemaTypeFixup, "
18201 "complex type '%s' with <simpleContent>: unhandled "
18202 "derivation case", type->name);
18203 goto exit_failure;
18204 }
18205 } else {
18206 int dummySequence = 0;
18207 xmlSchemaParticlePtr particle =
18208 (xmlSchemaParticlePtr) type->subtypes;
18209 /*
18210 * Corresponds to <complexType><complexContent>...
18211 *
18212 * NOTE that the effective mixed was already set during parsing of
18213 * <complexType> and <complexContent>; its flag value is
18214 * XML_SCHEMAS_TYPE_MIXED.
18215 *
18216 * Compute the "effective content":
18217 * (2.1.1) + (2.1.2) + (2.1.3)
18218 */
18219 if ((particle == NULL) ||
18220 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18221 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18222 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18223 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18224 (particle->minOccurs == 0))) &&
18225 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18226 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18227 /*
18228 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18229 * a particle whose properties are as follows:..."
18230 *
18231 * Empty sequence model group with
18232 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18233 * NOTE that we sill assign it the <complexType> node to
18234 * somehow anchor it in the doc.
18235 */
18236 if ((particle == NULL) ||
18237 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18238 /*
18239 * Create the particle.
18240 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018241 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018242 type->node, 1, 1);
18243 if (particle == NULL)
18244 goto exit_failure;
18245 /*
18246 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018247 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018248 particle->children = (xmlSchemaTreeItemPtr)
18249 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18250 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18251 if (particle->children == NULL)
18252 goto exit_failure;
18253
18254 type->subtypes = (xmlSchemaTypePtr) particle;
18255 }
18256 dummySequence = 1;
18257 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18258 } else {
18259 /*
18260 * SPEC (2.1.5) "otherwise empty"
18261 */
18262 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18263 }
18264 } else {
18265 /*
18266 * SPEC (2.2) "otherwise the particle corresponding to the
18267 * <all>, <choice>, <group> or <sequence> among the
18268 * [children]."
18269 */
18270 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18271 }
18272 /*
18273 * Compute the "content type".
18274 */
18275 if (WXS_IS_RESTRICTION(type)) {
18276 /*
18277 * SPEC (3.1) "If <restriction>..."
18278 * (3.1.1) + (3.1.2) */
18279 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18280 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18281 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18282 }
18283 } else {
18284 /*
18285 * SPEC (3.2) "If <extension>..."
18286 */
18287 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18288 /*
18289 * SPEC (3.2.1)
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018290 * "If the ·effective content· is empty, then the
18291 * {content type} of the [...] base ..."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018292 */
18293 type->contentType = baseType->contentType;
18294 type->subtypes = baseType->subtypes;
18295 /*
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018296 * Fixes bug #347316:
18297 * This is the case when the base type has a simple
18298 * type definition as content.
18299 */
18300 type->contentTypeDef = baseType->contentTypeDef;
18301 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018302 * NOTE that the effective mixed is ignored here.
18303 */
18304 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18305 /*
18306 * SPEC (3.2.2)
18307 */
18308 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18309 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18310 } else {
18311 /*
18312 * SPEC (3.2.3)
18313 */
18314 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18315 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18316 /*
18317 * "A model group whose {compositor} is sequence and whose
18318 * {particles} are..."
18319 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018320 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18321 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18322 ((WXS_TYPE_PARTICLE_TERM(type))->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 "The type has an 'all' model group in its "
18333 "{content type} and thus cannot be derived from "
18334 "a non-empty 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 ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18341 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18342 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18343 XML_SCHEMA_TYPE_ALL))
18344 {
18345 /*
18346 * SPEC cos-all-limited (1)
18347 */
18348 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18349 /* TODO: error code */
18350 XML_SCHEMAP_COS_ALL_LIMITED,
18351 WXS_ITEM_NODE(type), NULL,
18352 "A type cannot be derived by extension from a type "
18353 "which has an 'all' model group in its "
18354 "{content type}, since this would produce a "
18355 "'sequence' model group containing the 'all' "
18356 "model group; 'all' model groups are not "
18357 "allowed to appear inside other model groups",
18358 NULL, NULL);
18359
18360 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018361 xmlSchemaTreeItemPtr effectiveContent =
18362 (xmlSchemaTreeItemPtr) type->subtypes;
18363 /*
18364 * Create the particle.
18365 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018366 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018367 type->node, 1, 1);
18368 if (particle == NULL)
18369 goto exit_failure;
18370 /*
18371 * Create the "sequence" model group.
18372 */
18373 particle->children = (xmlSchemaTreeItemPtr)
18374 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18375 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18376 if (particle->children == NULL)
18377 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018378 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018379 /*
18380 * SPEC "the particle of the {content type} of
18381 * the ... base ..."
18382 * Create a duplicate of the base type's particle
18383 * and assign its "term" to it.
18384 */
18385 particle->children->children =
18386 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018387 type->node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018388 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18389 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18390 if (particle->children->children == NULL)
18391 goto exit_failure;
18392 particle = (xmlSchemaParticlePtr)
18393 particle->children->children;
18394 particle->children =
18395 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18396 /*
18397 * SPEC "followed by the ·effective content·."
18398 */
18399 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018400 /*
18401 * This all will result in:
18402 * new-particle
18403 * --> new-sequence(
18404 * new-particle
18405 * --> base-model,
18406 * this-particle
18407 * --> this-model
18408 * )
18409 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018410 } else {
18411 /*
18412 * This is the case when there is already an empty
18413 * <sequence> with minOccurs==maxOccurs==1.
18414 * Just add the base types's content type.
18415 * NOTE that, although we miss to add an intermediate
18416 * <sequence>, this should produce no difference to
18417 * neither the regex compilation of the content model,
18418 * nor to the complex type contraints.
18419 */
18420 particle->children->children =
18421 (xmlSchemaTreeItemPtr) baseType->subtypes;
18422 }
18423 }
18424 }
18425 }
18426 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018427 * Now fixup attribute uses:
18428 * - expand attr. group references
18429 * - intersect attribute wildcards
18430 * - inherit attribute uses of the base type
18431 * - inherit or union attr. wildcards if extending
18432 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018433 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018434 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018435 HFAILURE HERROR
18436 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018437 * Apply the complex type component constraints; this will not
18438 * check attributes, since this is done in
18439 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018440 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018441 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018442 HFAILURE HERROR
18443
18444#ifdef DEBUG_TYPE
18445 xmlSchemaDebugFixedType(pctxt, type);
18446#endif
18447 if (olderrs != pctxt->nberrors)
18448 return(pctxt->err);
18449 else
18450 return(0);
18451
18452exit_error:
18453 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18454#ifdef DEBUG_TYPE
18455 xmlSchemaDebugFixedType(pctxt, type);
18456#endif
18457 return(pctxt->err);
18458
18459exit_failure:
18460 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18461#ifdef DEBUG_TYPE
18462 xmlSchemaDebugFixedType(pctxt, type);
18463#endif
18464 return(-1);
18465}
18466
18467
18468/**
18469 * xmlSchemaTypeFixup:
18470 * @typeDecl: the schema type definition
18471 * @ctxt: the schema parser context
18472 *
18473 * Fixes the content model of the type.
18474 * URGENT TODO: We need an int result!
18475 */
18476static int
18477xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018478 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018479{
18480 if (type == NULL)
18481 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018482 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18483 AERROR_INT("xmlSchemaTypeFixup",
18484 "this function needs a parser context");
18485 return(-1);
18486 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018487 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018488 return(0);
18489 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018490 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018491 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018492 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018493 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018494}
18495
18496/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018497 * xmlSchemaCheckFacet:
18498 * @facet: the facet
18499 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018500 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018501 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018502 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018503 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018504 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018505 * Returns 0 if valid, a positive error code if not valid and
18506 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018507 */
18508int
18509xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018510 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018511 xmlSchemaParserCtxtPtr pctxt,
18512 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018513{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018514 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018515
Daniel Veillardce682bc2004-11-05 17:22:25 +000018516 if ((facet == NULL) || (typeDecl == NULL))
18517 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018518 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018519 * TODO: will the parser context be given if used from
18520 * the relaxNG module?
18521 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018522 if (pctxt == NULL)
18523 ctxtGiven = 0;
18524 else
18525 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018526
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018527 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018528 case XML_SCHEMA_FACET_MININCLUSIVE:
18529 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18530 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018531 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18532 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018533 /*
18534 * Okay we need to validate the value
18535 * at that point.
18536 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018537 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018538
18539 /* 4.3.5.5 Constraints on enumeration Schema Components
18540 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018541 * It is an ·error· if any member of {value} is not in the
18542 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018543 *
18544 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018545 * The value ·must· be in the
18546 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018547 */
18548 /*
18549 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018550 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018551 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018552 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018553 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018554 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018555 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018556 */
18557 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18558 base = typeDecl->baseType;
18559 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018560 PERROR_INT("xmlSchemaCheckFacet",
18561 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018562 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018563 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018564 } else
18565 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018566
18567 if (! ctxtGiven) {
18568 /*
18569 * A context is needed if called from RelaxNG.
18570 */
18571 pctxt = xmlSchemaNewParserCtxt("*");
18572 if (pctxt == NULL)
18573 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018574 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018575 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018576 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018577 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018578 * facet->node is just the node holding the facet
18579 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018580 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018581 */
18582 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018583 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018584 facet->value, &(facet->val), 1, 1, 0);
18585 if (ret != 0) {
18586 if (ret < 0) {
18587 /* No error message for RelaxNG. */
18588 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018589 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018590 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18591 "Internal error: xmlSchemaCheckFacet, "
18592 "failed to validate the value '%s' of the "
18593 "facet '%s' against the base type",
18594 facet->value, xmlSchemaFacetTypeToString(facet->type));
18595 }
18596 goto internal_error;
18597 }
18598 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18599 /* No error message for RelaxNG. */
18600 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018601 xmlChar *str = NULL;
18602
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018603 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018604 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018605 "The value '%s' of the facet does not validate "
18606 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018607 facet->value,
18608 xmlSchemaFormatQName(&str,
18609 base->targetNamespace, base->name));
18610 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018611 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018612 goto exit;
18613 } else if (facet->val == NULL) {
18614 if (ctxtGiven) {
18615 PERROR_INT("xmlSchemaCheckFacet",
18616 "value was not computed");
18617 }
18618 TODO
18619 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018620 break;
18621 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018622 case XML_SCHEMA_FACET_PATTERN:
18623 facet->regexp = xmlRegexpCompile(facet->value);
18624 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018625 ret = XML_SCHEMAP_REGEXP_INVALID;
18626 /* No error message for RelaxNG. */
18627 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018628 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018629 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018630 "The value '%s' of the facet 'pattern' is not a "
18631 "valid regular expression",
18632 facet->value, NULL);
18633 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018634 }
18635 break;
18636 case XML_SCHEMA_FACET_TOTALDIGITS:
18637 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18638 case XML_SCHEMA_FACET_LENGTH:
18639 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018640 case XML_SCHEMA_FACET_MINLENGTH:
18641
18642 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18643 ret = xmlSchemaValidatePredefinedType(
18644 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18645 facet->value, &(facet->val));
18646 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018647 ret = xmlSchemaValidatePredefinedType(
18648 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18649 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018650 }
18651 if (ret != 0) {
18652 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018653 /* No error message for RelaxNG. */
18654 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018655 PERROR_INT("xmlSchemaCheckFacet",
18656 "validating facet value");
18657 }
18658 goto internal_error;
18659 }
18660 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18661 /* No error message for RelaxNG. */
18662 if (ctxtGiven) {
18663 /* error code */
18664 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18665 ret, facet->node, WXS_BASIC_CAST typeDecl,
18666 "The value '%s' of the facet '%s' is not a valid '%s'",
18667 facet->value,
18668 xmlSchemaFacetTypeToString(facet->type),
18669 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
18670 BAD_CAST "nonNegativeInteger" :
18671 BAD_CAST "positiveInteger",
18672 NULL);
18673 }
18674 }
18675 break;
18676
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018677 case XML_SCHEMA_FACET_WHITESPACE:{
18678 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18679 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18680 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18681 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18682 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18683 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18684 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018685 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18686 /* No error message for RelaxNG. */
18687 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018688 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018689 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018690 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018691 "The value '%s' of the facet 'whitespace' is not "
18692 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018693 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018694 }
18695 }
18696 default:
18697 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018698 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018699exit:
18700 if ((! ctxtGiven) && (pctxt != NULL))
18701 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018702 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018703internal_error:
18704 if ((! ctxtGiven) && (pctxt != NULL))
18705 xmlSchemaFreeParserCtxt(pctxt);
18706 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018707}
18708
18709/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018710 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018711 * @typeDecl: the schema type definition
18712 * @ctxt: the schema parser context
18713 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018714 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018715 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018716static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018717xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018718 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018719{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018720 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018721 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018722 /*
18723 * NOTE: It is intended to use the facets list, instead
18724 * of facetSet.
18725 */
18726 if (typeDecl->facets != NULL) {
18727 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018728
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018729 /*
18730 * Temporarily assign the "schema" to the validation context
18731 * of the parser context. This is needed for NOTATION validation.
18732 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018733 if (pctxt->vctxt == NULL) {
18734 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18735 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018736 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018737 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018738 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018739 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18740 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018741 facet = facet->next;
18742 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018743 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018744 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018745 if (olderrs != pctxt->nberrors)
18746 return(pctxt->err);
18747 return(0);
18748exit_failure:
18749 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018750}
18751
18752/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018753 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018754 * @ctxtMGroup: the searched model group
18755 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018756 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018757 *
18758 * This one is intended to be used by
18759 * xmlSchemaCheckGroupDefCircular only.
18760 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018761 * Returns the particle with the circular model group definition reference,
18762 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018763 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018764static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018765xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018766 xmlSchemaTreeItemPtr particle)
18767{
18768 xmlSchemaTreeItemPtr circ = NULL;
18769 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018770 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018771
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018772 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018773 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018774 if (term == NULL)
18775 continue;
18776 switch (term->type) {
18777 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018778 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018779 if (gdef == groupDef)
18780 return (particle);
18781 /*
18782 * Mark this model group definition to avoid infinite
18783 * recursion on circular references not yet examined.
18784 */
18785 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18786 continue;
18787 if (gdef->children != NULL) {
18788 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18789 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18790 gdef->children->children);
18791 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18792 if (circ != NULL)
18793 return (circ);
18794 }
18795 break;
18796 case XML_SCHEMA_TYPE_SEQUENCE:
18797 case XML_SCHEMA_TYPE_CHOICE:
18798 case XML_SCHEMA_TYPE_ALL:
18799 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18800 if (circ != NULL)
18801 return (circ);
18802 break;
18803 default:
18804 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018805 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018806 }
18807 return (NULL);
18808}
18809
18810/**
18811 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018812 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018813 * @ctxt: the parser context
18814 * @name: the name
18815 *
18816 * Checks for circular references to model group definitions.
18817 */
18818static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018819xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018820 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018821{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018822 /*
18823 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018824 * 2 Circular groups are disallowed. That is, within the {particles}
18825 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018826 * is the group itself.
18827 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018828 if ((item == NULL) ||
18829 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18830 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018831 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018832 {
18833 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018834
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018835 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018836 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018837 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018838 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018839 * TODO: The error report is not adequate: this constraint
18840 * is defined for model groups but not definitions, but since
18841 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018842 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018843 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018844 */
18845 xmlSchemaPCustomErr(ctxt,
18846 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018847 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018848 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018849 "defined", xmlSchemaFormatQName(&str,
18850 item->targetNamespace, item->name));
18851 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018852 /*
18853 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018854 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018855 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018856 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018857 }
18858 }
18859}
18860
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018861/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018862 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018863 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018864 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018865 *
18866 * Assigns the model group of model group definitions to the "term"
18867 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018868 * In xmlSchemaResolveModelGroupParticleReferences the model group
18869 * definitions were assigned to the "term", since needed for the
18870 * circularity check.
18871 *
18872 * Schema Component Constraint:
18873 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018874 */
18875static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018876xmlSchemaModelGroupToModelGroupDefFixup(
18877 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18878 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018879{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018880 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18881
18882 while (particle != NULL) {
18883 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18884 ((WXS_PARTICLE_TERM(particle))->type !=
18885 XML_SCHEMA_TYPE_GROUP))
18886 {
18887 particle = WXS_PTC_CAST particle->next;
18888 continue;
18889 }
18890 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18891 /*
18892 * TODO: Remove the particle.
18893 */
18894 WXS_PARTICLE_TERM(particle) = NULL;
18895 particle = WXS_PTC_CAST particle->next;
18896 continue;
18897 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018898 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018899 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018900 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018901 WXS_PARTICLE_TERM(particle) =
18902 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18903
18904 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018905 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018906}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018907
18908/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018909 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018910 * @ctxtGr: the searched attribute group
18911 * @attr: the current attribute list to be processed
18912 *
18913 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018914 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018915 *
18916 * Returns the circular attribute grou reference, otherwise NULL.
18917 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018918static xmlSchemaQNameRefPtr
18919xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18920 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018921{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018922 xmlSchemaAttributeGroupPtr gr;
18923 xmlSchemaQNameRefPtr ref, circ;
18924 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018925 /*
18926 * We will search for an attribute group reference which
18927 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018928 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018929 for (i = 0; i < list->nbItems; i++) {
18930 ref = list->items[i];
18931 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18932 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18933 (ref->item != NULL))
18934 {
18935 gr = WXS_ATTR_GROUP_CAST ref->item;
18936 if (gr == ctxtGr)
18937 return(ref);
18938 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18939 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018940 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018941 * Mark as visited to avoid infinite recursion on
18942 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018943 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018944 if ((gr->attrUses) &&
18945 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18946 {
18947 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18948 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18949 (xmlSchemaItemListPtr) gr->attrUses);
18950 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18951 if (circ != NULL)
18952 return (circ);
18953 }
18954
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018955 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018956 }
18957 return (NULL);
18958}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018959
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018960/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018961 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018962 * attrGr: the attribute group definition
18963 * @ctxt: the parser context
18964 * @name: the name
18965 *
18966 * Checks for circular references of attribute groups.
18967 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018968static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018969xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018970 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018971{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018972 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018973 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018974 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018975 * 3 Circular group reference is disallowed outside <redefine>.
18976 * That is, unless this element information item's parent is
18977 * <redefine>, then among the [children], if any, there must
18978 * not be an <attributeGroup> with ref [attribute] which resolves
18979 * to the component corresponding to this <attributeGroup>. Indirect
18980 * circularity is also ruled out. That is, when QName resolution
18981 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18982 * any <attributeGroup>s with a ref [attribute] among the [children],
18983 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018984 * which resolves to the component corresponding to this <attributeGroup>.
18985 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018986 if (attrGr->attrUses == NULL)
18987 return(0);
18988 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18989 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018990 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018991 xmlSchemaQNameRefPtr circ;
18992
18993 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18994 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018995 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018996 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018997 /*
18998 * TODO: Report the referenced attr group as QName.
18999 */
19000 xmlSchemaPCustomErr(ctxt,
19001 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019002 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019003 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019004 "defined", xmlSchemaGetComponentQName(&str, attrGr));
19005 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019006 /*
19007 * NOTE: We will cut the reference to avoid further
19008 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019009 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019010 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019011 circ->item = NULL;
19012 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019013 }
19014 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019015 return(0);
19016}
19017
19018static int
19019xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19020 xmlSchemaAttributeGroupPtr attrGr);
19021
19022/**
19023 * xmlSchemaExpandAttributeGroupRefs:
19024 * @pctxt: the parser context
19025 * @node: the node of the component holding the attribute uses
19026 * @completeWild: the intersected wildcard to be returned
19027 * @list: the attribute uses
19028 *
19029 * Substitutes contained attribute group references
19030 * for their attribute uses. Wilcards are intersected.
19031 * Attribute use prohibitions are removed from the list
19032 * and returned via the @prohibs list.
19033 * Pointlessness of attr. prohibs, if a matching attr. decl
19034 * is existent a well, are checked.
19035 */
19036static int
19037xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19038 xmlSchemaBasicItemPtr item,
19039 xmlSchemaWildcardPtr *completeWild,
19040 xmlSchemaItemListPtr list,
19041 xmlSchemaItemListPtr prohibs)
19042{
19043 xmlSchemaAttributeGroupPtr gr;
19044 xmlSchemaAttributeUsePtr use;
19045 xmlSchemaItemListPtr sublist;
19046 int i, j;
19047 int created = (*completeWild == NULL) ? 0 : 1;
19048
19049 if (prohibs)
19050 prohibs->nbItems = 0;
19051
19052 for (i = 0; i < list->nbItems; i++) {
19053 use = list->items[i];
19054
19055 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
19056 if (prohibs == NULL) {
19057 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19058 "unexpected attr prohibition found");
19059 return(-1);
19060 }
19061 /*
19062 * Remove from attribute uses.
19063 */
19064 if (xmlSchemaItemListRemove(list, i) == -1)
19065 return(-1);
19066 i--;
19067 /*
19068 * Note that duplicate prohibitions were already
19069 * handled at parsing time.
19070 */
19071 /*
19072 * Add to list of prohibitions.
19073 */
19074 xmlSchemaItemListAddSize(prohibs, 2, use);
19075 continue;
19076 }
19077 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19078 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19079 {
19080 if ((WXS_QNAME_CAST use)->item == NULL)
19081 return(-1);
19082 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19083 /*
19084 * Expand the referenced attr. group.
19085 * TODO: remove this, this is done in a previous step, so
19086 * already done here.
19087 */
19088 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19089 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19090 return(-1);
19091 }
19092 /*
19093 * Build the 'complete' wildcard; i.e. intersect multiple
19094 * wildcards.
19095 */
19096 if (gr->attributeWildcard != NULL) {
19097 if (*completeWild == NULL) {
19098 *completeWild = gr->attributeWildcard;
19099 } else {
19100 if (! created) {
19101 xmlSchemaWildcardPtr tmpWild;
19102
19103 /*
19104 * Copy the first encountered wildcard as context,
19105 * except for the annotation.
19106 *
19107 * Although the complete wildcard might not correspond
19108 * to any node in the schema, we will anchor it on
19109 * the node of the owner component.
19110 */
19111 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19112 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19113 WXS_ITEM_NODE(item));
19114 if (tmpWild == NULL)
19115 return(-1);
19116 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19117 tmpWild, *completeWild) == -1)
19118 return (-1);
19119 tmpWild->processContents = (*completeWild)->processContents;
19120 *completeWild = tmpWild;
19121 created = 1;
19122 }
19123
19124 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19125 gr->attributeWildcard) == -1)
19126 return(-1);
19127 }
19128 }
19129 /*
19130 * Just remove the reference if the referenced group does not
19131 * contain any attribute uses.
19132 */
19133 if (gr->attrUses == NULL) {
19134 if (xmlSchemaItemListRemove(list, i) == -1)
19135 return(-1);
19136 i--;
19137 continue;
19138 }
19139 /*
19140 * Add the attribute uses.
19141 */
19142 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19143 if (sublist->nbItems != 0) {
19144 list->items[i] = sublist->items[0];
19145 if (sublist->nbItems != 1) {
19146 for (j = 1; j < sublist->nbItems; j++) {
19147 i++;
19148 if (xmlSchemaItemListInsert(list,
19149 sublist->items[j], i) == -1)
19150 return(-1);
19151 }
19152 }
19153 }
19154 }
19155
19156 }
19157 /*
19158 * Handle pointless prohibitions of declared attributes.
19159 */
19160 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19161 xmlSchemaAttributeUseProhibPtr prohib;
19162
19163 for (i = prohibs->nbItems -1; i >= 0; i--) {
19164 prohib = prohibs->items[i];
19165 for (j = 0; j < list->nbItems; j++) {
19166 use = list->items[j];
19167
19168 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19169 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19170 {
19171 xmlChar *str = NULL;
19172
19173 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19174 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19175 prohib->node, NULL,
19176 "Skipping pointless attribute use prohibition "
19177 "'%s', since a corresponding attribute use "
19178 "exists already in the type definition",
19179 xmlSchemaFormatQName(&str,
19180 prohib->targetNamespace, prohib->name),
19181 NULL, NULL);
19182 FREE_AND_NULL(str);
19183 /*
19184 * Remove the prohibition.
19185 */
19186 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19187 return(-1);
19188 break;
19189 }
19190 }
19191 }
19192 }
19193 return(0);
19194}
19195
19196/**
19197 * xmlSchemaAttributeGroupExpandRefs:
19198 * @pctxt: the parser context
19199 * @attrGr: the attribute group definition
19200 *
19201 * Computation of:
19202 * {attribute uses} property
19203 * {attribute wildcard} property
19204 *
19205 * Substitutes contained attribute group references
19206 * for their attribute uses. Wilcards are intersected.
19207 */
19208static int
19209xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19210 xmlSchemaAttributeGroupPtr attrGr)
19211{
19212 if ((attrGr->attrUses == NULL) ||
19213 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19214 return(0);
19215
19216 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19217 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19218 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19219 return(-1);
19220 return(0);
19221}
19222
19223/**
19224 * xmlSchemaAttributeGroupExpandRefs:
19225 * @pctxt: the parser context
19226 * @attrGr: the attribute group definition
19227 *
19228 * Substitutes contained attribute group references
19229 * for their attribute uses. Wilcards are intersected.
19230 *
19231 * Schema Component Constraint:
19232 * Attribute Group Definition Properties Correct (ag-props-correct)
19233 */
19234static int
19235xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19236 xmlSchemaAttributeGroupPtr attrGr)
19237{
19238 /*
19239 * SPEC ag-props-correct
19240 * (1) "The values of the properties of an attribute group definition
19241 * must be as described in the property tableau in The Attribute
19242 * Group Definition Schema Component (§3.6.1), modulo the impact of
19243 * Missing Sub-components (§5.3);"
19244 */
19245
19246 if ((attrGr->attrUses != NULL) &&
19247 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19248 {
19249 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19250 xmlSchemaAttributeUsePtr use, tmp;
19251 int i, j, hasId = 0;
19252
19253 for (i = uses->nbItems -1; i >= 0; i--) {
19254 use = uses->items[i];
19255 /*
19256 * SPEC ag-props-correct
19257 * (2) "Two distinct members of the {attribute uses} must not have
19258 * {attribute declaration}s both of whose {name}s match and whose
19259 * {target namespace}s are identical."
19260 */
19261 if (i > 0) {
19262 for (j = i -1; j >= 0; j--) {
19263 tmp = uses->items[j];
19264 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19265 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19266 (WXS_ATTRUSE_DECL_TNS(use) ==
19267 WXS_ATTRUSE_DECL_TNS(tmp)))
19268 {
19269 xmlChar *str = NULL;
19270
19271 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19272 XML_SCHEMAP_AG_PROPS_CORRECT,
19273 attrGr->node, WXS_BASIC_CAST attrGr,
19274 "Duplicate %s",
19275 xmlSchemaGetComponentDesignation(&str, use),
19276 NULL);
19277 FREE_AND_NULL(str);
19278 /*
19279 * Remove the duplicate.
19280 */
19281 if (xmlSchemaItemListRemove(uses, i) == -1)
19282 return(-1);
19283 goto next_use;
19284 }
19285 }
19286 }
19287 /*
19288 * SPEC ag-props-correct
19289 * (3) "Two distinct members of the {attribute uses} must not have
19290 * {attribute declaration}s both of whose {type definition}s are or
19291 * are derived from ID."
19292 * TODO: Does 'derived' include member-types of unions?
19293 */
19294 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19295 if (xmlSchemaIsDerivedFromBuiltInType(
19296 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19297 {
19298 if (hasId) {
19299 xmlChar *str = NULL;
19300
19301 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19302 XML_SCHEMAP_AG_PROPS_CORRECT,
19303 attrGr->node, WXS_BASIC_CAST attrGr,
19304 "There must not exist more than one attribute "
19305 "declaration of type 'xs:ID' "
19306 "(or derived from 'xs:ID'). The %s violates this "
19307 "constraint",
19308 xmlSchemaGetComponentDesignation(&str, use),
19309 NULL);
19310 FREE_AND_NULL(str);
19311 if (xmlSchemaItemListRemove(uses, i) == -1)
19312 return(-1);
19313 }
19314 hasId = 1;
19315 }
19316 }
19317next_use: {}
19318 }
19319 }
19320 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019321}
19322
19323/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019324 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019325 * @attrgrpDecl: the schema attribute definition
19326 * @ctxt: the schema parser context
19327 * @name: the attribute name
19328 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019329 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019330 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019331static int
19332xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19333 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019334{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019335 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019337 if (ref->item != NULL)
19338 return(0);
19339 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19340 ref->name,
19341 ref->targetNamespace);
19342 if (group == NULL) {
19343 xmlSchemaPResCompAttrErr(ctxt,
19344 XML_SCHEMAP_SRC_RESOLVE,
19345 NULL, ref->node,
19346 "ref", ref->name, ref->targetNamespace,
19347 ref->itemType, NULL);
19348 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019350 ref->item = WXS_BASIC_CAST group;
19351 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019352}
19353
19354/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019355 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019356 * @item: an schema attribute declaration/use
19357 * @ctxt: a schema parser context
19358 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019359 *
19360 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019361 * Schema Component Constraint:
19362 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019363 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019364 * Validates the value constraints of an attribute declaration/use.
19365 * NOTE that this needs the simle type definitions to be already
19366 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019367 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019368static int
19369xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19370 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019371{
19372
19373 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019374 * SPEC a-props-correct (1)
19375 * "The values of the properties of an attribute declaration must
19376 * be as described in the property tableau in The Attribute
19377 * Declaration Schema Component (§3.2.1), modulo the impact of
19378 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019379 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019380
19381 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19382 return(0);
19383
19384 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019385 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019386
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019387 /*
19388 * SPEC a-props-correct (3)
19389 * "If the {type definition} is or is derived from ID then there
19390 * must not be a {value constraint}."
19391 */
19392 if (xmlSchemaIsDerivedFromBuiltInType(
19393 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19394 {
19395 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19396 XML_SCHEMAP_A_PROPS_CORRECT_3,
19397 NULL, WXS_BASIC_CAST attr,
19398 "Value constraints are not allowed if the type definition "
19399 "is or is derived from xs:ID",
19400 NULL, NULL);
19401 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019402 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019403 /*
19404 * SPEC a-props-correct (2)
19405 * "if there is a {value constraint}, the canonical lexical
19406 * representation of its value must be ·valid· with respect
19407 * to the {type definition} as defined in String Valid (§3.14.4)."
19408 * TODO: Don't care about the *cononical* stuff here, this requirement
19409 * will be removed in WXS 1.1 anyway.
19410 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019411 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019412 attr->node, WXS_ATTR_TYPEDEF(attr),
19413 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019414 1, 1, 0);
19415 if (ret != 0) {
19416 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019417 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019418 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019419 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019420 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019421 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019422 XML_SCHEMAP_A_PROPS_CORRECT_2,
19423 NULL, WXS_BASIC_CAST attr,
19424 "The value of the value constraint is not valid",
19425 NULL, NULL);
19426 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019427 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019428 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019429
19430 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019431}
19432
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019433static xmlSchemaElementPtr
19434xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19435 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019436{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019437 xmlSchemaElementPtr ret;
19438
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019439 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019440 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019441 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019442 return (ancestor);
19443
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019444 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019445 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019446 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019447 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019448 WXS_SUBST_HEAD(ancestor));
19449 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019450
19451 return (ret);
19452}
19453
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019454/**
19455 * xmlSchemaCheckElemPropsCorrect:
19456 * @ctxt: a schema parser context
19457 * @decl: the element declaration
19458 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019459 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019460 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019461 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019462 *
19463 * STATUS:
19464 * missing: (6)
19465 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019466static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019467xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19468 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019469{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019470 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019471 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019472 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019473 * SPEC (1) "The values of the properties of an element declaration
19474 * must be as described in the property tableau in The Element
19475 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19476 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019477 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019478 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19479 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019480
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019481 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019482 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019483 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019484 * affiliation}, then {scope} must be global."
19485 */
19486 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19487 xmlSchemaPCustomErr(pctxt,
19488 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019489 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019490 "Only global element declarations can have a "
19491 "substitution group affiliation", NULL);
19492 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019493 }
19494 /*
19495 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19496 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019497 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019498 * property."
19499 */
19500 if (head == elemDecl)
19501 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019502 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019503 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19504 else
19505 circ = NULL;
19506 if (circ != NULL) {
19507 xmlChar *strA = NULL, *strB = NULL;
19508
19509 xmlSchemaPCustomErrExt(pctxt,
19510 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019511 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019512 "The element declaration '%s' defines a circular "
19513 "substitution group to element declaration '%s'",
19514 xmlSchemaGetComponentQName(&strA, circ),
19515 xmlSchemaGetComponentQName(&strB, head),
19516 NULL);
19517 FREE_AND_NULL(strA)
19518 FREE_AND_NULL(strB)
19519 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19520 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019521 /*
19522 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019523 * the {type definition}
19524 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019525 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019526 * of the {substitution group exclusions} of the {substitution group
19527 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19528 * (if the {type definition} is complex) or as defined in
19529 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019530 * simple)."
19531 *
19532 * NOTE: {substitution group exclusions} means the values of the
19533 * attribute "final".
19534 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019535
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019536 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019537 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019538
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019539 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19540 set |= SUBSET_EXTENSION;
19541 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19542 set |= SUBSET_RESTRICTION;
19543
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019544 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019545 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019546 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19547
19548 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019549 xmlSchemaPCustomErrExt(pctxt,
19550 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019551 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019552 "The type definition '%s' was "
19553 "either rejected by the substitution group "
19554 "affiliation '%s', or not validly derived from its type "
19555 "definition '%s'",
19556 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019557 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019558 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019559 FREE_AND_NULL(strA)
19560 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019561 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019562 }
19563 }
19564 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019565 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019566 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019567 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019568 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019569 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019570 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019571 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019572 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019573 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019574 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019575 (WXS_IS_COMPLEX(typeDef) &&
19576 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019577 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19578 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019579
19580 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19581 xmlSchemaPCustomErr(pctxt,
19582 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019583 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019584 "The type definition (or type definition's content type) is or "
19585 "is derived from ID; value constraints are not allowed in "
19586 "conjunction with such a type definition", NULL);
19587 } else if (elemDecl->value != NULL) {
19588 int vcret;
19589 xmlNodePtr node = NULL;
19590
19591 /*
19592 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19593 * representation of its value must be ·valid· with respect to the
19594 * {type definition} as defined in Element Default Valid (Immediate)
19595 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019596 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019597 if (typeDef == NULL) {
19598 xmlSchemaPErr(pctxt, elemDecl->node,
19599 XML_SCHEMAP_INTERNAL,
19600 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19601 "type is missing... skipping validation of "
19602 "the value constraint", NULL, NULL);
19603 return (-1);
19604 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019605 if (elemDecl->node != NULL) {
19606 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19607 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19608 BAD_CAST "fixed");
19609 else
19610 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19611 BAD_CAST "default");
19612 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019613 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19614 typeDef, elemDecl->value, &(elemDecl->defVal));
19615 if (vcret != 0) {
19616 if (vcret < 0) {
19617 PERROR_INT("xmlSchemaElemCheckValConstr",
19618 "failed to validate the value constraint of an "
19619 "element declaration");
19620 return (-1);
19621 }
19622 return (vcret);
19623 }
19624 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019625
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019626 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019627}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019628
19629/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019630 * xmlSchemaCheckElemSubstGroup:
19631 * @ctxt: a schema parser context
19632 * @decl: the element declaration
19633 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019634 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019635 * Schema Component Constraint:
19636 * Substitution Group (cos-equiv-class)
19637 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019638 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019639 * a list will be built for each subst. group head, holding all direct
19640 * referents to this head.
19641 * NOTE that this function needs:
19642 * 1. circular subst. groups to be checked beforehand
19643 * 2. the declaration's type to be derived from the head's type
19644 *
19645 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019646 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019647 */
19648static void
19649xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19650 xmlSchemaElementPtr elemDecl)
19651{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019652 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019653 /* SPEC (1) "Its {abstract} is false." */
19654 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19655 return;
19656 {
19657 xmlSchemaElementPtr head;
19658 xmlSchemaTypePtr headType, type;
19659 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019660 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019661 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19662 * {disallowed substitutions} as the blocking constraint, as defined in
19663 * Substitution Group OK (Transitive) (§3.3.6)."
19664 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019665 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19666 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019667 set = 0;
19668 methSet = 0;
19669 /*
19670 * The blocking constraints.
19671 */
19672 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19673 continue;
19674 headType = head->subtypes;
19675 type = elemDecl->subtypes;
19676 if (headType == type)
19677 goto add_member;
19678 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19679 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19680 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19681 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19682 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019683 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019684 * "The set of all {derivation method}s involved in the
19685 * derivation of D's {type definition} from C's {type definition}
19686 * does not intersect with the union of the blocking constraint,
19687 * C's {prohibited substitutions} (if C is complex, otherwise the
19688 * empty set) and the {prohibited substitutions} (respectively the
19689 * empty set) of any intermediate {type definition}s in the
19690 * derivation of D's {type definition} from C's {type definition}."
19691 */
19692 /*
19693 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19694 * subst.head axis, the methSet does not need to be computed for
19695 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019696 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019697 /*
19698 * The set of all {derivation method}s involved in the derivation
19699 */
19700 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019701 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019702 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19703 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019704
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019705 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019706 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19707 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19708
19709 type = type->baseType;
19710 }
19711 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019712 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019713 * the head's type.
19714 */
19715 type = elemDecl->subtypes->baseType;
19716 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019717 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019718 if ((type->flags &
19719 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19720 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19721 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19722 if ((type->flags &
19723 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19724 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19725 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19726 } else
19727 break;
19728 if (type == headType)
19729 break;
19730 type = type->baseType;
19731 }
19732 if ((set != 0) &&
19733 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19734 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19735 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19736 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19737 continue;
19738 }
19739add_member:
19740 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19741 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19742 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19743 }
19744 }
19745}
19746
19747/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019748 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019749 * @item: an schema element declaration/particle
19750 * @ctxt: a schema parser context
19751 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019752 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019753 * Validates the value constraints of an element declaration.
19754 *
19755 * Fixes finish doing the computations on the element declarations.
19756 */
19757static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019758xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019759 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019760{
19761 if (elemDecl == NULL)
19762 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019763 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19764 return;
19765 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019766 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19767 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019768}
19769
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019770/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019771 * xmlSchemaResolveModelGroupParticleReferences:
19772 * @particle: a particle component
19773 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019774 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019775 * Resolves references of a model group's {particles} to
19776 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019777 */
19778static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019779xmlSchemaResolveModelGroupParticleReferences(
19780 xmlSchemaParserCtxtPtr ctxt,
19781 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019782{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019783 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19784 xmlSchemaQNameRefPtr ref;
19785 xmlSchemaBasicItemPtr refItem;
19786
19787 /*
19788 * URGENT TODO: Test this.
19789 */
19790 while (particle != NULL) {
19791 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19792 ((WXS_PARTICLE_TERM(particle))->type !=
19793 XML_SCHEMA_EXTRA_QNAMEREF))
19794 {
19795 goto next_particle;
19796 }
19797 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019798 /*
19799 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019800 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019801 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019802 particle->children = NULL;
19803
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019804 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19805 ref->itemType, ref->name, ref->targetNamespace);
19806 if (refItem == NULL) {
19807 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019808 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019809 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019810 /* TODO: remove the particle. */
19811 goto next_particle;
19812 }
19813 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19814 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19815 /* TODO: remove the particle. */
19816 goto next_particle;
19817 /*
19818 * NOTE that we will assign the model group definition
19819 * itself to the "term" of the particle. This will ease
19820 * the check for circular model group definitions. After
19821 * that the "term" will be assigned the model group of the
19822 * model group definition.
19823 */
19824 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19825 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019826 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019827 * SPEC cos-all-limited (1)
19828 * SPEC cos-all-limited (1.2)
19829 * "It appears only as the value of one or both of the
19830 * following properties:"
19831 * (1.1) "the {model group} property of a model group
19832 * definition."
19833 * (1.2) "the {term} property of a particle [... of] the "
19834 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019835 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019836 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19837 /* TODO: error code */
19838 XML_SCHEMAP_COS_ALL_LIMITED,
19839 WXS_ITEM_NODE(particle), NULL,
19840 "A model group definition is referenced, but "
19841 "it contains an 'all' model group, which "
19842 "cannot be contained by model groups",
19843 NULL, NULL);
19844 /* TODO: remove the particle. */
19845 goto next_particle;
19846 }
19847 particle->children = (xmlSchemaTreeItemPtr) refItem;
19848 } else {
19849 /*
19850 * TODO: Are referenced element declarations the only
19851 * other components we expect here?
19852 */
19853 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019854 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019855next_particle:
19856 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019857 }
19858}
19859
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019860static int
19861xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19862 xmlSchemaValPtr y)
19863{
19864 xmlSchemaTypePtr tx, ty, ptx, pty;
19865 int ret;
19866
19867 while (x != NULL) {
19868 /* Same types. */
19869 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19870 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19871 ptx = xmlSchemaGetPrimitiveType(tx);
19872 pty = xmlSchemaGetPrimitiveType(ty);
19873 /*
19874 * (1) if a datatype T' is ·derived· by ·restriction· from an
19875 * atomic datatype T then the ·value space· of T' is a subset of
19876 * the ·value space· of T. */
19877 /*
19878 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19879 * from a common atomic ancestor T then the ·value space·s of T'
19880 * and T'' may overlap.
19881 */
19882 if (ptx != pty)
19883 return(0);
19884 /*
19885 * We assume computed values to be normalized, so do a fast
19886 * string comparison for string based types.
19887 */
19888 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019889 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019890 if (! xmlStrEqual(
19891 xmlSchemaValueGetAsString(x),
19892 xmlSchemaValueGetAsString(y)))
19893 return (0);
19894 } else {
19895 ret = xmlSchemaCompareValuesWhtsp(
19896 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19897 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19898 if (ret == -2)
19899 return(-1);
19900 if (ret != 0)
19901 return(0);
19902 }
19903 /*
19904 * Lists.
19905 */
19906 x = xmlSchemaValueGetNext(x);
19907 if (x != NULL) {
19908 y = xmlSchemaValueGetNext(y);
19909 if (y == NULL)
19910 return (0);
19911 } else if (xmlSchemaValueGetNext(y) != NULL)
19912 return (0);
19913 else
19914 return (1);
19915 }
19916 return (0);
19917}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019918
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019919/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019920 * xmlSchemaResolveAttrUseReferences:
19921 * @item: an attribute use
19922 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019923 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019924 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019925 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019926static int
19927xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19928 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019929{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019930 if ((ctxt == NULL) || (ause == NULL))
19931 return(-1);
19932 if ((ause->attrDecl == NULL) ||
19933 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19934 return(0);
19935
19936 {
19937 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19938
19939 /*
19940 * TODO: Evaluate, what errors could occur if the declaration is not
19941 * found.
19942 */
19943 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19944 ref->name, ref->targetNamespace);
19945 if (ause->attrDecl == NULL) {
19946 xmlSchemaPResCompAttrErr(ctxt,
19947 XML_SCHEMAP_SRC_RESOLVE,
19948 WXS_BASIC_CAST ause, ause->node,
19949 "ref", ref->name, ref->targetNamespace,
19950 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19951 return(ctxt->err);;
19952 }
19953 }
19954 return(0);
19955}
19956
19957/**
19958 * xmlSchemaCheckAttrUsePropsCorrect:
19959 * @ctxt: a parser context
19960 * @use: an attribute use
19961 *
19962 * Schema Component Constraint:
19963 * Attribute Use Correct (au-props-correct)
19964 *
19965 */
19966static int
19967xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19968 xmlSchemaAttributeUsePtr use)
19969{
19970 if ((ctxt == NULL) || (use == NULL))
19971 return(-1);
19972 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19973 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19974 return(0);
19975
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019976 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019977 * SPEC au-props-correct (1)
19978 * "The values of the properties of an attribute use must be as
19979 * described in the property tableau in The Attribute Use Schema
19980 * Component (§3.5.1), modulo the impact of Missing
19981 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019982 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019983
19984 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19985 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19986 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19987 {
19988 xmlSchemaPCustomErr(ctxt,
19989 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19990 WXS_BASIC_CAST use, NULL,
19991 "The attribute declaration has a 'fixed' value constraint "
19992 ", thus the attribute use must also have a 'fixed' value "
19993 "constraint",
19994 NULL);
19995 return(ctxt->err);
19996 }
19997 /*
19998 * Compute and check the value constraint's value.
19999 */
20000 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
20001 int ret;
20002 /*
20003 * TODO: The spec seems to be missing a check of the
20004 * value constraint of the attribute use. We will do it here.
20005 */
20006 /*
20007 * SPEC a-props-correct (3)
20008 */
20009 if (xmlSchemaIsDerivedFromBuiltInType(
20010 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
20011 {
20012 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20013 XML_SCHEMAP_AU_PROPS_CORRECT,
20014 NULL, WXS_BASIC_CAST use,
20015 "Value constraints are not allowed if the type definition "
20016 "is or is derived from xs:ID",
20017 NULL, NULL);
20018 return(ctxt->err);
20019 }
20020
20021 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
20022 use->node, WXS_ATTRUSE_TYPEDEF(use),
20023 use->defValue, &(use->defVal),
20024 1, 1, 0);
20025 if (ret != 0) {
20026 if (ret < 0) {
20027 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
20028 "calling xmlSchemaVCheckCVCSimpleType()");
20029 return(-1);
20030 }
20031 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20032 XML_SCHEMAP_AU_PROPS_CORRECT,
20033 NULL, WXS_BASIC_CAST use,
20034 "The value of the value constraint is not valid",
20035 NULL, NULL);
20036 return(ctxt->err);
20037 }
20038 }
20039 /*
20040 * SPEC au-props-correct (2)
20041 * "If the {attribute declaration} has a fixed
20042 * {value constraint}, then if the attribute use itself has a
20043 * {value constraint}, it must also be fixed and its value must match
20044 * that of the {attribute declaration}'s {value constraint}."
20045 */
20046 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20047 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20048 {
20049 if (! xmlSchemaAreValuesEqual(use->defVal,
20050 (WXS_ATTRUSE_DECL(use))->defVal))
20051 {
20052 xmlSchemaPCustomErr(ctxt,
20053 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20054 WXS_BASIC_CAST use, NULL,
20055 "The 'fixed' value constraint of the attribute use "
20056 "must match the attribute declaration's value "
20057 "constraint '%s'",
20058 (WXS_ATTRUSE_DECL(use))->defValue);
20059 }
20060 return(ctxt->err);
20061 }
20062 return(0);
20063}
20064
20065
20066
20067
20068/**
20069 * xmlSchemaResolveAttrTypeReferences:
20070 * @item: an attribute declaration
20071 * @ctxt: a parser context
20072 *
20073 * Resolves the referenced type definition component.
20074 */
20075static int
20076xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20077 xmlSchemaParserCtxtPtr ctxt)
20078{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020079 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020080 * The simple type definition corresponding to the <simpleType> element
20081 * information item in the [children], if present, otherwise the simple
20082 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020083 * [attribute], if present, otherwise the ·simple ur-type definition·.
20084 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020085 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020086 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020087 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20088 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020089 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020090 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020091 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020092
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020093 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20094 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020095 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020096 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020097 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020098 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020099 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020100 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020101 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020102 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020103 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020104
Daniel Veillard3646d642004-06-02 19:19:14 +000020105 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020106 /*
20107 * The type defaults to the xs:anySimpleType.
20108 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020109 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20110 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020111 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020112}
20113
20114/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020115 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020116 * @idc: the identity-constraint definition
20117 * @ctxt: the schema parser context
20118 * @name: the attribute name
20119 *
20120 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020121 * Schema Component Constraint:
20122 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020123 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020124static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020125xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020126 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020127{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020128 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020129 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020130 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020131 idc->ref->item = (xmlSchemaBasicItemPtr)
20132 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20133 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020134 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020135 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020136 * TODO: It is actually not an error to fail to resolve
20137 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020138 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020139 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020140 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020141 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020142 "refer", idc->ref->name,
20143 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020144 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020145 return(pctxt->err);
20146 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20147 /*
20148 * SPEC c-props-correct (1)
20149 */
20150 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20151 XML_SCHEMAP_C_PROPS_CORRECT,
20152 NULL, WXS_BASIC_CAST idc,
20153 "The keyref references a keyref",
20154 NULL, NULL);
20155 idc->ref->item = NULL;
20156 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020157 } else {
20158 if (idc->nbFields !=
20159 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20160 xmlChar *str = NULL;
20161 xmlSchemaIDCPtr refer;
20162
20163 refer = (xmlSchemaIDCPtr) idc->ref->item;
20164 /*
20165 * SPEC c-props-correct(2)
20166 * "If the {identity-constraint category} is keyref,
20167 * the cardinality of the {fields} must equal that of
20168 * the {fields} of the {referenced key}.
20169 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020170 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020171 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020172 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020173 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020174 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020175 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020176 refer->name),
20177 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020178 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020179 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020180 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020181 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020182 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020183 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020184}
20185
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020186static int
20187xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20188 xmlSchemaParserCtxtPtr pctxt)
20189{
20190 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20191 prohib->targetNamespace) == NULL) {
20192
20193 xmlSchemaPResCompAttrErr(pctxt,
20194 XML_SCHEMAP_SRC_RESOLVE,
20195 NULL, prohib->node,
20196 "ref", prohib->name, prohib->targetNamespace,
20197 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20198 return(XML_SCHEMAP_SRC_RESOLVE);
20199 }
20200 return(0);
20201}
20202
20203#define WXS_REDEFINED_TYPE(c) \
20204(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20205
20206#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20207(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20208
20209#define WXS_REDEFINED_ATTR_GROUP(c) \
20210(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20211
20212static int
20213xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20214{
20215 int err = 0;
20216 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20217 xmlSchemaBasicItemPtr prev, item;
20218 int wasRedefined;
20219
20220 if (redef == NULL)
20221 return(0);
20222
20223 do {
20224 item = redef->item;
20225 /*
20226 * First try to locate the redefined component in the
20227 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020228 * NOTE: According to this schema bug entry:
20229 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20230 * it's not clear if the referenced component needs to originate
20231 * from the <redefine>d schema _document_ or the schema; the latter
20232 * would include all imported and included sub-schemas of the
20233 * <redefine>d schema. Currenlty we latter approach is used.
20234 * SUPPLEMENT: It seems that the WG moves towards the latter
20235 * approach, so we are doing it right.
20236 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020237 */
20238 prev = xmlSchemaFindRedefCompInGraph(
20239 redef->targetBucket, item->type,
20240 redef->refName, redef->refTargetNs);
20241 if (prev == NULL) {
20242 xmlChar *str = NULL;
20243 xmlNodePtr node;
20244
20245 /*
20246 * SPEC src-redefine:
20247 * (6.2.1) "The ·actual value· of its own name attribute plus
20248 * target namespace must successfully ·resolve· to a model
20249 * group definition in I."
20250 * (7.2.1) "The ·actual value· of its own name attribute plus
20251 * target namespace must successfully ·resolve· to an attribute
20252 * group definition in I."
20253
20254 *
20255 * Note that, if we are redefining with the use of references
20256 * to components, the spec assumes the src-resolve to be used;
20257 * but this won't assure that we search only *inside* the
20258 * redefined schema.
20259 */
20260 if (redef->reference)
20261 node = WXS_ITEM_NODE(redef->reference);
20262 else
20263 node = WXS_ITEM_NODE(item);
20264 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20265 /*
20266 * TODO: error code.
20267 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20268 * reference kind.
20269 */
20270 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020271 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020272 "the redefined schema",
20273 WXS_ITEM_TYPE_NAME(item),
20274 xmlSchemaFormatQName(&str, redef->refTargetNs,
20275 redef->refName));
20276 FREE_AND_NULL(str);
20277 err = pctxt->err;
20278 redef = redef->next;
20279 continue;
20280 }
20281 /*
20282 * TODO: Obtaining and setting the redefinition state is really
20283 * clumsy.
20284 */
20285 wasRedefined = 0;
20286 switch (item->type) {
20287 case XML_SCHEMA_TYPE_COMPLEX:
20288 case XML_SCHEMA_TYPE_SIMPLE:
20289 if ((WXS_TYPE_CAST prev)->flags &
20290 XML_SCHEMAS_TYPE_REDEFINED)
20291 {
20292 wasRedefined = 1;
20293 break;
20294 }
20295 /* Mark it as redefined. */
20296 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20297 /*
20298 * Assign the redefined type to the
20299 * base type of the redefining type.
20300 * TODO: How
20301 */
20302 ((xmlSchemaTypePtr) item)->baseType =
20303 (xmlSchemaTypePtr) prev;
20304 break;
20305 case XML_SCHEMA_TYPE_GROUP:
20306 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20307 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20308 {
20309 wasRedefined = 1;
20310 break;
20311 }
20312 /* Mark it as redefined. */
20313 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20314 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20315 if (redef->reference != NULL) {
20316 /*
20317 * Overwrite the QName-reference with the
20318 * referenced model group def.
20319 */
20320 (WXS_PTC_CAST redef->reference)->children =
20321 WXS_TREE_CAST prev;
20322 }
20323 redef->target = prev;
20324 break;
20325 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20326 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20327 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20328 {
20329 wasRedefined = 1;
20330 break;
20331 }
20332 (WXS_ATTR_GROUP_CAST prev)->flags |=
20333 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20334 if (redef->reference != NULL) {
20335 /*
20336 * Assign the redefined attribute group to the
20337 * QName-reference component.
20338 * This is the easy case, since we will just
20339 * expand the redefined group.
20340 */
20341 (WXS_QNAME_CAST redef->reference)->item = prev;
20342 redef->target = NULL;
20343 } else {
20344 /*
20345 * This is the complicated case: we need
20346 * to apply src-redefine (7.2.2) at a later
20347 * stage, i.e. when attribute group references
20348 * have beed expanded and simple types have
20349 * beed fixed.
20350 */
20351 redef->target = prev;
20352 }
20353 break;
20354 default:
20355 PERROR_INT("xmlSchemaResolveRedefReferences",
20356 "Unexpected redefined component type");
20357 return(-1);
20358 }
20359 if (wasRedefined) {
20360 xmlChar *str = NULL;
20361 xmlNodePtr node;
20362
20363 if (redef->reference)
20364 node = WXS_ITEM_NODE(redef->reference);
20365 else
20366 node = WXS_ITEM_NODE(redef->item);
20367
20368 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20369 /* TODO: error code. */
20370 XML_SCHEMAP_SRC_REDEFINE,
20371 node, NULL,
20372 "The referenced %s was already redefined. Multiple "
20373 "redefinition of the same component is not supported",
20374 xmlSchemaGetComponentDesignation(&str, prev),
20375 NULL);
20376 FREE_AND_NULL(str)
20377 err = pctxt->err;
20378 redef = redef->next;
20379 continue;
20380 }
20381 redef = redef->next;
20382 } while (redef != NULL);
20383
20384 return(err);
20385}
20386
20387static int
20388xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20389{
20390 int err = 0;
20391 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20392 xmlSchemaBasicItemPtr item;
20393
20394 if (redef == NULL)
20395 return(0);
20396
20397 do {
20398 if (redef->target == NULL) {
20399 redef = redef->next;
20400 continue;
20401 }
20402 item = redef->item;
20403
20404 switch (item->type) {
20405 case XML_SCHEMA_TYPE_SIMPLE:
20406 case XML_SCHEMA_TYPE_COMPLEX:
20407 /*
20408 * Since the spec wants the {name} of the redefined
20409 * type to be 'absent', we'll NULL it.
20410 */
20411 (WXS_TYPE_CAST redef->target)->name = NULL;
20412
20413 /*
20414 * TODO: Seems like there's nothing more to do. The normal
20415 * inheritance mechanism is used. But not 100% sure.
20416 */
20417 break;
20418 case XML_SCHEMA_TYPE_GROUP:
20419 /*
20420 * URGENT TODO:
20421 * SPEC src-redefine:
20422 * (6.2.2) "The {model group} of the model group definition
20423 * which corresponds to it per XML Representation of Model
20424 * Group Definition Schema Components (§3.7.2) must be a
20425 * ·valid restriction· of the {model group} of that model
20426 * group definition in I, as defined in Particle Valid
20427 * (Restriction) (§3.9.6)."
20428 */
20429 break;
20430 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20431 /*
20432 * SPEC src-redefine:
20433 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20434 * the attribute group definition which corresponds to it
20435 * per XML Representation of Attribute Group Definition Schema
20436 * Components (§3.6.2) must be ·valid restrictions· of the
20437 * {attribute uses} and {attribute wildcard} of that attribute
20438 * group definition in I, as defined in clause 2, clause 3 and
20439 * clause 4 of Derivation Valid (Restriction, Complex)
20440 * (§3.4.6) (where references to the base type definition are
20441 * understood as references to the attribute group definition
20442 * in I)."
20443 */
20444 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20445 XML_SCHEMA_ACTION_REDEFINE,
20446 item, redef->target,
20447 (WXS_ATTR_GROUP_CAST item)->attrUses,
20448 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20449 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20450 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20451 if (err == -1)
20452 return(-1);
20453 break;
20454 default:
20455 break;
20456 }
20457 redef = redef->next;
20458 } while (redef != NULL);
20459 return(0);
20460}
20461
20462
20463static int
20464xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20465 xmlSchemaBucketPtr bucket)
20466{
20467 xmlSchemaBasicItemPtr item;
20468 int err;
20469 xmlHashTablePtr *table;
20470 const xmlChar *name;
20471 int i;
20472
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020473#define WXS_GET_GLOBAL_HASH(c, slot) { \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020474 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20475 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20476 else \
20477 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20478
20479 /*
20480 * Add global components to the schema's hash tables.
20481 * This is the place where duplicate components will be
20482 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020483 * TODO: I think normally we should support imports of the
20484 * same namespace from multiple locations. We don't do currently,
20485 * but if we do then according to:
20486 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20487 * we would need, if imported directly, to import redefined
20488 * components as well to be able to catch clashing components.
20489 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020490 */
20491 if (bucket == NULL)
20492 return(-1);
20493 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20494 return(0);
20495 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20496
20497 for (i = 0; i < bucket->globals->nbItems; i++) {
20498 item = bucket->globals->items[i];
20499 table = NULL;
20500 switch (item->type) {
20501 case XML_SCHEMA_TYPE_COMPLEX:
20502 case XML_SCHEMA_TYPE_SIMPLE:
20503 if (WXS_REDEFINED_TYPE(item))
20504 continue;
20505 name = (WXS_TYPE_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020506 WXS_GET_GLOBAL_HASH(bucket, typeDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020507 break;
20508 case XML_SCHEMA_TYPE_ELEMENT:
20509 name = (WXS_ELEM_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020510 WXS_GET_GLOBAL_HASH(bucket, elemDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020511 break;
20512 case XML_SCHEMA_TYPE_ATTRIBUTE:
20513 name = (WXS_ATTR_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020514 WXS_GET_GLOBAL_HASH(bucket, attrDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020515 break;
20516 case XML_SCHEMA_TYPE_GROUP:
20517 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20518 continue;
20519 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020520 WXS_GET_GLOBAL_HASH(bucket, groupDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020521 break;
20522 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20523 if (WXS_REDEFINED_ATTR_GROUP(item))
20524 continue;
20525 name = (WXS_ATTR_GROUP_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020526 WXS_GET_GLOBAL_HASH(bucket, attrgrpDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020527 break;
20528 case XML_SCHEMA_TYPE_IDC_KEY:
20529 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20530 case XML_SCHEMA_TYPE_IDC_KEYREF:
20531 name = (WXS_IDC_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020532 WXS_GET_GLOBAL_HASH(bucket, idcDef)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020533 break;
20534 case XML_SCHEMA_TYPE_NOTATION:
20535 name = ((xmlSchemaNotationPtr) item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020536 WXS_GET_GLOBAL_HASH(bucket, notaDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020537 break;
20538 default:
20539 PERROR_INT("xmlSchemaAddComponents",
20540 "Unexpected global component type");
20541 continue;
20542 }
20543 if (*table == NULL) {
20544 *table = xmlHashCreateDict(10, pctxt->dict);
20545 if (*table == NULL) {
20546 PERROR_INT("xmlSchemaAddComponents",
20547 "failed to create a component hash table");
20548 return(-1);
20549 }
20550 }
20551 err = xmlHashAddEntry(*table, name, item);
20552 if (err != 0) {
20553 xmlChar *str = NULL;
20554
20555 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20556 XML_SCHEMAP_REDEFINED_TYPE,
20557 WXS_ITEM_NODE(item),
20558 WXS_BASIC_CAST item,
20559 "A global %s '%s' does already exist",
20560 WXS_ITEM_TYPE_NAME(item),
20561 xmlSchemaGetComponentQName(&str, item));
20562 FREE_AND_NULL(str);
20563 }
20564 }
20565 /*
20566 * Process imported/included schemas.
20567 */
20568 if (bucket->relations != NULL) {
20569 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20570 do {
20571 if ((rel->bucket != NULL) &&
20572 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20573 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20574 return(-1);
20575 }
20576 rel = rel->next;
20577 } while (rel != NULL);
20578 }
20579 return(0);
20580}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020581
20582static int
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020583xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt,
20584 xmlSchemaBucketPtr rootBucket)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020585{
20586 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20587 xmlSchemaTreeItemPtr item, *items;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020588 int nbItems, i, ret = 0;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020589 xmlSchemaBucketPtr oldbucket = con->bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020590
20591#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20592
20593 if ((con->pending == NULL) ||
20594 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020595 return(0);
20596
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020597 /*
20598 * Since xmlSchemaFixupComplexType() will create new particles
20599 * (local components), and those particle components need a bucket
20600 * on the constructor, we'll assure here that the constructor has
20601 * a bucket.
20602 * TODO: Think about storing locals _only_ on the main bucket.
20603 */
20604 if (con->bucket == NULL)
20605 con->bucket = rootBucket;
20606
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020607 /* TODO:
20608 * SPEC (src-redefine):
20609 * (6.2) "If it has no such self-reference, then all of the
20610 * following must be true:"
20611
20612 * (6.2.2) The {model group} of the model group definition which
20613 * corresponds to it per XML Representation of Model Group
20614 * Definition Schema Components (§3.7.2) must be a ·valid
20615 * restriction· of the {model group} of that model group definition
20616 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20617 */
20618 xmlSchemaCheckSRCRedefineFirst(pctxt);
20619
20620 /*
20621 * Add global components to the schemata's hash tables.
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020622 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020623 xmlSchemaAddComponents(pctxt, rootBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020624
20625 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020626 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20627 nbItems = con->pending->nbItems;
20628 /*
20629 * Now that we have parsed *all* the schema document(s) and converted
20630 * them to schema components, we can resolve references, apply component
20631 * constraints, create the FSA from the content model, etc.
20632 */
20633 /*
20634 * Resolve references of..
20635 *
20636 * 1. element declarations:
20637 * - the type definition
20638 * - the substitution group affiliation
20639 * 2. simple/complex types:
20640 * - the base type definition
20641 * - the memberTypes of union types
20642 * - the itemType of list types
20643 * 3. attributes declarations and attribute uses:
20644 * - the type definition
20645 * - if an attribute use, then the attribute declaration
20646 * 4. attribute group references:
20647 * - the attribute group definition
20648 * 5. particles:
20649 * - the term of the particle (e.g. a model group)
20650 * 6. IDC key-references:
20651 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020652 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020653 */
20654 for (i = 0; i < nbItems; i++) {
20655 item = items[i];
20656 switch (item->type) {
20657 case XML_SCHEMA_TYPE_ELEMENT:
20658 xmlSchemaResolveElementReferences(
20659 (xmlSchemaElementPtr) item, pctxt);
20660 FIXHFAILURE;
20661 break;
20662 case XML_SCHEMA_TYPE_COMPLEX:
20663 case XML_SCHEMA_TYPE_SIMPLE:
20664 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020665 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020666 FIXHFAILURE;
20667 break;
20668 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020669 xmlSchemaResolveAttrTypeReferences(
20670 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020671 FIXHFAILURE;
20672 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020673 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20674 xmlSchemaResolveAttrUseReferences(
20675 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020676 FIXHFAILURE;
20677 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020678 case XML_SCHEMA_EXTRA_QNAMEREF:
20679 if ((WXS_QNAME_CAST item)->itemType ==
20680 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20681 {
20682 xmlSchemaResolveAttrGroupReferences(
20683 WXS_QNAME_CAST item, pctxt);
20684 }
20685 FIXHFAILURE;
20686 break;
20687 case XML_SCHEMA_TYPE_SEQUENCE:
20688 case XML_SCHEMA_TYPE_CHOICE:
20689 case XML_SCHEMA_TYPE_ALL:
20690 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20691 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020692 FIXHFAILURE;
20693 break;
20694 case XML_SCHEMA_TYPE_IDC_KEY:
20695 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20696 case XML_SCHEMA_TYPE_IDC_KEYREF:
20697 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020698 (xmlSchemaIDCPtr) item, pctxt);
20699 FIXHFAILURE;
20700 break;
20701 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20702 /*
20703 * Handle attribue prohibition which had a
20704 * "ref" attribute.
20705 */
20706 xmlSchemaResolveAttrUseProhibReferences(
20707 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020708 FIXHFAILURE;
20709 break;
20710 default:
20711 break;
20712 }
20713 }
20714 if (pctxt->nberrors != 0)
20715 goto exit_error;
20716
20717 /*
20718 * Now that all references are resolved we
20719 * can check for circularity of...
20720 * 1. the base axis of type definitions
20721 * 2. nested model group definitions
20722 * 3. nested attribute group definitions
20723 * TODO: check for circual substitution groups.
20724 */
20725 for (i = 0; i < nbItems; i++) {
20726 item = items[i];
20727 /*
20728 * Let's better stop on the first error here.
20729 */
20730 switch (item->type) {
20731 case XML_SCHEMA_TYPE_COMPLEX:
20732 case XML_SCHEMA_TYPE_SIMPLE:
20733 xmlSchemaCheckTypeDefCircular(
20734 (xmlSchemaTypePtr) item, pctxt);
20735 FIXHFAILURE;
20736 if (pctxt->nberrors != 0)
20737 goto exit_error;
20738 break;
20739 case XML_SCHEMA_TYPE_GROUP:
20740 xmlSchemaCheckGroupDefCircular(
20741 (xmlSchemaModelGroupDefPtr) item, pctxt);
20742 FIXHFAILURE;
20743 if (pctxt->nberrors != 0)
20744 goto exit_error;
20745 break;
20746 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20747 xmlSchemaCheckAttrGroupCircular(
20748 (xmlSchemaAttributeGroupPtr) item, pctxt);
20749 FIXHFAILURE;
20750 if (pctxt->nberrors != 0)
20751 goto exit_error;
20752 break;
20753 default:
20754 break;
20755 }
20756 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020757 if (pctxt->nberrors != 0)
20758 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020759 /*
20760 * Model group definition references:
20761 * Such a reference is reflected by a particle at the component
20762 * level. Until now the 'term' of such particles pointed
20763 * to the model group definition; this was done, in order to
20764 * ease circularity checks. Now we need to set the 'term' of
20765 * such particles to the model group of the model group definition.
20766 */
20767 for (i = 0; i < nbItems; i++) {
20768 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020769 switch (item->type) {
20770 case XML_SCHEMA_TYPE_SEQUENCE:
20771 case XML_SCHEMA_TYPE_CHOICE:
20772 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20773 WXS_MODEL_GROUP_CAST item);
20774 break;
20775 default:
20776 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020777 }
20778 }
20779 if (pctxt->nberrors != 0)
20780 goto exit_error;
20781 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020782 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020783 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020784 for (i = 0; i < nbItems; i++) {
20785 item = items[i];
20786 switch (item->type) {
20787 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20788 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20789 WXS_ATTR_GROUP_HAS_REFS(item))
20790 {
20791 xmlSchemaAttributeGroupExpandRefs(pctxt,
20792 WXS_ATTR_GROUP_CAST item);
20793 FIXHFAILURE;
20794 }
20795 break;
20796 default:
20797 break;
20798 }
20799 }
20800 if (pctxt->nberrors != 0)
20801 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020802 /*
20803 * First compute the variety of simple types. This is needed as
20804 * a seperate step, since otherwise we won't be able to detect
20805 * circular union types in all cases.
20806 */
20807 for (i = 0; i < nbItems; i++) {
20808 item = items[i];
20809 switch (item->type) {
20810 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020811 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020812 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20813 (xmlSchemaTypePtr) item);
20814 FIXHFAILURE;
20815 }
20816 break;
20817 default:
20818 break;
20819 }
20820 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020821 if (pctxt->nberrors != 0)
20822 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020823 /*
20824 * Detect circular union types. Note that this needs the variety to
20825 * be already computed.
20826 */
20827 for (i = 0; i < nbItems; i++) {
20828 item = items[i];
20829 switch (item->type) {
20830 case XML_SCHEMA_TYPE_SIMPLE:
20831 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20832 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20833 (xmlSchemaTypePtr) item);
20834 FIXHFAILURE;
20835 }
20836 break;
20837 default:
20838 break;
20839 }
20840 }
20841 if (pctxt->nberrors != 0)
20842 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020843
20844 /*
20845 * Do the complete type fixup for simple types.
20846 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020847 for (i = 0; i < nbItems; i++) {
20848 item = items[i];
20849 switch (item->type) {
20850 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020851 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20852 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20853 FIXHFAILURE;
20854 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020855 break;
20856 default:
20857 break;
20858 }
20859 }
20860 if (pctxt->nberrors != 0)
20861 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020862 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020863 * At this point we need all simple types to be builded and checked.
20864 */
20865 /*
20866 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020867 */
20868 for (i = 0; i < nbItems; i++) {
20869 item = items[i];
20870 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020871 case XML_SCHEMA_TYPE_ATTRIBUTE:
20872 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20873 FIXHFAILURE;
20874 break;
20875 default:
20876 break;
20877 }
20878 }
20879 if (pctxt->nberrors != 0)
20880 goto exit_error;
20881 /*
20882 * Apply constraints for attribute uses.
20883 */
20884 for (i = 0; i < nbItems; i++) {
20885 item = items[i];
20886 switch (item->type) {
20887 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20888 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20889 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20890 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020891 FIXHFAILURE;
20892 }
20893 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020894 default:
20895 break;
20896 }
20897 }
20898 if (pctxt->nberrors != 0)
20899 goto exit_error;
20900
20901 /*
20902 * Apply constraints for attribute group definitions.
20903 */
20904 for (i = 0; i < nbItems; i++) {
20905 item = items[i];
20906 switch (item->type) {
20907 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20908 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20909 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20910 {
20911 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20912 FIXHFAILURE;
20913 }
20914 break;
20915 default:
20916 break;
20917 }
20918 }
20919 if (pctxt->nberrors != 0)
20920 goto exit_error;
20921
20922 /*
20923 * Apply constraints for redefinitions.
20924 */
20925 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20926 xmlSchemaCheckSRCRedefineSecond(pctxt);
20927 if (pctxt->nberrors != 0)
20928 goto exit_error;
20929
20930 /*
20931 * Fixup complex types.
20932 */
20933 for (i = 0; i < nbItems; i++) {
20934 item = con->pending->items[i];
20935 switch (item->type) {
20936 case XML_SCHEMA_TYPE_COMPLEX:
20937 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20938 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20939 FIXHFAILURE;
20940 }
20941 break;
20942 default:
20943 break;
20944 }
20945 }
20946 if (pctxt->nberrors != 0)
20947 goto exit_error;
20948
20949 /*
20950 * The list could have changed, since xmlSchemaFixupComplexType()
20951 * will create particles and model groups in some cases.
20952 */
20953 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20954 nbItems = con->pending->nbItems;
20955
20956 /*
20957 * At this point all complex types need to be builded and checked.
20958 */
20959 /*
20960 * Apply some constraints for element declarations.
20961 */
20962 for (i = 0; i < nbItems; i++) {
20963 item = items[i];
20964 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020965 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020966
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020967 if ((((xmlSchemaElementPtr) item)->flags &
20968 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20969 xmlSchemaCheckElementDeclComponent(
20970 (xmlSchemaElementPtr) item, pctxt);
20971 FIXHFAILURE;
20972 }
20973 break;
20974 default:
20975 break;
20976 }
20977 }
20978 if (pctxt->nberrors != 0)
20979 goto exit_error;
20980 /*
20981 * Finally we can build the automaton from the content model of
20982 * complex types.
20983 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020984
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020985 for (i = 0; i < nbItems; i++) {
20986 item = items[i];
20987 switch (item->type) {
20988 case XML_SCHEMA_TYPE_COMPLEX:
20989 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020990 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020991 break;
20992 default:
20993 break;
20994 }
20995 }
20996 if (pctxt->nberrors != 0)
20997 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020998 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020999 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021000 */
21001 goto exit;
21002
21003exit_error:
21004 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021005 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021006
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021007exit_failure:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021008 ret = -1;
21009
21010exit:
21011 /*
21012 * Reset the constructor. This is needed for XSI acquisition, since
21013 * those items will be processed over and over again for every XSI
21014 * if not cleared here.
21015 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021016 con->bucket = oldbucket;
21017 con->pending->nbItems = 0;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021018 if (con->substGroups != NULL) {
21019 xmlHashFree(con->substGroups,
21020 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
21021 con->substGroups = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021022 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021023 if (con->redefs != NULL) {
21024 xmlSchemaRedefListFree(con->redefs);
21025 con->redefs = NULL;
21026 }
21027 return(ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021028}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021029/**
Daniel Veillard4255d502002-04-16 15:50:10 +000021030 * xmlSchemaParse:
21031 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021032 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021033 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000021034 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000021035 *
21036 * Returns the internal XML Schema structure built from the resource or
21037 * NULL in case of error
21038 */
21039xmlSchemaPtr
21040xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
21041{
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021042 xmlSchemaPtr mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021043 xmlSchemaBucketPtr bucket = NULL;
21044 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000021045
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021046 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021047 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021048 * the API; i.e. not automatically by the validated instance document.
21049 */
21050
Daniel Veillard4255d502002-04-16 15:50:10 +000021051 xmlSchemaInitTypes();
21052
Daniel Veillard6045c902002-10-09 21:13:59 +000021053 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000021054 return (NULL);
21055
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021056 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021057 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021058 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021059 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021060
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021061 /* Create the *main* schema. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021062 mainSchema = xmlSchemaNewSchema(ctxt);
21063 if (mainSchema == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021064 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021065 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021066 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021067 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021068 if (ctxt->constructor == NULL) {
21069 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21070 if (ctxt->constructor == NULL)
21071 return(NULL);
21072 /* Take ownership of the constructor to be able to free it. */
21073 ctxt->ownsConstructor = 1;
21074 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021075 ctxt->constructor->mainSchema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021076 /*
21077 * Locate and add the schema document.
21078 */
21079 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21080 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21081 NULL, NULL, &bucket);
21082 if (res == -1)
21083 goto exit_failure;
21084 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000021085 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021086
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021087 if (bucket == NULL) {
21088 /* TODO: Error code, actually we failed to *locate* the schema. */
21089 if (ctxt->URL)
21090 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21091 NULL, NULL,
21092 "Failed to locate the main schema resource at '%s'",
21093 ctxt->URL, NULL);
21094 else
21095 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21096 NULL, NULL,
21097 "Failed to locate the main schema resource",
21098 NULL, NULL);
21099 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021100 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021101 /* Then do the parsing for good. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021102 if (xmlSchemaParseNewDocWithContext(ctxt, mainSchema, bucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021103 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021104 if (ctxt->nberrors != 0)
21105 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021106
21107 mainSchema->doc = bucket->doc;
21108 mainSchema->preserve = ctxt->preserve;
21109
21110 ctxt->schema = mainSchema;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021111
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021112 if (xmlSchemaFixupComponents(ctxt, WXS_CONSTRUCTOR(ctxt)->mainBucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021113 goto exit_failure;
21114
21115 /*
21116 * TODO: This is not nice, since we cannot distinguish from the
21117 * result if there was an internal error or not.
21118 */
21119exit:
21120 if (ctxt->nberrors != 0) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021121 if (mainSchema) {
21122 xmlSchemaFree(mainSchema);
21123 mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021124 }
21125 if (ctxt->constructor) {
21126 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21127 ctxt->constructor = NULL;
21128 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021129 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021130 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021131 ctxt->schema = NULL;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021132 return(mainSchema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021133exit_failure:
21134 /*
21135 * Quite verbose, but should catch internal errors, which were
21136 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021137 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021138 if (mainSchema) {
21139 xmlSchemaFree(mainSchema);
21140 mainSchema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021141 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021142 if (ctxt->constructor) {
21143 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21144 ctxt->constructor = NULL;
21145 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021146 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021147 PERROR_INT2("xmlSchemaParse",
21148 "An internal error occured");
21149 ctxt->schema = NULL;
21150 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021151}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021152
Daniel Veillard4255d502002-04-16 15:50:10 +000021153/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021154 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021155 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021156 * @err: the error callback
21157 * @warn: the warning callback
21158 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021159 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021160 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021161 */
21162void
21163xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021164 xmlSchemaValidityErrorFunc err,
21165 xmlSchemaValidityWarningFunc warn, void *ctx)
21166{
Daniel Veillard4255d502002-04-16 15:50:10 +000021167 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021168 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021169 ctxt->error = err;
21170 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021171 ctxt->errCtxt = ctx;
21172 if (ctxt->vctxt != NULL)
21173 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21174}
21175
21176/**
21177 * xmlSchemaSetParserStructuredErrors:
21178 * @ctxt: a schema parser context
21179 * @serror: the structured error function
21180 * @ctx: the functions context
21181 *
21182 * Set the structured error callback
21183 */
21184void
21185xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21186 xmlStructuredErrorFunc serror,
21187 void *ctx)
21188{
21189 if (ctxt == NULL)
21190 return;
21191 ctxt->serror = serror;
21192 ctxt->errCtxt = ctx;
21193 if (ctxt->vctxt != NULL)
21194 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021195}
21196
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021197/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021198 * xmlSchemaGetParserErrors:
21199 * @ctxt: a XMl-Schema parser context
21200 * @err: the error callback result
21201 * @warn: the warning callback result
21202 * @ctx: contextual data for the callbacks result
21203 *
21204 * Get the callback information used to handle errors for a parser context
21205 *
21206 * Returns -1 in case of failure, 0 otherwise
21207 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021208int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021209xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021210 xmlSchemaValidityErrorFunc * err,
21211 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021212{
21213 if (ctxt == NULL)
21214 return(-1);
21215 if (err != NULL)
21216 *err = ctxt->error;
21217 if (warn != NULL)
21218 *warn = ctxt->warning;
21219 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021220 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021221 return(0);
21222}
21223
21224/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021225 * xmlSchemaFacetTypeToString:
21226 * @type: the facet type
21227 *
21228 * Convert the xmlSchemaTypeType to a char string.
21229 *
21230 * Returns the char string representation of the facet type if the
21231 * type is a facet and an "Internal Error" string otherwise.
21232 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021233static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021234xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21235{
21236 switch (type) {
21237 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021238 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021239 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021240 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021241 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021242 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021243 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021244 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021245 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021246 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021247 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021248 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021249 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021250 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021251 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021252 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021253 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021254 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021255 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021256 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021257 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021258 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021259 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021260 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021261 default:
21262 break;
21263 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021264 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021265}
21266
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021267static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021268xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21269{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021270 /*
21271 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021272 * from xsd:string.
21273 */
21274 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021275 /*
21276 * Note that we assume a whitespace of preserve for anySimpleType.
21277 */
21278 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21279 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21280 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021281 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021282 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021283 else {
21284 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021285 * For all ·atomic· datatypes other than string (and types ·derived·
21286 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021287 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021288 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021289 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021290 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021291 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021292 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021293 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021294 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021295 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021296 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021297 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021298 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021299 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021300 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21301 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21302 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21303 return (XML_SCHEMA_WHITESPACE_REPLACE);
21304 else
21305 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021306 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021307 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021308}
21309
Daniel Veillard4255d502002-04-16 15:50:10 +000021310/************************************************************************
21311 * *
21312 * Simple type validation *
21313 * *
21314 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021315
Daniel Veillard4255d502002-04-16 15:50:10 +000021316
21317/************************************************************************
21318 * *
21319 * DOM Validation code *
21320 * *
21321 ************************************************************************/
21322
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021323/**
21324 * xmlSchemaAssembleByLocation:
21325 * @pctxt: a schema parser context
21326 * @vctxt: a schema validation context
21327 * @schema: the existing schema
21328 * @node: the node that fired the assembling
21329 * @nsName: the namespace name of the new schema
21330 * @location: the location of the schema
21331 *
21332 * Expands an existing schema by an additional schema.
21333 *
21334 * Returns 0 if the new schema is correct, a positive error code
21335 * number otherwise and -1 in case of an internal or API error.
21336 */
21337static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021338xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021339 xmlSchemaPtr schema,
21340 xmlNodePtr node,
21341 const xmlChar *nsName,
21342 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021343{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021344 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021345 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021346 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021347
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021348 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021349 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021350
21351 if (vctxt->pctxt == NULL) {
21352 VERROR_INT("xmlSchemaAssembleByLocation",
21353 "no parser context available");
21354 return(-1);
21355 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021356 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021357 if (pctxt->constructor == NULL) {
21358 PERROR_INT("xmlSchemaAssembleByLocation",
21359 "no constructor");
21360 return(-1);
21361 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021362 /*
21363 * Acquire the schema document.
21364 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021365 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21366 location, node);
21367 /*
21368 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21369 * the process will automatically change this to
21370 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21371 */
21372 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21373 location, NULL, NULL, 0, node, NULL, nsName,
21374 &bucket);
21375 if (ret != 0)
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021376 return(ret);
21377 if (bucket == NULL) {
21378 /*
21379 * Generate a warning that the document could not be located.
21380 */
21381 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21382 node, NULL,
21383 "The document at location '%s' could not be acquired",
21384 location, NULL, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021385 return(ret);
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021386 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021387 /*
21388 * The first located schema will be handled as if all other
21389 * schemas imported by XSI were imported by this first schema.
21390 */
21391 if ((bucket != NULL) &&
21392 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21393 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21394 /*
21395 * TODO: Is this handled like an import? I.e. is it not an error
21396 * if the schema cannot be located?
21397 */
21398 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21399 return(0);
21400 /*
21401 * We will reuse the parser context for every schema imported
21402 * directly via XSI. So reset the context.
21403 */
21404 pctxt->nberrors = 0;
21405 pctxt->err = 0;
21406 pctxt->doc = bucket->doc;
21407
21408 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21409 if (ret == -1) {
21410 pctxt->doc = NULL;
21411 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021412 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021413 /* Paranoid error channelling. */
21414 if ((ret == 0) && (pctxt->nberrors != 0))
21415 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021416 if (pctxt->nberrors == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021417 /*
21418 * Only bother to fixup pending components, if there was
21419 * no error yet.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021420 * For every XSI acquired schema (and its sub-schemata) we will
21421 * fixup the components.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021422 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021423 xmlSchemaFixupComponents(pctxt, bucket);
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000021424 ret = pctxt->err;
21425 /*
21426 * Not nice, but we need somehow to channel the schema parser
21427 * error to the validation context.
21428 */
21429 if ((ret != 0) && (vctxt->err == 0))
21430 vctxt->err = ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021431 vctxt->nberrors += pctxt->nberrors;
21432 } else {
21433 /* Add to validation error sum. */
21434 vctxt->nberrors += pctxt->nberrors;
21435 }
21436 pctxt->doc = NULL;
21437 return(ret);
21438exit_failure:
21439 pctxt->doc = NULL;
21440 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021441}
21442
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021443static xmlSchemaAttrInfoPtr
21444xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21445 int metaType)
21446{
21447 if (vctxt->nbAttrInfos == 0)
21448 return (NULL);
21449 {
21450 int i;
21451 xmlSchemaAttrInfoPtr iattr;
21452
21453 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21454 iattr = vctxt->attrInfos[i];
21455 if (iattr->metaType == metaType)
21456 return (iattr);
21457 }
21458
21459 }
21460 return (NULL);
21461}
21462
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021463/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021464 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021465 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021466 *
21467 * Expands an existing schema by an additional schema using
21468 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21469 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21470 * must be set to 1.
21471 *
21472 * Returns 0 if the new schema is correct, a positive error code
21473 * number otherwise and -1 in case of an internal or API error.
21474 */
21475static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021476xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021477{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021478 const xmlChar *cur, *end;
21479 const xmlChar *nsname = NULL, *location;
21480 int count = 0;
21481 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021482 xmlSchemaAttrInfoPtr iattr;
21483
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021484 /*
21485 * Parse the value; we will assume an even number of values
21486 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021487 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021488 * URGENT TODO: !! This needs to work for both
21489 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21490 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021491 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021492 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21493 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21494 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021495 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021496 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21497 if (iattr == NULL)
21498 return (0);
21499 cur = iattr->value;
21500 do {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021501 /*
21502 * TODO: Move the string parsing mechanism away from here.
21503 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021504 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021505 /*
21506 * Get the namespace name.
21507 */
21508 while (IS_BLANK_CH(*cur))
21509 cur++;
21510 end = cur;
21511 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21512 end++;
21513 if (end == cur)
21514 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021515 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021516 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021517 cur = end;
21518 }
21519 /*
21520 * Get the URI.
21521 */
21522 while (IS_BLANK_CH(*cur))
21523 cur++;
21524 end = cur;
21525 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21526 end++;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021527 if (end == cur) {
21528 if (iattr->metaType ==
21529 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC)
21530 {
21531 /*
21532 * If using @schemaLocation then tuples are expected.
21533 * I.e. the namespace name *and* the document's URI.
21534 */
21535 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21536 iattr->node, NULL,
21537 "The value must consist of tuples: the target namespace "
21538 "name and the document's URI", NULL, NULL, NULL);
21539 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021540 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021541 }
21542 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021543 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021544 cur = end;
21545 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21546 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021547 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021548 VERROR_INT("xmlSchemaAssembleByXSI",
21549 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021550 return (-1);
21551 }
21552 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021553 return (ret);
21554}
21555
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021556static const xmlChar *
21557xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21558 const xmlChar *prefix)
21559{
21560 if (vctxt->sax != NULL) {
21561 int i, j;
21562 xmlSchemaNodeInfoPtr inode;
21563
21564 for (i = vctxt->depth; i >= 0; i--) {
21565 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21566 inode = vctxt->elemInfos[i];
21567 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21568 if (((prefix == NULL) &&
21569 (inode->nsBindings[j] == NULL)) ||
21570 ((prefix != NULL) && xmlStrEqual(prefix,
21571 inode->nsBindings[j]))) {
21572
21573 /*
21574 * Note that the namespace bindings are already
21575 * in a string dict.
21576 */
21577 return (inode->nsBindings[j+1]);
21578 }
21579 }
21580 }
21581 }
21582 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021583#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021584 } else if (vctxt->reader != NULL) {
21585 xmlChar *nsName;
21586
21587 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21588 if (nsName != NULL) {
21589 const xmlChar *ret;
21590
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021591 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21592 xmlFree(nsName);
21593 return (ret);
21594 } else
21595 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021596#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021597 } else {
21598 xmlNsPtr ns;
21599
21600 if ((vctxt->inode->node == NULL) ||
21601 (vctxt->inode->node->doc == NULL)) {
21602 VERROR_INT("xmlSchemaLookupNamespace",
21603 "no node or node's doc avaliable");
21604 return (NULL);
21605 }
21606 ns = xmlSearchNs(vctxt->inode->node->doc,
21607 vctxt->inode->node, prefix);
21608 if (ns != NULL)
21609 return (ns->href);
21610 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021611 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021612}
21613
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021614/*
21615* This one works on the schema of the validation context.
21616*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021617static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021618xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21619 xmlSchemaPtr schema,
21620 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021621 const xmlChar *value,
21622 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021623 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021624{
21625 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021626
21627 if (vctxt && (vctxt->schema == NULL)) {
21628 VERROR_INT("xmlSchemaValidateNotation",
21629 "a schema is needed on the validation context");
21630 return (-1);
21631 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021632 ret = xmlValidateQName(value, 1);
21633 if (ret != 0)
21634 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021635 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021636 xmlChar *localName = NULL;
21637 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021638
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021639 localName = xmlSplitQName2(value, &prefix);
21640 if (prefix != NULL) {
21641 const xmlChar *nsName = NULL;
21642
21643 if (vctxt != NULL)
21644 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21645 else if (node != NULL) {
21646 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21647 if (ns != NULL)
21648 nsName = ns->href;
21649 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021650 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021651 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021652 return (1);
21653 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021654 if (nsName == NULL) {
21655 xmlFree(prefix);
21656 xmlFree(localName);
21657 return (1);
21658 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021659 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021660 if (valNeeded && (val != NULL)) {
21661 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21662 BAD_CAST xmlStrdup(nsName));
21663 if (*val == NULL)
21664 ret = -1;
21665 }
21666 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021667 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021668 xmlFree(prefix);
21669 xmlFree(localName);
21670 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021671 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021672 if (valNeeded && (val != NULL)) {
21673 (*val) = xmlSchemaNewNOTATIONValue(
21674 BAD_CAST xmlStrdup(value), NULL);
21675 if (*val == NULL)
21676 ret = -1;
21677 }
21678 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021679 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021680 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021681 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021682 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021683}
21684
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021685static int
21686xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21687 const xmlChar* lname,
21688 const xmlChar* nsname)
21689{
21690 int i;
21691
21692 lname = xmlDictLookup(vctxt->dict, lname, -1);
21693 if (lname == NULL)
21694 return(-1);
21695 if (nsname != NULL) {
21696 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21697 if (nsname == NULL)
21698 return(-1);
21699 }
21700 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21701 if ((vctxt->nodeQNames->items [i] == lname) &&
21702 (vctxt->nodeQNames->items[i +1] == nsname))
21703 /* Already there */
21704 return(i);
21705 }
21706 /* Add new entry. */
21707 i = vctxt->nodeQNames->nbItems;
21708 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21709 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21710 return(i);
21711}
21712
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021713/************************************************************************
21714 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021715 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021716 * *
21717 ************************************************************************/
21718
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021719/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021720 * xmlSchemaAugmentIDC:
21721 * @idcDef: the IDC definition
21722 *
21723 * Creates an augmented IDC definition item.
21724 *
21725 * Returns the item, or NULL on internal errors.
21726 */
21727static void
21728xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21729 xmlSchemaValidCtxtPtr vctxt)
21730{
21731 xmlSchemaIDCAugPtr aidc;
21732
21733 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21734 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021735 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021736 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21737 NULL);
21738 return;
21739 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021740 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021741 aidc->def = idcDef;
21742 aidc->next = NULL;
21743 if (vctxt->aidcs == NULL)
21744 vctxt->aidcs = aidc;
21745 else {
21746 aidc->next = vctxt->aidcs;
21747 vctxt->aidcs = aidc;
21748 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021749 /*
21750 * Save if we have keyrefs at all.
21751 */
21752 if ((vctxt->hasKeyrefs == 0) &&
21753 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21754 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021755}
21756
21757/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021758 * xmlSchemaIDCNewBinding:
21759 * @idcDef: the IDC definition of this binding
21760 *
21761 * Creates a new IDC binding.
21762 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021763 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021764 */
21765static xmlSchemaPSVIIDCBindingPtr
21766xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21767{
21768 xmlSchemaPSVIIDCBindingPtr ret;
21769
21770 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21771 sizeof(xmlSchemaPSVIIDCBinding));
21772 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021773 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021774 "allocating a PSVI IDC binding item", NULL);
21775 return (NULL);
21776 }
21777 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21778 ret->definition = idcDef;
21779 return (ret);
21780}
21781
21782/**
21783 * xmlSchemaIDCStoreNodeTableItem:
21784 * @vctxt: the WXS validation context
21785 * @item: the IDC node table item
21786 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021787 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021788 * They are stored to avoid copying them if IDC node-tables are merged
21789 * with corresponding parent IDC node-tables (bubbling).
21790 *
21791 * Returns 0 if succeeded, -1 on internal errors.
21792 */
21793static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021794xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021795 xmlSchemaPSVIIDCNodePtr item)
21796{
21797 /*
21798 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021799 */
21800 if (vctxt->idcNodes == NULL) {
21801 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021802 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21803 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021804 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021805 "allocating the IDC node table item list", NULL);
21806 return (-1);
21807 }
21808 vctxt->sizeIdcNodes = 20;
21809 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21810 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021811 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21812 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021813 sizeof(xmlSchemaPSVIIDCNodePtr));
21814 if (vctxt->idcNodes == 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 node table item list", NULL);
21817 return (-1);
21818 }
21819 }
21820 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021821
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021822 return (0);
21823}
21824
21825/**
21826 * xmlSchemaIDCStoreKey:
21827 * @vctxt: the WXS validation context
21828 * @item: the IDC key
21829 *
21830 * The validation context is used to store an IDC key.
21831 *
21832 * Returns 0 if succeeded, -1 on internal errors.
21833 */
21834static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021835xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021836 xmlSchemaPSVIIDCKeyPtr key)
21837{
21838 /*
21839 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021840 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021841 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021842 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021843 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21844 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021845 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021846 "allocating the IDC key storage list", NULL);
21847 return (-1);
21848 }
21849 vctxt->sizeIdcKeys = 40;
21850 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21851 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021852 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21853 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021854 sizeof(xmlSchemaPSVIIDCKeyPtr));
21855 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021856 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021857 "re-allocating the IDC key storage list", NULL);
21858 return (-1);
21859 }
21860 }
21861 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021862
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021863 return (0);
21864}
21865
21866/**
21867 * xmlSchemaIDCAppendNodeTableItem:
21868 * @bind: the IDC binding
21869 * @ntItem: the node-table item
21870 *
21871 * Appends the IDC node-table item to the binding.
21872 *
21873 * Returns 0 on success and -1 on internal errors.
21874 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021875static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021876xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21877 xmlSchemaPSVIIDCNodePtr ntItem)
21878{
21879 if (bind->nodeTable == NULL) {
21880 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021881 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021882 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21883 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021884 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021885 "allocating an array of IDC node-table items", NULL);
21886 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021887 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021888 } else if (bind->sizeNodes <= bind->nbNodes) {
21889 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021890 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21891 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021892 sizeof(xmlSchemaPSVIIDCNodePtr));
21893 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021894 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021895 "re-allocating an array of IDC node-table items", NULL);
21896 return(-1);
21897 }
21898 }
21899 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021900 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021901}
21902
21903/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021904 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021905 * @vctxt: the WXS validation context
21906 * @matcher: the IDC matcher
21907 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021908 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021909 * of the given matcher. If none found, a new one is created
21910 * and added to the IDC table.
21911 *
21912 * Returns an IDC binding or NULL on internal errors.
21913 */
21914static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021915xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021916 xmlSchemaIDCMatcherPtr matcher)
21917{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021918 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021919
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021920 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021921
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021922 if (ielem->idcTable == NULL) {
21923 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21924 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021925 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021926 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021927 } else {
21928 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021929
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021930 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021931 do {
21932 if (bind->definition == matcher->aidc->def)
21933 return(bind);
21934 if (bind->next == NULL) {
21935 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21936 if (bind->next == NULL)
21937 return (NULL);
21938 return(bind->next);
21939 }
21940 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021941 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021942 }
21943 return (NULL);
21944}
21945
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021946static xmlSchemaItemListPtr
21947xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21948 xmlSchemaIDCMatcherPtr matcher)
21949{
21950 if (matcher->targets == NULL)
21951 matcher->targets = xmlSchemaItemListCreate();
21952 return(matcher->targets);
21953}
21954
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021955/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021956 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021957 * @key: the IDC key
21958 *
21959 * Frees an IDC key together with its compiled value.
21960 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021961static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021962xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21963{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021964 if (key->val != NULL)
21965 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021966 xmlFree(key);
21967}
21968
21969/**
21970 * xmlSchemaIDCFreeBinding:
21971 *
21972 * Frees an IDC binding. Note that the node table-items
21973 * are not freed.
21974 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021975static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021976xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21977{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021978 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021979 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021980 if (bind->dupls != NULL)
21981 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021982 xmlFree(bind);
21983}
21984
21985/**
21986 * xmlSchemaIDCFreeIDCTable:
21987 * @bind: the first IDC binding in the list
21988 *
21989 * Frees an IDC table, i.e. all the IDC bindings in the list.
21990 */
21991static void
21992xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21993{
21994 xmlSchemaPSVIIDCBindingPtr prev;
21995
21996 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021997 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021998 bind = bind->next;
21999 xmlSchemaIDCFreeBinding(prev);
22000 }
22001}
22002
22003/**
22004 * xmlSchemaIDCFreeMatcherList:
22005 * @matcher: the first IDC matcher in the list
22006 *
22007 * Frees a list of IDC matchers.
22008 */
22009static void
22010xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
22011{
22012 xmlSchemaIDCMatcherPtr next;
22013
22014 while (matcher != NULL) {
22015 next = matcher->next;
22016 if (matcher->keySeqs != NULL) {
22017 int i;
22018 for (i = 0; i < matcher->sizeKeySeqs; i++)
22019 if (matcher->keySeqs[i] != NULL)
22020 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022021 xmlFree(matcher->keySeqs);
22022 }
22023 if (matcher->targets != NULL) {
22024 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22025 int i;
22026 xmlSchemaPSVIIDCNodePtr idcNode;
22027 /*
22028 * Node-table items for keyrefs are not stored globally
22029 * to the validation context, since they are not bubbled.
22030 * We need to free them here.
22031 */
22032 for (i = 0; i < matcher->targets->nbItems; i++) {
22033 idcNode =
22034 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22035 xmlFree(idcNode->keys);
22036 xmlFree(idcNode);
22037 }
22038 }
22039 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022040 }
22041 xmlFree(matcher);
22042 matcher = next;
22043 }
22044}
22045
22046/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022047 * xmlSchemaIDCAddStateObject:
22048 * @vctxt: the WXS validation context
22049 * @matcher: the IDC matcher
22050 * @sel: the XPath information
22051 * @parent: the parent "selector" state object if any
22052 * @type: "selector" or "field"
22053 *
22054 * Creates/reuses and activates state objects for the given
22055 * XPath information; if the XPath expression consists of unions,
22056 * multiple state objects are created for every unioned expression.
22057 *
22058 * Returns 0 on success and -1 on internal errors.
22059 */
22060static int
22061xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
22062 xmlSchemaIDCMatcherPtr matcher,
22063 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022064 int type)
22065{
22066 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022067
22068 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022069 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022070 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022071 if (vctxt->xpathStatePool != NULL) {
22072 sto = vctxt->xpathStatePool;
22073 vctxt->xpathStatePool = sto->next;
22074 sto->next = NULL;
22075 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022076 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022077 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022078 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022079 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
22080 if (sto == NULL) {
22081 xmlSchemaVErrMemory(NULL,
22082 "allocating an IDC state object", NULL);
22083 return (-1);
22084 }
22085 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
22086 }
22087 /*
22088 * Add to global list.
22089 */
22090 if (vctxt->xpathStates != NULL)
22091 sto->next = vctxt->xpathStates;
22092 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022093
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022094 /*
22095 * Free the old xpath validation context.
22096 */
22097 if (sto->xpathCtxt != NULL)
22098 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22099
22100 /*
22101 * Create a new XPath (pattern) validation context.
22102 */
22103 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22104 (xmlPatternPtr) sel->xpathComp);
22105 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022106 VERROR_INT("xmlSchemaIDCAddStateObject",
22107 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022108 return (-1);
22109 }
22110 sto->type = type;
22111 sto->depth = vctxt->depth;
22112 sto->matcher = matcher;
22113 sto->sel = sel;
22114 sto->nbHistory = 0;
22115
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022116#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022117 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22118 sto->sel->xpath);
22119#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022120 return (0);
22121}
22122
22123/**
22124 * xmlSchemaXPathEvaluate:
22125 * @vctxt: the WXS validation context
22126 * @nodeType: the nodeType of the current node
22127 *
22128 * Evaluates all active XPath state objects.
22129 *
22130 * Returns the number of IC "field" state objects which resolved to
22131 * this node, 0 if none resolved and -1 on internal errors.
22132 */
22133static int
22134xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022135 xmlElementType nodeType)
22136{
22137 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022138 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022139
22140 if (vctxt->xpathStates == NULL)
22141 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022142
22143 if (nodeType == XML_ATTRIBUTE_NODE)
22144 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022145#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022146 {
22147 xmlChar *str = NULL;
22148 xmlGenericError(xmlGenericErrorContext,
22149 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022150 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22151 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022152 FREE_AND_NULL(str)
22153 }
22154#endif
22155 /*
22156 * Process all active XPath state objects.
22157 */
22158 first = vctxt->xpathStates;
22159 sto = first;
22160 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022161#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022162 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022163 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22164 sto->matcher->aidc->def->name, sto->sel->xpath);
22165 else
22166 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22167 sto->matcher->aidc->def->name, sto->sel->xpath);
22168#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022169 if (nodeType == XML_ELEMENT_NODE)
22170 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022171 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022172 else
22173 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022174 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022175
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022176 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022177 VERROR_INT("xmlSchemaXPathEvaluate",
22178 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022179 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022180 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022181 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022182 goto next_sto;
22183 /*
22184 * Full match.
22185 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022186#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022187 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022188 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022189#endif
22190 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022191 * Register a match in the state object history.
22192 */
22193 if (sto->history == NULL) {
22194 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22195 if (sto->history == NULL) {
22196 xmlSchemaVErrMemory(NULL,
22197 "allocating the state object history", NULL);
22198 return(-1);
22199 }
Daniel Veillard6f9b0872006-08-12 14:09:01 +000022200 sto->sizeHistory = 5;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022201 } else if (sto->sizeHistory <= sto->nbHistory) {
22202 sto->sizeHistory *= 2;
22203 sto->history = (int *) xmlRealloc(sto->history,
22204 sto->sizeHistory * sizeof(int));
22205 if (sto->history == NULL) {
22206 xmlSchemaVErrMemory(NULL,
22207 "re-allocating the state object history", NULL);
22208 return(-1);
22209 }
22210 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022211 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022212
22213#ifdef DEBUG_IDC
22214 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22215 vctxt->depth);
22216#endif
22217
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022218 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22219 xmlSchemaIDCSelectPtr sel;
22220 /*
22221 * Activate state objects for the IDC fields of
22222 * the IDC selector.
22223 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022224#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022225 xmlGenericError(xmlGenericErrorContext, "IDC: "
22226 "activating field states\n");
22227#endif
22228 sel = sto->matcher->aidc->def->fields;
22229 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022230 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22231 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22232 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022233 sel = sel->next;
22234 }
22235 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22236 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022237 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022238 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022239#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022240 xmlGenericError(xmlGenericErrorContext,
22241 "IDC: key found\n");
22242#endif
22243 /*
22244 * Notify that the character value of this node is
22245 * needed.
22246 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022247 if (resolved == 0) {
22248 if ((vctxt->inode->flags &
22249 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22250 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22251 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022252 resolved++;
22253 }
22254next_sto:
22255 if (sto->next == NULL) {
22256 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022257 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022258 */
22259 head = first;
22260 sto = vctxt->xpathStates;
22261 } else
22262 sto = sto->next;
22263 }
22264 return (resolved);
22265}
22266
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022267static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022268xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022269 xmlChar **buf,
22270 xmlSchemaPSVIIDCKeyPtr *seq,
22271 int count)
22272{
22273 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022274 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022275
22276 *buf = xmlStrdup(BAD_CAST "[");
22277 for (i = 0; i < count; i++) {
22278 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022279 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
22280 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22281 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022282 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022283 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022284 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022285 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22286 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022287 *buf = xmlStrcat(*buf, BAD_CAST "???");
22288 }
22289 if (i < count -1)
22290 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22291 else
22292 *buf = xmlStrcat(*buf, BAD_CAST "'");
22293 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022294 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022295 value = NULL;
22296 }
22297 }
22298 *buf = xmlStrcat(*buf, BAD_CAST "]");
22299
22300 return (BAD_CAST *buf);
22301}
22302
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022303/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022304 * xmlSchemaXPathPop:
22305 * @vctxt: the WXS validation context
22306 *
22307 * Pops all XPath states.
22308 *
22309 * Returns 0 on success and -1 on internal errors.
22310 */
22311static int
22312xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22313{
22314 xmlSchemaIDCStateObjPtr sto;
22315 int res;
22316
22317 if (vctxt->xpathStates == NULL)
22318 return(0);
22319 sto = vctxt->xpathStates;
22320 do {
22321 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22322 if (res == -1)
22323 return (-1);
22324 sto = sto->next;
22325 } while (sto != NULL);
22326 return(0);
22327}
22328
22329/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022330 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022331 * @vctxt: the WXS validation context
22332 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022333 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022334 *
22335 * Processes and pops the history items of the IDC state objects.
22336 * IDC key-sequences are validated/created on IDC bindings.
22337 *
22338 * Returns 0 on success and -1 on internal errors.
22339 */
22340static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022341xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022342 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022343{
22344 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022345 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022346 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022347 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022348
22349 if (vctxt->xpathStates == NULL)
22350 return (0);
22351 sto = vctxt->xpathStates;
22352
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022353#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022354 {
22355 xmlChar *str = NULL;
22356 xmlGenericError(xmlGenericErrorContext,
22357 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022358 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22359 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022360 FREE_AND_NULL(str)
22361 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022362#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022363 /*
22364 * Evaluate the state objects.
22365 */
22366 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022367 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22368 if (res == -1) {
22369 VERROR_INT("xmlSchemaXPathProcessHistory",
22370 "calling xmlStreamPop()");
22371 return (-1);
22372 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022373#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022374 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22375 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022376#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022377 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022378 goto deregister_check;
22379
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022380 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022381
22382 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022383 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022384 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022385 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022386 sto = sto->next;
22387 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022388 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022389 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022390 /*
22391 * NOTE: According to
22392 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22393 * ... the simple-content of complex types is also allowed.
22394 */
22395
22396 if (WXS_IS_COMPLEX(type)) {
22397 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22398 /*
22399 * Sanity check for complex types with simple content.
22400 */
22401 simpleType = type->contentTypeDef;
22402 if (simpleType == NULL) {
22403 VERROR_INT("xmlSchemaXPathProcessHistory",
22404 "field resolves to a CT with simple content "
22405 "but the CT is missing the ST definition");
22406 return (-1);
22407 }
22408 } else
22409 simpleType = NULL;
22410 } else
22411 simpleType = type;
22412 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022413 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022414
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022415 /*
22416 * Not qualified if the field resolves to a node of non
22417 * simple type.
22418 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022419 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022420 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022421 WXS_BASIC_CAST sto->matcher->aidc->def,
22422 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022423 "non-simple type",
22424 sto->sel->xpath,
22425 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22426 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022427 sto->nbHistory--;
22428 goto deregister_check;
22429 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022430
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022431 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022432 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022433 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022434 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022435 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022436 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022437 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022438 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022439 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022440 sto->nbHistory--;
22441 goto deregister_check;
22442 } else {
22443 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22444 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022445 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022446
22447 /*
22448 * The key will be anchored on the matcher's list of
22449 * key-sequences. The position in this list is determined
22450 * by the target node's depth relative to the matcher's
22451 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022452 *
22453 * Element Depth Pos List-entries
22454 * <scope> 0 NULL
22455 * <bar> 1 NULL
22456 * <target/> 2 2 target
22457 * <bar>
22458 * </scope>
22459 *
22460 * The size of the list is only dependant on the depth of
22461 * the tree.
22462 * An entry will be NULLed in selector_leave, i.e. when
22463 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022464 */
22465 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022466 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022467
22468 /*
22469 * Create/grow the array of key-sequences.
22470 */
22471 if (matcher->keySeqs == NULL) {
22472 if (pos > 9)
22473 matcher->sizeKeySeqs = pos * 2;
22474 else
22475 matcher->sizeKeySeqs = 10;
22476 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22477 xmlMalloc(matcher->sizeKeySeqs *
22478 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22479 if (matcher->keySeqs == NULL) {
22480 xmlSchemaVErrMemory(NULL,
22481 "allocating an array of key-sequences",
22482 NULL);
22483 return(-1);
22484 }
22485 memset(matcher->keySeqs, 0,
22486 matcher->sizeKeySeqs *
22487 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22488 } else if (pos >= matcher->sizeKeySeqs) {
22489 int i = matcher->sizeKeySeqs;
22490
22491 matcher->sizeKeySeqs *= 2;
22492 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22493 xmlRealloc(matcher->keySeqs,
22494 matcher->sizeKeySeqs *
22495 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022496 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022497 xmlSchemaVErrMemory(NULL,
22498 "reallocating an array of key-sequences",
22499 NULL);
22500 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022501 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022502 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022503 * The array needs to be NULLed.
22504 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022505 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022506 for (; i < matcher->sizeKeySeqs; i++)
22507 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022508 }
22509
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022510 /*
22511 * Get/create the key-sequence.
22512 */
22513 keySeq = matcher->keySeqs[pos];
22514 if (keySeq == NULL) {
22515 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022516 } else if (keySeq[idx] != NULL) {
22517 xmlChar *str = NULL;
22518 /*
22519 * cvc-identity-constraint:
22520 * 3 For each node in the ·target node set· all
22521 * of the {fields}, with that node as the context
22522 * node, evaluate to either an empty node-set or
22523 * a node-set with exactly one member, which must
22524 * have a simple type.
22525 *
22526 * The key was already set; report an error.
22527 */
22528 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22529 XML_SCHEMAV_CVC_IDC, NULL,
22530 WXS_BASIC_CAST matcher->aidc->def,
22531 "The XPath '%s' of a field of %s evaluates to a "
22532 "node-set with more than one member",
22533 sto->sel->xpath,
22534 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22535 FREE_AND_NULL(str);
22536 sto->nbHistory--;
22537 goto deregister_check;
22538 } else
22539 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022540
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022541create_sequence:
22542 /*
22543 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022544 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022545 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22546 matcher->aidc->def->nbFields *
22547 sizeof(xmlSchemaPSVIIDCKeyPtr));
22548 if (keySeq == NULL) {
22549 xmlSchemaVErrMemory(NULL,
22550 "allocating an IDC key-sequence", NULL);
22551 return(-1);
22552 }
22553 memset(keySeq, 0, matcher->aidc->def->nbFields *
22554 sizeof(xmlSchemaPSVIIDCKeyPtr));
22555 matcher->keySeqs[pos] = keySeq;
22556create_key:
22557 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022558 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022559 */
22560 if (key == NULL) {
22561 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22562 sizeof(xmlSchemaPSVIIDCKey));
22563 if (key == NULL) {
22564 xmlSchemaVErrMemory(NULL,
22565 "allocating a IDC key", NULL);
22566 xmlFree(keySeq);
22567 matcher->keySeqs[pos] = NULL;
22568 return(-1);
22569 }
22570 /*
22571 * Consume the compiled value.
22572 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022573 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022574 key->val = vctxt->inode->val;
22575 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022576 /*
22577 * Store the key in a global list.
22578 */
22579 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22580 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022581 return (-1);
22582 }
22583 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022584 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022585 }
22586 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022587
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022588 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022589 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022590 xmlSchemaPSVIIDCNodePtr ntItem;
22591 xmlSchemaIDCMatcherPtr matcher;
22592 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022593 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022594 int pos, i, j, nbKeys;
22595 /*
22596 * Here we have the following scenario:
22597 * An IDC 'selector' state object resolved to a target node,
22598 * during the time this target node was in the
22599 * ancestor-or-self axis, the 'field' state object(s) looked
22600 * out for matching nodes to create a key-sequence for this
22601 * target node. Now we are back to this target node and need
22602 * to put the key-sequence, together with the target node
22603 * itself, into the node-table of the corresponding IDC
22604 * binding.
22605 */
22606 matcher = sto->matcher;
22607 idc = matcher->aidc->def;
22608 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022609 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022610 /*
22611 * Check if the matcher has any key-sequences at all, plus
22612 * if it has a key-sequence for the current target node.
22613 */
22614 if ((matcher->keySeqs == NULL) ||
22615 (matcher->sizeKeySeqs <= pos)) {
22616 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22617 goto selector_key_error;
22618 else
22619 goto selector_leave;
22620 }
22621
22622 keySeq = &(matcher->keySeqs[pos]);
22623 if (*keySeq == NULL) {
22624 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22625 goto selector_key_error;
22626 else
22627 goto selector_leave;
22628 }
22629
22630 for (i = 0; i < nbKeys; i++) {
22631 if ((*keySeq)[i] == NULL) {
22632 /*
22633 * Not qualified, if not all fields did resolve.
22634 */
22635 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22636 /*
22637 * All fields of a "key" IDC must resolve.
22638 */
22639 goto selector_key_error;
22640 }
22641 goto selector_leave;
22642 }
22643 }
22644 /*
22645 * All fields did resolve.
22646 */
22647
22648 /*
22649 * 4.1 If the {identity-constraint category} is unique(/key),
22650 * then no two members of the ·qualified node set· have
22651 * ·key-sequences· whose members are pairwise equal, as
22652 * defined by Equal in [XML Schemas: Datatypes].
22653 *
22654 * Get the IDC binding from the matcher and check for
22655 * duplicate key-sequences.
22656 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022657#if 0
22658 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22659#endif
22660 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022661 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022662 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022663 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022664
22665 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022666 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022667 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022668 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022669 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022670 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022671 bkeySeq =
22672 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022673 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022674 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022675 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022676 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022677 if (res == -1) {
22678 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022679 } else if (res == 0) {
22680 /*
22681 * One of the keys differs, so the key-sequence
22682 * won't be equal; get out.
22683 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022684 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022685 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022686 }
22687 if (res == 1) {
22688 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022689 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022690 */
22691 break;
22692 }
22693 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022694 } while (i < targets->nbItems);
22695 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022696 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022697 /*
22698 * TODO: Try to report the key-sequence.
22699 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022700 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022701 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022702 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022703 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022704 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022705 (*keySeq), nbKeys),
22706 xmlSchemaGetIDCDesignation(&strB, idc));
22707 FREE_AND_NULL(str);
22708 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022709 goto selector_leave;
22710 }
22711 }
22712 /*
22713 * Add a node-table item to the IDC binding.
22714 */
22715 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22716 sizeof(xmlSchemaPSVIIDCNode));
22717 if (ntItem == NULL) {
22718 xmlSchemaVErrMemory(NULL,
22719 "allocating an IDC node-table item", NULL);
22720 xmlFree(*keySeq);
22721 *keySeq = NULL;
22722 return(-1);
22723 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022724 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022725
22726 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022727 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022728 */
22729 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22730 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22731 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022732 xmlFree(*keySeq);
22733 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022734 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022735 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022736 ntItem->nodeQNameID = -1;
22737 } else {
22738 /*
22739 * Save a cached QName for this node on the IDC node, to be
22740 * able to report it, even if the node is not saved.
22741 */
22742 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22743 vctxt->inode->localName, vctxt->inode->nsName);
22744 if (ntItem->nodeQNameID == -1) {
22745 xmlFree(ntItem);
22746 xmlFree(*keySeq);
22747 *keySeq = NULL;
22748 return (-1);
22749 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022750 }
22751 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022752 * Init the node-table item: Save the node, position and
22753 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022754 */
22755 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022756 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022757 ntItem->keys = *keySeq;
22758 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022759#if 0
22760 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22761#endif
22762 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022763 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22764 /*
22765 * Free the item, since keyref items won't be
22766 * put on a global list.
22767 */
22768 xmlFree(ntItem->keys);
22769 xmlFree(ntItem);
22770 }
22771 return (-1);
22772 }
22773
22774 goto selector_leave;
22775selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022776 {
22777 xmlChar *str = NULL;
22778 /*
22779 * 4.2.1 (KEY) The ·target node set· and the
22780 * ·qualified node set· are equal, that is, every
22781 * member of the ·target node set· is also a member
22782 * of the ·qualified node set· and vice versa.
22783 */
22784 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22785 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022786 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022787 "Not all fields of %s evaluate to a node",
22788 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22789 FREE_AND_NULL(str);
22790 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022791selector_leave:
22792 /*
22793 * Free the key-sequence if not added to the IDC table.
22794 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022795 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022796 xmlFree(*keySeq);
22797 *keySeq = NULL;
22798 }
22799 } /* if selector */
22800
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022801 sto->nbHistory--;
22802
22803deregister_check:
22804 /*
22805 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022806 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022807 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022808#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022809 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22810 sto->sel->xpath);
22811#endif
22812 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022813 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022814 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022815 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022816 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022817 nextsto = sto->next;
22818 /*
22819 * Unlink from the list of active XPath state objects.
22820 */
22821 vctxt->xpathStates = sto->next;
22822 sto->next = vctxt->xpathStatePool;
22823 /*
22824 * Link it to the pool of reusable state objects.
22825 */
22826 vctxt->xpathStatePool = sto;
22827 sto = nextsto;
22828 } else
22829 sto = sto->next;
22830 } /* while (sto != NULL) */
22831 return (0);
22832}
22833
22834/**
22835 * xmlSchemaIDCRegisterMatchers:
22836 * @vctxt: the WXS validation context
22837 * @elemDecl: the element declaration
22838 *
22839 * Creates helper objects to evaluate IDC selectors/fields
22840 * successively.
22841 *
22842 * Returns 0 if OK and -1 on internal errors.
22843 */
22844static int
22845xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22846 xmlSchemaElementPtr elemDecl)
22847{
22848 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22849 xmlSchemaIDCPtr idc, refIdc;
22850 xmlSchemaIDCAugPtr aidc;
22851
22852 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22853 if (idc == NULL)
22854 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022855
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022856#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022857 {
22858 xmlChar *str = NULL;
22859 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022860 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022861 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22862 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022863 FREE_AND_NULL(str)
22864 }
22865#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022866 if (vctxt->inode->idcMatchers != NULL) {
22867 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22868 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022869 return (-1);
22870 }
22871 do {
22872 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22873 /*
22874 * Since IDCs bubbles are expensive we need to know the
22875 * depth at which the bubbles should stop; this will be
22876 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022877 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022878 * be -1, indicating that no bubbles are needed.
22879 */
22880 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22881 if (refIdc != NULL) {
22882 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022883 * Remember that we have keyrefs on this node.
22884 */
22885 vctxt->inode->hasKeyrefs = 1;
22886 /*
22887 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022888 */
22889 aidc = vctxt->aidcs;
22890 while (aidc != NULL) {
22891 if (aidc->def == refIdc)
22892 break;
22893 aidc = aidc->next;
22894 }
22895 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022896 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022897 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022898 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022899 return (-1);
22900 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022901 if ((aidc->keyrefDepth == -1) ||
22902 (vctxt->depth < aidc->keyrefDepth))
22903 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022904 }
22905 }
22906 /*
22907 * Lookup the augmented IDC item for the IDC definition.
22908 */
22909 aidc = vctxt->aidcs;
22910 while (aidc != NULL) {
22911 if (aidc->def == idc)
22912 break;
22913 aidc = aidc->next;
22914 }
22915 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022916 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22917 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022918 return (-1);
22919 }
22920 /*
22921 * Create an IDC matcher for every IDC definition.
22922 */
22923 matcher = (xmlSchemaIDCMatcherPtr)
22924 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22925 if (matcher == NULL) {
22926 xmlSchemaVErrMemory(vctxt,
22927 "allocating an IDC matcher", NULL);
22928 return (-1);
22929 }
22930 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22931 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022932 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022933 else
22934 last->next = matcher;
22935 last = matcher;
22936
22937 matcher->type = IDC_MATCHER;
22938 matcher->depth = vctxt->depth;
22939 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022940 matcher->idcType = aidc->def->type;
22941#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022942 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22943#endif
22944 /*
22945 * Init the automaton state object.
22946 */
22947 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022948 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022949 return (-1);
22950
22951 idc = idc->next;
22952 } while (idc != NULL);
22953 return (0);
22954}
22955
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022956static int
22957xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22958 xmlSchemaNodeInfoPtr ielem)
22959{
22960 xmlSchemaPSVIIDCBindingPtr bind;
22961 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22962 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22963 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22964
22965 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22966 /* vctxt->createIDCNodeTables */
22967 while (matcher != NULL) {
22968 /*
22969 * Skip keyref IDCs and empty IDC target-lists.
22970 */
22971 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22972 WXS_ILIST_IS_EMPTY(matcher->targets))
22973 {
22974 matcher = matcher->next;
22975 continue;
22976 }
22977 /*
22978 * If we _want_ the IDC node-table to be created in any case
22979 * then do so. Otherwise create them only if keyrefs need them.
22980 */
22981 if ((! vctxt->createIDCNodeTables) &&
22982 ((matcher->aidc->keyrefDepth == -1) ||
22983 (matcher->aidc->keyrefDepth > vctxt->depth)))
22984 {
22985 matcher = matcher->next;
22986 continue;
22987 }
22988 /*
22989 * Get/create the IDC binding on this element for the IDC definition.
22990 */
22991 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22992
22993 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22994 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22995 nbDupls = bind->dupls->nbItems;
22996 } else {
22997 dupls = NULL;
22998 nbDupls = 0;
22999 }
23000 if (bind->nodeTable != NULL) {
23001 nbNodeTable = bind->nbNodes;
23002 } else {
23003 nbNodeTable = 0;
23004 }
23005
23006 if ((nbNodeTable == 0) && (nbDupls == 0)) {
23007 /*
23008 * Transfer all IDC target-nodes to the IDC node-table.
23009 */
23010 bind->nodeTable =
23011 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
23012 bind->sizeNodes = matcher->targets->sizeItems;
23013 bind->nbNodes = matcher->targets->nbItems;
23014
23015 matcher->targets->items = NULL;
23016 matcher->targets->sizeItems = 0;
23017 matcher->targets->nbItems = 0;
23018 } else {
23019 /*
23020 * Compare the key-sequences and add to the IDC node-table.
23021 */
23022 nbTargets = matcher->targets->nbItems;
23023 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
23024 nbFields = matcher->aidc->def->nbFields;
23025 i = 0;
23026 do {
23027 keys = targets[i]->keys;
23028 if (nbDupls) {
23029 /*
23030 * Search in already found duplicates first.
23031 */
23032 j = 0;
23033 do {
23034 if (nbFields == 1) {
23035 res = xmlSchemaAreValuesEqual(keys[0]->val,
23036 dupls[j]->keys[0]->val);
23037 if (res == -1)
23038 goto internal_error;
23039 if (res == 1) {
23040 /*
23041 * Equal key-sequence.
23042 */
23043 goto next_target;
23044 }
23045 } else {
23046 res = 0;
23047 ntkeys = dupls[j]->keys;
23048 for (k = 0; k < nbFields; k++) {
23049 res = xmlSchemaAreValuesEqual(keys[k]->val,
23050 ntkeys[k]->val);
23051 if (res == -1)
23052 goto internal_error;
23053 if (res == 0) {
23054 /*
23055 * One of the keys differs.
23056 */
23057 break;
23058 }
23059 }
23060 if (res == 1) {
23061 /*
23062 * Equal key-sequence found.
23063 */
23064 goto next_target;
23065 }
23066 }
23067 j++;
23068 } while (j < nbDupls);
23069 }
23070 if (nbNodeTable) {
23071 j = 0;
23072 do {
23073 if (nbFields == 1) {
23074 res = xmlSchemaAreValuesEqual(keys[0]->val,
23075 bind->nodeTable[j]->keys[0]->val);
23076 if (res == -1)
23077 goto internal_error;
23078 if (res == 0) {
23079 /*
23080 * The key-sequence differs.
23081 */
23082 goto next_node_table_entry;
23083 }
23084 } else {
23085 res = 0;
23086 ntkeys = bind->nodeTable[j]->keys;
23087 for (k = 0; k < nbFields; k++) {
23088 res = xmlSchemaAreValuesEqual(keys[k]->val,
23089 ntkeys[k]->val);
23090 if (res == -1)
23091 goto internal_error;
23092 if (res == 0) {
23093 /*
23094 * One of the keys differs.
23095 */
23096 goto next_node_table_entry;
23097 }
23098 }
23099 }
23100 /*
23101 * Add the duplicate to the list of duplicates.
23102 */
23103 if (bind->dupls == NULL) {
23104 bind->dupls = xmlSchemaItemListCreate();
23105 if (bind->dupls == NULL)
23106 goto internal_error;
23107 }
23108 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23109 goto internal_error;
23110 /*
23111 * Remove the duplicate entry from the IDC node-table.
23112 */
23113 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23114 bind->nbNodes--;
23115
23116 goto next_target;
23117
23118next_node_table_entry:
23119 j++;
23120 } while (j < nbNodeTable);
23121 }
23122 /*
23123 * If everything is fine, then add the IDC target-node to
23124 * the IDC node-table.
23125 */
23126 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23127 goto internal_error;
23128
23129next_target:
23130 i++;
23131 } while (i < nbTargets);
23132 }
23133 matcher = matcher->next;
23134 }
23135 return(0);
23136
23137internal_error:
23138 return(-1);
23139}
23140
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023141/**
23142 * xmlSchemaBubbleIDCNodeTables:
23143 * @depth: the current tree depth
23144 *
23145 * Merges IDC bindings of an element at @depth into the corresponding IDC
23146 * bindings of its parent element. If a duplicate note-table entry is found,
23147 * both, the parent node-table entry and child entry are discarded from the
23148 * node-table of the parent.
23149 *
23150 * Returns 0 if OK and -1 on internal errors.
23151 */
23152static int
23153xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23154{
23155 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023156 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23157 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023158 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023159 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023160
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023161 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023162 if (bind == NULL) {
23163 /* Fine, no table, no bubbles. */
23164 return (0);
23165 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023166
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023167 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23168 /*
23169 * Walk all bindings; create new or add to existing bindings.
23170 * Remove duplicate key-sequences.
23171 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023172 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023173
23174 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23175 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023176 /*
23177 * Check if the key/unique IDC table needs to be bubbled.
23178 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023179 if (! vctxt->createIDCNodeTables) {
23180 aidc = vctxt->aidcs;
23181 do {
23182 if (aidc->def == bind->definition) {
23183 if ((aidc->keyrefDepth == -1) ||
23184 (aidc->keyrefDepth >= vctxt->depth)) {
23185 goto next_binding;
23186 }
23187 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023188 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023189 aidc = aidc->next;
23190 } while (aidc != NULL);
23191 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023192
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023193 if (parTable != NULL)
23194 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023195 /*
23196 * Search a matching parent binding for the
23197 * IDC definition.
23198 */
23199 while (parBind != NULL) {
23200 if (parBind->definition == bind->definition)
23201 break;
23202 parBind = parBind->next;
23203 }
23204
23205 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023206 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023207 * Compare every node-table entry of the child node,
23208 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023209 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023210 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023211
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023212 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23213 oldDupls = parBind->dupls->nbItems;
23214 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23215 } else {
23216 dupls = NULL;
23217 oldDupls = 0;
23218 }
23219
23220 parNodes = parBind->nodeTable;
23221 nbFields = bind->definition->nbFields;
23222
23223 for (i = 0; i < bind->nbNodes; i++) {
23224 node = bind->nodeTable[i];
23225 if (node == NULL)
23226 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023227 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023228 * ...with every key-sequence of the parent node, already
23229 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023230 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023231 if (oldDupls) {
23232 j = 0;
23233 while (j < oldDupls) {
23234 if (nbFields == 1) {
23235 ret = xmlSchemaAreValuesEqual(
23236 node->keys[0]->val,
23237 dupls[j]->keys[0]->val);
23238 if (ret == -1)
23239 goto internal_error;
23240 if (ret == 0) {
23241 j++;
23242 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023243 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023244 } else {
23245 parNode = dupls[j];
23246 for (k = 0; k < nbFields; k++) {
23247 ret = xmlSchemaAreValuesEqual(
23248 node->keys[k]->val,
23249 parNode->keys[k]->val);
23250 if (ret == -1)
23251 goto internal_error;
23252 if (ret == 0)
23253 break;
23254 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023255 }
23256 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023257 /* Duplicate found. */
23258 break;
23259 j++;
23260 }
23261 if (j != oldDupls) {
23262 /* Duplicate found. Skip this entry. */
23263 continue;
23264 }
23265 }
23266 /*
23267 * ... and with every key-sequence of the parent node.
23268 */
23269 if (oldNum) {
23270 j = 0;
23271 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023272 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023273 if (nbFields == 1) {
23274 ret = xmlSchemaAreValuesEqual(
23275 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023276 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023277 if (ret == -1)
23278 goto internal_error;
23279 if (ret == 0) {
23280 j++;
23281 continue;
23282 }
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023283 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023284 for (k = 0; k < nbFields; k++) {
23285 ret = xmlSchemaAreValuesEqual(
23286 node->keys[k]->val,
23287 parNode->keys[k]->val);
23288 if (ret == -1)
23289 goto internal_error;
23290 if (ret == 0)
23291 break;
23292 }
23293 }
23294 if (ret == 1)
23295 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023296 break;
23297 j++;
23298 }
23299 if (j != oldNum) {
23300 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023301 * Handle duplicates. Move the duplicate in
23302 * the parent's node-table to the list of
23303 * duplicates.
23304 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023305 oldNum--;
23306 parBind->nbNodes--;
23307 /*
23308 * Move last old item to pos of duplicate.
23309 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023310 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023311
23312 if (parBind->nbNodes != oldNum) {
23313 /*
23314 * If new items exist, move last new item to
23315 * last of old items.
23316 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023317 parNodes[oldNum] =
23318 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023319 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023320 if (parBind->dupls == NULL) {
23321 parBind->dupls = xmlSchemaItemListCreate();
23322 if (parBind->dupls == NULL)
23323 goto internal_error;
23324 }
23325 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023326 } else {
23327 /*
23328 * Add the node-table entry (node and key-sequence) of
23329 * the child node to the node table of the parent node.
23330 */
23331 if (parBind->nodeTable == NULL) {
23332 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023333 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023334 if (parBind->nodeTable == NULL) {
23335 xmlSchemaVErrMemory(NULL,
23336 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023337 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023338 }
23339 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023340 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023341 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023342 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23343 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23344 sizeof(xmlSchemaPSVIIDCNodePtr));
23345 if (parBind->nodeTable == NULL) {
23346 xmlSchemaVErrMemory(NULL,
23347 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023348 goto internal_error;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023349 }
23350 }
23351 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023352 /*
23353 * Append the new node-table entry to the 'new node-table
23354 * entries' section.
23355 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023356 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023357 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023358
23359 }
23360
23361 }
23362 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023363 /*
23364 * No binding for the IDC was found: create a new one and
23365 * copy all node-tables.
23366 */
23367 parBind = xmlSchemaIDCNewBinding(bind->definition);
23368 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023369 goto internal_error;
23370
23371 /*
23372 * TODO: Hmm, how to optimize the initial number of
23373 * allocated entries?
23374 */
23375 if (bind->nbNodes != 0) {
23376 /*
23377 * Add all IDC node-table entries.
23378 */
23379 if (! vctxt->psviExposeIDCNodeTables) {
23380 /*
23381 * Just move the entries.
23382 * NOTE: this is quite save here, since
23383 * all the keyref lookups have already been
23384 * performed.
23385 */
23386 parBind->nodeTable = bind->nodeTable;
23387 bind->nodeTable = NULL;
23388 parBind->sizeNodes = bind->sizeNodes;
23389 bind->sizeNodes = 0;
23390 parBind->nbNodes = bind->nbNodes;
23391 bind->nbNodes = 0;
23392 } else {
23393 /*
23394 * Copy the entries.
23395 */
23396 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23397 xmlMalloc(bind->nbNodes *
23398 sizeof(xmlSchemaPSVIIDCNodePtr));
23399 if (parBind->nodeTable == NULL) {
23400 xmlSchemaVErrMemory(NULL,
23401 "allocating an array of IDC node-table "
23402 "items", NULL);
23403 xmlSchemaIDCFreeBinding(parBind);
23404 goto internal_error;
23405 }
23406 parBind->sizeNodes = bind->nbNodes;
23407 parBind->nbNodes = bind->nbNodes;
23408 memcpy(parBind->nodeTable, bind->nodeTable,
23409 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23410 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023411 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023412 if (bind->dupls) {
23413 /*
23414 * Move the duplicates.
23415 */
23416 if (parBind->dupls != NULL)
23417 xmlSchemaItemListFree(parBind->dupls);
23418 parBind->dupls = bind->dupls;
23419 bind->dupls = NULL;
23420 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023421 if (*parTable == NULL)
23422 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023423 else {
23424 parBind->next = *parTable;
23425 *parTable = parBind;
23426 }
23427 }
23428
23429next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023430 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023431 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023432 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023433
23434internal_error:
23435 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023436}
23437
23438/**
23439 * xmlSchemaCheckCVCIDCKeyRef:
23440 * @vctxt: the WXS validation context
23441 * @elemDecl: the element declaration
23442 *
23443 * Check the cvc-idc-keyref constraints.
23444 */
23445static int
23446xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23447{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023448 xmlSchemaIDCMatcherPtr matcher;
23449 xmlSchemaPSVIIDCBindingPtr bind;
23450
23451 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023452 /*
23453 * Find a keyref.
23454 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023455 while (matcher != NULL) {
23456 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23457 matcher->targets &&
23458 matcher->targets->nbItems)
23459 {
23460 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023461 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023462 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023463
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023464 nbFields = matcher->aidc->def->nbFields;
23465
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023466 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023467 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023468 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023469 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023470 while (bind != NULL) {
23471 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023472 bind->definition)
23473 break;
23474 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023475 }
23476 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023477 /*
23478 * Search for a matching key-sequences.
23479 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023480 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023481 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023482 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023483 if (bind != NULL) {
23484 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023485 for (j = 0; j < bind->nbNodes; j++) {
23486 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023487 for (k = 0; k < nbFields; k++) {
23488 res = xmlSchemaAreValuesEqual(keys[k]->val,
23489 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023490 if (res == 0)
23491 break;
23492 else if (res == -1) {
23493 return (-1);
23494 }
23495 }
23496 if (res == 1) {
23497 /*
23498 * Match found.
23499 */
23500 break;
23501 }
23502 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023503 if ((res == 0) && hasDupls) {
23504 /*
23505 * Search in duplicates
23506 */
23507 for (j = 0; j < bind->dupls->nbItems; j++) {
23508 keys = ((xmlSchemaPSVIIDCNodePtr)
23509 bind->dupls->items[j])->keys;
23510 for (k = 0; k < nbFields; k++) {
23511 res = xmlSchemaAreValuesEqual(keys[k]->val,
23512 refKeys[k]->val);
23513 if (res == 0)
23514 break;
23515 else if (res == -1) {
23516 return (-1);
23517 }
23518 }
23519 if (res == 1) {
23520 /*
23521 * Match in duplicates found.
23522 */
23523 xmlChar *str = NULL, *strB = NULL;
23524 xmlSchemaKeyrefErr(vctxt,
23525 XML_SCHEMAV_CVC_IDC, refNode,
23526 (xmlSchemaTypePtr) matcher->aidc->def,
23527 "More than one match found for "
23528 "key-sequence %s of keyref '%s'",
23529 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23530 refNode->keys, nbFields),
23531 xmlSchemaGetComponentQName(&strB,
23532 matcher->aidc->def));
23533 FREE_AND_NULL(str);
23534 FREE_AND_NULL(strB);
23535 break;
23536 }
23537 }
23538 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023539 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023540
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023541 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023542 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023543 xmlSchemaKeyrefErr(vctxt,
23544 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023545 (xmlSchemaTypePtr) matcher->aidc->def,
23546 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023547 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023548 refNode->keys, nbFields),
23549 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023550 FREE_AND_NULL(str);
23551 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023552 }
23553 }
23554 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023555 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023556 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023557 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023558 return (0);
23559}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023560
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023561/************************************************************************
23562 * *
23563 * XML Reader validation code *
23564 * *
23565 ************************************************************************/
23566
23567static xmlSchemaAttrInfoPtr
23568xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023569{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023570 xmlSchemaAttrInfoPtr iattr;
23571 /*
23572 * Grow/create list of attribute infos.
23573 */
23574 if (vctxt->attrInfos == NULL) {
23575 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23576 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23577 vctxt->sizeAttrInfos = 1;
23578 if (vctxt->attrInfos == NULL) {
23579 xmlSchemaVErrMemory(vctxt,
23580 "allocating attribute info list", NULL);
23581 return (NULL);
23582 }
23583 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23584 vctxt->sizeAttrInfos++;
23585 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23586 xmlRealloc(vctxt->attrInfos,
23587 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23588 if (vctxt->attrInfos == NULL) {
23589 xmlSchemaVErrMemory(vctxt,
23590 "re-allocating attribute info list", NULL);
23591 return (NULL);
23592 }
23593 } else {
23594 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23595 if (iattr->localName != NULL) {
23596 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23597 "attr info not cleared");
23598 return (NULL);
23599 }
23600 iattr->nodeType = XML_ATTRIBUTE_NODE;
23601 return (iattr);
23602 }
23603 /*
23604 * Create an attribute info.
23605 */
23606 iattr = (xmlSchemaAttrInfoPtr)
23607 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23608 if (iattr == NULL) {
23609 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23610 return (NULL);
23611 }
23612 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23613 iattr->nodeType = XML_ATTRIBUTE_NODE;
23614 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23615
23616 return (iattr);
23617}
23618
23619static int
23620xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23621 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023622 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023623 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023624 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023625 int ownedNames,
23626 xmlChar *value,
23627 int ownedValue)
23628{
23629 xmlSchemaAttrInfoPtr attr;
23630
23631 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23632 if (attr == NULL) {
23633 VERROR_INT("xmlSchemaPushAttribute",
23634 "calling xmlSchemaGetFreshAttrInfo()");
23635 return (-1);
23636 }
23637 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023638 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023639 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23640 attr->localName = localName;
23641 attr->nsName = nsName;
23642 if (ownedNames)
23643 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23644 /*
23645 * Evaluate if it's an XSI attribute.
23646 */
23647 if (nsName != NULL) {
23648 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23649 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23650 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23651 }
23652 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23653 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23654 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23655 }
23656 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23657 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23658 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23659 }
23660 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23661 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23662 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23663 }
23664 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23665 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23666 }
23667 }
23668 attr->value = value;
23669 if (ownedValue)
23670 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23671 if (attr->metaType != 0)
23672 attr->state = XML_SCHEMAS_ATTR_META;
23673 return (0);
23674}
23675
23676static void
23677xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23678{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023679 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023680 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023681 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23682 FREE_AND_NULL(ielem->localName);
23683 FREE_AND_NULL(ielem->nsName);
23684 } else {
23685 ielem->localName = NULL;
23686 ielem->nsName = NULL;
23687 }
23688 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23689 FREE_AND_NULL(ielem->value);
23690 } else {
23691 ielem->value = NULL;
23692 }
23693 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023694 /*
23695 * PSVI TODO: Be careful not to free it when the value is
23696 * exposed via PSVI.
23697 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023698 xmlSchemaFreeValue(ielem->val);
23699 ielem->val = NULL;
23700 }
23701 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023702 /*
23703 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23704 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023705 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23706 ielem->idcMatchers = NULL;
23707 }
23708 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023709 /*
23710 * OPTIMIZE TODO: Use a pool of IDC tables??.
23711 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023712 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23713 ielem->idcTable = NULL;
23714 }
23715 if (ielem->regexCtxt != NULL) {
23716 xmlRegFreeExecCtxt(ielem->regexCtxt);
23717 ielem->regexCtxt = NULL;
23718 }
23719 if (ielem->nsBindings != NULL) {
23720 xmlFree((xmlChar **)ielem->nsBindings);
23721 ielem->nsBindings = NULL;
23722 ielem->nbNsBindings = 0;
23723 ielem->sizeNsBindings = 0;
23724 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023725}
23726
23727/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023728 * xmlSchemaGetFreshElemInfo:
23729 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023730 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023731 * Creates/reuses and initializes the element info item for
23732 * the currect tree depth.
23733 *
23734 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023735 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023736static xmlSchemaNodeInfoPtr
23737xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023738{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023739 xmlSchemaNodeInfoPtr info = NULL;
23740
23741 if (vctxt->depth > vctxt->sizeElemInfos) {
23742 VERROR_INT("xmlSchemaGetFreshElemInfo",
23743 "inconsistent depth encountered");
23744 return (NULL);
23745 }
23746 if (vctxt->elemInfos == NULL) {
23747 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23748 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23749 if (vctxt->elemInfos == NULL) {
23750 xmlSchemaVErrMemory(vctxt,
23751 "allocating the element info array", NULL);
23752 return (NULL);
23753 }
23754 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23755 vctxt->sizeElemInfos = 10;
23756 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23757 int i = vctxt->sizeElemInfos;
23758
23759 vctxt->sizeElemInfos *= 2;
23760 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23761 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23762 sizeof(xmlSchemaNodeInfoPtr));
23763 if (vctxt->elemInfos == NULL) {
23764 xmlSchemaVErrMemory(vctxt,
23765 "re-allocating the element info array", NULL);
23766 return (NULL);
23767 }
23768 /*
23769 * We need the new memory to be NULLed.
23770 * TODO: Use memset instead?
23771 */
23772 for (; i < vctxt->sizeElemInfos; i++)
23773 vctxt->elemInfos[i] = NULL;
23774 } else
23775 info = vctxt->elemInfos[vctxt->depth];
23776
23777 if (info == NULL) {
23778 info = (xmlSchemaNodeInfoPtr)
23779 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23780 if (info == NULL) {
23781 xmlSchemaVErrMemory(vctxt,
23782 "allocating an element info", NULL);
23783 return (NULL);
23784 }
23785 vctxt->elemInfos[vctxt->depth] = info;
23786 } else {
23787 if (info->localName != NULL) {
23788 VERROR_INT("xmlSchemaGetFreshElemInfo",
23789 "elem info has not been cleared");
23790 return (NULL);
23791 }
23792 }
23793 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23794 info->nodeType = XML_ELEMENT_NODE;
23795 info->depth = vctxt->depth;
23796
23797 return (info);
23798}
23799
23800#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23801#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23802#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23803
23804static int
23805xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23806 xmlNodePtr node,
23807 xmlSchemaTypePtr type,
23808 xmlSchemaValType valType,
23809 const xmlChar * value,
23810 xmlSchemaValPtr val,
23811 unsigned long length,
23812 int fireErrors)
23813{
23814 int ret, error = 0;
23815
23816 xmlSchemaTypePtr tmpType;
23817 xmlSchemaFacetLinkPtr facetLink;
23818 xmlSchemaFacetPtr facet;
23819 unsigned long len = 0;
23820 xmlSchemaWhitespaceValueType ws;
23821
23822 /*
23823 * In Libxml2, derived built-in types have currently no explicit facets.
23824 */
23825 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023826 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023827
23828 /*
23829 * NOTE: Do not jump away, if the facetSet of the given type is
23830 * empty: until now, "pattern" and "enumeration" facets of the
23831 * *base types* need to be checked as well.
23832 */
23833 if (type->facetSet == NULL)
23834 goto pattern_and_enum;
23835
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023836 if (! WXS_IS_ATOMIC(type)) {
23837 if (WXS_IS_LIST(type))
23838 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023839 else
23840 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023841 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023842 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023843 * Whitespace handling is only of importance for string-based
23844 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023845 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023846 tmpType = xmlSchemaGetPrimitiveType(type);
23847 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023848 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023849 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23850 } else
23851 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23852 /*
23853 * If the value was not computed (for string or
23854 * anySimpleType based types), then use the provided
23855 * type.
23856 */
23857 if (val == NULL)
23858 valType = valType;
23859 else
23860 valType = xmlSchemaGetValType(val);
23861
23862 ret = 0;
23863 for (facetLink = type->facetSet; facetLink != NULL;
23864 facetLink = facetLink->next) {
23865 /*
23866 * Skip the pattern "whiteSpace": it is used to
23867 * format the character content beforehand.
23868 */
23869 switch (facetLink->facet->type) {
23870 case XML_SCHEMA_FACET_WHITESPACE:
23871 case XML_SCHEMA_FACET_PATTERN:
23872 case XML_SCHEMA_FACET_ENUMERATION:
23873 continue;
23874 case XML_SCHEMA_FACET_LENGTH:
23875 case XML_SCHEMA_FACET_MINLENGTH:
23876 case XML_SCHEMA_FACET_MAXLENGTH:
23877 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23878 valType, value, val, &len, ws);
23879 break;
23880 default:
23881 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23882 valType, value, val, ws);
23883 break;
23884 }
23885 if (ret < 0) {
23886 AERROR_INT("xmlSchemaValidateFacets",
23887 "validating against a atomic type facet");
23888 return (-1);
23889 } else if (ret > 0) {
23890 if (fireErrors)
23891 xmlSchemaFacetErr(actxt, ret, node,
23892 value, len, type, facetLink->facet, NULL, NULL, NULL);
23893 else
23894 return (ret);
23895 if (error == 0)
23896 error = ret;
23897 }
23898 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023899 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023900
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023901WXS_IS_LIST:
23902 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023903 goto pattern_and_enum;
23904 /*
23905 * "length", "minLength" and "maxLength" of list types.
23906 */
23907 ret = 0;
23908 for (facetLink = type->facetSet; facetLink != NULL;
23909 facetLink = facetLink->next) {
23910
23911 switch (facetLink->facet->type) {
23912 case XML_SCHEMA_FACET_LENGTH:
23913 case XML_SCHEMA_FACET_MINLENGTH:
23914 case XML_SCHEMA_FACET_MAXLENGTH:
23915 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23916 value, length, NULL);
23917 break;
23918 default:
23919 continue;
23920 }
23921 if (ret < 0) {
23922 AERROR_INT("xmlSchemaValidateFacets",
23923 "validating against a list type facet");
23924 return (-1);
23925 } else if (ret > 0) {
23926 if (fireErrors)
23927 xmlSchemaFacetErr(actxt, ret, node,
23928 value, length, type, facetLink->facet, NULL, NULL, NULL);
23929 else
23930 return (ret);
23931 if (error == 0)
23932 error = ret;
23933 }
23934 ret = 0;
23935 }
23936
23937pattern_and_enum:
23938 if (error >= 0) {
23939 int found = 0;
23940 /*
23941 * Process enumerations. Facet values are in the value space
23942 * of the defining type's base type. This seems to be a bug in the
23943 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23944 * Only the first set of enumerations in the ancestor-or-self axis
23945 * is used for validation.
23946 */
23947 ret = 0;
23948 tmpType = type;
23949 do {
23950 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23951 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23952 continue;
23953 found = 1;
23954 ret = xmlSchemaAreValuesEqual(facet->val, val);
23955 if (ret == 1)
23956 break;
23957 else if (ret < 0) {
23958 AERROR_INT("xmlSchemaValidateFacets",
23959 "validating against an enumeration facet");
23960 return (-1);
23961 }
23962 }
23963 if (ret != 0)
23964 break;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +000023965 /*
23966 * Break on the first set of enumerations. Any additional
23967 * enumerations which might be existent on the ancestors
23968 * of the current type are restricted by this set; thus
23969 * *must* *not* be taken into account.
23970 */
23971 if (found)
23972 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023973 tmpType = tmpType->baseType;
23974 } while ((tmpType != NULL) &&
23975 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23976 if (found && (ret == 0)) {
23977 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23978 if (fireErrors) {
23979 xmlSchemaFacetErr(actxt, ret, node,
23980 value, 0, type, NULL, NULL, NULL, NULL);
23981 } else
23982 return (ret);
23983 if (error == 0)
23984 error = ret;
23985 }
23986 }
23987
23988 if (error >= 0) {
23989 int found;
23990 /*
23991 * Process patters. Pattern facets are ORed at type level
23992 * and ANDed if derived. Walk the base type axis.
23993 */
23994 tmpType = type;
23995 facet = NULL;
23996 do {
23997 found = 0;
23998 for (facetLink = tmpType->facetSet; facetLink != NULL;
23999 facetLink = facetLink->next) {
24000 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
24001 continue;
24002 found = 1;
24003 /*
24004 * NOTE that for patterns, @value needs to be the
24005 * normalized vaule.
24006 */
24007 ret = xmlRegexpExec(facetLink->facet->regexp, value);
24008 if (ret == 1)
24009 break;
24010 else if (ret < 0) {
24011 AERROR_INT("xmlSchemaValidateFacets",
24012 "validating against a pattern facet");
24013 return (-1);
24014 } else {
24015 /*
24016 * Save the last non-validating facet.
24017 */
24018 facet = facetLink->facet;
24019 }
24020 }
24021 if (found && (ret != 1)) {
24022 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
24023 if (fireErrors) {
24024 xmlSchemaFacetErr(actxt, ret, node,
24025 value, 0, type, facet, NULL, NULL, NULL);
24026 } else
24027 return (ret);
24028 if (error == 0)
24029 error = ret;
24030 break;
24031 }
24032 tmpType = tmpType->baseType;
24033 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24034 }
24035
24036 return (error);
24037}
24038
24039static xmlChar *
24040xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
24041 const xmlChar *value)
24042{
24043 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
24044 case XML_SCHEMA_WHITESPACE_COLLAPSE:
24045 return (xmlSchemaCollapseString(value));
24046 case XML_SCHEMA_WHITESPACE_REPLACE:
24047 return (xmlSchemaWhiteSpaceReplace(value));
24048 default:
24049 return (NULL);
24050 }
24051}
24052
24053static int
24054xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
24055 const xmlChar *value,
24056 xmlSchemaValPtr *val,
24057 int valNeeded)
24058{
24059 int ret;
24060 const xmlChar *nsName;
24061 xmlChar *local, *prefix = NULL;
24062
24063 ret = xmlValidateQName(value, 1);
24064 if (ret != 0) {
24065 if (ret == -1) {
24066 VERROR_INT("xmlSchemaValidateQName",
24067 "calling xmlValidateQName()");
24068 return (-1);
24069 }
24070 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
24071 }
24072 /*
24073 * NOTE: xmlSplitQName2 will always return a duplicated
24074 * strings.
24075 */
24076 local = xmlSplitQName2(value, &prefix);
24077 if (local == NULL)
24078 local = xmlStrdup(value);
24079 /*
24080 * OPTIMIZE TODO: Use flags for:
24081 * - is there any namespace binding?
24082 * - is there a default namespace?
24083 */
24084 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24085
24086 if (prefix != NULL) {
24087 xmlFree(prefix);
24088 /*
24089 * A namespace must be found if the prefix is
24090 * NOT NULL.
24091 */
24092 if (nsName == NULL) {
24093 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024094 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024095 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024096 "The QName value '%s' has no "
24097 "corresponding namespace declaration in "
24098 "scope", value, NULL);
24099 if (local != NULL)
24100 xmlFree(local);
24101 return (ret);
24102 }
24103 }
24104 if (valNeeded && val) {
24105 if (nsName != NULL)
24106 *val = xmlSchemaNewQNameValue(
24107 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24108 else
24109 *val = xmlSchemaNewQNameValue(NULL,
24110 BAD_CAST local);
24111 } else
24112 xmlFree(local);
24113 return (0);
24114}
24115
24116/*
24117* cvc-simple-type
24118*/
24119static int
24120xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24121 xmlNodePtr node,
24122 xmlSchemaTypePtr type,
24123 const xmlChar *value,
24124 xmlSchemaValPtr *retVal,
24125 int fireErrors,
24126 int normalize,
24127 int isNormalized)
24128{
24129 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24130 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024131 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024132 xmlChar *normValue = NULL;
24133
24134#define NORMALIZE(atype) \
24135 if ((! isNormalized) && \
24136 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24137 normValue = xmlSchemaNormalizeValue(atype, value); \
24138 if (normValue != NULL) \
24139 value = normValue; \
24140 isNormalized = 1; \
24141 }
24142
24143 if ((retVal != NULL) && (*retVal != NULL)) {
24144 xmlSchemaFreeValue(*retVal);
24145 *retVal = NULL;
24146 }
24147 /*
24148 * 3.14.4 Simple Type Definition Validation Rules
24149 * Validation Rule: String Valid
24150 */
24151 /*
24152 * 1 It is schema-valid with respect to that definition as defined
24153 * by Datatype Valid in [XML Schemas: Datatypes].
24154 */
24155 /*
24156 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
24157 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
24158 * the string must be a ·declared entity name·.
24159 */
24160 /*
24161 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
24162 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24163 * then every whitespace-delimited substring of the string must be a ·declared
24164 * entity name·.
24165 */
24166 /*
24167 * 2.3 otherwise no further condition applies.
24168 */
24169 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24170 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024171 if (value == NULL)
24172 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024173 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024174 xmlSchemaTypePtr biType; /* The built-in type. */
24175 /*
24176 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24177 * a literal in the ·lexical space· of {base type definition}"
24178 */
24179 /*
24180 * Whitespace-normalize.
24181 */
24182 NORMALIZE(type);
24183 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24184 /*
24185 * Get the built-in type.
24186 */
24187 biType = type->baseType;
24188 while ((biType != NULL) &&
24189 (biType->type != XML_SCHEMA_TYPE_BASIC))
24190 biType = biType->baseType;
24191
24192 if (biType == NULL) {
24193 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24194 "could not get the built-in type");
24195 goto internal_error;
24196 }
24197 } else
24198 biType = type;
24199 /*
24200 * NOTATIONs need to be processed here, since they need
24201 * to lookup in the hashtable of NOTATION declarations of the schema.
24202 */
24203 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24204 switch (biType->builtInType) {
24205 case XML_SCHEMAS_NOTATION:
24206 ret = xmlSchemaValidateNotation(
24207 (xmlSchemaValidCtxtPtr) actxt,
24208 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24209 NULL, value, &val, valNeeded);
24210 break;
24211 case XML_SCHEMAS_QNAME:
24212 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24213 value, &val, valNeeded);
24214 break;
24215 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024216 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024217 if (valNeeded)
24218 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24219 value, &val, NULL);
24220 else
24221 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24222 value, NULL, NULL);
24223 break;
24224 }
24225 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24226 switch (biType->builtInType) {
24227 case XML_SCHEMAS_NOTATION:
24228 ret = xmlSchemaValidateNotation(NULL,
24229 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24230 value, &val, valNeeded);
24231 break;
24232 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024233 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024234 if (valNeeded)
24235 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24236 value, &val, node);
24237 else
24238 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24239 value, NULL, node);
24240 break;
24241 }
24242 } else {
24243 /*
24244 * Validation via a public API is not implemented yet.
24245 */
24246 TODO
24247 goto internal_error;
24248 }
24249 if (ret != 0) {
24250 if (ret < 0) {
24251 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24252 "validating against a built-in type");
24253 goto internal_error;
24254 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024255 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024256 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24257 else
24258 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24259 }
24260 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24261 /*
24262 * Check facets.
24263 */
24264 ret = xmlSchemaValidateFacets(actxt, node, type,
24265 (xmlSchemaValType) biType->builtInType, value, val,
24266 0, fireErrors);
24267 if (ret != 0) {
24268 if (ret < 0) {
24269 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24270 "validating facets of atomic simple type");
24271 goto internal_error;
24272 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024273 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024274 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24275 else
24276 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24277 }
24278 }
24279 if (fireErrors && (ret > 0))
24280 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024281 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024282
24283 xmlSchemaTypePtr itemType;
24284 const xmlChar *cur, *end;
24285 xmlChar *tmpValue = NULL;
24286 unsigned long len = 0;
24287 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24288 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24289 * of white space separated tokens, each of which ·match·es a literal
24290 * in the ·lexical space· of {item type definition}
24291 */
24292 /*
24293 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24294 * the list type has an enum or pattern facet.
24295 */
24296 NORMALIZE(type);
24297 /*
24298 * VAL TODO: Optimize validation of empty values.
24299 * VAL TODO: We do not have computed values for lists.
24300 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024301 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024302 cur = value;
24303 do {
24304 while (IS_BLANK_CH(*cur))
24305 cur++;
24306 end = cur;
24307 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24308 end++;
24309 if (end == cur)
24310 break;
24311 tmpValue = xmlStrndup(cur, end - cur);
24312 len++;
24313
24314 if (valNeeded)
24315 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24316 tmpValue, &curVal, fireErrors, 0, 1);
24317 else
24318 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24319 tmpValue, NULL, fireErrors, 0, 1);
24320 FREE_AND_NULL(tmpValue);
24321 if (curVal != NULL) {
24322 /*
24323 * Add to list of computed values.
24324 */
24325 if (val == NULL)
24326 val = curVal;
24327 else
24328 xmlSchemaValueAppend(prevVal, curVal);
24329 prevVal = curVal;
24330 curVal = NULL;
24331 }
24332 if (ret != 0) {
24333 if (ret < 0) {
24334 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24335 "validating an item of list simple type");
24336 goto internal_error;
24337 }
24338 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24339 break;
24340 }
24341 cur = end;
24342 } while (*cur != 0);
24343 FREE_AND_NULL(tmpValue);
24344 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24345 /*
24346 * Apply facets (pattern, enumeration).
24347 */
24348 ret = xmlSchemaValidateFacets(actxt, node, type,
24349 XML_SCHEMAS_UNKNOWN, value, val,
24350 len, fireErrors);
24351 if (ret != 0) {
24352 if (ret < 0) {
24353 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24354 "validating facets of list simple type");
24355 goto internal_error;
24356 }
24357 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24358 }
24359 }
24360 if (fireErrors && (ret > 0)) {
24361 /*
24362 * Report the normalized value.
24363 */
24364 normalize = 1;
24365 NORMALIZE(type);
24366 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24367 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024368 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024369 xmlSchemaTypeLinkPtr memberLink;
24370 /*
24371 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24372 * not apply directly; however, the normalization behavior of ·union·
24373 * types is controlled by the value of whiteSpace on that one of the
24374 * ·memberTypes· against which the ·union· is successfully validated.
24375 *
24376 * This means that the value is normalized by the first validating
24377 * member type, then the facets of the union type are applied. This
24378 * needs changing of the value!
24379 */
24380
24381 /*
24382 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24383 * literal in the ·lexical space· of at least one member of
24384 * {member type definitions}
24385 */
24386 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24387 if (memberLink == NULL) {
24388 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24389 "union simple type has no member types");
24390 goto internal_error;
24391 }
24392 /*
24393 * Always normalize union type values, since we currently
24394 * cannot store the whitespace information with the value
24395 * itself; otherwise a later value-comparison would be
24396 * not possible.
24397 */
24398 while (memberLink != NULL) {
24399 if (valNeeded)
24400 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24401 memberLink->type, value, &val, 0, 1, 0);
24402 else
24403 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24404 memberLink->type, value, NULL, 0, 1, 0);
24405 if (ret <= 0)
24406 break;
24407 memberLink = memberLink->next;
24408 }
24409 if (ret != 0) {
24410 if (ret < 0) {
24411 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24412 "validating members of union simple type");
24413 goto internal_error;
24414 }
24415 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24416 }
24417 /*
24418 * Apply facets (pattern, enumeration).
24419 */
24420 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24421 /*
24422 * The normalization behavior of ·union· types is controlled by
24423 * the value of whiteSpace on that one of the ·memberTypes·
24424 * against which the ·union· is successfully validated.
24425 */
24426 NORMALIZE(memberLink->type);
24427 ret = xmlSchemaValidateFacets(actxt, node, type,
24428 XML_SCHEMAS_UNKNOWN, value, val,
24429 0, fireErrors);
24430 if (ret != 0) {
24431 if (ret < 0) {
24432 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24433 "validating facets of union simple type");
24434 goto internal_error;
24435 }
24436 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24437 }
24438 }
24439 if (fireErrors && (ret > 0))
24440 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24441 }
24442
24443 if (normValue != NULL)
24444 xmlFree(normValue);
24445 if (ret == 0) {
24446 if (retVal != NULL)
24447 *retVal = val;
24448 else if (val != NULL)
24449 xmlSchemaFreeValue(val);
24450 } else if (val != NULL)
24451 xmlSchemaFreeValue(val);
24452 return (ret);
24453internal_error:
24454 if (normValue != NULL)
24455 xmlFree(normValue);
24456 if (val != NULL)
24457 xmlSchemaFreeValue(val);
24458 return (-1);
24459}
24460
24461static int
24462xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24463 const xmlChar *value,
24464 const xmlChar **nsName,
24465 const xmlChar **localName)
24466{
24467 int ret = 0;
24468
24469 if ((nsName == NULL) || (localName == NULL))
24470 return (-1);
24471 *nsName = NULL;
24472 *localName = NULL;
24473
24474 ret = xmlValidateQName(value, 1);
24475 if (ret == -1)
24476 return (-1);
24477 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024478 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024479 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24480 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24481 return (1);
24482 }
24483 {
24484 xmlChar *local = NULL;
24485 xmlChar *prefix;
24486
24487 /*
24488 * NOTE: xmlSplitQName2 will return a duplicated
24489 * string.
24490 */
24491 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024492 if (local == NULL)
24493 *localName = xmlDictLookup(vctxt->dict, value, -1);
24494 else {
24495 *localName = xmlDictLookup(vctxt->dict, local, -1);
24496 xmlFree(local);
24497 }
24498
24499 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24500
24501 if (prefix != NULL) {
24502 xmlFree(prefix);
24503 /*
24504 * A namespace must be found if the prefix is NOT NULL.
24505 */
24506 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024507 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024508 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024509 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024510 "The QName value '%s' has no "
24511 "corresponding namespace declaration in scope",
24512 value, NULL);
24513 return (2);
24514 }
24515 }
24516 }
24517 return (0);
24518}
24519
24520static int
24521xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24522 xmlSchemaAttrInfoPtr iattr,
24523 xmlSchemaTypePtr *localType,
24524 xmlSchemaElementPtr elemDecl)
24525{
24526 int ret = 0;
24527 /*
24528 * cvc-elt (3.3.4) : (4)
24529 * AND
24530 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24531 * (1.2.1.2.1) - (1.2.1.2.4)
24532 * Handle 'xsi:type'.
24533 */
24534 if (localType == NULL)
24535 return (-1);
24536 *localType = NULL;
24537 if (iattr == NULL)
24538 return (0);
24539 else {
24540 const xmlChar *nsName = NULL, *local = NULL;
24541 /*
24542 * TODO: We should report a *warning* that the type was overriden
24543 * by the instance.
24544 */
24545 ACTIVATE_ATTRIBUTE(iattr);
24546 /*
24547 * (cvc-elt) (3.3.4) : (4.1)
24548 * (cvc-assess-elt) (1.2.1.2.2)
24549 */
24550 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24551 &nsName, &local);
24552 if (ret != 0) {
24553 if (ret < 0) {
24554 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24555 "calling xmlSchemaQNameExpand() to validate the "
24556 "attribute 'xsi:type'");
24557 goto internal_error;
24558 }
24559 goto exit;
24560 }
24561 /*
24562 * (cvc-elt) (3.3.4) : (4.2)
24563 * (cvc-assess-elt) (1.2.1.2.3)
24564 */
24565 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24566 if (*localType == NULL) {
24567 xmlChar *str = NULL;
24568
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024569 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024570 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024571 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024572 "The QName value '%s' of the xsi:type attribute does not "
24573 "resolve to a type definition",
24574 xmlSchemaFormatQName(&str, nsName, local), NULL);
24575 FREE_AND_NULL(str);
24576 ret = vctxt->err;
24577 goto exit;
24578 }
24579 if (elemDecl != NULL) {
24580 int set = 0;
24581
24582 /*
24583 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24584 * "The ·local type definition· must be validly
24585 * derived from the {type definition} given the union of
24586 * the {disallowed substitutions} and the {type definition}'s
24587 * {prohibited substitutions}, as defined in
24588 * Type Derivation OK (Complex) (§3.4.6)
24589 * (if it is a complex type definition),
24590 * or given {disallowed substitutions} as defined in Type
24591 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24592 * definition)."
24593 *
24594 * {disallowed substitutions}: the "block" on the element decl.
24595 * {prohibited substitutions}: the "block" on the type def.
24596 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024597 /*
24598 * OPTIMIZE TODO: We could map types already evaluated
24599 * to be validly derived from other types to avoid checking
24600 * this over and over for the same types.
24601 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024602 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24603 (elemDecl->subtypes->flags &
24604 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24605 set |= SUBSET_EXTENSION;
24606
24607 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24608 (elemDecl->subtypes->flags &
24609 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24610 set |= SUBSET_RESTRICTION;
24611
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024612 /*
24613 * REMOVED and CHANGED since this produced a parser context
24614 * which adds to the string dict of the schema. So this would
24615 * change the schema and we don't want this. We don't need
24616 * the parser context anymore.
24617 *
24618 * if ((vctxt->pctxt == NULL) &&
24619 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24620 * return (-1);
24621 */
24622
24623 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024624 elemDecl->subtypes, set) != 0) {
24625 xmlChar *str = NULL;
24626
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024627 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024628 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24629 "The type definition '%s', specified by xsi:type, is "
24630 "blocked or not validly derived from the type definition "
24631 "of the element declaration",
24632 xmlSchemaFormatQName(&str,
24633 (*localType)->targetNamespace,
24634 (*localType)->name),
24635 NULL);
24636 FREE_AND_NULL(str);
24637 ret = vctxt->err;
24638 *localType = NULL;
24639 }
24640 }
24641 }
24642exit:
24643 ACTIVATE_ELEM;
24644 return (ret);
24645internal_error:
24646 ACTIVATE_ELEM;
24647 return (-1);
24648}
24649
24650static int
24651xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24652{
24653 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Daniel Veillard14b56432006-03-09 18:41:40 +000024654 xmlSchemaTypePtr actualType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024655
24656 /*
24657 * cvc-elt (3.3.4) : 1
24658 */
24659 if (elemDecl == NULL) {
24660 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24661 "No matching declaration available");
24662 return (vctxt->err);
24663 }
Daniel Veillard14b56432006-03-09 18:41:40 +000024664 actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024665 /*
24666 * cvc-elt (3.3.4) : 2
24667 */
24668 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24669 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24670 "The element declaration is abstract");
24671 return (vctxt->err);
24672 }
24673 if (actualType == NULL) {
24674 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24675 "The type definition is absent");
24676 return (XML_SCHEMAV_CVC_TYPE_1);
24677 }
24678 if (vctxt->nbAttrInfos != 0) {
24679 int ret;
24680 xmlSchemaAttrInfoPtr iattr;
24681 /*
24682 * cvc-elt (3.3.4) : 3
24683 * Handle 'xsi:nil'.
24684 */
24685 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24686 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24687 if (iattr) {
24688 ACTIVATE_ATTRIBUTE(iattr);
24689 /*
24690 * Validate the value.
24691 */
24692 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024693 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024694 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24695 iattr->value, &(iattr->val), 1, 0, 0);
24696 ACTIVATE_ELEM;
24697 if (ret < 0) {
24698 VERROR_INT("xmlSchemaValidateElemDecl",
24699 "calling xmlSchemaVCheckCVCSimpleType() to "
24700 "validate the attribute 'xsi:nil'");
24701 return (-1);
24702 }
24703 if (ret == 0) {
24704 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24705 /*
24706 * cvc-elt (3.3.4) : 3.1
24707 */
24708 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24709 "The element is not 'nillable'");
24710 /* Does not return an error on purpose. */
24711 } else {
24712 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24713 /*
24714 * cvc-elt (3.3.4) : 3.2.2
24715 */
24716 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24717 (elemDecl->value != NULL)) {
24718 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24719 "The element cannot be 'nilled' because "
24720 "there is a fixed value constraint defined "
24721 "for it");
24722 /* Does not return an error on purpose. */
24723 } else
24724 vctxt->inode->flags |=
24725 XML_SCHEMA_ELEM_INFO_NILLED;
24726 }
24727 }
24728 }
24729 }
24730 /*
24731 * cvc-elt (3.3.4) : 4
24732 * Handle 'xsi:type'.
24733 */
24734 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24735 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24736 if (iattr) {
24737 xmlSchemaTypePtr localType = NULL;
24738
24739 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24740 elemDecl);
24741 if (ret != 0) {
24742 if (ret == -1) {
24743 VERROR_INT("xmlSchemaValidateElemDecl",
24744 "calling xmlSchemaProcessXSIType() to "
24745 "process the attribute 'xsi:type'");
24746 return (-1);
24747 }
24748 /* Does not return an error on purpose. */
24749 }
24750 if (localType != NULL) {
24751 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24752 actualType = localType;
24753 }
24754 }
24755 }
24756 /*
24757 * IDC: Register identity-constraint XPath matchers.
24758 */
24759 if ((elemDecl->idcs != NULL) &&
24760 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24761 return (-1);
24762 /*
24763 * No actual type definition.
24764 */
24765 if (actualType == NULL) {
24766 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24767 "The type definition is absent");
24768 return (XML_SCHEMAV_CVC_TYPE_1);
24769 }
24770 /*
24771 * Remember the actual type definition.
24772 */
24773 vctxt->inode->typeDef = actualType;
24774
24775 return (0);
24776}
24777
24778static int
24779xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24780{
24781 xmlSchemaAttrInfoPtr iattr;
24782 int ret = 0, i;
24783
24784 /*
24785 * SPEC cvc-type (3.1.1)
24786 * "The attributes of must be empty, excepting those whose namespace
24787 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24788 * whose local name is one of type, nil, schemaLocation or
24789 * noNamespaceSchemaLocation."
24790 */
24791 if (vctxt->nbAttrInfos == 0)
24792 return (0);
24793 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24794 iattr = vctxt->attrInfos[i];
24795 if (! iattr->metaType) {
24796 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024797 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024798 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24799 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24800 }
24801 }
24802 ACTIVATE_ELEM
24803 return (ret);
24804}
24805
24806/*
24807* Cleanup currently used attribute infos.
24808*/
24809static void
24810xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24811{
24812 int i;
24813 xmlSchemaAttrInfoPtr attr;
24814
24815 if (vctxt->nbAttrInfos == 0)
24816 return;
24817 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24818 attr = vctxt->attrInfos[i];
24819 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24820 if (attr->localName != NULL)
24821 xmlFree((xmlChar *) attr->localName);
24822 if (attr->nsName != NULL)
24823 xmlFree((xmlChar *) attr->nsName);
24824 }
24825 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24826 if (attr->value != NULL)
24827 xmlFree((xmlChar *) attr->value);
24828 }
24829 if (attr->val != NULL) {
24830 xmlSchemaFreeValue(attr->val);
24831 attr->val = NULL;
24832 }
24833 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24834 }
24835 vctxt->nbAttrInfos = 0;
24836}
24837
24838/*
24839* 3.4.4 Complex Type Definition Validation Rules
24840* Element Locally Valid (Complex Type) (cvc-complex-type)
24841* 3.2.4 Attribute Declaration Validation Rules
24842* Validation Rule: Attribute Locally Valid (cvc-attribute)
24843* Attribute Locally Valid (Use) (cvc-au)
24844*
24845* Only "assessed" attribute information items will be visible to
24846* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24847*/
24848static int
24849xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24850{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024851 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24852 xmlSchemaItemListPtr attrUseList;
24853 xmlSchemaAttributeUsePtr attrUse = NULL;
24854 xmlSchemaAttributePtr attrDecl = NULL;
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000024855 xmlSchemaAttrInfoPtr iattr, tmpiattr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024856 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024857 int xpathRes = 0, res, wildIDs = 0, fixed;
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000024858 xmlNodePtr defAttrOwnerElem = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024859
24860 /*
24861 * SPEC (cvc-attribute)
24862 * (1) "The declaration must not be ·absent· (see Missing
24863 * Sub-components (§5.3) for how this can fail to be
24864 * the case)."
24865 * (2) "Its {type definition} must not be absent."
24866 *
24867 * NOTE (1) + (2): This is not handled here, since we currently do not
24868 * allow validation against schemas which have missing sub-components.
24869 *
24870 * SPEC (cvc-complex-type)
24871 * (3) "For each attribute information item in the element information
24872 * item's [attributes] excepting those whose [namespace name] is
24873 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24874 * [local name] is one of type, nil, schemaLocation or
24875 * noNamespaceSchemaLocation, the appropriate case among the following
24876 * must be true:
24877 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024878 */
24879 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24880 /*
24881 * @nbAttrs is the number of attributes present in the instance.
24882 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024883 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024884 if (attrUseList != NULL)
24885 nbUses = attrUseList->nbItems;
24886 else
24887 nbUses = 0;
24888 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024889 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024890 attrUse = attrUseList->items[i];
24891 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24892 for (j = 0; j < nbAttrs; j++) {
24893 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024894 /*
24895 * SPEC (cvc-complex-type) (3)
24896 * Skip meta attributes.
24897 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024898 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024899 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024900 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024901 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024902 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024903 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024904 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024905 continue;
24906 found = 1;
24907 /*
24908 * SPEC (cvc-complex-type)
24909 * (3.1) "If there is among the {attribute uses} an attribute
24910 * use with an {attribute declaration} whose {name} matches
24911 * the attribute information item's [local name] and whose
24912 * {target namespace} is identical to the attribute information
24913 * item's [namespace name] (where an ·absent· {target namespace}
24914 * is taken to be identical to a [namespace name] with no value),
24915 * then the attribute information must be ·valid· with respect
24916 * to that attribute use as per Attribute Locally Valid (Use)
24917 * (§3.5.4). In this case the {attribute declaration} of that
24918 * attribute use is the ·context-determined declaration· for the
24919 * attribute information item with respect to Schema-Validity
24920 * Assessment (Attribute) (§3.2.4) and
24921 * Assessment Outcome (Attribute) (§3.2.5).
24922 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024923 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24924 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024925 /*
24926 * Context-determined declaration.
24927 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024928 iattr->decl = attrDecl;
24929 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024930 break;
24931 }
24932
24933 if (found)
24934 continue;
24935
24936 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24937 /*
24938 * Handle non-existent, required attributes.
24939 *
24940 * SPEC (cvc-complex-type)
24941 * (4) "The {attribute declaration} of each attribute use in
24942 * the {attribute uses} whose {required} is true matches one
24943 * of the attribute information items in the element information
24944 * item's [attributes] as per clause 3.1 above."
24945 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024946 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24947 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024948 VERROR_INT(
24949 "xmlSchemaVAttributesComplex",
24950 "calling xmlSchemaGetFreshAttrInfo()");
24951 return (-1);
24952 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024953 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24954 tmpiattr->use = attrUse;
24955 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024956 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24957 ((attrUse->defValue != NULL) ||
24958 (attrDecl->defValue != NULL))) {
24959 /*
24960 * Handle non-existent, optional, default/fixed attributes.
24961 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024962 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24963 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024964 VERROR_INT(
24965 "xmlSchemaVAttributesComplex",
24966 "calling xmlSchemaGetFreshAttrInfo()");
24967 return (-1);
24968 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024969 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24970 tmpiattr->use = attrUse;
24971 tmpiattr->decl = attrDecl;
24972 tmpiattr->typeDef = attrDecl->subtypes;
24973 tmpiattr->localName = attrDecl->name;
24974 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024975 }
24976 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024977
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024978 if (vctxt->nbAttrInfos == 0)
24979 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024980 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024981 /*
24982 * Validate against the wildcard.
24983 */
24984 if (type->attributeWildcard != NULL) {
24985 /*
24986 * SPEC (cvc-complex-type)
24987 * (3.2.1) "There must be an {attribute wildcard}."
24988 */
24989 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024990 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024991 /*
24992 * SPEC (cvc-complex-type) (3)
24993 * Skip meta attributes.
24994 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024995 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024996 continue;
24997 /*
24998 * SPEC (cvc-complex-type)
24999 * (3.2.2) "The attribute information item must be ·valid· with
25000 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
25001 *
25002 * SPEC Item Valid (Wildcard) (cvc-wildcard)
25003 * "... its [namespace name] must be ·valid· with respect to
25004 * the wildcard constraint, as defined in Wildcard allows
25005 * Namespace Name (§3.10.4)."
25006 */
25007 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025008 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025009 /*
25010 * Handle processContents.
25011 *
25012 * SPEC (cvc-wildcard):
25013 * processContents | context-determined declaration:
25014 * "strict" "mustFind"
25015 * "lax" "none"
25016 * "skip" "skip"
25017 */
25018 if (type->attributeWildcard->processContents ==
25019 XML_SCHEMAS_ANY_SKIP) {
25020 /*
25021 * context-determined declaration = "skip"
25022 *
25023 * SPEC PSVI Assessment Outcome (Attribute)
25024 * [validity] = "notKnown"
25025 * [validation attempted] = "none"
25026 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025027 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025028 continue;
25029 }
25030 /*
25031 * Find an attribute declaration.
25032 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025033 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
25034 iattr->localName, iattr->nsName);
25035 if (iattr->decl != NULL) {
25036 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025037 /*
25038 * SPEC (cvc-complex-type)
25039 * (5) "Let [Definition:] the wild IDs be the set of
25040 * all attribute information item to which clause 3.2
25041 * applied and whose ·validation· resulted in a
25042 * ·context-determined declaration· of mustFind or no
25043 * ·context-determined declaration· at all, and whose
25044 * [local name] and [namespace name] resolve (as
25045 * defined by QName resolution (Instance) (§3.15.4)) to
25046 * an attribute declaration whose {type definition} is
25047 * or is derived from ID. Then all of the following
25048 * must be true:"
25049 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025050 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025051 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025052 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025053 /*
25054 * SPEC (5.1) "There must be no more than one
25055 * item in ·wild IDs·."
25056 */
25057 if (wildIDs != 0) {
25058 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025059 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025060 TODO
25061 continue;
25062 }
25063 wildIDs++;
25064 /*
25065 * SPEC (cvc-complex-type)
25066 * (5.2) "If ·wild IDs· is non-empty, there must not
25067 * be any attribute uses among the {attribute uses}
25068 * whose {attribute declaration}'s {type definition}
25069 * is or is derived from ID."
25070 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025071 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025072 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025073 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025074 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025075 /* URGENT VAL TODO: implement */
25076 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025077 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025078 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025079 }
25080 }
25081 }
25082 } else if (type->attributeWildcard->processContents ==
25083 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025084 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025085 /*
25086 * SPEC PSVI Assessment Outcome (Attribute)
25087 * [validity] = "notKnown"
25088 * [validation attempted] = "none"
25089 */
25090 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025091 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025092 }
25093 }
25094 }
25095 }
25096
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025097 if (vctxt->nbAttrInfos == 0)
25098 return (0);
25099
25100 /*
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025101 * Get the owner element; needed for creation of default attributes.
25102 * This fixes bug #341337, reported by David Grohmann.
25103 */
25104 if (vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) {
25105 xmlSchemaNodeInfoPtr ielem = vctxt->elemInfos[vctxt->depth];
25106 if (ielem && ielem->node && ielem->node->doc)
25107 defAttrOwnerElem = ielem->node;
25108 }
25109 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025110 * Validate values, create default attributes, evaluate IDCs.
25111 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025112 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025113 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025114 /*
25115 * VAL TODO: Note that we won't try to resolve IDCs to
25116 * "lax" and "skip" validated attributes. Check what to
25117 * do in this case.
25118 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025119 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25120 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025121 continue;
25122 /*
25123 * VAL TODO: What to do if the type definition is missing?
25124 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025125 if (iattr->typeDef == NULL) {
25126 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025127 continue;
25128 }
25129
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025130 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025131 fixed = 0;
25132 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025133
25134 if (vctxt->xpathStates != NULL) {
25135 /*
25136 * Evaluate IDCs.
25137 */
25138 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25139 XML_ATTRIBUTE_NODE);
25140 if (xpathRes == -1) {
25141 VERROR_INT("xmlSchemaVAttributesComplex",
25142 "calling xmlSchemaXPathEvaluate()");
25143 goto internal_error;
25144 }
25145 }
25146
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025147 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025148 /*
25149 * Default/fixed attributes.
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025150 * We need the value only if we need to resolve IDCs or
25151 * will create default attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025152 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025153 if ((xpathRes) || (defAttrOwnerElem)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025154 if (iattr->use->defValue != NULL) {
25155 iattr->value = (xmlChar *) iattr->use->defValue;
25156 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025157 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025158 iattr->value = (xmlChar *) iattr->decl->defValue;
25159 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025160 }
25161 /*
25162 * IDCs will consume the precomputed default value,
25163 * so we need to clone it.
25164 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025165 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025166 VERROR_INT("xmlSchemaVAttributesComplex",
25167 "default/fixed value on an attribute use was "
25168 "not precomputed");
25169 goto internal_error;
25170 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025171 iattr->val = xmlSchemaCopyValue(iattr->val);
25172 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025173 VERROR_INT("xmlSchemaVAttributesComplex",
25174 "calling xmlSchemaCopyValue()");
25175 goto internal_error;
25176 }
25177 }
25178 /*
25179 * PSVI: Add the default attribute to the current element.
25180 * VAL TODO: Should we use the *normalized* value? This currently
25181 * uses the *initial* value.
25182 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025183
25184 if (defAttrOwnerElem) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025185 xmlChar *normValue;
25186 const xmlChar *value;
25187
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025188 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025189 /*
25190 * Normalize the value.
25191 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025192 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25193 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025194 if (normValue != NULL)
25195 value = BAD_CAST normValue;
25196
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025197 if (iattr->nsName == NULL) {
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025198 if (xmlNewProp(defAttrOwnerElem,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025199 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025200 VERROR_INT("xmlSchemaVAttributesComplex",
25201 "callling xmlNewProp()");
25202 if (normValue != NULL)
25203 xmlFree(normValue);
25204 goto internal_error;
25205 }
25206 } else {
25207 xmlNsPtr ns;
25208
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025209 ns = xmlSearchNsByHref(defAttrOwnerElem->doc,
25210 defAttrOwnerElem, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025211 if (ns == NULL) {
25212 xmlChar prefix[12];
25213 int counter = 0;
25214
25215 /*
25216 * Create a namespace declaration on the validation
25217 * root node if no namespace declaration is in scope.
25218 */
25219 do {
25220 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025221 ns = xmlSearchNs(defAttrOwnerElem->doc,
25222 defAttrOwnerElem, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025223 if (counter > 1000) {
25224 VERROR_INT(
25225 "xmlSchemaVAttributesComplex",
25226 "could not compute a ns prefix for a "
25227 "default/fixed attribute");
25228 if (normValue != NULL)
25229 xmlFree(normValue);
25230 goto internal_error;
25231 }
25232 } while (ns != NULL);
25233 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025234 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025235 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025236 /*
25237 * TODO:
25238 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25239 * If we have QNames: do we need to ensure there's a
25240 * prefix defined for the QName?
25241 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025242 xmlNewNsProp(defAttrOwnerElem, ns, iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025243 }
25244 if (normValue != NULL)
25245 xmlFree(normValue);
25246 }
25247 /*
25248 * Go directly to IDC evaluation.
25249 */
25250 goto eval_idcs;
25251 }
25252 /*
25253 * Validate the value.
25254 */
25255 if (vctxt->value != NULL) {
25256 /*
25257 * Free last computed value; just for safety reasons.
25258 */
25259 xmlSchemaFreeValue(vctxt->value);
25260 vctxt->value = NULL;
25261 }
25262 /*
25263 * Note that the attribute *use* can be unavailable, if
25264 * the attribute was a wild attribute.
25265 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025266 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25267 ((iattr->use != NULL) &&
25268 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025269 fixed = 1;
25270 else
25271 fixed = 0;
25272 /*
25273 * SPEC (cvc-attribute)
25274 * (3) "The item's ·normalized value· must be locally ·valid·
25275 * with respect to that {type definition} as per
25276 * String Valid (§3.14.4)."
25277 *
25278 * VAL TODO: Do we already have the
25279 * "normalized attribute value" here?
25280 */
25281 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025282 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025283 /*
25284 * Request a computed value.
25285 */
25286 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025287 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025288 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025289 1, 1, 0);
25290 } else {
25291 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025292 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025293 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025294 1, 0, 0);
25295 }
25296
25297 if (res != 0) {
25298 if (res == -1) {
25299 VERROR_INT("xmlSchemaVAttributesComplex",
25300 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25301 goto internal_error;
25302 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025303 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025304 /*
25305 * SPEC PSVI Assessment Outcome (Attribute)
25306 * [validity] = "invalid"
25307 */
25308 goto eval_idcs;
25309 }
25310
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025311 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025312 /*
25313 * SPEC Attribute Locally Valid (Use) (cvc-au)
25314 * "For an attribute information item to be·valid·
25315 * with respect to an attribute use its *normalized*
25316 * value· must match the *canonical* lexical
25317 * representation of the attribute use's {value
25318 * constraint}value, if it is present and fixed."
25319 *
25320 * VAL TODO: The requirement for the *canonical* value
25321 * will be removed in XML Schema 1.1.
25322 */
25323 /*
25324 * SPEC Attribute Locally Valid (cvc-attribute)
25325 * (4) "The item's *actual* value· must match the *value* of
25326 * the {value constraint}, if it is present and fixed."
25327 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025328 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025329 /* VAL TODO: A value was not precomputed. */
25330 TODO
25331 goto eval_idcs;
25332 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025333 if ((iattr->use != NULL) &&
25334 (iattr->use->defValue != NULL)) {
25335 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025336 /* VAL TODO: A default value was not precomputed. */
25337 TODO
25338 goto eval_idcs;
25339 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025340 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025341 /*
25342 if (xmlSchemaCompareValuesWhtsp(attr->val,
25343 (xmlSchemaWhitespaceValueType) ws,
25344 attr->use->defVal,
25345 (xmlSchemaWhitespaceValueType) ws) != 0) {
25346 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025347 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25348 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025349 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025350 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025351 /* VAL TODO: A default value was not precomputed. */
25352 TODO
25353 goto eval_idcs;
25354 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025355 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025356 /*
25357 if (xmlSchemaCompareValuesWhtsp(attr->val,
25358 (xmlSchemaWhitespaceValueType) ws,
25359 attrDecl->defVal,
25360 (xmlSchemaWhitespaceValueType) ws) != 0) {
25361 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025362 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25363 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025364 }
25365 /*
25366 * [validity] = "valid"
25367 */
25368 }
25369eval_idcs:
25370 /*
25371 * Evaluate IDCs.
25372 */
25373 if (xpathRes) {
25374 if (xmlSchemaXPathProcessHistory(vctxt,
25375 vctxt->depth +1) == -1) {
25376 VERROR_INT("xmlSchemaVAttributesComplex",
25377 "calling xmlSchemaXPathEvaluate()");
25378 goto internal_error;
25379 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025380 } else if (vctxt->xpathStates != NULL)
25381 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025382 }
25383
25384 /*
25385 * Report errors.
25386 */
25387 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025388 iattr = vctxt->attrInfos[i];
25389 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25390 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25391 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25392 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025393 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025394 ACTIVATE_ATTRIBUTE(iattr);
25395 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025396 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25397 xmlChar *str = NULL;
25398 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025399 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025400 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25401 "The attribute '%s' is required but missing",
25402 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025403 iattr->decl->targetNamespace,
25404 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025405 NULL);
25406 FREE_AND_NULL(str)
25407 break;
25408 }
25409 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25410 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25411 "The type definition is absent");
25412 break;
25413 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025414 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025415 XML_SCHEMAV_CVC_AU, NULL, NULL,
25416 "The value '%s' does not match the fixed "
25417 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025418 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025419 break;
25420 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25421 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25422 "No matching global attribute declaration available, but "
25423 "demanded by the strict wildcard");
25424 break;
25425 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025426 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025427 break;
25428 /*
25429 * MAYBE VAL TODO: One might report different error messages
25430 * for the following errors.
25431 */
25432 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025433 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025434 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025435 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025436 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025437 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025438 }
25439 break;
25440 default:
25441 break;
25442 }
25443 }
25444
25445 ACTIVATE_ELEM;
25446 return (0);
25447internal_error:
25448 ACTIVATE_ELEM;
25449 return (-1);
25450}
25451
25452static int
25453xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25454 int *skip)
25455{
25456 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25457 /*
25458 * The namespace of the element was already identified to be
25459 * matching the wildcard.
25460 */
25461 if ((skip == NULL) || (wild == NULL) ||
25462 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25463 VERROR_INT("xmlSchemaValidateElemWildcard",
25464 "bad arguments");
25465 return (-1);
25466 }
25467 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025468 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25469 /*
25470 * URGENT VAL TODO: Either we need to position the stream to the
25471 * next sibling, or walk the whole subtree.
25472 */
25473 *skip = 1;
25474 return (0);
25475 }
25476 {
25477 xmlSchemaElementPtr decl = NULL;
25478
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025479 decl = xmlSchemaGetElem(vctxt->schema,
25480 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025481 if (decl != NULL) {
25482 vctxt->inode->decl = decl;
25483 return (0);
25484 }
25485 }
25486 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25487 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025488 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025489 "No matching global element declaration available, but "
25490 "demanded by the strict wildcard");
25491 return (vctxt->err);
25492 }
25493 if (vctxt->nbAttrInfos != 0) {
25494 xmlSchemaAttrInfoPtr iattr;
25495 /*
25496 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25497 * (1.2.1.2.1) - (1.2.1.2.3 )
25498 *
25499 * Use the xsi:type attribute for the type definition.
25500 */
25501 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25502 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25503 if (iattr != NULL) {
25504 if (xmlSchemaProcessXSIType(vctxt, iattr,
25505 &(vctxt->inode->typeDef), NULL) == -1) {
25506 VERROR_INT("xmlSchemaValidateElemWildcard",
25507 "calling xmlSchemaProcessXSIType() to "
25508 "process the attribute 'xsi:nil'");
25509 return (-1);
25510 }
25511 /*
25512 * Don't return an error on purpose.
25513 */
25514 return (0);
25515 }
25516 }
25517 /*
25518 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25519 *
25520 * Fallback to "anyType".
25521 */
25522 vctxt->inode->typeDef =
25523 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25524 return (0);
25525}
25526
25527/*
25528* xmlSchemaCheckCOSValidDefault:
25529*
25530* This will be called if: not nilled, no content and a default/fixed
25531* value is provided.
25532*/
25533
25534static int
25535xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25536 const xmlChar *value,
25537 xmlSchemaValPtr *val)
25538{
25539 int ret = 0;
25540 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25541
25542 /*
25543 * cos-valid-default:
25544 * Schema Component Constraint: Element Default Valid (Immediate)
25545 * For a string to be a valid default with respect to a type
25546 * definition the appropriate case among the following must be true:
25547 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025548 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025549 /*
25550 * Complex type.
25551 *
25552 * SPEC (2.1) "its {content type} must be a simple type definition
25553 * or mixed."
25554 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25555 * type}'s particle must be ·emptiable· as defined by
25556 * Particle Emptiable (§3.9.6)."
25557 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025558 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25559 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25560 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025561 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25562 /* NOTE that this covers (2.2.2) as well. */
25563 VERROR(ret, NULL,
25564 "For a string to be a valid default, the type definition "
25565 "must be a simple type or a complex type with simple content "
25566 "or mixed content and a particle emptiable");
25567 return(ret);
25568 }
25569 }
25570 /*
25571 * 1 If the type definition is a simple type definition, then the string
25572 * must be ·valid· with respect to that definition as defined by String
25573 * Valid (§3.14.4).
25574 *
25575 * AND
25576 *
25577 * 2.2.1 If the {content type} is a simple type definition, then the
25578 * string must be ·valid· with respect to that simple type definition
25579 * as defined by String Valid (§3.14.4).
25580 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025581 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025582
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025583 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025584 NULL, inode->typeDef, value, val, 1, 1, 0);
25585
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025586 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025587
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025588 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025589 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25590 }
25591 if (ret < 0) {
25592 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25593 "calling xmlSchemaVCheckCVCSimpleType()");
25594 }
25595 return (ret);
25596}
25597
25598static void
25599xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25600 const xmlChar * name ATTRIBUTE_UNUSED,
25601 xmlSchemaElementPtr item,
25602 xmlSchemaNodeInfoPtr inode)
25603{
25604 inode->decl = item;
25605#ifdef DEBUG_CONTENT
25606 {
25607 xmlChar *str = NULL;
25608
25609 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25610 xmlGenericError(xmlGenericErrorContext,
25611 "AUTOMATON callback for '%s' [declaration]\n",
25612 xmlSchemaFormatQName(&str,
25613 inode->localName, inode->nsName));
25614 } else {
25615 xmlGenericError(xmlGenericErrorContext,
25616 "AUTOMATON callback for '%s' [wildcard]\n",
25617 xmlSchemaFormatQName(&str,
25618 inode->localName, inode->nsName));
25619
25620 }
25621 FREE_AND_NULL(str)
25622 }
25623#endif
25624}
25625
25626static int
25627xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025628{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025629 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25630 if (vctxt->inode == NULL) {
25631 VERROR_INT("xmlSchemaValidatorPushElem",
25632 "calling xmlSchemaGetFreshElemInfo()");
25633 return (-1);
25634 }
25635 vctxt->nbAttrInfos = 0;
25636 return (0);
25637}
25638
25639static int
25640xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25641 xmlSchemaNodeInfoPtr inode,
25642 xmlSchemaTypePtr type,
25643 const xmlChar *value)
25644{
25645 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25646 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025647 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025648 type, value, &(inode->val), 1, 1, 0));
25649 else
25650 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025651 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025652 type, value, NULL, 1, 0, 0));
25653}
25654
25655
25656
25657/*
25658* Process END of element.
25659*/
25660static int
25661xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25662{
25663 int ret = 0;
25664 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25665
25666 if (vctxt->nbAttrInfos != 0)
25667 xmlSchemaClearAttrInfos(vctxt);
25668 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25669 /*
25670 * This element was not expected;
25671 * we will not validate child elements of broken parents.
25672 * Skip validation of all content of the parent.
25673 */
25674 vctxt->skipDepth = vctxt->depth -1;
25675 goto end_elem;
25676 }
25677 if ((inode->typeDef == NULL) ||
25678 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25679 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025680 * 1. the type definition might be missing if the element was
25681 * error prone
25682 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025683 */
25684 goto end_elem;
25685 }
25686 /*
25687 * Check the content model.
25688 */
25689 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25690 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25691
25692 /*
25693 * Workaround for "anyType".
25694 */
25695 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25696 goto character_content;
25697
25698 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25699 xmlChar *values[10];
25700 int terminal, nbval = 10, nbneg;
25701
25702 if (inode->regexCtxt == NULL) {
25703 /*
25704 * Create the regex context.
25705 */
25706 inode->regexCtxt =
25707 xmlRegNewExecCtxt(inode->typeDef->contModel,
25708 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25709 vctxt);
25710 if (inode->regexCtxt == NULL) {
25711 VERROR_INT("xmlSchemaValidatorPopElem",
25712 "failed to create a regex context");
25713 goto internal_error;
25714 }
25715#ifdef DEBUG_AUTOMATA
25716 xmlGenericError(xmlGenericErrorContext,
25717 "AUTOMATON create on '%s'\n", inode->localName);
25718#endif
25719 }
25720 /*
25721 * Get hold of the still expected content, since a further
25722 * call to xmlRegExecPushString() will loose this information.
25723 */
25724 xmlRegExecNextValues(inode->regexCtxt,
25725 &nbval, &nbneg, &values[0], &terminal);
25726 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25727 if (ret <= 0) {
25728 /*
25729 * Still missing something.
25730 */
25731 ret = 1;
25732 inode->flags |=
25733 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025734 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025735 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25736 "Missing child element(s)",
25737 nbval, nbneg, values);
25738#ifdef DEBUG_AUTOMATA
25739 xmlGenericError(xmlGenericErrorContext,
25740 "AUTOMATON missing ERROR on '%s'\n",
25741 inode->localName);
25742#endif
25743 } else {
25744 /*
25745 * Content model is satisfied.
25746 */
25747 ret = 0;
25748#ifdef DEBUG_AUTOMATA
25749 xmlGenericError(xmlGenericErrorContext,
25750 "AUTOMATON succeeded on '%s'\n",
25751 inode->localName);
25752#endif
25753 }
25754
25755 }
25756 }
25757 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25758 goto end_elem;
25759
25760character_content:
25761
25762 if (vctxt->value != NULL) {
25763 xmlSchemaFreeValue(vctxt->value);
25764 vctxt->value = NULL;
25765 }
25766 /*
25767 * Check character content.
25768 */
25769 if (inode->decl == NULL) {
25770 /*
25771 * Speedup if no declaration exists.
25772 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025773 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025774 ret = xmlSchemaVCheckINodeDataType(vctxt,
25775 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025776 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025777 ret = xmlSchemaVCheckINodeDataType(vctxt,
25778 inode, inode->typeDef->contentTypeDef,
25779 inode->value);
25780 }
25781 if (ret < 0) {
25782 VERROR_INT("xmlSchemaValidatorPopElem",
25783 "calling xmlSchemaVCheckCVCSimpleType()");
25784 goto internal_error;
25785 }
25786 goto end_elem;
25787 }
25788 /*
25789 * cvc-elt (3.3.4) : 5
25790 * The appropriate case among the following must be true:
25791 */
25792 /*
25793 * cvc-elt (3.3.4) : 5.1
25794 * If the declaration has a {value constraint},
25795 * the item has neither element nor character [children] and
25796 * clause 3.2 has not applied, then all of the following must be true:
25797 */
25798 if ((inode->decl->value != NULL) &&
25799 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25800 (! INODE_NILLED(inode))) {
25801 /*
25802 * cvc-elt (3.3.4) : 5.1.1
25803 * If the ·actual type definition· is a ·local type definition·
25804 * then the canonical lexical representation of the {value constraint}
25805 * value must be a valid default for the ·actual type definition· as
25806 * defined in Element Default Valid (Immediate) (§3.3.6).
25807 */
25808 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025809 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025810 * NOTE: Although the *canonical* value is stated, it is not
25811 * relevant if canonical or not. Additionally XML Schema 1.1
25812 * will removed this requirement as well.
25813 */
25814 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25815
25816 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25817 inode->decl->value, &(inode->val));
25818 if (ret != 0) {
25819 if (ret < 0) {
25820 VERROR_INT("xmlSchemaValidatorPopElem",
25821 "calling xmlSchemaCheckCOSValidDefault()");
25822 goto internal_error;
25823 }
25824 goto end_elem;
25825 }
25826 /*
25827 * Stop here, to avoid redundant validation of the value
25828 * (see following).
25829 */
25830 goto default_psvi;
25831 }
25832 /*
25833 * cvc-elt (3.3.4) : 5.1.2
25834 * The element information item with the canonical lexical
25835 * representation of the {value constraint} value used as its
25836 * ·normalized value· must be ·valid· with respect to the
25837 * ·actual type definition· as defined by Element Locally Valid (Type)
25838 * (§3.3.4).
25839 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025840 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025841 ret = xmlSchemaVCheckINodeDataType(vctxt,
25842 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025843 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025844 ret = xmlSchemaVCheckINodeDataType(vctxt,
25845 inode, inode->typeDef->contentTypeDef,
25846 inode->decl->value);
25847 }
25848 if (ret != 0) {
25849 if (ret < 0) {
25850 VERROR_INT("xmlSchemaValidatorPopElem",
25851 "calling xmlSchemaVCheckCVCSimpleType()");
25852 goto internal_error;
25853 }
25854 goto end_elem;
25855 }
25856
25857default_psvi:
25858 /*
25859 * PSVI: Create a text node on the instance element.
25860 */
25861 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25862 (inode->node != NULL)) {
25863 xmlNodePtr textChild;
25864 xmlChar *normValue;
25865 /*
25866 * VAL TODO: Normalize the value.
25867 */
25868 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25869 inode->decl->value);
25870 if (normValue != NULL) {
25871 textChild = xmlNewText(BAD_CAST normValue);
25872 xmlFree(normValue);
25873 } else
25874 textChild = xmlNewText(inode->decl->value);
25875 if (textChild == NULL) {
25876 VERROR_INT("xmlSchemaValidatorPopElem",
25877 "calling xmlNewText()");
25878 goto internal_error;
25879 } else
25880 xmlAddChild(inode->node, textChild);
25881 }
25882
25883 } else if (! INODE_NILLED(inode)) {
25884 /*
25885 * 5.2.1 The element information item must be ·valid· with respect
25886 * to the ·actual type definition· as defined by Element Locally
25887 * Valid (Type) (§3.3.4).
25888 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025889 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025890 /*
25891 * SPEC (cvc-type) (3.1)
25892 * "If the type definition is a simple type definition, ..."
25893 * (3.1.3) "If clause 3.2 of Element Locally Valid
25894 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25895 * must be ·valid· with respect to the type definition as defined
25896 * by String Valid (§3.14.4).
25897 */
25898 ret = xmlSchemaVCheckINodeDataType(vctxt,
25899 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025900 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025901 /*
25902 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25903 * definition, then the element information item must be
25904 * ·valid· with respect to the type definition as per
25905 * Element Locally Valid (Complex Type) (§3.4.4);"
25906 *
25907 * SPEC (cvc-complex-type) (2.2)
25908 * "If the {content type} is a simple type definition, ...
25909 * the ·normalized value· of the element information item is
25910 * ·valid· with respect to that simple type definition as
25911 * defined by String Valid (§3.14.4)."
25912 */
25913 ret = xmlSchemaVCheckINodeDataType(vctxt,
25914 inode, inode->typeDef->contentTypeDef, inode->value);
25915 }
25916 if (ret != 0) {
25917 if (ret < 0) {
25918 VERROR_INT("xmlSchemaValidatorPopElem",
25919 "calling xmlSchemaVCheckCVCSimpleType()");
25920 goto internal_error;
25921 }
25922 goto end_elem;
25923 }
25924 /*
25925 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25926 * not applied, all of the following must be true:
25927 */
25928 if ((inode->decl->value != NULL) &&
25929 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25930
25931 /*
25932 * TODO: We will need a computed value, when comparison is
25933 * done on computed values.
25934 */
25935 /*
25936 * 5.2.2.1 The element information item must have no element
25937 * information item [children].
25938 */
25939 if (inode->flags &
25940 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25941 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25942 VERROR(ret, NULL,
25943 "The content must not containt element nodes since "
25944 "there is a fixed value constraint");
25945 goto end_elem;
25946 } else {
25947 /*
25948 * 5.2.2.2 The appropriate case among the following must
25949 * be true:
25950 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025951 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025952 /*
25953 * 5.2.2.2.1 If the {content type} of the ·actual type
25954 * definition· is mixed, then the *initial value* of the
25955 * item must match the canonical lexical representation
25956 * of the {value constraint} value.
25957 *
25958 * ... the *initial value* of an element information
25959 * item is the string composed of, in order, the
25960 * [character code] of each character information item in
25961 * the [children] of that element information item.
25962 */
25963 if (! xmlStrEqual(inode->value, inode->decl->value)){
25964 /*
25965 * VAL TODO: Report invalid & expected values as well.
25966 * VAL TODO: Implement the canonical stuff.
25967 */
25968 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025969 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025970 ret, NULL, NULL,
25971 "The initial value '%s' does not match the fixed "
25972 "value constraint '%s'",
25973 inode->value, inode->decl->value);
25974 goto end_elem;
25975 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025976 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025977 /*
25978 * 5.2.2.2.2 If the {content type} of the ·actual type
25979 * definition· is a simple type definition, then the
25980 * *actual value* of the item must match the canonical
25981 * lexical representation of the {value constraint} value.
25982 */
25983 /*
25984 * VAL TODO: *actual value* is the normalized value, impl.
25985 * this.
25986 * VAL TODO: Report invalid & expected values as well.
25987 * VAL TODO: Implement a comparison with the computed values.
25988 */
25989 if (! xmlStrEqual(inode->value,
25990 inode->decl->value)) {
25991 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025992 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025993 ret, NULL, NULL,
25994 "The actual value '%s' does not match the fixed "
25995 "value constraint '%s'",
25996 inode->value,
25997 inode->decl->value);
25998 goto end_elem;
25999 }
26000 }
26001 }
26002 }
26003 }
26004
26005end_elem:
26006 if (vctxt->depth < 0) {
26007 /* TODO: raise error? */
26008 return (0);
26009 }
26010 if (vctxt->depth == vctxt->skipDepth)
26011 vctxt->skipDepth = -1;
26012 /*
26013 * Evaluate the history of XPath state objects.
26014 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026015 if (inode->appliedXPath &&
26016 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026017 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026018 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026019 * MAYBE TODO:
26020 * SPEC (6) "The element information item must be ·valid· with
26021 * respect to each of the {identity-constraint definitions} as per
26022 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026023 */
26024 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026025 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
26026 * need to be built in any case.
26027 * We will currently build IDC node-tables and bubble them only if
26028 * keyrefs do exist.
26029 */
26030
26031 /*
26032 * Add the current IDC target-nodes to the IDC node-tables.
26033 */
26034 if ((inode->idcMatchers != NULL) &&
26035 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26036 {
26037 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
26038 goto internal_error;
26039 }
26040 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026041 * Validate IDC keyrefs.
26042 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026043 if (vctxt->inode->hasKeyrefs)
26044 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
26045 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026046 /*
26047 * Merge/free the IDC table.
26048 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026049 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026050#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026051 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026052 inode->nsName,
26053 inode->localName,
26054 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026055#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026056 if ((vctxt->depth > 0) &&
26057 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26058 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026059 /*
26060 * Merge the IDC node table with the table of the parent node.
26061 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026062 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
26063 goto internal_error;
26064 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026065 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026066 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026067 * Clear the current ielem.
26068 * VAL TODO: Don't free the PSVI IDC tables if they are
26069 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026070 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026071 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026072 /*
26073 * Skip further processing if we are on the validation root.
26074 */
26075 if (vctxt->depth == 0) {
26076 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026077 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026078 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026079 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026080 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026081 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026082 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026083 if (vctxt->aidcs != NULL) {
26084 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
26085 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026086 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026087 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026088 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026089 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026090 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026091 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026092 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026093 }
26094 aidc = aidc->next;
26095 } while (aidc != NULL);
26096 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026097 vctxt->depth--;
26098 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000026099 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026100 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026101 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
26102 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026103 return (ret);
26104
26105internal_error:
26106 vctxt->err = -1;
26107 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026108}
26109
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026110/*
26111* 3.4.4 Complex Type Definition Validation Rules
26112* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26113*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026114static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026115xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026116{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026117 xmlSchemaNodeInfoPtr pielem;
26118 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026119 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026120
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026121 if (vctxt->depth <= 0) {
26122 VERROR_INT("xmlSchemaValidateChildElem",
26123 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026124 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026125 }
26126 pielem = vctxt->elemInfos[vctxt->depth -1];
26127 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26128 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026129 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026130 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026131 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026132 if (INODE_NILLED(pielem)) {
26133 /*
26134 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26135 */
26136 ACTIVATE_PARENT_ELEM;
26137 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26138 VERROR(ret, NULL,
26139 "Neither character nor element content is allowed, "
26140 "because the element was 'nilled'");
26141 ACTIVATE_ELEM;
26142 goto unexpected_elem;
26143 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026144
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026145 ptype = pielem->typeDef;
26146
26147 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26148 /*
26149 * Workaround for "anyType": we have currently no content model
26150 * assigned for "anyType", so handle it explicitely.
26151 * "anyType" has an unbounded, lax "any" wildcard.
26152 */
26153 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26154 vctxt->inode->localName,
26155 vctxt->inode->nsName);
26156
26157 if (vctxt->inode->decl == NULL) {
26158 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026159 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026160 * Process "xsi:type".
26161 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026162 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026163 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26164 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26165 if (iattr != NULL) {
26166 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26167 &(vctxt->inode->typeDef), NULL);
26168 if (ret != 0) {
26169 if (ret == -1) {
26170 VERROR_INT("xmlSchemaValidateChildElem",
26171 "calling xmlSchemaProcessXSIType() to "
26172 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026173 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026174 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026175 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026176 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026177 } else {
26178 /*
26179 * Fallback to "anyType".
26180 *
26181 * SPEC (cvc-assess-elt)
26182 * "If the item cannot be ·strictly assessed·, [...]
26183 * an element information item's schema validity may be laxly
26184 * assessed if its ·context-determined declaration· is not
26185 * skip by ·validating· with respect to the ·ur-type
26186 * definition· as per Element Locally Valid (Type) (§3.3.4)."
26187 */
26188 vctxt->inode->typeDef =
26189 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026190 }
26191 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026192 return (0);
26193 }
26194
26195 switch (ptype->contentType) {
26196 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026197 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026198 * SPEC (2.1) "If the {content type} is empty, then the
26199 * element information item has no character or element
26200 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026201 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026202 ACTIVATE_PARENT_ELEM
26203 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26204 VERROR(ret, NULL,
26205 "Element content is not allowed, "
26206 "because the content type is empty");
26207 ACTIVATE_ELEM
26208 goto unexpected_elem;
26209 break;
26210
26211 case XML_SCHEMA_CONTENT_MIXED:
26212 case XML_SCHEMA_CONTENT_ELEMENTS: {
26213 xmlRegExecCtxtPtr regexCtxt;
26214 xmlChar *values[10];
26215 int terminal, nbval = 10, nbneg;
26216
26217 /* VAL TODO: Optimized "anyType" validation.*/
26218
26219 if (ptype->contModel == NULL) {
26220 VERROR_INT("xmlSchemaValidateChildElem",
26221 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026222 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026223 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026224 /*
26225 * Safety belf for evaluation if the cont. model was already
26226 * examined to be invalid.
26227 */
26228 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26229 VERROR_INT("xmlSchemaValidateChildElem",
26230 "validating elem, but elem content is already invalid");
26231 return (-1);
26232 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026233
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026234 regexCtxt = pielem->regexCtxt;
26235 if (regexCtxt == NULL) {
26236 /*
26237 * Create the regex context.
26238 */
26239 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26240 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26241 vctxt);
26242 if (regexCtxt == NULL) {
26243 VERROR_INT("xmlSchemaValidateChildElem",
26244 "failed to create a regex context");
26245 return (-1);
26246 }
26247 pielem->regexCtxt = regexCtxt;
26248#ifdef DEBUG_AUTOMATA
26249 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26250 pielem->localName);
26251#endif
26252 }
26253
26254 /*
26255 * SPEC (2.4) "If the {content type} is element-only or mixed,
26256 * then the sequence of the element information item's
26257 * element information item [children], if any, taken in
26258 * order, is ·valid· with respect to the {content type}'s
26259 * particle, as defined in Element Sequence Locally Valid
26260 * (Particle) (§3.9.4)."
26261 */
26262 ret = xmlRegExecPushString2(regexCtxt,
26263 vctxt->inode->localName,
26264 vctxt->inode->nsName,
26265 vctxt->inode);
26266#ifdef DEBUG_AUTOMATA
26267 if (ret < 0)
26268 xmlGenericError(xmlGenericErrorContext,
26269 "AUTOMATON push ERROR for '%s' on '%s'\n",
26270 vctxt->inode->localName, pielem->localName);
26271 else
26272 xmlGenericError(xmlGenericErrorContext,
26273 "AUTOMATON push OK for '%s' on '%s'\n",
26274 vctxt->inode->localName, pielem->localName);
26275#endif
26276 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26277 VERROR_INT("xmlSchemaValidateChildElem",
26278 "calling xmlRegExecPushString2()");
26279 return (-1);
26280 }
26281 if (ret < 0) {
26282 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26283 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026284 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026285 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26286 "This element is not expected",
26287 nbval, nbneg, values);
26288 ret = vctxt->err;
26289 goto unexpected_elem;
26290 } else
26291 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026292 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026293 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026294 case XML_SCHEMA_CONTENT_SIMPLE:
26295 case XML_SCHEMA_CONTENT_BASIC:
26296 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026297 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026298 /*
26299 * SPEC (cvc-complex-type) (2.2)
26300 * "If the {content type} is a simple type definition, then
26301 * the element information item has no element information
26302 * item [children], ..."
26303 */
26304 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26305 VERROR(ret, NULL, "Element content is not allowed, "
26306 "because the content type is a simple type definition");
26307 } else {
26308 /*
26309 * SPEC (cvc-type) (3.1.2) "The element information item must
26310 * have no element information item [children]."
26311 */
26312 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26313 VERROR(ret, NULL, "Element content is not allowed, "
26314 "because the type definition is simple");
26315 }
26316 ACTIVATE_ELEM
26317 ret = vctxt->err;
26318 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026319 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026320
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026321 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026322 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026323 }
26324 return (ret);
26325unexpected_elem:
26326 /*
26327 * Pop this element and set the skipDepth to skip
26328 * all further content of the parent element.
26329 */
26330 vctxt->skipDepth = vctxt->depth;
26331 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26332 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26333 return (ret);
26334}
26335
26336#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26337#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26338#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26339
26340static int
26341xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26342 int nodeType, const xmlChar *value, int len,
26343 int mode, int *consumed)
26344{
26345 /*
26346 * Unfortunately we have to duplicate the text sometimes.
26347 * OPTIMIZE: Maybe we could skip it, if:
26348 * 1. content type is simple
26349 * 2. whitespace is "collapse"
26350 * 3. it consists of whitespace only
26351 *
26352 * Process character content.
26353 */
26354 if (consumed != NULL)
26355 *consumed = 0;
26356 if (INODE_NILLED(vctxt->inode)) {
26357 /*
26358 * SPEC cvc-elt (3.3.4 - 3.2.1)
26359 * "The element information item must have no character or
26360 * element information item [children]."
26361 */
26362 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26363 "Neither character nor element content is allowed "
26364 "because the element is 'nilled'");
26365 return (vctxt->err);
26366 }
26367 /*
26368 * SPEC (2.1) "If the {content type} is empty, then the
26369 * element information item has no character or element
26370 * information item [children]."
26371 */
26372 if (vctxt->inode->typeDef->contentType ==
26373 XML_SCHEMA_CONTENT_EMPTY) {
26374 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26375 "Character content is not allowed, "
26376 "because the content type is empty");
26377 return (vctxt->err);
26378 }
26379
26380 if (vctxt->inode->typeDef->contentType ==
26381 XML_SCHEMA_CONTENT_ELEMENTS) {
26382 if ((nodeType != XML_TEXT_NODE) ||
26383 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26384 /*
26385 * SPEC cvc-complex-type (2.3)
26386 * "If the {content type} is element-only, then the
26387 * element information item has no character information
26388 * item [children] other than those whose [character
26389 * code] is defined as a white space in [XML 1.0 (Second
26390 * Edition)]."
26391 */
26392 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26393 "Character content other than whitespace is not allowed "
26394 "because the content type is 'element-only'");
26395 return (vctxt->err);
26396 }
26397 return (0);
26398 }
26399
26400 if ((value == NULL) || (value[0] == 0))
26401 return (0);
26402 /*
26403 * Save the value.
26404 * NOTE that even if the content type is *mixed*, we need the
26405 * *initial value* for default/fixed value constraints.
26406 */
26407 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26408 ((vctxt->inode->decl == NULL) ||
26409 (vctxt->inode->decl->value == NULL)))
26410 return (0);
26411
26412 if (vctxt->inode->value == NULL) {
26413 /*
26414 * Set the value.
26415 */
26416 switch (mode) {
26417 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26418 /*
26419 * When working on a tree.
26420 */
26421 vctxt->inode->value = value;
26422 break;
26423 case XML_SCHEMA_PUSH_TEXT_CREATED:
26424 /*
26425 * When working with the reader.
26426 * The value will be freed by the element info.
26427 */
26428 vctxt->inode->value = value;
26429 if (consumed != NULL)
26430 *consumed = 1;
26431 vctxt->inode->flags |=
26432 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26433 break;
26434 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26435 /*
26436 * When working with SAX.
26437 * The value will be freed by the element info.
26438 */
26439 if (len != -1)
26440 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26441 else
26442 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26443 vctxt->inode->flags |=
26444 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26445 break;
26446 default:
26447 break;
26448 }
Kasimier T. Buchcik5bb0c082005-12-20 10:48:33 +000026449 } else {
26450 if (len < 0)
26451 len = xmlStrlen(value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026452 /*
26453 * Concat the value.
26454 */
26455 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026456 vctxt->inode->value = BAD_CAST xmlStrncat(
26457 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026458 } else {
26459 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026460 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026461 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26462 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026463 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026464
26465 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026466}
26467
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026468static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026469xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026470{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026471 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026472
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026473 if ((vctxt->skipDepth != -1) &&
26474 (vctxt->depth >= vctxt->skipDepth)) {
26475 VERROR_INT("xmlSchemaValidateElem",
26476 "in skip-state");
26477 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026478 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026479 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026480 /*
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026481 * We will stop validation if there was an error during
26482 * dynamic schema construction.
26483 * Note that we simply set @skipDepth to 0, this could
26484 * mean that a streaming document via SAX would be
26485 * still read to the end but it won't be validated any more.
26486 * TODO: If we are sure how to stop the validation at once
26487 * for all input scenarios, then this should be changed to
26488 * instantly stop the validation.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026489 */
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026490 ret = xmlSchemaAssembleByXSI(vctxt);
26491 if (ret != 0) {
26492 if (ret == -1)
26493 goto internal_error;
26494 vctxt->skipDepth = 0;
26495 return(ret);
26496 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026497 }
26498 if (vctxt->depth > 0) {
26499 /*
26500 * Validate this element against the content model
26501 * of the parent.
26502 */
26503 ret = xmlSchemaValidateChildElem(vctxt);
26504 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026505 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026506 VERROR_INT("xmlSchemaValidateElem",
26507 "calling xmlSchemaStreamValidateChildElement()");
26508 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026509 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026510 goto exit;
26511 }
26512 if (vctxt->depth == vctxt->skipDepth)
26513 goto exit;
26514 if ((vctxt->inode->decl == NULL) &&
26515 (vctxt->inode->typeDef == NULL)) {
26516 VERROR_INT("xmlSchemaValidateElem",
26517 "the child element was valid but neither the "
26518 "declaration nor the type was set");
26519 goto internal_error;
26520 }
26521 } else {
26522 /*
26523 * Get the declaration of the validation root.
26524 */
26525 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26526 vctxt->inode->localName,
26527 vctxt->inode->nsName);
26528 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026529 ret = XML_SCHEMAV_CVC_ELT_1;
26530 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026531 "No matching global declaration available "
26532 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026533 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026534 }
26535 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026536
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026537 if (vctxt->inode->decl == NULL)
26538 goto type_validation;
26539
26540 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26541 int skip;
26542 /*
26543 * Wildcards.
26544 */
26545 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26546 if (ret != 0) {
26547 if (ret < 0) {
26548 VERROR_INT("xmlSchemaValidateElem",
26549 "calling xmlSchemaValidateElemWildcard()");
26550 goto internal_error;
26551 }
26552 goto exit;
26553 }
26554 if (skip) {
26555 vctxt->skipDepth = vctxt->depth;
26556 goto exit;
26557 }
26558 /*
26559 * The declaration might be set by the wildcard validation,
26560 * when the processContents is "lax" or "strict".
26561 */
26562 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26563 /*
26564 * Clear the "decl" field to not confuse further processing.
26565 */
26566 vctxt->inode->decl = NULL;
26567 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026568 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026569 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026570 /*
26571 * Validate against the declaration.
26572 */
26573 ret = xmlSchemaValidateElemDecl(vctxt);
26574 if (ret != 0) {
26575 if (ret < 0) {
26576 VERROR_INT("xmlSchemaValidateElem",
26577 "calling xmlSchemaValidateElemDecl()");
26578 goto internal_error;
26579 }
26580 goto exit;
26581 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026582 /*
26583 * Validate against the type definition.
26584 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026585type_validation:
26586
26587 if (vctxt->inode->typeDef == NULL) {
26588 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26589 ret = XML_SCHEMAV_CVC_TYPE_1;
26590 VERROR(ret, NULL,
26591 "The type definition is absent");
26592 goto exit;
26593 }
26594 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26595 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26596 ret = XML_SCHEMAV_CVC_TYPE_2;
26597 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026598 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026599 goto exit;
26600 }
26601 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026602 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026603 * during validation against the declaration. This must be done
26604 * _before_ attribute validation.
26605 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026606 if (vctxt->xpathStates != NULL) {
26607 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026608 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026609 if (ret == -1) {
26610 VERROR_INT("xmlSchemaValidateElem",
26611 "calling xmlSchemaXPathEvaluate()");
26612 goto internal_error;
26613 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026614 }
26615 /*
26616 * Validate attributes.
26617 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026618 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026619 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026620 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026621
26622 ret = xmlSchemaVAttributesComplex(vctxt);
26623 }
26624 } else if (vctxt->nbAttrInfos != 0) {
26625
26626 ret = xmlSchemaVAttributesSimple(vctxt);
26627 }
26628 /*
26629 * Clear registered attributes.
26630 */
26631 if (vctxt->nbAttrInfos != 0)
26632 xmlSchemaClearAttrInfos(vctxt);
26633 if (ret == -1) {
26634 VERROR_INT("xmlSchemaValidateElem",
26635 "calling attributes validation");
26636 goto internal_error;
26637 }
26638 /*
26639 * Don't return an error if attributes are invalid on purpose.
26640 */
26641 ret = 0;
26642
26643exit:
26644 if (ret != 0)
26645 vctxt->skipDepth = vctxt->depth;
26646 return (ret);
26647internal_error:
26648 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026649}
26650
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026651#ifdef XML_SCHEMA_READER_ENABLED
26652static int
26653xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026654{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026655 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26656 int depth, nodeType, ret = 0, consumed;
26657 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026658
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026659 vctxt->depth = -1;
26660 ret = xmlTextReaderRead(vctxt->reader);
26661 /*
26662 * Move to the document element.
26663 */
26664 while (ret == 1) {
26665 nodeType = xmlTextReaderNodeType(vctxt->reader);
26666 if (nodeType == XML_ELEMENT_NODE)
26667 goto root_found;
26668 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026669 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026670 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026671
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026672root_found:
26673
26674 do {
26675 depth = xmlTextReaderDepth(vctxt->reader);
26676 nodeType = xmlTextReaderNodeType(vctxt->reader);
26677
26678 if (nodeType == XML_ELEMENT_NODE) {
26679
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026680 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026681 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26682 VERROR_INT("xmlSchemaVReaderWalk",
26683 "calling xmlSchemaValidatorPushElem()");
26684 goto internal_error;
26685 }
26686 ielem = vctxt->inode;
26687 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26688 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26689 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26690 /*
26691 * Is the element empty?
26692 */
26693 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26694 if (ret == -1) {
26695 VERROR_INT("xmlSchemaVReaderWalk",
26696 "calling xmlTextReaderIsEmptyElement()");
26697 goto internal_error;
26698 }
26699 if (ret) {
26700 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26701 }
26702 /*
26703 * Register attributes.
26704 */
26705 vctxt->nbAttrInfos = 0;
26706 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26707 if (ret == -1) {
26708 VERROR_INT("xmlSchemaVReaderWalk",
26709 "calling xmlTextReaderMoveToFirstAttribute()");
26710 goto internal_error;
26711 }
26712 if (ret == 1) {
26713 do {
26714 /*
26715 * VAL TODO: How do we know that the reader works on a
26716 * node tree, to be able to pass a node here?
26717 */
26718 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26719 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26720 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26721 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26722
26723 VERROR_INT("xmlSchemaVReaderWalk",
26724 "calling xmlSchemaValidatorPushAttribute()");
26725 goto internal_error;
26726 }
26727 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26728 if (ret == -1) {
26729 VERROR_INT("xmlSchemaVReaderWalk",
26730 "calling xmlTextReaderMoveToFirstAttribute()");
26731 goto internal_error;
26732 }
26733 } while (ret == 1);
26734 /*
26735 * Back to element position.
26736 */
26737 ret = xmlTextReaderMoveToElement(vctxt->reader);
26738 if (ret == -1) {
26739 VERROR_INT("xmlSchemaVReaderWalk",
26740 "calling xmlTextReaderMoveToElement()");
26741 goto internal_error;
26742 }
26743 }
26744 /*
26745 * Validate the element.
26746 */
26747 ret= xmlSchemaValidateElem(vctxt);
26748 if (ret != 0) {
26749 if (ret == -1) {
26750 VERROR_INT("xmlSchemaVReaderWalk",
26751 "calling xmlSchemaValidateElem()");
26752 goto internal_error;
26753 }
26754 goto exit;
26755 }
26756 if (vctxt->depth == vctxt->skipDepth) {
26757 int curDepth;
26758 /*
26759 * Skip all content.
26760 */
26761 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26762 ret = xmlTextReaderRead(vctxt->reader);
26763 curDepth = xmlTextReaderDepth(vctxt->reader);
26764 while ((ret == 1) && (curDepth != depth)) {
26765 ret = xmlTextReaderRead(vctxt->reader);
26766 curDepth = xmlTextReaderDepth(vctxt->reader);
26767 }
26768 if (ret < 0) {
26769 /*
26770 * VAL TODO: A reader error occured; what to do here?
26771 */
26772 ret = 1;
26773 goto exit;
26774 }
26775 }
26776 goto leave_elem;
26777 }
26778 /*
26779 * READER VAL TODO: Is an END_ELEM really never called
26780 * if the elem is empty?
26781 */
26782 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26783 goto leave_elem;
26784 } else if (nodeType == END_ELEM) {
26785 /*
26786 * Process END of element.
26787 */
26788leave_elem:
26789 ret = xmlSchemaValidatorPopElem(vctxt);
26790 if (ret != 0) {
26791 if (ret < 0) {
26792 VERROR_INT("xmlSchemaVReaderWalk",
26793 "calling xmlSchemaValidatorPopElem()");
26794 goto internal_error;
26795 }
26796 goto exit;
26797 }
26798 if (vctxt->depth >= 0)
26799 ielem = vctxt->inode;
26800 else
26801 ielem = NULL;
26802 } else if ((nodeType == XML_TEXT_NODE) ||
26803 (nodeType == XML_CDATA_SECTION_NODE) ||
26804 (nodeType == WHTSP) ||
26805 (nodeType == SIGN_WHTSP)) {
26806 /*
26807 * Process character content.
26808 */
26809 xmlChar *value;
26810
26811 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26812 nodeType = XML_TEXT_NODE;
26813
26814 value = xmlTextReaderValue(vctxt->reader);
26815 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26816 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26817 if (! consumed)
26818 xmlFree(value);
26819 if (ret == -1) {
26820 VERROR_INT("xmlSchemaVReaderWalk",
26821 "calling xmlSchemaVPushText()");
26822 goto internal_error;
26823 }
26824 } else if ((nodeType == XML_ENTITY_NODE) ||
26825 (nodeType == XML_ENTITY_REF_NODE)) {
26826 /*
26827 * VAL TODO: What to do with entities?
26828 */
26829 TODO
26830 }
26831 /*
26832 * Read next node.
26833 */
26834 ret = xmlTextReaderRead(vctxt->reader);
26835 } while (ret == 1);
26836
26837exit:
26838 return (ret);
26839internal_error:
26840 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026841}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026842#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026843
26844/************************************************************************
26845 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026846 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026847 * *
26848 ************************************************************************/
26849
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026850/*
26851* Process text content.
26852*/
26853static void
26854xmlSchemaSAXHandleText(void *ctx,
26855 const xmlChar * ch,
26856 int len)
26857{
26858 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26859
26860 if (vctxt->depth < 0)
26861 return;
26862 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26863 return;
26864 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26865 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26866 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26867 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26868 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26869 "calling xmlSchemaVPushText()");
26870 vctxt->err = -1;
26871 xmlStopParser(vctxt->parserCtxt);
26872 }
26873}
26874
26875/*
26876* Process CDATA content.
26877*/
26878static void
26879xmlSchemaSAXHandleCDataSection(void *ctx,
26880 const xmlChar * ch,
26881 int len)
26882{
26883 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26884
26885 if (vctxt->depth < 0)
26886 return;
26887 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26888 return;
26889 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26890 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26891 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26892 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26893 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26894 "calling xmlSchemaVPushText()");
26895 vctxt->err = -1;
26896 xmlStopParser(vctxt->parserCtxt);
26897 }
26898}
26899
26900static void
26901xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26902 const xmlChar * name ATTRIBUTE_UNUSED)
26903{
26904 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26905
26906 if (vctxt->depth < 0)
26907 return;
26908 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26909 return;
26910 /* SAX VAL TODO: What to do here? */
26911 TODO
26912}
26913
26914static void
26915xmlSchemaSAXHandleStartElementNs(void *ctx,
26916 const xmlChar * localname,
26917 const xmlChar * prefix ATTRIBUTE_UNUSED,
26918 const xmlChar * URI,
26919 int nb_namespaces,
26920 const xmlChar ** namespaces,
26921 int nb_attributes,
26922 int nb_defaulted ATTRIBUTE_UNUSED,
26923 const xmlChar ** attributes)
26924{
26925 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26926 int ret;
26927 xmlSchemaNodeInfoPtr ielem;
26928 int i, j;
26929
26930 /*
26931 * SAX VAL TODO: What to do with nb_defaulted?
26932 */
26933 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026934 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026935 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026936 vctxt->depth++;
26937 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026938 return;
26939 /*
26940 * Push the element.
26941 */
26942 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26943 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26944 "calling xmlSchemaValidatorPushElem()");
26945 goto internal_error;
26946 }
26947 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026948 /*
26949 * TODO: Is this OK?
26950 */
26951 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026952 ielem->localName = localname;
26953 ielem->nsName = URI;
26954 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26955 /*
26956 * Register namespaces on the elem info.
26957 */
26958 if (nb_namespaces != 0) {
26959 /*
26960 * Although the parser builds its own namespace list,
26961 * we have no access to it, so we'll use an own one.
26962 */
26963 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26964 /*
26965 * Store prefix and namespace name.
26966 */
26967 if (ielem->nsBindings == NULL) {
26968 ielem->nsBindings =
26969 (const xmlChar **) xmlMalloc(10 *
26970 sizeof(const xmlChar *));
26971 if (ielem->nsBindings == NULL) {
26972 xmlSchemaVErrMemory(vctxt,
26973 "allocating namespace bindings for SAX validation",
26974 NULL);
26975 goto internal_error;
26976 }
26977 ielem->nbNsBindings = 0;
26978 ielem->sizeNsBindings = 5;
26979 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26980 ielem->sizeNsBindings *= 2;
26981 ielem->nsBindings =
26982 (const xmlChar **) xmlRealloc(
26983 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026984 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026985 if (ielem->nsBindings == NULL) {
26986 xmlSchemaVErrMemory(vctxt,
26987 "re-allocating namespace bindings for SAX validation",
26988 NULL);
26989 goto internal_error;
26990 }
26991 }
26992
26993 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26994 if (namespaces[j+1][0] == 0) {
26995 /*
26996 * Handle xmlns="".
26997 */
26998 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26999 } else
27000 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
27001 namespaces[j+1];
27002 ielem->nbNsBindings++;
27003 }
27004 }
27005 /*
27006 * Register attributes.
27007 * SAX VAL TODO: We are not adding namespace declaration
27008 * attributes yet.
27009 */
27010 if (nb_attributes != 0) {
27011 xmlChar *value;
27012
27013 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
27014 /*
27015 * Duplicate the value.
27016 */
27017 value = xmlStrndup(attributes[j+3],
27018 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027019 /*
27020 * TODO: Set the node line.
27021 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027022 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027023 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027024 value, 1);
27025 if (ret == -1) {
27026 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27027 "calling xmlSchemaValidatorPushAttribute()");
27028 goto internal_error;
27029 }
27030 }
27031 }
27032 /*
27033 * Validate the element.
27034 */
27035 ret = xmlSchemaValidateElem(vctxt);
27036 if (ret != 0) {
27037 if (ret == -1) {
27038 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27039 "calling xmlSchemaValidateElem()");
27040 goto internal_error;
27041 }
27042 goto exit;
27043 }
27044
27045exit:
27046 return;
27047internal_error:
27048 vctxt->err = -1;
27049 xmlStopParser(vctxt->parserCtxt);
27050 return;
27051}
27052
27053static void
27054xmlSchemaSAXHandleEndElementNs(void *ctx,
27055 const xmlChar * localname ATTRIBUTE_UNUSED,
27056 const xmlChar * prefix ATTRIBUTE_UNUSED,
27057 const xmlChar * URI ATTRIBUTE_UNUSED)
27058{
27059 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27060 int res;
27061
27062 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027063 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027064 */
27065 if (vctxt->skipDepth != -1) {
27066 if (vctxt->depth > vctxt->skipDepth) {
27067 vctxt->depth--;
27068 return;
27069 } else
27070 vctxt->skipDepth = -1;
27071 }
27072 /*
27073 * SAX VAL TODO: Just a temporary check.
27074 */
27075 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
27076 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
27077 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27078 "elem pop mismatch");
27079 }
27080 res = xmlSchemaValidatorPopElem(vctxt);
27081 if (res != 0) {
27082 if (res < 0) {
27083 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27084 "calling xmlSchemaValidatorPopElem()");
27085 goto internal_error;
27086 }
27087 goto exit;
27088 }
27089exit:
27090 return;
27091internal_error:
27092 vctxt->err = -1;
27093 xmlStopParser(vctxt->parserCtxt);
27094 return;
27095}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027096
Daniel Veillard4255d502002-04-16 15:50:10 +000027097/************************************************************************
27098 * *
27099 * Validation interfaces *
27100 * *
27101 ************************************************************************/
27102
27103/**
27104 * xmlSchemaNewValidCtxt:
27105 * @schema: a precompiled XML Schemas
27106 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027107 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000027108 *
27109 * Returns the validation context or NULL in case of error
27110 */
27111xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027112xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
27113{
Daniel Veillard4255d502002-04-16 15:50:10 +000027114 xmlSchemaValidCtxtPtr ret;
27115
27116 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
27117 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027118 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000027119 return (NULL);
27120 }
27121 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027122 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027123 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027124 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027125 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027126 return (ret);
27127}
27128
27129/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027130 * xmlSchemaClearValidCtxt:
27131 * @ctxt: the schema validation context
27132 *
27133 * Free the resources associated to the schema validation context;
27134 * leaves some fields alive intended for reuse of the context.
27135 */
27136static void
27137xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27138{
27139 if (vctxt == NULL)
27140 return;
27141
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027142 /*
27143 * TODO: Should we clear the flags?
27144 * Might be problematic if one reuses the context
27145 * and assumes that the options remain the same.
27146 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027147 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027148 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027149 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027150#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027151 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027152#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027153 vctxt->hasKeyrefs = 0;
27154
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027155 if (vctxt->value != NULL) {
27156 xmlSchemaFreeValue(vctxt->value);
27157 vctxt->value = NULL;
27158 }
27159 /*
27160 * Augmented IDC information.
27161 */
27162 if (vctxt->aidcs != NULL) {
27163 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27164 do {
27165 next = cur->next;
27166 xmlFree(cur);
27167 cur = next;
27168 } while (cur != NULL);
27169 vctxt->aidcs = NULL;
27170 }
27171 if (vctxt->idcNodes != NULL) {
27172 int i;
27173 xmlSchemaPSVIIDCNodePtr item;
27174
27175 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027176 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027177 xmlFree(item->keys);
27178 xmlFree(item);
27179 }
27180 xmlFree(vctxt->idcNodes);
27181 vctxt->idcNodes = NULL;
Daniel Veillard1131e112006-08-07 11:02:54 +000027182 vctxt->nbIdcNodes = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027183 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027184 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027185 * Note that we won't delete the XPath state pool here.
27186 */
27187 if (vctxt->xpathStates != NULL) {
27188 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27189 vctxt->xpathStates = NULL;
27190 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027191 /*
27192 * Attribute info.
27193 */
27194 if (vctxt->nbAttrInfos != 0) {
27195 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027196 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027197 /*
27198 * Element info.
27199 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027200 if (vctxt->elemInfos != NULL) {
27201 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027202 xmlSchemaNodeInfoPtr ei;
27203
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027204 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027205 ei = vctxt->elemInfos[i];
27206 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027207 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027208 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027209 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027210 }
27211 xmlSchemaItemListClear(vctxt->nodeQNames);
27212 /* Recreate the dict. */
27213 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027214 /*
27215 * TODO: Is is save to recreate it? Do we have a scenario
27216 * where the user provides the dict?
27217 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027218 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027219}
27220
27221/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027222 * xmlSchemaFreeValidCtxt:
27223 * @ctxt: the schema validation context
27224 *
27225 * Free the resources associated to the schema validation context
27226 */
27227void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027228xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27229{
Daniel Veillard4255d502002-04-16 15:50:10 +000027230 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027231 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027232 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027233 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027234 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027235 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027236 if (ctxt->idcNodes != NULL) {
27237 int i;
27238 xmlSchemaPSVIIDCNodePtr item;
27239
27240 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027241 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027242 xmlFree(item->keys);
27243 xmlFree(item);
27244 }
27245 xmlFree(ctxt->idcNodes);
27246 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027247 if (ctxt->idcKeys != NULL) {
27248 int i;
27249 for (i = 0; i < ctxt->nbIdcKeys; i++)
27250 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27251 xmlFree(ctxt->idcKeys);
27252 }
27253
27254 if (ctxt->xpathStates != NULL)
27255 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27256 if (ctxt->xpathStatePool != NULL)
27257 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27258
27259 /*
27260 * Augmented IDC information.
27261 */
27262 if (ctxt->aidcs != NULL) {
27263 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27264 do {
27265 next = cur->next;
27266 xmlFree(cur);
27267 cur = next;
27268 } while (cur != NULL);
27269 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027270 if (ctxt->attrInfos != NULL) {
27271 int i;
27272 xmlSchemaAttrInfoPtr attr;
27273
27274 /* Just a paranoid call to the cleanup. */
27275 if (ctxt->nbAttrInfos != 0)
27276 xmlSchemaClearAttrInfos(ctxt);
27277 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27278 attr = ctxt->attrInfos[i];
27279 xmlFree(attr);
27280 }
27281 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027282 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027283 if (ctxt->elemInfos != NULL) {
27284 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027285 xmlSchemaNodeInfoPtr ei;
27286
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027287 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027288 ei = ctxt->elemInfos[i];
27289 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027290 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027291 xmlSchemaClearElemInfo(ei);
27292 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027293 }
27294 xmlFree(ctxt->elemInfos);
27295 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027296 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027297 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027298 if (ctxt->dict != NULL)
27299 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027300 xmlFree(ctxt);
27301}
27302
27303/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027304 * xmlSchemaIsValid:
27305 * @ctxt: the schema validation context
27306 *
27307 * Check if any error was detected during validation.
27308 *
27309 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27310 * of internal error.
27311 */
27312int
27313xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27314{
27315 if (ctxt == NULL)
27316 return(-1);
27317 return(ctxt->err == 0);
27318}
27319
27320/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027321 * xmlSchemaSetValidErrors:
27322 * @ctxt: a schema validation context
27323 * @err: the error function
27324 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027325 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027326 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027327 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027328 */
27329void
27330xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027331 xmlSchemaValidityErrorFunc err,
27332 xmlSchemaValidityWarningFunc warn, void *ctx)
27333{
Daniel Veillard4255d502002-04-16 15:50:10 +000027334 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027335 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027336 ctxt->error = err;
27337 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027338 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027339 if (ctxt->pctxt != NULL)
27340 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027341}
27342
27343/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027344 * xmlSchemaSetValidStructuredErrors:
27345 * @ctxt: a schema validation context
27346 * @serror: the structured error function
27347 * @ctx: the functions context
27348 *
27349 * Set the structured error callback
27350 */
27351void
27352xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027353 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027354{
27355 if (ctxt == NULL)
27356 return;
27357 ctxt->serror = serror;
27358 ctxt->error = NULL;
27359 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027360 ctxt->errCtxt = ctx;
27361 if (ctxt->pctxt != NULL)
27362 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027363}
27364
27365/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027366 * xmlSchemaGetValidErrors:
27367 * @ctxt: a XML-Schema validation context
27368 * @err: the error function result
27369 * @warn: the warning function result
27370 * @ctx: the functions context result
27371 *
27372 * Get the error and warning callback informations
27373 *
27374 * Returns -1 in case of error and 0 otherwise
27375 */
27376int
27377xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027378 xmlSchemaValidityErrorFunc * err,
27379 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027380{
27381 if (ctxt == NULL)
27382 return (-1);
27383 if (err != NULL)
27384 *err = ctxt->error;
27385 if (warn != NULL)
27386 *warn = ctxt->warning;
27387 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027388 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027389 return (0);
27390}
27391
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027392
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027393/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027394 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027395 * @ctxt: a schema validation context
27396 * @options: a combination of xmlSchemaValidOption
27397 *
27398 * Sets the options to be used during the validation.
27399 *
27400 * Returns 0 in case of success, -1 in case of an
27401 * API error.
27402 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027403int
27404xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27405 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027406
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027407{
27408 int i;
27409
27410 if (ctxt == NULL)
27411 return (-1);
27412 /*
27413 * WARNING: Change the start value if adding to the
27414 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027415 * TODO: Is there an other, more easy to maintain,
27416 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027417 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027418 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027419 if (options & 1<<i)
27420 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027421 }
27422 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027423 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027424}
27425
27426/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027427 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027428 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027429 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027430 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027431 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027432 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027433 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027434int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027435xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027436
27437{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027438 if (ctxt == NULL)
27439 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027440 else
27441 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027442}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027443
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027444static int
27445xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27446{
27447 xmlAttrPtr attr;
27448 int ret = 0;
27449 xmlSchemaNodeInfoPtr ielem = NULL;
27450 xmlNodePtr node, valRoot;
27451 const xmlChar *nsName;
27452
27453 /* DOC VAL TODO: Move this to the start function. */
27454 valRoot = xmlDocGetRootElement(vctxt->doc);
27455 if (valRoot == NULL) {
27456 /* VAL TODO: Error code? */
27457 VERROR(1, NULL, "The document has no document element");
27458 return (1);
27459 }
27460 vctxt->depth = -1;
27461 vctxt->validationRoot = valRoot;
27462 node = valRoot;
27463 while (node != NULL) {
27464 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27465 goto next_sibling;
27466 if (node->type == XML_ELEMENT_NODE) {
27467
27468 /*
27469 * Init the node-info.
27470 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027471 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027472 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27473 goto internal_error;
27474 ielem = vctxt->inode;
27475 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027476 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027477 ielem->localName = node->name;
27478 if (node->ns != NULL)
27479 ielem->nsName = node->ns->href;
27480 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27481 /*
27482 * Register attributes.
27483 * DOC VAL TODO: We do not register namespace declaration
27484 * attributes yet.
27485 */
27486 vctxt->nbAttrInfos = 0;
27487 if (node->properties != NULL) {
27488 attr = node->properties;
27489 do {
27490 if (attr->ns != NULL)
27491 nsName = attr->ns->href;
27492 else
27493 nsName = NULL;
27494 ret = xmlSchemaValidatorPushAttribute(vctxt,
27495 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027496 /*
27497 * Note that we give it the line number of the
27498 * parent element.
27499 */
27500 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027501 attr->name, nsName, 0,
27502 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27503 if (ret == -1) {
27504 VERROR_INT("xmlSchemaDocWalk",
27505 "calling xmlSchemaValidatorPushAttribute()");
27506 goto internal_error;
27507 }
27508 attr = attr->next;
27509 } while (attr);
27510 }
27511 /*
27512 * Validate the element.
27513 */
27514 ret = xmlSchemaValidateElem(vctxt);
27515 if (ret != 0) {
27516 if (ret == -1) {
27517 VERROR_INT("xmlSchemaDocWalk",
27518 "calling xmlSchemaValidateElem()");
27519 goto internal_error;
27520 }
27521 /*
27522 * Don't stop validation; just skip the content
27523 * of this element.
27524 */
27525 goto leave_node;
27526 }
27527 if ((vctxt->skipDepth != -1) &&
27528 (vctxt->depth >= vctxt->skipDepth))
27529 goto leave_node;
27530 } else if ((node->type == XML_TEXT_NODE) ||
27531 (node->type == XML_CDATA_SECTION_NODE)) {
27532 /*
27533 * Process character content.
27534 */
Daniel Veillard14b56432006-03-09 18:41:40 +000027535 if ((ielem != NULL) && (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027536 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27537 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27538 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27539 if (ret < 0) {
27540 VERROR_INT("xmlSchemaVDocWalk",
27541 "calling xmlSchemaVPushText()");
27542 goto internal_error;
27543 }
27544 /*
27545 * DOC VAL TODO: Should we skip further validation of the
27546 * element content here?
27547 */
27548 } else if ((node->type == XML_ENTITY_NODE) ||
27549 (node->type == XML_ENTITY_REF_NODE)) {
27550 /*
27551 * DOC VAL TODO: What to do with entities?
Kasimier T. Buchcik146a9822006-05-04 17:19:07 +000027552 */
27553 VERROR_INT("xmlSchemaVDocWalk",
27554 "there is at least one entity reference in the node-tree "
27555 "currently being validated. Processing of entities with "
27556 "this XML Schema processor is not supported (yet). Please "
27557 "substitute entities before validation.");
27558 goto internal_error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027559 } else {
27560 goto leave_node;
27561 /*
27562 * DOC VAL TODO: XInclude nodes, etc.
27563 */
27564 }
27565 /*
27566 * Walk the doc.
27567 */
27568 if (node->children != NULL) {
27569 node = node->children;
27570 continue;
27571 }
27572leave_node:
27573 if (node->type == XML_ELEMENT_NODE) {
27574 /*
27575 * Leaving the scope of an element.
27576 */
27577 if (node != vctxt->inode->node) {
27578 VERROR_INT("xmlSchemaVDocWalk",
27579 "element position mismatch");
27580 goto internal_error;
27581 }
27582 ret = xmlSchemaValidatorPopElem(vctxt);
27583 if (ret != 0) {
27584 if (ret < 0) {
27585 VERROR_INT("xmlSchemaVDocWalk",
27586 "calling xmlSchemaValidatorPopElem()");
27587 goto internal_error;
27588 }
27589 }
27590 if (node == valRoot)
27591 goto exit;
27592 }
27593next_sibling:
27594 if (node->next != NULL)
27595 node = node->next;
27596 else {
27597 node = node->parent;
27598 goto leave_node;
27599 }
27600 }
27601
27602exit:
27603 return (ret);
27604internal_error:
27605 return (-1);
27606}
27607
27608static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027609xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027610 /*
27611 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027612 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027613 vctxt->err = 0;
27614 vctxt->nberrors = 0;
27615 vctxt->depth = -1;
27616 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027617 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027618 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000027619#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027620 vctxt->createIDCNodeTables = 1;
27621#else
27622 vctxt->createIDCNodeTables = 0;
27623#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027624 /*
27625 * Create a schema + parser if necessary.
27626 */
27627 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027628 xmlSchemaParserCtxtPtr pctxt;
27629
27630 vctxt->xsiAssemble = 1;
27631 /*
27632 * If not schema was given then we will create a schema
27633 * dynamically using XSI schema locations.
27634 *
27635 * Create the schema parser context.
27636 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027637 if ((vctxt->pctxt == NULL) &&
27638 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27639 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027640 pctxt = vctxt->pctxt;
27641 pctxt->xsiAssemble = 1;
27642 /*
27643 * Create the schema.
27644 */
27645 vctxt->schema = xmlSchemaNewSchema(pctxt);
27646 if (vctxt->schema == NULL)
27647 return (-1);
27648 /*
27649 * Create the schema construction context.
27650 */
27651 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27652 if (pctxt->constructor == NULL)
27653 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027654 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027655 /*
27656 * Take ownership of the constructor to be able to free it.
27657 */
27658 pctxt->ownsConstructor = 1;
27659 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027660 /*
27661 * Augment the IDC definitions.
27662 */
27663 if (vctxt->schema->idcDef != NULL) {
27664 xmlHashScan(vctxt->schema->idcDef,
27665 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27666 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027667 return(0);
27668}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027669
Daniel Veillardf10ae122005-07-10 19:03:16 +000027670static void
27671xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027672 if (vctxt->xsiAssemble) {
27673 if (vctxt->schema != NULL) {
27674 xmlSchemaFree(vctxt->schema);
27675 vctxt->schema = NULL;
27676 }
27677 }
27678 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027679}
27680
27681static int
27682xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27683{
27684 int ret = 0;
27685
27686 if (xmlSchemaPreRun(vctxt) < 0)
27687 return(-1);
27688
27689 if (vctxt->doc != NULL) {
27690 /*
27691 * Tree validation.
27692 */
27693 ret = xmlSchemaVDocWalk(vctxt);
27694#ifdef LIBXML_READER_ENABLED
27695 } else if (vctxt->reader != NULL) {
27696 /*
27697 * XML Reader validation.
27698 */
27699#ifdef XML_SCHEMA_READER_ENABLED
27700 ret = xmlSchemaVReaderWalk(vctxt);
27701#endif
27702#endif
27703 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27704 /*
27705 * SAX validation.
27706 */
27707 ret = xmlParseDocument(vctxt->parserCtxt);
27708 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027709 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027710 "no instance to validate");
27711 ret = -1;
27712 }
27713
27714 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027715 if (ret == 0)
27716 ret = vctxt->err;
27717 return (ret);
27718}
27719
27720/**
27721 * xmlSchemaValidateOneElement:
27722 * @ctxt: a schema validation context
27723 * @elem: an element node
27724 *
27725 * Validate a branch of a tree, starting with the given @elem.
27726 *
27727 * Returns 0 if the element and its subtree is valid, a positive error
27728 * code number otherwise and -1 in case of an internal or API error.
27729 */
27730int
27731xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27732{
27733 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27734 return (-1);
27735
27736 if (ctxt->schema == NULL)
27737 return (-1);
27738
27739 ctxt->doc = elem->doc;
27740 ctxt->node = elem;
27741 ctxt->validationRoot = elem;
27742 return(xmlSchemaVStart(ctxt));
27743}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027744
Daniel Veillard259f0df2004-08-18 09:13:18 +000027745/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027746 * xmlSchemaValidateDoc:
27747 * @ctxt: a schema validation context
27748 * @doc: a parsed document tree
27749 *
27750 * Validate a document tree in memory.
27751 *
27752 * Returns 0 if the document is schemas valid, a positive error code
27753 * number otherwise and -1 in case of internal or API error.
27754 */
27755int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027756xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27757{
Daniel Veillard4255d502002-04-16 15:50:10 +000027758 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027759 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027760
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027761 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027762 ctxt->node = xmlDocGetRootElement(doc);
27763 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027764 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027765 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27766 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027767 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027768 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027769 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027770 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027771 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027772}
27773
Daniel Veillardcdc82732005-07-08 15:04:06 +000027774
27775/************************************************************************
27776 * *
27777 * Function and data for SAX streaming API *
27778 * *
27779 ************************************************************************/
27780typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27781typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27782
27783struct _xmlSchemaSplitSAXData {
27784 xmlSAXHandlerPtr user_sax;
27785 void *user_data;
27786 xmlSchemaValidCtxtPtr ctxt;
27787 xmlSAXHandlerPtr schemas_sax;
27788};
27789
Daniel Veillard971771e2005-07-09 17:32:57 +000027790#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27791
27792struct _xmlSchemaSAXPlug {
27793 unsigned int magic;
27794
27795 /* the original callbacks informations */
27796 xmlSAXHandlerPtr *user_sax_ptr;
27797 xmlSAXHandlerPtr user_sax;
27798 void **user_data_ptr;
27799 void *user_data;
27800
27801 /* the block plugged back and validation informations */
27802 xmlSAXHandler schemas_sax;
27803 xmlSchemaValidCtxtPtr ctxt;
27804};
27805
Daniel Veillardcdc82732005-07-08 15:04:06 +000027806/* All those functions just bounces to the user provided SAX handlers */
27807static void
27808internalSubsetSplit(void *ctx, const xmlChar *name,
27809 const xmlChar *ExternalID, const xmlChar *SystemID)
27810{
Daniel Veillard971771e2005-07-09 17:32:57 +000027811 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027812 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27813 (ctxt->user_sax->internalSubset != NULL))
27814 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27815 SystemID);
27816}
27817
27818static int
27819isStandaloneSplit(void *ctx)
27820{
Daniel Veillard971771e2005-07-09 17:32:57 +000027821 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027822 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27823 (ctxt->user_sax->isStandalone != NULL))
27824 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27825 return(0);
27826}
27827
27828static int
27829hasInternalSubsetSplit(void *ctx)
27830{
Daniel Veillard971771e2005-07-09 17:32:57 +000027831 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027832 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27833 (ctxt->user_sax->hasInternalSubset != NULL))
27834 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27835 return(0);
27836}
27837
27838static int
27839hasExternalSubsetSplit(void *ctx)
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->hasExternalSubset != NULL))
27844 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27845 return(0);
27846}
27847
27848static void
27849externalSubsetSplit(void *ctx, const xmlChar *name,
27850 const xmlChar *ExternalID, const xmlChar *SystemID)
27851{
Daniel Veillard971771e2005-07-09 17:32:57 +000027852 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027853 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27854 (ctxt->user_sax->internalSubset != NULL))
27855 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27856 SystemID);
27857}
27858
27859static xmlParserInputPtr
27860resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27861{
Daniel Veillard971771e2005-07-09 17:32:57 +000027862 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027863 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27864 (ctxt->user_sax->resolveEntity != NULL))
27865 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27866 systemId));
27867 return(NULL);
27868}
27869
27870static xmlEntityPtr
27871getEntitySplit(void *ctx, const xmlChar *name)
27872{
Daniel Veillard971771e2005-07-09 17:32:57 +000027873 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027874 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27875 (ctxt->user_sax->getEntity != NULL))
27876 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27877 return(NULL);
27878}
27879
27880static xmlEntityPtr
27881getParameterEntitySplit(void *ctx, const xmlChar *name)
27882{
Daniel Veillard971771e2005-07-09 17:32:57 +000027883 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027884 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27885 (ctxt->user_sax->getParameterEntity != NULL))
27886 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27887 return(NULL);
27888}
27889
27890
27891static void
27892entityDeclSplit(void *ctx, const xmlChar *name, int type,
27893 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27894{
Daniel Veillard971771e2005-07-09 17:32:57 +000027895 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027896 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27897 (ctxt->user_sax->entityDecl != NULL))
27898 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27899 systemId, content);
27900}
27901
27902static void
27903attributeDeclSplit(void *ctx, const xmlChar * elem,
27904 const xmlChar * name, int type, int def,
27905 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27906{
Daniel Veillard971771e2005-07-09 17:32:57 +000027907 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027908 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27909 (ctxt->user_sax->attributeDecl != NULL)) {
27910 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27911 def, defaultValue, tree);
27912 } else {
27913 xmlFreeEnumeration(tree);
27914 }
27915}
27916
27917static void
27918elementDeclSplit(void *ctx, const xmlChar *name, int type,
27919 xmlElementContentPtr content)
27920{
Daniel Veillard971771e2005-07-09 17:32:57 +000027921 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027922 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27923 (ctxt->user_sax->elementDecl != NULL))
27924 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27925}
27926
27927static void
27928notationDeclSplit(void *ctx, const xmlChar *name,
27929 const xmlChar *publicId, const xmlChar *systemId)
27930{
Daniel Veillard971771e2005-07-09 17:32:57 +000027931 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027932 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27933 (ctxt->user_sax->notationDecl != NULL))
27934 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27935 systemId);
27936}
27937
27938static void
27939unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27940 const xmlChar *publicId, const xmlChar *systemId,
27941 const xmlChar *notationName)
27942{
Daniel Veillard971771e2005-07-09 17:32:57 +000027943 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027944 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27945 (ctxt->user_sax->unparsedEntityDecl != NULL))
27946 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27947 systemId, notationName);
27948}
27949
27950static void
27951setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27952{
Daniel Veillard971771e2005-07-09 17:32:57 +000027953 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027954 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27955 (ctxt->user_sax->setDocumentLocator != NULL))
27956 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27957}
27958
27959static void
27960startDocumentSplit(void *ctx)
27961{
Daniel Veillard971771e2005-07-09 17:32:57 +000027962 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027963 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27964 (ctxt->user_sax->startDocument != NULL))
27965 ctxt->user_sax->startDocument(ctxt->user_data);
27966}
27967
27968static void
27969endDocumentSplit(void *ctx)
27970{
Daniel Veillard971771e2005-07-09 17:32:57 +000027971 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027972 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27973 (ctxt->user_sax->endDocument != NULL))
27974 ctxt->user_sax->endDocument(ctxt->user_data);
27975}
27976
27977static void
27978processingInstructionSplit(void *ctx, const xmlChar *target,
27979 const xmlChar *data)
27980{
Daniel Veillard971771e2005-07-09 17:32:57 +000027981 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027982 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27983 (ctxt->user_sax->processingInstruction != NULL))
27984 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27985}
27986
27987static void
27988commentSplit(void *ctx, const xmlChar *value)
27989{
Daniel Veillard971771e2005-07-09 17:32:57 +000027990 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027991 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27992 (ctxt->user_sax->comment != NULL))
27993 ctxt->user_sax->comment(ctxt->user_data, value);
27994}
27995
27996/*
27997 * Varargs error callbacks to the user application, harder ...
27998 */
27999
Daniel Veillardffa3c742005-07-21 13:24:09 +000028000static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028001warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028002 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028003 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28004 (ctxt->user_sax->warning != NULL)) {
28005 TODO
28006 }
28007}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028008static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028009errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028010 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028011 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28012 (ctxt->user_sax->error != NULL)) {
28013 TODO
28014 }
28015}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028016static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028017fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028018 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028019 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28020 (ctxt->user_sax->fatalError != NULL)) {
28021 TODO
28022 }
28023}
28024
28025/*
28026 * Those are function where both the user handler and the schemas handler
28027 * need to be called.
28028 */
28029static void
28030charactersSplit(void *ctx, const xmlChar *ch, int len)
28031{
Daniel Veillard971771e2005-07-09 17:32:57 +000028032 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028033 if (ctxt == NULL)
28034 return;
28035 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
28036 ctxt->user_sax->characters(ctxt->user_data, ch, len);
28037 if (ctxt->ctxt != NULL)
28038 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28039}
28040
28041static void
28042ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
28043{
Daniel Veillard971771e2005-07-09 17:32:57 +000028044 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028045 if (ctxt == NULL)
28046 return;
28047 if ((ctxt->user_sax != NULL) &&
28048 (ctxt->user_sax->ignorableWhitespace != NULL))
28049 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
28050 if (ctxt->ctxt != NULL)
28051 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28052}
28053
28054static void
28055cdataBlockSplit(void *ctx, const xmlChar *value, int len)
28056{
Daniel Veillard971771e2005-07-09 17:32:57 +000028057 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028058 if (ctxt == NULL)
28059 return;
28060 if ((ctxt->user_sax != NULL) &&
28061 (ctxt->user_sax->ignorableWhitespace != NULL))
28062 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
28063 if (ctxt->ctxt != NULL)
28064 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
28065}
28066
28067static void
28068referenceSplit(void *ctx, const xmlChar *name)
28069{
Daniel Veillard971771e2005-07-09 17:32:57 +000028070 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillard14b56432006-03-09 18:41:40 +000028071 if (ctxt == NULL)
28072 return;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028073 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28074 (ctxt->user_sax->reference != NULL))
28075 ctxt->user_sax->reference(ctxt->user_data, name);
28076 if (ctxt->ctxt != NULL)
28077 xmlSchemaSAXHandleReference(ctxt->user_data, name);
28078}
28079
28080static void
28081startElementNsSplit(void *ctx, const xmlChar * localname,
28082 const xmlChar * prefix, const xmlChar * URI,
28083 int nb_namespaces, const xmlChar ** namespaces,
28084 int nb_attributes, int nb_defaulted,
28085 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028086 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028087 if (ctxt == NULL)
28088 return;
28089 if ((ctxt->user_sax != NULL) &&
28090 (ctxt->user_sax->startElementNs != NULL))
28091 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
28092 URI, nb_namespaces, namespaces,
28093 nb_attributes, nb_defaulted,
28094 attributes);
28095 if (ctxt->ctxt != NULL)
28096 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
28097 URI, nb_namespaces, namespaces,
28098 nb_attributes, nb_defaulted,
28099 attributes);
28100}
28101
28102static void
28103endElementNsSplit(void *ctx, const xmlChar * localname,
28104 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028105 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028106 if (ctxt == NULL)
28107 return;
28108 if ((ctxt->user_sax != NULL) &&
28109 (ctxt->user_sax->endElementNs != NULL))
28110 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
28111 if (ctxt->ctxt != NULL)
28112 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
28113}
28114
Daniel Veillard4255d502002-04-16 15:50:10 +000028115/**
Daniel Veillard971771e2005-07-09 17:32:57 +000028116 * xmlSchemaSAXPlug:
28117 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000028118 * @sax: a pointer to the original xmlSAXHandlerPtr
28119 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000028120 *
28121 * Plug a SAX based validation layer in a SAX parsing event flow.
28122 * The original @saxptr and @dataptr data are replaced by new pointers
28123 * but the calls to the original will be maintained.
28124 *
28125 * Returns a pointer to a data structure needed to unplug the validation layer
28126 * or NULL in case of errors.
28127 */
28128xmlSchemaSAXPlugPtr
28129xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28130 xmlSAXHandlerPtr *sax, void **user_data)
28131{
28132 xmlSchemaSAXPlugPtr ret;
28133 xmlSAXHandlerPtr old_sax;
28134
28135 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28136 return(NULL);
28137
28138 /*
28139 * We only allow to plug into SAX2 event streams
28140 */
28141 old_sax = *sax;
28142 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28143 return(NULL);
28144 if ((old_sax != NULL) &&
28145 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28146 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28147 return(NULL);
28148
28149 /*
28150 * everything seems right allocate the local data needed for that layer
28151 */
28152 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28153 if (ret == NULL) {
28154 return(NULL);
28155 }
28156 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28157 ret->magic = XML_SAX_PLUG_MAGIC;
28158 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28159 ret->ctxt = ctxt;
28160 ret->user_sax_ptr = sax;
28161 ret->user_sax = old_sax;
28162 if (old_sax == NULL) {
28163 /*
28164 * go direct, no need for the split block and functions.
28165 */
28166 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28167 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28168 /*
28169 * Note that we use the same text-function for both, to prevent
28170 * the parser from testing for ignorable whitespace.
28171 */
28172 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28173 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28174
28175 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28176 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28177
28178 ret->user_data = ctxt;
28179 *user_data = ctxt;
28180 } else {
28181 /*
28182 * for each callback unused by Schemas initialize it to the Split
28183 * routine only if non NULL in the user block, this can speed up
28184 * things at the SAX level.
28185 */
28186 if (old_sax->internalSubset != NULL)
28187 ret->schemas_sax.internalSubset = internalSubsetSplit;
28188 if (old_sax->isStandalone != NULL)
28189 ret->schemas_sax.isStandalone = isStandaloneSplit;
28190 if (old_sax->hasInternalSubset != NULL)
28191 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28192 if (old_sax->hasExternalSubset != NULL)
28193 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28194 if (old_sax->resolveEntity != NULL)
28195 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28196 if (old_sax->getEntity != NULL)
28197 ret->schemas_sax.getEntity = getEntitySplit;
28198 if (old_sax->entityDecl != NULL)
28199 ret->schemas_sax.entityDecl = entityDeclSplit;
28200 if (old_sax->notationDecl != NULL)
28201 ret->schemas_sax.notationDecl = notationDeclSplit;
28202 if (old_sax->attributeDecl != NULL)
28203 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28204 if (old_sax->elementDecl != NULL)
28205 ret->schemas_sax.elementDecl = elementDeclSplit;
28206 if (old_sax->unparsedEntityDecl != NULL)
28207 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28208 if (old_sax->setDocumentLocator != NULL)
28209 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28210 if (old_sax->startDocument != NULL)
28211 ret->schemas_sax.startDocument = startDocumentSplit;
28212 if (old_sax->endDocument != NULL)
28213 ret->schemas_sax.endDocument = endDocumentSplit;
28214 if (old_sax->processingInstruction != NULL)
28215 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28216 if (old_sax->comment != NULL)
28217 ret->schemas_sax.comment = commentSplit;
28218 if (old_sax->warning != NULL)
28219 ret->schemas_sax.warning = warningSplit;
28220 if (old_sax->error != NULL)
28221 ret->schemas_sax.error = errorSplit;
28222 if (old_sax->fatalError != NULL)
28223 ret->schemas_sax.fatalError = fatalErrorSplit;
28224 if (old_sax->getParameterEntity != NULL)
28225 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28226 if (old_sax->externalSubset != NULL)
28227 ret->schemas_sax.externalSubset = externalSubsetSplit;
28228
28229 /*
28230 * the 6 schemas callback have to go to the splitter functions
28231 * Note that we use the same text-function for ignorableWhitespace
28232 * if possible, to prevent the parser from testing for ignorable
28233 * whitespace.
28234 */
28235 ret->schemas_sax.characters = charactersSplit;
28236 if ((old_sax->ignorableWhitespace != NULL) &&
28237 (old_sax->ignorableWhitespace != old_sax->characters))
28238 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28239 else
28240 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28241 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28242 ret->schemas_sax.reference = referenceSplit;
28243 ret->schemas_sax.startElementNs = startElementNsSplit;
28244 ret->schemas_sax.endElementNs = endElementNsSplit;
28245
28246 ret->user_data_ptr = user_data;
28247 ret->user_data = *user_data;
28248 *user_data = ret;
28249 }
28250
28251 /*
28252 * plug the pointers back.
28253 */
28254 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028255 ctxt->sax = *sax;
28256 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28257 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028258 return(ret);
28259}
28260
28261/**
28262 * xmlSchemaSAXUnplug:
28263 * @plug: a data structure returned by xmlSchemaSAXPlug
28264 *
28265 * Unplug a SAX based validation layer in a SAX parsing event flow.
28266 * The original pointers used in the call are restored.
28267 *
28268 * Returns 0 in case of success and -1 in case of failure.
28269 */
28270int
28271xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28272{
28273 xmlSAXHandlerPtr *sax;
28274 void **user_data;
28275
28276 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28277 return(-1);
28278 plug->magic = 0;
28279
Daniel Veillardf10ae122005-07-10 19:03:16 +000028280 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028281 /* restore the data */
28282 sax = plug->user_sax_ptr;
28283 *sax = plug->user_sax;
28284 if (plug->user_sax != NULL) {
28285 user_data = plug->user_data_ptr;
28286 *user_data = plug->user_data;
28287 }
28288
28289 /* free and return */
28290 xmlFree(plug);
28291 return(0);
28292}
28293
28294/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028295 * xmlSchemaValidateStream:
28296 * @ctxt: a schema validation context
28297 * @input: the input to use for reading the data
28298 * @enc: an optional encoding information
28299 * @sax: a SAX handler for the resulting events
28300 * @user_data: the context to provide to the SAX handler.
28301 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028302 * Validate an input based on a flow of SAX event from the parser
28303 * and forward the events to the @sax handler with the provided @user_data
28304 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028305 *
28306 * Returns 0 if the document is schemas valid, a positive error code
28307 * number otherwise and -1 in case of internal or API error.
28308 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028309int
Daniel Veillard4255d502002-04-16 15:50:10 +000028310xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028311 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28312 xmlSAXHandlerPtr sax, void *user_data)
28313{
Daniel Veillard971771e2005-07-09 17:32:57 +000028314 xmlSchemaSAXPlugPtr plug = NULL;
28315 xmlSAXHandlerPtr old_sax = NULL;
28316 xmlParserCtxtPtr pctxt = NULL;
28317 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028318 int ret;
28319
Daniel Veillard4255d502002-04-16 15:50:10 +000028320 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028321 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028322
Daniel Veillardcdc82732005-07-08 15:04:06 +000028323 /*
28324 * prepare the parser
28325 */
28326 pctxt = xmlNewParserCtxt();
28327 if (pctxt == NULL)
28328 return (-1);
28329 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028330 pctxt->sax = sax;
28331 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028332#if 0
28333 if (options)
28334 xmlCtxtUseOptions(pctxt, options);
28335#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028336 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028337
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028338 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028339 if (inputStream == NULL) {
28340 ret = -1;
28341 goto done;
28342 }
28343 inputPush(pctxt, inputStream);
28344 ctxt->parserCtxt = pctxt;
28345 ctxt->input = input;
28346
28347 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028348 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028349 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028350 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28351 if (plug == NULL) {
28352 ret = -1;
28353 goto done;
28354 }
28355 ctxt->input = input;
28356 ctxt->enc = enc;
28357 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028358 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28359 ret = xmlSchemaVStart(ctxt);
28360
28361 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28362 ret = ctxt->parserCtxt->errNo;
28363 if (ret == 0)
28364 ret = 1;
28365 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028366
28367done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028368 ctxt->parserCtxt = NULL;
28369 ctxt->sax = NULL;
28370 ctxt->input = NULL;
28371 if (plug != NULL) {
28372 xmlSchemaSAXUnplug(plug);
28373 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028374 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028375 if (pctxt != NULL) {
28376 pctxt->sax = old_sax;
28377 xmlFreeParserCtxt(pctxt);
28378 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028379 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028380}
28381
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028382/**
28383 * xmlSchemaValidateFile:
28384 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028385 * @filename: the URI of the instance
28386 * @options: a future set of options, currently unused
28387 *
28388 * Do a schemas validation of the given resource, it will use the
28389 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028390 *
28391 * Returns 0 if the document is valid, a positive error code
28392 * number otherwise and -1 in case of an internal or API error.
28393 */
28394int
28395xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028396 const char * filename,
28397 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028398{
28399 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028400 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028401
28402 if ((ctxt == NULL) || (filename == NULL))
28403 return (-1);
28404
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028405 input = xmlParserInputBufferCreateFilename(filename,
28406 XML_CHAR_ENCODING_NONE);
28407 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028408 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028409 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28410 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028411 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028412}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028413
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028414#define bottom_xmlschemas
28415#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028416#endif /* LIBXML_SCHEMAS_ENABLED */